shaderFlat.cpp 6.05 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <string.h>
#include <GL/glew.h>
27
#include "Utils/Shaders/shaderFlat.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
34
namespace Utils
{

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
std::string ShaderFlat::vertexShaderText =
"ATTRIBUTE vec3 VertexPosition;\n"
"void main()\n"
"{\n"
"	gl_Position = vec4(VertexPosition, 1.0);\n"
"}";


std::string ShaderFlat::geometryShaderText =
"uniform float explode;\n"
"uniform mat4 ModelViewProjectionMatrix;\n"
"uniform mat4 NormalMatrix;\n"
"uniform mat4 ModelViewMatrix;\n"
"uniform vec3 lightPosition;\n"
"uniform vec4 diffuse;\n"
"uniform vec4 ambient;\n"
"VARYING_OUT vec4 ColorFS;\n"
"void main(void)\n"
"{\n"
"	vec3 v1 = POSITION_IN(1).xyz - POSITION_IN(0).xyz;\n"
"	vec3 v2 = POSITION_IN(2).xyz - POSITION_IN(0).xyz;\n"
"	vec3 N  = cross(v1,v2);\n"
"	N  =  normalize (vec3(NormalMatrix*vec4(N,0.0))); \n"
"	vec3 center = POSITION_IN(0).xyz + POSITION_IN(1).xyz + POSITION_IN(2).xyz; \n"
"	center /= 3.0;\n"
"	vec4 newPos =  ModelViewMatrix * vec4(center,0.0);\n"
"	vec3 L =  normalize (lightPosition - newPos.xyz);\n"
"	float lambertTerm = dot(N,L);\n"
"	ColorFS = ambient;\n"
"	if(lambertTerm > 0.0)\n"
"		ColorFS += diffuse * lambertTerm;\n"
"	int i;\n"
67
"	for(i=0; i< NBVERTS_IN; i++)\n"
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"	{\n"
"		vec4 pos =  explode * POSITION_IN(i) + (1.0-explode)* vec4(center,1.0);\n"
"		gl_Position = ModelViewProjectionMatrix *  pos;\n"
"		EmitVertex();\n"
"	}\n"
"	EndPrimitive();\n"
"}";

std::string ShaderFlat::fragmentShaderText =
"VARYING_FRAG vec4 ColorFS; \n"
"void main()\n"
"{\n"
"	gl_FragColor = ColorFS;\n"
"}";
Sylvain Thery's avatar
Sylvain Thery committed
82
83
84
85


ShaderFlat::ShaderFlat()
{
Sylvain Thery's avatar
Sylvain Thery committed
86
87
88
89
	m_nameVS = "ShaderFlat_vs";
	m_nameFS = "ShaderFlat_fs";
	m_nameGS = "ShaderFlat_gs";

90
91
92
	std::string glxvert(*GLSLShader::DEFINES_GL);
	glxvert.append(vertexShaderText);

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
93
	std::string glxgeom = GLSLShader::defines_Geom("triangles", "triangle_strip", 3);
94
95
96
97
98
99
100
101
	glxgeom.append(geometryShaderText);

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

	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_TRIANGLE_STRIP);

	getLocations();
Sylvain Thery's avatar
Sylvain Thery committed
102
103

	//Default values
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
104
105
106
107
	m_explode = 1.0f;
	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_light_pos = Geom::Vec3f(10.0f, 10.0f, 1000.0f);
Sylvain Thery's avatar
Sylvain Thery committed
108

109
110
111
112
113
	setParams(m_explode, m_ambiant, m_diffuse, m_light_pos);
}

void ShaderFlat::getLocations()
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
114
115
116
117
	m_unif_explode  = glGetUniformLocation(program_handler(),"explode");
	m_unif_ambiant  = glGetUniformLocation(program_handler(),"ambient");
	m_unif_diffuse  = glGetUniformLocation(program_handler(),"diffuse");
	m_unif_lightPos = glGetUniformLocation(program_handler(),"lightPosition");
118
}
Sylvain Thery's avatar
Sylvain Thery committed
119

120
121
122
123
void ShaderFlat::setAttributePosition(VBO* vbo)
{
	m_vboPos = vbo;
	bindVA_VBO("VertexPosition", vbo);
Sylvain Thery's avatar
Sylvain Thery committed
124
125
126
127
}

void ShaderFlat::setParams(float expl, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec3f& lightPos)
{
128
129
130
131
132
133
	m_explode = expl;
	m_ambiant = ambiant;
	m_diffuse = diffuse;
	m_light_pos = lightPos;

	bind();
Sylvain Thery's avatar
Sylvain Thery committed
134

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
135
136
137
138
	glUniform1f(m_unif_explode, expl);
	glUniform4fv(m_unif_ambiant, 1, ambiant.data());
	glUniform4fv(m_unif_diffuse, 1, diffuse.data());
	glUniform3fv(m_unif_lightPos, 1, lightPos.data());
139
140
141
142
143
144
145
146

	unbind(); // ??
}

void ShaderFlat::setExplode(float explode)
{
	m_explode = explode;
	bind();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
147
	glUniform1f(m_unif_explode, explode);
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
}

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

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

void ShaderFlat::setLightPosition(const Geom::Vec3f& lp)
{
	m_light_pos = lp;
	bind();
	glUniform3fv(m_unif_lightPos,1,lp.data());
}

void ShaderFlat::restoreUniformsAttribs()
{
	m_unif_explode   = glGetUniformLocation(program_handler(),"explode");
	m_unif_ambiant   = glGetUniformLocation(program_handler(),"ambient");
	m_unif_diffuse   = glGetUniformLocation(program_handler(),"diffuse");
	m_unif_lightPos =  glGetUniformLocation(program_handler(),"lightPosition");

	bind();
	glUniform1f (m_unif_explode, m_explode);
	glUniform4fv(m_unif_ambiant,  1, m_ambiant.data());
	glUniform4fv(m_unif_diffuse,  1, m_diffuse.data());
	glUniform3fv(m_unif_lightPos, 1, m_light_pos.data());

	bindVA_VBO("VertexPosition", m_vboPos);
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
186
187
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
188
} // namespace Utils
Sylvain Thery's avatar
Sylvain Thery committed
189

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