shaderVectorPerVertex.cpp 4.78 KB
Newer Older
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           *
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/                                         *
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
25
#include <GL/glew.h>
26
#include "Utils/Shaders/shaderVectorPerVertex.h"
27 28 29

namespace CGoGN
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
30

31 32
namespace Utils
{
33 34 35
#include "shaderVectorPerVertex.vert"
#include "shaderVectorPerVertex.geom"
#include "shaderVectorPerVertex.frag"
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
//std::string ShaderVectorPerVertex::vertexShaderText =
//		"ATTRIBUTE vec3 VertexPosition;\n"
//		"ATTRIBUTE vec3 VertexVector;\n"
//		"VARYING_VERT vec3 VectorAttrib;\n"
//		"INVARIANT_POS;\n"
//		"void main ()\n"
//		"{\n"
//		"	VectorAttrib = VertexVector;\n"
//		"	gl_Position = vec4(VertexPosition, 1.0);\n"
//		"}";
//
//
//std::string ShaderVectorPerVertex::geometryShaderText =
//		"uniform float vectorScale;\n"
//		"uniform mat4 ModelViewProjectionMatrix;\n"
//		"VARYING_IN vec3 VectorAttrib[];\n"
//		"void main()\n"
//		"{\n"
//		"	gl_Position = ModelViewProjectionMatrix * POSITION_IN(0);\n"
//		"	EmitVertex();\n"
//		"	gl_Position = ModelViewProjectionMatrix * (POSITION_IN(0) + vec4(VectorAttrib[0] * vectorScale, 0.0));\n"
//		"	EmitVertex();\n"
//		"	EndPrimitive();\n"
//		"}";
//
//
//std::string ShaderVectorPerVertex::fragmentShaderText =
//		"PRECISON;\n"
//		"uniform vec4 vectorColor;\n"
//		"FRAG_OUT_DEF;\n"
//		"void main()\n"
//		"{\n"
//		"	gl_FragColor = vectorColor;\n"
//		"}";
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
71 72 73 74 75


ShaderVectorPerVertex::ShaderVectorPerVertex() :
	m_scale(1.0f),
	m_color(Geom::Vec4f(1.0f, 0.0f, 0.0f, 0.0f))
76
{
77 78 79 80 81

	m_nameVS = "ShaderVectorPerVertex_vs";
	m_nameFS = "ShaderVectorPerVertex_fs";
	m_nameGS = "ShaderVectorPerVertex_gs";

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

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
85
	std::string glxgeom = GLSLShader::defines_Geom("points", "line_strip", 4);
86 87 88 89 90
	glxgeom.append(geometryShaderText);

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

Sylvain Thery's avatar
Sylvain Thery committed
91
	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_POINTS, GL_LINE_STRIP,2);
92

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
93
	// get and fill uniforms
94
	bind();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
95 96
	getLocations();
	sendParams();
97 98
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
99
void ShaderVectorPerVertex::getLocations()
100
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
101 102
	m_uniform_scale = glGetUniformLocation(this->program_handler(), "vectorScale");
	m_uniform_color = glGetUniformLocation(this->program_handler(), "vectorColor");
103 104
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
105
void ShaderVectorPerVertex::sendParams()
106
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
107 108
	glUniform1f(m_uniform_scale, m_scale);
	glUniform4fv(m_uniform_color, 1, m_color.data());
109 110
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
111
void ShaderVectorPerVertex::setScale(float scale)
112 113 114
{
	bind();
	glUniform1f(m_uniform_scale, scale);
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
115
	m_scale = scale;
116 117
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
118
void ShaderVectorPerVertex::setColor(const Geom::Vec4f& color)
119 120
{
	bind();
121
	glUniform4fv(m_uniform_color, 1, color.data());
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
122 123 124 125 126 127 128
	m_color = color;
}

unsigned int ShaderVectorPerVertex::setAttributePosition(VBO* vbo)
{
	m_vboPos = vbo;
	return bindVA_VBO("VertexPosition", vbo);
129 130
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
131 132 133 134
unsigned int ShaderVectorPerVertex::setAttributeVector(VBO* vbo)
{
	m_vboVec = vbo;
	return bindVA_VBO("VertexVector", vbo);
135
}
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
136 137 138 139 140

void ShaderVectorPerVertex::restoreUniformsAttribs()
{
	getLocations();
	sendParams();
141
	bind();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
142 143
	bindVA_VBO("VertexPosition", m_vboPos);
	bindVA_VBO("VertexVector", m_vboVec);
144
	unbind();
145
}
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
146 147 148 149

} // namespace Utils

} // namespace CGoGN