shaderFlat.cpp 5.83 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 <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
ShaderFlat::ShaderFlat():
40
m_doubleSided(1)
Sylvain Thery's avatar
Sylvain Thery committed
41
{
Sylvain Thery's avatar
Sylvain Thery committed
42 43 44 45
	m_nameVS = "ShaderFlat_vs";
	m_nameFS = "ShaderFlat_fs";
	m_nameGS = "ShaderFlat_gs";

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

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

52
	std::string glxfrag(GLSLShader::defines_gl());
53 54
	glxfrag.append(fragmentShaderText);

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

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

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

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

71 72
void ShaderFlat::setDoubleSided(bool doubleSided)
{
73
	m_doubleSided = doubleSided;
74 75

	bind();
76
	glUniform1i(*m_unif_doubleSided, m_doubleSided);
77 78 79 80
	unbind();

}

81 82
void ShaderFlat::getLocations()
{
83 84 85
	*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
86
	*m_unif_diffuseback  = glGetUniformLocation(program_handler(), "diffuseBack");
87
	*m_unif_lightPos = glGetUniformLocation(program_handler(), "lightPosition");
88
	*m_unif_doubleSided = glGetUniformLocation(this->program_handler(), "doubleSided");
89
}
Sylvain Thery's avatar
Sylvain Thery committed
90

91
unsigned int ShaderFlat::setAttributePosition(VBO* vbo)
92 93
{
	m_vboPos = vbo;
94 95 96 97
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
Sylvain Thery's avatar
Sylvain Thery committed
98 99
}

Sylvain Thery's avatar
Sylvain Thery committed
100
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
101
{
102 103 104 105 106 107
	m_explode = expl;
	m_ambiant = ambiant;
	m_diffuse = diffuse;
	m_light_pos = lightPos;

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

109 110 111
	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
112
	glUniform4fv(*m_unif_diffuseback, 1, diffuseBack.data());
113
	glUniform3fv(*m_unif_lightPos, 1, lightPos.data());
114

115
	unbind();
116 117 118 119 120 121
}

void ShaderFlat::setExplode(float explode)
{
	m_explode = explode;
	bind();
122
	glUniform1f(*m_unif_explode, explode);
123
	unbind();
124 125 126 127 128 129
}

void ShaderFlat::setAmbiant(const Geom::Vec4f& ambiant)
{
	m_ambiant = ambiant;
	bind();
130
	glUniform4fv(*m_unif_ambiant,1, ambiant.data());
131
	unbind();
132 133 134 135 136 137
}

void ShaderFlat::setDiffuse(const Geom::Vec4f& diffuse)
{
	m_diffuse = diffuse;
	bind();
138
	glUniform4fv(*m_unif_diffuse,1, diffuse.data());
139
	unbind();
140 141
}

142
void ShaderFlat::setBackColor(const Geom::Vec4f& diffuseb)
Sylvain Thery's avatar
Sylvain Thery committed
143 144 145 146 147 148 149
{
	m_diffuseBack = diffuseb;
	bind();
	glUniform4fv(*m_unif_diffuseback,1, diffuseb.data());
	unbind();
}

150 151 152 153
void ShaderFlat::setLightPosition(const Geom::Vec3f& lp)
{
	m_light_pos = lp;
	bind();
154
	glUniform3fv(*m_unif_lightPos,1,lp.data());
155
	unbind();
156 157 158 159
}

void ShaderFlat::restoreUniformsAttribs()
{
Sylvain Thery's avatar
Sylvain Thery committed
160 161 162 163 164
	*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");
165
	*m_unif_doubleSided = glGetUniformLocation(this->program_handler(), "doubleSided");
166 167

	bind();
168

169 170 171
	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
172
	glUniform4fv(*m_unif_diffuseback,  1, m_diffuseBack.data());
173
	glUniform3fv(*m_unif_lightPos, 1, m_light_pos.data());
174
	glUniform1i(*m_unif_doubleSided, m_doubleSided);
175 176

	bindVA_VBO("VertexPosition", m_vboPos);
177

178
	unbind();
Sylvain Thery's avatar
Sylvain Thery committed
179 180
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
181
} // namespace Utils
Sylvain Thery's avatar
Sylvain Thery committed
182

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