shaderPhong.cpp 7.96 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery 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           *
Sylvain Thery's avatar
Sylvain Thery 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/                                           *
Sylvain Thery's avatar
Sylvain Thery committed
21
22
23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
24

Sylvain Thery's avatar
Sylvain Thery committed
25
#define CGoGN_UTILS_DLL_EXPORT 1
Sylvain Thery's avatar
Sylvain Thery committed
26
#include <GL/glew.h>
27
#include "Utils/Shaders/shaderPhong.h"
Sylvain Thery's avatar
Sylvain Thery committed
28
29
30

namespace CGoGN
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
31

Sylvain Thery's avatar
Sylvain Thery committed
32
33
namespace Utils
{
34

35
36
#include "shaderPhong.vert"
#include "shaderPhong.frag"
Sylvain Thery's avatar
Sylvain Thery committed
37
38
#include "shaderPhongClip.vert"
#include "shaderPhongClip.frag"
Sylvain Thery's avatar
Sylvain Thery committed
39

Sylvain Thery's avatar
Sylvain Thery committed
40
ShaderPhong::ShaderPhong(bool withClipping, bool doubleSided, bool withEyePosition):
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
41
	m_with_color(false),
42
	m_with_eyepos(withEyePosition),
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
43
44
45
46
	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_specular(Geom::Vec4f(1.0f,1.0f,1.0f,0.0f)),
	m_shininess(100.0f),
47
	m_lightPos(Geom::Vec3f(10.0f,10.0f,1000.0f)),
Sylvain Thery's avatar
Sylvain Thery committed
48
	m_backColor(0.0f,0.0f,0.0f,0.0f),
49
50
	m_vboPos(NULL),
	m_vboNormal(NULL),
Sylvain Thery's avatar
Sylvain Thery committed
51
52
	m_vboColor(NULL),
	m_planeClip(Geom::Vec4f(0.0f,0.0f,0.0f,0.0f))
Sylvain Thery's avatar
Sylvain Thery committed
53
{
54
55
	std::string glxvert(GLSLShader::defines_gl());
	std::string glxfrag(GLSLShader::defines_gl());
Sylvain Thery's avatar
Sylvain Thery committed
56

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	if (withClipping)
	{
		m_nameVS = "ShaderPhongClip_vs";
		m_nameFS = "ShaderPhongClip_fs";
		if (m_with_eyepos)
			glxvert.append("#define WITH_EYEPOSITION");
		glxvert.append(vertexShaderClipText);
		// Use double sided lighting if set
		if (doubleSided)
			glxfrag.append("#define DOUBLE_SIDED\n");
		glxfrag.append(fragmentShaderClipText);
	}
	else
	{
		m_nameVS = "ShaderPhong_vs";
		m_nameFS = "ShaderPhong_fs";
		if (m_with_eyepos)
			glxvert.append("#define WITH_EYEPOSITION");
		glxvert.append(vertexShaderText);
		// Use double sided lighting if set
		if (doubleSided)
			glxfrag.append("#define DOUBLE_SIDED\n");
		glxfrag.append(fragmentShaderText);
	}
Sylvain Thery's avatar
Sylvain Thery committed
81
82
83
84
85
86
87
88

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

	// and get and fill uniforms
	getLocations();
	sendParams();
}

Sylvain Thery's avatar
Sylvain Thery committed
89
void ShaderPhong::getLocations()
Sylvain Thery's avatar
Sylvain Thery committed
90
{
91
	bind();
92
93
94
95
96
	*m_unif_ambiant   = glGetUniformLocation(this->program_handler(), "materialAmbient");
	*m_unif_diffuse   = glGetUniformLocation(this->program_handler(), "materialDiffuse");
	*m_unif_specular  = glGetUniformLocation(this->program_handler(), "materialSpecular");
	*m_unif_shininess = glGetUniformLocation(this->program_handler(), "shininess");
	*m_unif_lightPos  = glGetUniformLocation(this->program_handler(), "lightPosition");
Sylvain's avatar
Sylvain committed
97
98
	if (m_with_eyepos)
		*m_unif_eyePos  = glGetUniformLocation(this->program_handler(), "eyePosition");
Sylvain Thery's avatar
Sylvain Thery committed
99
100
101
	*m_unif_backColor  = glGetUniformLocation(this->program_handler(), "backColor");
	*m_unif_planeClip = glGetUniformLocation(this->program_handler(), "planeClip");

102
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
103
104
}

Sylvain Thery's avatar
Sylvain Thery committed
105
void ShaderPhong::sendParams()
Sylvain Thery's avatar
Sylvain Thery committed
106
{
107
	bind();
108
109
110
111
112
	glUniform4fv(*m_unif_ambiant,  1, m_ambiant.data());
	glUniform4fv(*m_unif_diffuse,  1, m_diffuse.data());
	glUniform4fv(*m_unif_specular, 1, m_specular.data());
	glUniform1f(*m_unif_shininess,    m_shininess);
	glUniform3fv(*m_unif_lightPos, 1, m_lightPos.data());
Sylvain's avatar
Sylvain committed
113
114
	if (m_with_eyepos)
		glUniform3fv(*m_unif_eyePos, 1, m_eyePos.data());
Sylvain Thery's avatar
Sylvain Thery committed
115
116
117
	glUniform4fv(*m_unif_backColor,  1, m_backColor.data());
	if (*m_unif_planeClip > 0)
		glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
118
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
119
120
}

Sylvain Thery's avatar
Sylvain Thery committed
121
void ShaderPhong::setAmbiant(const Geom::Vec4f& ambiant)
Sylvain Thery's avatar
Sylvain Thery committed
122
{
123
	bind();
124
	glUniform4fv(*m_unif_ambiant,1, ambiant.data());
Sylvain Thery's avatar
Sylvain Thery committed
125
	m_ambiant = ambiant;
126
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
127
128
}

Sylvain Thery's avatar
Sylvain Thery committed
129
void ShaderPhong::setDiffuse(const Geom::Vec4f& diffuse)
Sylvain Thery's avatar
Sylvain Thery committed
130
{
131
	bind();
132
	glUniform4fv(*m_unif_diffuse,1, diffuse.data());
Sylvain Thery's avatar
Sylvain Thery committed
133
	m_diffuse = diffuse;
134
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
135
136
}

Sylvain Thery's avatar
Sylvain Thery committed
137
void ShaderPhong::setSpecular(const Geom::Vec4f& specular)
Sylvain Thery's avatar
Sylvain Thery committed
138
{
139
	bind();
140
	glUniform4fv(*m_unif_specular,1,specular.data());
Sylvain Thery's avatar
Sylvain Thery committed
141
	m_specular = specular;
142
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
143
144
}

Sylvain Thery's avatar
Sylvain Thery committed
145
void ShaderPhong::setShininess(float shininess)
Sylvain Thery's avatar
Sylvain Thery committed
146
{
147
	bind();
148
	glUniform1f (*m_unif_shininess, shininess);
Sylvain Thery's avatar
Sylvain Thery committed
149
	m_shininess = shininess;
150
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
151
152
}

Sylvain Thery's avatar
Sylvain Thery committed
153
154
155
156
157
158
159
160
161
void ShaderPhong::setBackColor(const Geom::Vec4f& back)
{
	bind();
	glUniform4fv(*m_unif_backColor,1, back.data());
	m_backColor = back;
	unbind();
}


162
void ShaderPhong::setLightPosition(const Geom::Vec3f& lightPos)
Sylvain Thery's avatar
Sylvain Thery committed
163
{
164
	bind();
165
	glUniform3fv(*m_unif_lightPos,1,lightPos.data());
Sylvain Thery's avatar
Sylvain Thery committed
166
	m_lightPos = lightPos;
167
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
168
169
}

170
void ShaderPhong::setEyePosition(const Geom::Vec3f& eyePos)
Sylvain's avatar
Sylvain committed
171
172
173
{
	if (m_with_eyepos)
	{
174
		bind();
Sylvain Thery's avatar
Sylvain Thery committed
175
		glUniform3fv(*m_unif_eyePos,1,eyePos.data());
Sylvain's avatar
Sylvain committed
176
		m_eyePos = eyePos;
177
		unbind();
Sylvain's avatar
Sylvain committed
178
179
180
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
181
void ShaderPhong::setParams(const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec4f& specular, float shininess, const Geom::Vec3f& lightPos)
Sylvain Thery's avatar
Sylvain Thery committed
182
183
184
185
186
187
188
189
190
{
	m_ambiant = ambiant;
	m_diffuse = diffuse;
	m_specular = specular;
	m_shininess = shininess;
	m_lightPos = lightPos;
	sendParams();
}

191
unsigned int ShaderPhong::setAttributeColor(VBO* vbo)
Sylvain Thery's avatar
Sylvain Thery committed
192
{
193
	m_vboColor = vbo;
Sylvain Thery's avatar
Sylvain Thery committed
194
195
196
197
	if (!m_with_color)
	{
		m_with_color=true;
		// set the define and recompile shader
198
		std::string gl3vert(GLSLShader::defines_gl());
Sylvain Thery's avatar
Sylvain Thery committed
199
200
		gl3vert.append("#define WITH_COLOR 1\n");
		gl3vert.append(vertexShaderText);
201
		std::string gl3frag(GLSLShader::defines_gl());
Sylvain Thery's avatar
Sylvain Thery committed
202
203
204
205
206
207
208
209
210
		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
211
212
213
214
	bind();
	unsigned int id = bindVA_VBO("VertexColor", vbo);
	unbind();
	return id;
Sylvain Thery's avatar
Sylvain Thery committed
215
216
}

Sylvain Thery's avatar
Sylvain Thery committed
217
void ShaderPhong::unsetAttributeColor()
Sylvain Thery's avatar
Sylvain Thery committed
218
{
219
	m_vboColor = NULL;
Sylvain Thery's avatar
Sylvain Thery committed
220
221
	if (m_with_color)
	{
222
		m_with_color = false;
Sylvain Thery's avatar
Sylvain Thery committed
223
		// unbind the VA
224
		bind();
Sylvain Thery's avatar
Sylvain Thery committed
225
		unbindVA("VertexColor");
226
		unbind();
Sylvain Thery's avatar
Sylvain Thery committed
227
		// recompile shader
228
		std::string gl3vert(GLSLShader::defines_gl());
Sylvain Thery's avatar
Sylvain Thery committed
229
		gl3vert.append(vertexShaderText);
230
		std::string gl3frag(GLSLShader::defines_gl());
Sylvain Thery's avatar
Sylvain Thery committed
231
232
233
234
235
236
		gl3frag.append(fragmentShaderText);
		loadShadersFromMemory(gl3vert.c_str(), gl3frag.c_str());
		// and treat uniforms
		getLocations();
		sendParams();
	}
237
238
}

Sylvain Thery's avatar
Sylvain Thery committed
239
240
241
242
243
244
245
246
247
248
249
void ShaderPhong::setClippingPlane(const Geom::Vec4f& plane)
{
	if (*m_unif_planeClip > 0)
	{
		m_planeClip = plane;
		bind();
		glUniform4fv(*m_unif_planeClip, 1, plane.data());
		unbind();
	}
}

250
251
252
253
void ShaderPhong::restoreUniformsAttribs()
{
	getLocations();
	sendParams();
Sylvain Thery's avatar
Sylvain Thery committed
254

255
	bind();
256
257
258
259
	bindVA_VBO("VertexPosition", m_vboPos);
	bindVA_VBO("VertexNormal", m_vboNormal);
	if (m_vboColor)
		bindVA_VBO("VertexColor", m_vboColor);
260

261
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
262
263
}

264
265
unsigned int ShaderPhong::setAttributePosition(VBO* vbo)
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
266
	m_vboPos = vbo;
267
268
269
270
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
271
272
273
274
275
}

unsigned int ShaderPhong::setAttributeNormal(VBO* vbo)
{
	m_vboNormal = vbo;
276
277
278
279
	bind();
	unsigned int id = bindVA_VBO("VertexNormal", vbo);
	unbind();
	return id;
280
281
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
282
} // namespace Utils
283

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