Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

shaderVectorPerVertex.cpp 4.6 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
33
namespace Utils
{

Pierre Kraemer's avatar
merge..    
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
77
78

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

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

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
82
	std::string glxgeom = GLSLShader::defines_Geom("points", "line_strip", 4);
83
84
85
86
87
88
89
	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
merge..    
Pierre Kraemer committed
90
	// get and fill uniforms
91
	bind();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
92
93
	getLocations();
	sendParams();
94
95
}

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

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

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

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

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

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

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

} // namespace Utils

} // namespace CGoGN