shaderFlat.cpp 6.21 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-2011, 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.u-strasbg.fr/                                         *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23 24 25 26
* 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
namespace Utils
{
34 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include "shaderFlat.vert"
#include "shaderFlat.frag"
#include "shaderFlat.geom"

//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"
//"	for(i=0; i< NBVERTS_IN; i++)\n"
//"	{\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
85 86 87 88


ShaderFlat::ShaderFlat()
{
Sylvain Thery's avatar
Sylvain Thery committed
89 90 91 92
	m_nameVS = "ShaderFlat_vs";
	m_nameFS = "ShaderFlat_fs";
	m_nameGS = "ShaderFlat_gs";

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

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

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

Sylvain Thery's avatar
Sylvain Thery committed
102
	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_TRIANGLES, GL_TRIANGLE_STRIP,3);
103 104

	getLocations();
Sylvain Thery's avatar
Sylvain Thery committed
105 106

	//Default values
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
107 108 109 110
	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
111

112 113 114 115 116
	setParams(m_explode, m_ambiant, m_diffuse, m_light_pos);
}

void ShaderFlat::getLocations()
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
117 118 119 120
	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");
121
}
Sylvain Thery's avatar
Sylvain Thery committed
122

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

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

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

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
138 139 140 141
	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());
142 143 144 145 146 147 148 149

	unbind(); // ??
}

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

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
189 190
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
191
} // namespace Utils
Sylvain Thery's avatar
Sylvain Thery committed
192

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