shaderSimpleColor.cpp 3.47 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
25
#include <GL/glew.h>
26
#include "Utils/Shaders/shaderSimpleColor.h"
Sylvain Thery's avatar
Sylvain Thery committed
27

Pierre Kraemer's avatar
Pierre Kraemer committed
28 29
namespace CGoGN
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
30

Sylvain Thery's avatar
Sylvain Thery committed
31
namespace Utils
Pierre Kraemer's avatar
Pierre Kraemer committed
32 33
{

Sylvain Thery's avatar
Sylvain Thery committed
34 35 36
std::string ShaderSimpleColor::vertexShaderText =
		"ATTRIBUTE vec3 VertexPosition, VertexNormal;\n"
		"uniform mat4 ModelViewProjectionMatrix;\n"
37
//		"INVARIANT_POS;\n"
Sylvain Thery's avatar
Sylvain Thery committed
38 39 40 41
		"void main ()\n"
		"{\n"
		"	gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);\n"
		"}";
Pierre Kraemer's avatar
Pierre Kraemer committed
42 43


Sylvain Thery's avatar
Sylvain Thery committed
44 45 46 47 48 49 50 51 52 53 54
std::string ShaderSimpleColor::fragmentShaderText =
		"PRECISON;\n"
		"uniform vec4 color;\n"
		"FRAG_OUT_DEF;\n"
		"void main()\n"
		"{\n"
		"	gl_FragColor=color;\n"
		"}";


ShaderSimpleColor::ShaderSimpleColor()
Pierre Kraemer's avatar
Pierre Kraemer committed
55
{
56 57 58 59
	m_nameVS = "ShaderSimpleColor_vs";
	m_nameFS = "ShaderSimpleColor_fs";
	m_nameGS = "ShaderSimpleColor_gs";

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
60 61
	// chose GL defines (2 or 3)
	// and compile shaders
Sylvain Thery's avatar
Sylvain Thery committed
62 63 64 65 66 67 68 69 70 71 72
	std::string glxvert(*GLSLShader::DEFINES_GL);
	glxvert.append(vertexShaderText);

	std::string glxfrag(*GLSLShader::DEFINES_GL);
	glxfrag.append(fragmentShaderText);

	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());

	m_unif_color = glGetUniformLocation(this->program_handler(),"color");

	//Default values
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
73
	Geom::Vec4f color(0.1f, 0.9f, 0.1f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
74
	setColor(color);
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76
}

Sylvain Thery's avatar
Sylvain Thery committed
77
void ShaderSimpleColor::setColor(const Geom::Vec4f& color)
Pierre Kraemer's avatar
Pierre Kraemer committed
78
{
79 80
	m_color = color;
	bind();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
81
	glUniform4fv(m_unif_color, 1, color.data());
Pierre Kraemer's avatar
Pierre Kraemer committed
82 83
}

84 85 86 87 88 89 90
unsigned int ShaderSimpleColor::setAttributePosition(VBO* vbo)
{
	m_vboPos = vbo;
	return bindVA_VBO("VertexPosition", vbo);
}

void ShaderSimpleColor::restoreUniformsAttribs()
Sylvain Thery's avatar
Sylvain Thery committed
91
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
92
	m_unif_color = glGetUniformLocation(this->program_handler(), "color");
93
	bind();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
94
	glUniform4fv(m_unif_color, 1, m_color.data());
95
	bindVA_VBO("VertexPosition", m_vboPos);
96
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
97 98
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
99
} // namespace Utils
Pierre Kraemer's avatar
Pierre Kraemer committed
100

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
101
} // namespace CGoGN