shaderExplodeVolumes.cpp 6.58 KB
Newer Older
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           *
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/                                           *
21 22 23
* 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
#include <string.h>
#include "Utils/Shaders/shaderExplodeVolumes.h"

namespace CGoGN
{

namespace Utils
{
33

34 35 36 37 38
#include "shaderExplodeVolumes.vert"
#include "shaderExplodeVolumes.frag"
#include "shaderExplodeVolumes.geom"


39
ShaderExplodeVolumes::ShaderExplodeVolumes(bool withColorPerFace, bool withExplodeFace):
40 41
	m_wcpf(withColorPerFace),
	m_wef(withExplodeFace)
42 43 44 45 46
{
	m_nameVS = "ShaderExplodeVolumes_vs";
	m_nameFS = "ShaderExplodeVolumes_fs";
	m_nameGS = "ShaderExplodeVolumes_gs";

47
	std::string glxvert(GLSLShader::defines_gl());
48 49
	glxvert.append(vertexShaderText);

50 51 52
	std::string glxgeom;
	glxgeom.append(GLSLShader::defines_Geom("lines_witw_adjacency", "triangle_strip", 3));

53 54
	if (withColorPerFace)
		glxgeom.append("#define WITH_COLORPF 1\n");
55 56
	if (withExplodeFace)
		glxgeom.append("#define WITH_EXPLODE_FACE 1\n");
57 58
	glxgeom.append(geometryShaderText);

59
	std::string glxfrag(GLSLShader::defines_gl());
60 61
	glxfrag.append(fragmentShaderText);

62
	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_LINES_ADJACENCY_EXT , GL_TRIANGLE_STRIP,4);
63 64 65 66 67

	getLocations();

	//Default values
	m_explodeV = 0.9f;
68
	m_explodeF = 0.9f;
69
	m_ambiant = Geom::Vec4f(0.05f, 0.05f, 0.1f, 0.0f);
70
	m_backColor = Geom::Vec4f(1.0f, 0.1f, 0.1f, 0.0f);
71
	m_light_pos = Geom::Vec3f(10.0f, 10.0f, 1000.0f);
72
	m_plane   = Geom::Vec4f(0.0f, 0.0f, 1000.f, 1000000000000000000000000000.0f);
73

74
	setParams(m_explodeV, m_explodeF, m_ambiant, m_backColor, m_light_pos, m_plane);
75 76 77 78
}

void ShaderExplodeVolumes::getLocations()
{
79
	bind();
80 81 82 83 84 85
	*m_unif_explodeV  = glGetUniformLocation(program_handler(),"explodeV");
	*m_unif_explodeF  = glGetUniformLocation(program_handler(),"explodeF");
	*m_unif_ambiant  = glGetUniformLocation(program_handler(),"ambient");
	*m_unif_backColor  = glGetUniformLocation(program_handler(),"backColor");
	*m_unif_lightPos = glGetUniformLocation(program_handler(),"lightPosition");
	*m_unif_plane   = glGetUniformLocation(program_handler(),"plane");
86
	unbind();
87 88
}

89
unsigned int ShaderExplodeVolumes::setAttributePosition(VBO* vbo)
90 91
{
	m_vboPos = vbo;
92 93 94 95
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
96 97
}

98
unsigned int ShaderExplodeVolumes::setAttributeColor(VBO* vbo)
99
{
100
	m_vboColors = vbo;
101 102 103 104
	bind();
	unsigned int id = bindVA_VBO("VertexColor", vbo);
	unbind();
	return id;
105
}
106

107 108
void ShaderExplodeVolumes::setParams(float explV, float explF, const Geom::Vec4f& ambiant, const Geom::Vec4f& backColor, const Geom::Vec3f& lightPos, const Geom::Vec4f& plane)
{
109
	bind();
110
	m_explodeV = explV;
111
	glUniform1f(*m_unif_explodeV, explV);
112
	m_explodeF = explF;
113
	glUniform1f(*m_unif_explodeF, explF);
114
	m_ambiant = ambiant;
115
	glUniform4fv(*m_unif_ambiant, 1, ambiant.data());
116
	m_backColor = backColor;
117
	glUniform4fv(*m_unif_backColor, 1, backColor.data());
118 119

	m_light_pos = lightPos;
120
	glUniform3fv(*m_unif_lightPos, 1, lightPos.data());
121 122

	m_plane = plane;
123
	glUniform4fv(*m_unif_plane,    1, m_plane.data());
124

125
	unbind();
126 127 128 129 130 131
}

void ShaderExplodeVolumes::setExplodeVolumes(float explode)
{
	m_explodeV = explode;
	bind();
132
	glUniform1f(*m_unif_explodeV, explode);
133
	unbind();
134 135
}

136 137 138 139
void ShaderExplodeVolumes::setExplodeFaces(float explode)
{
	m_explodeF = explode;
	bind();
140
	glUniform1f(*m_unif_explodeF, explode);
141
	unbind();
142 143
}

144 145 146 147
void ShaderExplodeVolumes::setAmbiant(const Geom::Vec4f& ambiant)
{
	m_ambiant = ambiant;
	bind();
148
	glUniform4fv(*m_unif_ambiant,1, ambiant.data());
149
	unbind();
150 151
}

152
void ShaderExplodeVolumes::setBackColor(const Geom::Vec4f& backColor)
153
{
154
	m_backColor = backColor;
155
	bind();
156
	glUniform4fv(*m_unif_backColor, 1, backColor.data());
157
	unbind();
158 159 160 161 162 163
}

void ShaderExplodeVolumes::setLightPosition(const Geom::Vec3f& lp)
{
	m_light_pos = lp;
	bind();
164
	glUniform3fv(*m_unif_lightPos,1,lp.data());
165
	unbind();
166 167
}

168 169 170 171
void ShaderExplodeVolumes::setClippingPlane(const Geom::Vec4f& plane)
{
	m_plane = plane;
	bind();
172
	glUniform4fv(*m_unif_plane,1, plane.data());
173
	unbind();
174 175
}

176 177 178
void ShaderExplodeVolumes::restoreUniformsAttribs()
{
	bind();
179

180 181
	*m_unif_explodeV   = glGetUniformLocation(program_handler(),"explodeV");
	glUniform1f (*m_unif_explodeV, m_explodeV);
182

183 184
	*m_unif_explodeF   = glGetUniformLocation(program_handler(),"explodeF");
	glUniform1f (*m_unif_explodeF, m_explodeF);
185

186 187
	*m_unif_ambiant   = glGetUniformLocation(program_handler(),"ambient");
	glUniform4fv(*m_unif_ambiant,  1, m_ambiant.data());
188

189 190
	*m_unif_backColor = glGetUniformLocation(program_handler(),"backColor");
	glUniform4fv(*m_unif_backColor, 1, m_backColor.data());
191

192 193
	*m_unif_lightPos =  glGetUniformLocation(program_handler(),"lightPosition");
	glUniform3fv(*m_unif_lightPos, 1, m_light_pos.data());
194

195 196
	*m_unif_plane   = glGetUniformLocation(program_handler(),"plane");
	glUniform4fv(*m_unif_plane,    1, m_plane.data());
197 198

	bindVA_VBO("VertexPosition", m_vboPos);
199
	bindVA_VBO("VertexColor", m_vboColors);
200

201 202 203 204 205 206
	unbind();
}

} // namespace Utils

} // namespace CGoGN