shaderExplodeVolumes.cpp 6.36 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <string.h>
#include "Utils/Shaders/shaderExplodeVolumes.h"

namespace CGoGN
{

namespace Utils
{
#include "shaderExplodeVolumes.vert"
#include "shaderExplodeVolumes.frag"
#include "shaderExplodeVolumes.geom"


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

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

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

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

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

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

	getLocations();

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

72
	setParams(m_explodeV, m_explodeF, m_ambiant, m_backColor, m_light_pos, m_plane);
73 74 75 76
}

void ShaderExplodeVolumes::getLocations()
{
77 78 79 80 81 82
	*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");
83 84 85 86 87 88 89 90
}

void ShaderExplodeVolumes::setAttributePosition(VBO* vbo)
{
	m_vboPos = vbo;
	bindVA_VBO("VertexPosition", vbo);
}

91
void ShaderExplodeVolumes::setAttributeColor(VBO* vbo)
92
{
93 94 95
	m_vboColors = vbo;
	bindVA_VBO("VertexColor", vbo);
}
96

97 98
void ShaderExplodeVolumes::setParams(float explV, float explF, const Geom::Vec4f& ambiant, const Geom::Vec4f& backColor, const Geom::Vec3f& lightPos, const Geom::Vec4f& plane)
{
99
	bind();
100
	m_explodeV = explV;
101
	glUniform1f(*m_unif_explodeV, explV);
102
	m_explodeF = explF;
103
	glUniform1f(*m_unif_explodeF, explF);
104
	m_ambiant = ambiant;
105
	glUniform4fv(*m_unif_ambiant, 1, ambiant.data());
106
	m_backColor = backColor;
107
	glUniform4fv(*m_unif_backColor, 1, backColor.data());
108 109

	m_light_pos = lightPos;
110
	glUniform3fv(*m_unif_lightPos, 1, lightPos.data());
111 112

	m_plane = plane;
113
	glUniform4fv(*m_unif_plane,    1, m_plane.data());
114 115 116 117

	unbind(); // ??
}

118

119 120 121 122
void ShaderExplodeVolumes::setExplodeVolumes(float explode)
{
	m_explodeV = explode;
	bind();
123
	glUniform1f(*m_unif_explodeV, explode);
124 125
}

126 127 128 129
void ShaderExplodeVolumes::setExplodeFaces(float explode)
{
	m_explodeF = explode;
	bind();
130
	glUniform1f(*m_unif_explodeF, explode);
131 132
}

133 134 135 136 137

void ShaderExplodeVolumes::setAmbiant(const Geom::Vec4f& ambiant)
{
	m_ambiant = ambiant;
	bind();
138
	glUniform4fv(*m_unif_ambiant,1, ambiant.data());
139 140
}

141
void ShaderExplodeVolumes::setBackColor(const Geom::Vec4f& backColor)
142
{
143
	m_backColor = backColor;
144
	bind();
145
	glUniform4fv(*m_unif_backColor, 1, backColor.data());
146 147 148 149 150 151
}

void ShaderExplodeVolumes::setLightPosition(const Geom::Vec3f& lp)
{
	m_light_pos = lp;
	bind();
152
	glUniform3fv(*m_unif_lightPos,1,lp.data());
153 154
}

155 156 157 158 159

void ShaderExplodeVolumes::setClippingPlane(const Geom::Vec4f& plane)
{
	m_plane = plane;
	bind();
160
	glUniform4fv(*m_unif_plane,1, plane.data());
161 162 163
}


164 165 166
void ShaderExplodeVolumes::restoreUniformsAttribs()
{
	bind();
167

168 169
	*m_unif_explodeV   = glGetUniformLocation(program_handler(),"explodeV");
	glUniform1f (*m_unif_explodeV, m_explodeV);
170

171 172
	*m_unif_explodeF   = glGetUniformLocation(program_handler(),"explodeF");
	glUniform1f (*m_unif_explodeF, m_explodeF);
173

174 175
	*m_unif_ambiant   = glGetUniformLocation(program_handler(),"ambient");
	glUniform4fv(*m_unif_ambiant,  1, m_ambiant.data());
176

177 178
	*m_unif_backColor = glGetUniformLocation(program_handler(),"backColor");
	glUniform4fv(*m_unif_backColor, 1, m_backColor.data());
179

180 181
	*m_unif_lightPos =  glGetUniformLocation(program_handler(),"lightPosition");
	glUniform3fv(*m_unif_lightPos, 1, m_light_pos.data());
182

183 184
	*m_unif_plane   = glGetUniformLocation(program_handler(),"plane");
	glUniform4fv(*m_unif_plane,    1, m_plane.data());
185 186

	bindVA_VBO("VertexPosition", m_vboPos);
187
	bindVA_VBO("VertexColor", m_vboColors);
188 189 190 191 192 193
	unbind();
}

} // namespace Utils

} // namespace CGoGN