shaderSimpleFlat.cpp 6.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Sylvain Thery's avatar
Sylvain Thery committed
24
#define CGoGN_UTILS_DLL_EXPORT 1
25
26
27
28
29
30
31
32
33
34
35
#include <GL/glew.h>
#include "Utils/Shaders/shaderSimpleFlat.h"

namespace CGoGN
{

namespace Utils
{

#include "shaderSimpleFlat.vert"
#include "shaderSimpleFlat.frag"
Sylvain Thery's avatar
Sylvain Thery committed
36
37
#include "shaderSimpleFlatClip.vert"
#include "shaderSimpleFlatClip.frag"
38
39


Sylvain Thery's avatar
Sylvain Thery committed
40
41

ShaderSimpleFlat::ShaderSimpleFlat(bool withClipping, bool doubleSided):
42
43
44
45
	m_with_color(false),
	m_ambiant(Geom::Vec4f(0.05f,0.05f,0.1f,0.0f)),
	m_diffuse(Geom::Vec4f(0.1f,1.0f,0.1f,0.0f)),
	m_lightPos(Geom::Vec3f(10.0f,10.0f,1000.0f)),
Sylvain Thery's avatar
Sylvain Thery committed
46
	m_backColor(0.0f,0.0f,0.0f,0.0f),
47
	m_vboPos(NULL),
Sylvain Thery's avatar
Sylvain Thery committed
48
49
	m_vboColor(NULL),
	m_planeClip(Geom::Vec4f(0.0f,0.0f,0.0f,0.0f))
50
51
52
53
{
	std::string glxvert(*GLSLShader::DEFINES_GL);
	std::string glxfrag(*GLSLShader::DEFINES_GL);

Sylvain Thery's avatar
Sylvain Thery committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	if (withClipping)
	{
		m_nameVS = "ShaderSimpleFlatClip_vs";
		m_nameFS = "ShaderSimpleFlatClip_fs";
		glxvert.append(vertexShaderClipText);
		if (doubleSided)
			glxfrag.append("#define DOUBLE_SIDED\n");
		glxfrag.append(fragmentShaderClipText);
	}
	else
	{
		m_nameVS = "ShaderSimpleFlat_vs";
		m_nameFS = "ShaderSimpleFlat_fs";
		// get choose GL defines (2 or 3)
		// ans compile shaders
		glxvert.append(vertexShaderText);
		if (doubleSided)
			glxfrag.append("#define DOUBLE_SIDED\n");
		glxfrag.append(fragmentShaderText);
	}
74

Sylvain Thery's avatar
Sylvain Thery committed
75
	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str());
76
77
78
79
80
81
82
83
84
85
86
	// and get and fill uniforms
	getLocations();
	sendParams();
}

void ShaderSimpleFlat::getLocations()
{
	bind();
	*m_unif_ambiant   = glGetUniformLocation(this->program_handler(), "materialAmbient");
	*m_unif_diffuse   = glGetUniformLocation(this->program_handler(), "materialDiffuse");
	*m_unif_lightPos  = glGetUniformLocation(this->program_handler(), "lightPosition");
Sylvain Thery's avatar
Sylvain Thery committed
87
88
	*m_unif_backColor  = glGetUniformLocation(this->program_handler(), "backColor");
	*m_unif_planeClip = glGetUniformLocation(this->program_handler(), "planeClip");
89
90
91
92
93
94
95
96
97
	unbind();
}

void ShaderSimpleFlat::sendParams()
{
	bind();
	glUniform4fv(*m_unif_ambiant,  1, m_ambiant.data());
	glUniform4fv(*m_unif_diffuse,  1, m_diffuse.data());
	glUniform3fv(*m_unif_lightPos, 1, m_lightPos.data());
Sylvain Thery's avatar
Sylvain Thery committed
98
99
100
	glUniform4fv(*m_unif_backColor,  1, m_backColor.data());
	if (*m_unif_planeClip > 0)
		glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	unbind();
}

void ShaderSimpleFlat::setAmbiant(const Geom::Vec4f& ambiant)
{
	bind();
	glUniform4fv(*m_unif_ambiant,1, ambiant.data());
	m_ambiant = ambiant;
	unbind();
}

void ShaderSimpleFlat::setDiffuse(const Geom::Vec4f& diffuse)
{
	bind();
	glUniform4fv(*m_unif_diffuse,1, diffuse.data());
	m_diffuse = diffuse;
	unbind();
}


void ShaderSimpleFlat::setLightPosition(const Geom::Vec3f& lightPos)
{
	bind();
	glUniform3fv(*m_unif_lightPos,1,lightPos.data());
	m_lightPos = lightPos;
	unbind();
}


Sylvain Thery's avatar
Sylvain Thery committed
130
131
132
133
134
135
136
137
138
void ShaderSimpleFlat::setBackColor(const Geom::Vec4f& back)
{
	bind();
	glUniform4fv(*m_unif_backColor,1, back.data());
	m_backColor = back;
	unbind();
}


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
void ShaderSimpleFlat::setParams(const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec3f& lightPos)
{
	m_ambiant = ambiant;
	m_diffuse = diffuse;
	m_lightPos = lightPos;
	sendParams();
}

unsigned int ShaderSimpleFlat::setAttributeColor(VBO* vbo)
{
	m_vboColor = vbo;
	if (!m_with_color)
	{
		m_with_color=true;
		// set the define and recompile shader
		std::string gl3vert(*GLSLShader::DEFINES_GL);
		gl3vert.append("#define WITH_COLOR 1\n");
		gl3vert.append(vertexShaderText);
		std::string gl3frag(*GLSLShader::DEFINES_GL);
		gl3frag.append("#define WITH_COLOR 1\n");
		gl3frag.append(fragmentShaderText);
		loadShadersFromMemory(gl3vert.c_str(), gl3frag.c_str());

		// and treat uniforms
		getLocations();
		sendParams();
	}
	// bind th VA with WBO
	bind();
	unsigned int id = bindVA_VBO("VertexColor", vbo);
	unbind();
	return id;
}

void ShaderSimpleFlat::unsetAttributeColor()
{
	m_vboColor = NULL;
	if (m_with_color)
	{
		m_with_color = false;
		// unbind the VA
		bind();
		unbindVA("VertexColor");
		unbind();
		// recompile shader
		std::string gl3vert(*GLSLShader::DEFINES_GL);
		gl3vert.append(vertexShaderText);
		std::string gl3frag(*GLSLShader::DEFINES_GL);
		gl3frag.append(fragmentShaderText);
		loadShadersFromMemory(gl3vert.c_str(), gl3frag.c_str());
		// and treat uniforms
		getLocations();
		sendParams();
	}
}

void ShaderSimpleFlat::restoreUniformsAttribs()
{
	getLocations();
	sendParams();

	bind();
	bindVA_VBO("VertexPosition", m_vboPos);
	if (m_vboColor)
		bindVA_VBO("VertexColor", m_vboColor);

	unbind();
}

unsigned int ShaderSimpleFlat::setAttributePosition(VBO* vbo)
{
	m_vboPos = vbo;
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
}

Sylvain Thery's avatar
Sylvain Thery committed
217
218
219
220
221
222
223
224
225
226
227
228
void ShaderSimpleFlat::setClippingPlane(const Geom::Vec4f& plane)
{
	if (*m_unif_planeClip > 0)
	{
		m_planeClip = plane;
		bind();
		glUniform4fv(*m_unif_planeClip, 1, plane.data());
		unbind();
	}
}


229
230
231
232

} // namespace Utils

} // namespace CGoGN