shaderSimpleColor.cpp 3.71 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-2012, 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.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25
#include "Utils/Shaders/shaderSimpleColor.h"
Sylvain Thery's avatar
Sylvain Thery committed
26

Pierre Kraemer's avatar
Pierre Kraemer committed
27 28
namespace CGoGN
{
Pierre Kraemer's avatar
Pierre Kraemer committed
29

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

33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "shaderSimpleColor.vert"
#include "shaderSimpleColor.frag"

//std::string ShaderSimpleColor::vertexShaderText =
//		"ATTRIBUTE vec3 VertexPosition, VertexNormal;\n"
//		"uniform mat4 ModelViewProjectionMatrix;\n"
////		"INVARIANT_POS;\n"
//		"void main ()\n"
//		"{\n"
//		"	gl_Position = ModelViewProjectionMatrix * vec4 (VertexPosition, 1.0);\n"
//		"}";
//
//
//std::string ShaderSimpleColor::fragmentShaderText =
47
//		"PRECISION;\n"
48 49 50 51
//		"uniform vec4 color;\n"
//		"FRAG_OUT_DEF;\n"
//		"void main()\n"
//		"{\n"
52
//		"	FRAG_OUT = color;\n"
53
//		"}";
Sylvain Thery's avatar
Sylvain Thery committed
54 55


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

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

	std::string glxfrag(*GLSLShader::DEFINES_GL);
68 69
	if (black_is_transparent)
		glxfrag.append("#define BLACK_TRANSPARENCY 1\n");
Sylvain Thery's avatar
Sylvain Thery committed
70 71 72 73
	glxfrag.append(fragmentShaderText);

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

74
	*m_unif_color = glGetUniformLocation(this->program_handler(),"color");
Sylvain Thery's avatar
Sylvain Thery committed
75 76

	//Default values
Pierre Kraemer's avatar
Pierre Kraemer committed
77
	Geom::Vec4f color(0.1f, 0.9f, 0.1f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
78
	setColor(color);
Pierre Kraemer's avatar
Pierre Kraemer committed
79 80
}

Sylvain Thery's avatar
Sylvain Thery committed
81
void ShaderSimpleColor::setColor(const Geom::Vec4f& color)
Pierre Kraemer's avatar
Pierre Kraemer committed
82
{
83 84
	m_color = color;
	bind();
85
	glUniform4fv(*m_unif_color, 1, color.data());
86
	unbind();
Pierre Kraemer's avatar
Pierre Kraemer committed
87 88
}

89 90 91
unsigned int ShaderSimpleColor::setAttributePosition(VBO* vbo)
{
	m_vboPos = vbo;
92 93 94 95
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
96 97 98
}

void ShaderSimpleColor::restoreUniformsAttribs()
Sylvain Thery's avatar
Sylvain Thery committed
99
{
100
	*m_unif_color = glGetUniformLocation(this->program_handler(), "color");
101
	bind();
102
	glUniform4fv(*m_unif_color, 1, m_color.data());
103
	bindVA_VBO("VertexPosition", m_vboPos);
104
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
105 106
}

Pierre Kraemer's avatar
Pierre Kraemer committed
107
} // namespace Utils
Pierre Kraemer's avatar
Pierre Kraemer committed
108

Pierre Kraemer's avatar
Pierre Kraemer committed
109
} // namespace CGoGN