shaderPhong.cpp 6.88 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                                        *
*                                                                              *
*******************************************************************************/
Sylvain Thery's avatar
Sylvain Thery committed
24
#define CGoGN_UTILS_DLL_EXPORT 1
Sylvain Thery's avatar
Sylvain Thery committed
25
#include <GL/glew.h>
26
#include "Utils/Shaders/shaderPhong.h"
Sylvain Thery's avatar
Sylvain Thery committed
27
28
29

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

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

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


Sylvain's avatar
Sylvain committed
38
ShaderPhong::ShaderPhong(bool doubleSided, bool withEyePosition):
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
39
	m_with_color(false),
40
	m_with_eyepos(withEyePosition),
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
41
42
43
44
	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),
45
46
47
	m_lightPos(Geom::Vec3f(10.0f,10.0f,1000.0f)),
	m_vboPos(NULL),
	m_vboNormal(NULL),
Sylvain Thery's avatar
Sylvain Thery committed
48
	m_vboColor(NULL)
Sylvain Thery's avatar
Sylvain Thery committed
49
{
50
51
	m_nameVS = "ShaderPhong_vs";
	m_nameFS = "ShaderPhong_fs";
Sylvain Thery's avatar
Sylvain Thery committed
52
//	m_nameGS = "ShaderPhong_gs";
53

Sylvain Thery's avatar
Sylvain Thery committed
54
55
56
	// get choose GL defines (2 or 3)
	// ans compile shaders
	std::string glxvert(*GLSLShader::DEFINES_GL);
Sylvain's avatar
Sylvain committed
57
58
	if (m_with_eyepos)
		glxvert.append("#define WITH_EYEPOSITION");
Sylvain Thery's avatar
Sylvain Thery committed
59
60
	glxvert.append(vertexShaderText);
	std::string glxfrag(*GLSLShader::DEFINES_GL);
61
62
63
	// Use double sided lighting if set
	if (doubleSided)
		glxfrag.append("#define DOUBLE_SIDED\n");
Sylvain Thery's avatar
Sylvain Thery committed
64
65
66
67
68
69
70
71
72
	glxfrag.append(fragmentShaderText);

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

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

Sylvain Thery's avatar
Sylvain Thery committed
73
void ShaderPhong::getLocations()
Sylvain Thery's avatar
Sylvain Thery committed
74
{
75
	bind();
76
77
78
79
80
	*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
81
82
	if (m_with_eyepos)
		*m_unif_eyePos  = glGetUniformLocation(this->program_handler(), "eyePosition");
83
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
84
85
}

Sylvain Thery's avatar
Sylvain Thery committed
86
void ShaderPhong::sendParams()
Sylvain Thery's avatar
Sylvain Thery committed
87
{
88
	bind();
89
90
91
92
93
	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
94
95
	if (m_with_eyepos)
		glUniform3fv(*m_unif_eyePos, 1, m_eyePos.data());
96
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
97
98
}

Sylvain Thery's avatar
Sylvain Thery committed
99
void ShaderPhong::setAmbiant(const Geom::Vec4f& ambiant)
Sylvain Thery's avatar
Sylvain Thery committed
100
{
101
	bind();
102
	glUniform4fv(*m_unif_ambiant,1, ambiant.data());
Sylvain Thery's avatar
Sylvain Thery committed
103
	m_ambiant = ambiant;
104
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
105
106
}

Sylvain Thery's avatar
Sylvain Thery committed
107
void ShaderPhong::setDiffuse(const Geom::Vec4f& diffuse)
Sylvain Thery's avatar
Sylvain Thery committed
108
{
109
	bind();
110
	glUniform4fv(*m_unif_diffuse,1, diffuse.data());
Sylvain Thery's avatar
Sylvain Thery committed
111
	m_diffuse = diffuse;
112
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
113
114
}

Sylvain Thery's avatar
Sylvain Thery committed
115
void ShaderPhong::setSpecular(const Geom::Vec4f& specular)
Sylvain Thery's avatar
Sylvain Thery committed
116
{
117
	bind();
118
	glUniform4fv(*m_unif_specular,1,specular.data());
Sylvain Thery's avatar
Sylvain Thery committed
119
	m_specular = specular;
120
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
121
122
}

Sylvain Thery's avatar
Sylvain Thery committed
123
void ShaderPhong::setShininess(float shininess)
Sylvain Thery's avatar
Sylvain Thery committed
124
{
125
	bind();
126
	glUniform1f (*m_unif_shininess, shininess);
Sylvain Thery's avatar
Sylvain Thery committed
127
	m_shininess = shininess;
128
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
129
130
}

131
void ShaderPhong::setLightPosition(const Geom::Vec3f& lightPos)
Sylvain Thery's avatar
Sylvain Thery committed
132
{
133
	bind();
134
	glUniform3fv(*m_unif_lightPos,1,lightPos.data());
Sylvain Thery's avatar
Sylvain Thery committed
135
	m_lightPos = lightPos;
136
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
137
138
}

139
void ShaderPhong::setEyePosition(const Geom::Vec3f& eyePos)
Sylvain's avatar
Sylvain committed
140
141
142
{
	if (m_with_eyepos)
	{
143
		bind();
Sylvain Thery's avatar
Sylvain Thery committed
144
		glUniform3fv(*m_unif_eyePos,1,eyePos.data());
Sylvain's avatar
Sylvain committed
145
		m_eyePos = eyePos;
146
		unbind();
Sylvain's avatar
Sylvain committed
147
148
149
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
150
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
151
152
153
154
155
156
157
158
159
{
	m_ambiant = ambiant;
	m_diffuse = diffuse;
	m_specular = specular;
	m_shininess = shininess;
	m_lightPos = lightPos;
	sendParams();
}

160
unsigned int ShaderPhong::setAttributeColor(VBO* vbo)
Sylvain Thery's avatar
Sylvain Thery committed
161
{
162
	m_vboColor = vbo;
Sylvain Thery's avatar
Sylvain Thery committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
	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
180
181
182
183
	bind();
	unsigned int id = bindVA_VBO("VertexColor", vbo);
	unbind();
	return id;
Sylvain Thery's avatar
Sylvain Thery committed
184
185
}

Sylvain Thery's avatar
Sylvain Thery committed
186
void ShaderPhong::unsetAttributeColor()
Sylvain Thery's avatar
Sylvain Thery committed
187
{
188
	m_vboColor = NULL;
Sylvain Thery's avatar
Sylvain Thery committed
189
190
	if (m_with_color)
	{
191
		m_with_color = false;
Sylvain Thery's avatar
Sylvain Thery committed
192
		// unbind the VA
193
		bind();
Sylvain Thery's avatar
Sylvain Thery committed
194
		unbindVA("VertexColor");
195
		unbind();
Sylvain Thery's avatar
Sylvain Thery committed
196
197
198
199
200
201
202
203
204
205
		// 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();
	}
206
207
208
209
210
211
}

void ShaderPhong::restoreUniformsAttribs()
{
	getLocations();
	sendParams();
Sylvain Thery's avatar
Sylvain Thery committed
212

213
	bind();
214
215
216
217
	bindVA_VBO("VertexPosition", m_vboPos);
	bindVA_VBO("VertexNormal", m_vboNormal);
	if (m_vboColor)
		bindVA_VBO("VertexColor", m_vboColor);
218

219
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
220
221
}

222
223
unsigned int ShaderPhong::setAttributePosition(VBO* vbo)
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
224
	m_vboPos = vbo;
225
226
227
228
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
229
230
231
232
233
}

unsigned int ShaderPhong::setAttributeNormal(VBO* vbo)
{
	m_vboNormal = vbo;
234
235
236
237
	bind();
	unsigned int id = bindVA_VBO("VertexNormal", vbo);
	unbind();
	return id;
238
239
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
240
} // namespace Utils
241

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