Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

shaderFlat.cpp 5.42 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
24
25
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <string.h>
26
#include "Utils/Shaders/shaderFlat.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
36
37
#include "shaderFlat.vert"
#include "shaderFlat.frag"
#include "shaderFlat.geom"

Sylvain Thery's avatar
Sylvain Thery committed
38
39
40

ShaderFlat::ShaderFlat()
{
Sylvain Thery's avatar
Sylvain Thery committed
41
42
43
44
	m_nameVS = "ShaderFlat_vs";
	m_nameFS = "ShaderFlat_fs";
	m_nameGS = "ShaderFlat_gs";

45
46
47
	std::string glxvert(*GLSLShader::DEFINES_GL);
	glxvert.append(vertexShaderText);

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
48
	std::string glxgeom = GLSLShader::defines_Geom("triangles", "triangle_strip", 3);
49
50
51
52
53
	glxgeom.append(geometryShaderText);

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

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

56
	bind();
57
	getLocations();
58
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
59
60

	//Default values
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
61
62
63
	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);
Sylvain Thery's avatar
Sylvain Thery committed
64
	m_diffuseBack = m_diffuse;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
65
	m_light_pos = Geom::Vec3f(10.0f, 10.0f, 1000.0f);
Sylvain Thery's avatar
Sylvain Thery committed
66

Sylvain Thery's avatar
Sylvain Thery committed
67
	setParams(m_explode, m_ambiant, m_diffuse, m_diffuseBack ,m_light_pos);
68
69
70
71
}

void ShaderFlat::getLocations()
{
72
73
74
	*m_unif_explode  = glGetUniformLocation(program_handler(), "explode");
	*m_unif_ambiant  = glGetUniformLocation(program_handler(), "ambient");
	*m_unif_diffuse  = glGetUniformLocation(program_handler(), "diffuse");
Sylvain Thery's avatar
Sylvain Thery committed
75
	*m_unif_diffuseback  = glGetUniformLocation(program_handler(), "diffuseBack");
76
	*m_unif_lightPos = glGetUniformLocation(program_handler(), "lightPosition");
77
}
Sylvain Thery's avatar
Sylvain Thery committed
78

79
unsigned int ShaderFlat::setAttributePosition(VBO* vbo)
80
81
{
	m_vboPos = vbo;
82
83
84
85
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
Sylvain Thery's avatar
Sylvain Thery committed
86
87
}

Sylvain Thery's avatar
Sylvain Thery committed
88
void ShaderFlat::setParams(float expl, const Geom::Vec4f& ambiant, const Geom::Vec4f& diffuse, const Geom::Vec4f& diffuseBack, const Geom::Vec3f& lightPos)
Sylvain Thery's avatar
Sylvain Thery committed
89
{
90
91
92
93
94
95
	m_explode = expl;
	m_ambiant = ambiant;
	m_diffuse = diffuse;
	m_light_pos = lightPos;

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

97
98
99
	glUniform1f(*m_unif_explode, expl);
	glUniform4fv(*m_unif_ambiant, 1, ambiant.data());
	glUniform4fv(*m_unif_diffuse, 1, diffuse.data());
Sylvain Thery's avatar
Sylvain Thery committed
100
	glUniform4fv(*m_unif_diffuseback, 1, diffuseBack.data());
101
	glUniform3fv(*m_unif_lightPos, 1, lightPos.data());
102

103
	unbind();
104
105
106
107
108
109
}

void ShaderFlat::setExplode(float explode)
{
	m_explode = explode;
	bind();
110
	glUniform1f(*m_unif_explode, explode);
111
	unbind();
112
113
114
115
116
117
}

void ShaderFlat::setAmbiant(const Geom::Vec4f& ambiant)
{
	m_ambiant = ambiant;
	bind();
118
	glUniform4fv(*m_unif_ambiant,1, ambiant.data());
119
	unbind();
120
121
122
123
124
125
}

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

Sylvain Thery's avatar
Sylvain Thery committed
130
131
132
133
134
135
136
137
void ShaderFlat::setDiffuseBack(const Geom::Vec4f& diffuseb)
{
	m_diffuseBack = diffuseb;
	bind();
	glUniform4fv(*m_unif_diffuseback,1, diffuseb.data());
	unbind();
}

138
139
140
141
void ShaderFlat::setLightPosition(const Geom::Vec3f& lp)
{
	m_light_pos = lp;
	bind();
142
	glUniform3fv(*m_unif_lightPos,1,lp.data());
143
	unbind();
144
145
146
147
}

void ShaderFlat::restoreUniformsAttribs()
{
Sylvain Thery's avatar
Sylvain Thery committed
148
149
150
151
152
	*m_unif_explode     = glGetUniformLocation(program_handler(),"explode");
	*m_unif_ambiant     = glGetUniformLocation(program_handler(),"ambient");
	*m_unif_diffuse     = glGetUniformLocation(program_handler(),"diffuse");
	*m_unif_diffuseback = glGetUniformLocation(program_handler(),"diffuseBack");
	*m_unif_lightPos    =  glGetUniformLocation(program_handler(),"lightPosition");
153
154

	bind();
155

156
157
158
	glUniform1f (*m_unif_explode, m_explode);
	glUniform4fv(*m_unif_ambiant,  1, m_ambiant.data());
	glUniform4fv(*m_unif_diffuse,  1, m_diffuse.data());
Sylvain Thery's avatar
Sylvain Thery committed
159
	glUniform4fv(*m_unif_diffuseback,  1, m_diffuseBack.data());
160
	glUniform3fv(*m_unif_lightPos, 1, m_light_pos.data());
161
162

	bindVA_VBO("VertexPosition", m_vboPos);
163

164
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
165
166
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
167
} // namespace Utils
Sylvain Thery's avatar
Sylvain Thery committed
168

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