shaderVectorPerVertex.cpp 4.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

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

31 32 33
namespace Utils
{

Pierre Kraemer's avatar
Pierre Kraemer committed
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
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"
		"}";


ShaderVectorPerVertex::ShaderVectorPerVertex() :
	m_scale(1.0f),
	m_color(Geom::Vec4f(1.0f, 0.0f, 0.0f, 0.0f))
73 74 75 76
{
	std::string glxvert(*GLSLShader::DEFINES_GL);
	glxvert.append(vertexShaderText);

Pierre Kraemer's avatar
Pierre Kraemer committed
77
	std::string glxgeom = GLSLShader::defines_Geom("points", "line_strip", 4);
78 79 80 81 82 83 84
	glxgeom.append(geometryShaderText);

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

	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_POINTS, GL_LINE_STRIP);

Pierre Kraemer's avatar
Pierre Kraemer committed
85
	// get and fill uniforms
86
	bind();
Pierre Kraemer's avatar
Pierre Kraemer committed
87 88
	getLocations();
	sendParams();
89 90
}

Pierre Kraemer's avatar
Pierre Kraemer committed
91
void ShaderVectorPerVertex::getLocations()
92
{
Pierre Kraemer's avatar
Pierre Kraemer committed
93 94
	m_uniform_scale = glGetUniformLocation(this->program_handler(), "vectorScale");
	m_uniform_color = glGetUniformLocation(this->program_handler(), "vectorColor");
95 96
}

Pierre Kraemer's avatar
Pierre Kraemer committed
97
void ShaderVectorPerVertex::sendParams()
98
{
Pierre Kraemer's avatar
Pierre Kraemer committed
99 100
	glUniform1f(m_uniform_scale, m_scale);
	glUniform4fv(m_uniform_color, 1, m_color.data());
101 102
}

Pierre Kraemer's avatar
Pierre Kraemer committed
103
void ShaderVectorPerVertex::setScale(float scale)
104 105 106
{
	bind();
	glUniform1f(m_uniform_scale, scale);
Pierre Kraemer's avatar
Pierre Kraemer committed
107
	m_scale = scale;
108 109
}

Pierre Kraemer's avatar
Pierre Kraemer committed
110
void ShaderVectorPerVertex::setColor(const Geom::Vec4f& color)
111 112
{
	bind();
113
	glUniform4fv(m_uniform_color, 1, color.data());
Pierre Kraemer's avatar
Pierre Kraemer committed
114 115 116 117 118 119 120
	m_color = color;
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
123 124 125 126
unsigned int ShaderVectorPerVertex::setAttributeVector(VBO* vbo)
{
	m_vboVec = vbo;
	return bindVA_VBO("VertexVector", vbo);
127
}
Pierre Kraemer's avatar
Pierre Kraemer committed
128 129 130 131 132 133 134 135

void ShaderVectorPerVertex::restoreUniformsAttribs()
{
	getLocations();
	sendParams();

	bindVA_VBO("VertexPosition", m_vboPos);
	bindVA_VBO("VertexVector", m_vboVec);
136
}
Pierre Kraemer's avatar
Pierre Kraemer committed
137 138 139 140

} // namespace Utils

} // namespace CGoGN