Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

pointSprite.cpp 5.55 KB
Newer Older
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           *
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/                                           *
21
22
23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Sylvain Thery's avatar
Sylvain Thery committed
24
#define CGoGN_UTILS_DLL_EXPORT 1
25
26
27
28
#include "Utils/pointSprite.h"

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

30
31
32
namespace Utils
{

33
34
35
36
#include "pointSprite.vert"
#include "pointSprite.frag"
#include "pointSprite.geom"

37
38
39
40
PointSprite::PointSprite(bool withColorPerVertex, bool withPlane) :
	colorPerVertex(withColorPerVertex),
	plane(withPlane),
	m_size(1.0f),
41
42
43
44
45
	m_color(0.0f, 0.0f, 1.0f, 1.0f),
	m_lightPos(100.0f, 100.0f, 100.0f),
	m_ambiant(0.1f, 0.1f, 0.1f),
	m_eyePos(0.0f, 0.0f, 0.0f),
	m_planeClip(0.0f,0.0f,0.0f,0.0f)
46
47
{
	std::string glxvert(*GLSLShader::DEFINES_GL);
48
49
	if (withColorPerVertex)
		glxvert.append("#define WITH_COLOR_PER_VERTEX 1\n");
50
51
	glxvert.append(vertexShaderText);

52
53
54
55
	std::string glxgeom = GLSLShader::defines_Geom("points", "triangle_strip", 4);
	if (withColorPerVertex)
		glxgeom.append("#define WITH_COLOR_PER_VERTEX 1\n");
	if (withPlane)
56
		glxgeom.append("#define WITH_PLANE 1\n");
57
58
59
	glxgeom.append(geometryShaderText);

	std::string glxfrag(*GLSLShader::DEFINES_GL);
60
61
62
	if (withColorPerVertex)
		glxfrag.append("#define WITH_COLOR_PER_VERTEX 1\n");
	if (withPlane)
63
		glxfrag.append("#define WITH_PLANE 1\n");
64
65
	glxfrag.append(fragmentShaderText);

66
67
68
69
70
71
	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_POINTS, GL_TRIANGLE_STRIP, 4);

	// get and fill uniforms
	getLocations();
	sendParams();
}
72

73
74
void PointSprite::getLocations()
{
75
	bind();
76
	*m_uniform_size = glGetUniformLocation(program_handler(),"size");
77
78
	if (!colorPerVertex)
		*m_uniform_color = glGetUniformLocation(program_handler(),"colorsprite");
79
80
	*m_uniform_ambiant = glGetUniformLocation(program_handler(),"ambiant");
	*m_uniform_lightPos = glGetUniformLocation(program_handler(),"lightPos");
81
82
	if (plane)
		*m_uniform_eyePos = glGetUniformLocation(program_handler(),"eyePos");
83
	*m_unif_planeClip = glGetUniformLocation(this->program_handler(), "planeClip");
84
	unbind();
85
86
}

87
void PointSprite::sendParams()
88
{
89
90
91
92
93
94
95
96
	bind();
	glUniform1f(*m_uniform_size, m_size);
	if (!colorPerVertex)
		glUniform4fv(*m_uniform_color, 1, m_color.data());
	glUniform3fv(*m_uniform_ambiant, 1, m_ambiant.data());
	glUniform3fv(*m_uniform_lightPos, 1, m_lightPos.data());
	if (plane)
		glUniform3fv(*m_uniform_eyePos, 1, m_eyePos.data());
97
98
	if (*m_unif_planeClip > 0)
		glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
99
	unbind();
100
101
102
103
}

unsigned int PointSprite::setAttributePosition(VBO* vbo)
{
104
105
106
107
108
	m_vboPos = vbo;
	bind();
	unsigned int id = bindVA_VBO("VertexPosition", vbo);
	unbind();
	return id;
109
110
}

111
112
unsigned int PointSprite::setAttributeColor(VBO* vbo)
{
113
114
115
116
117
118
119
120
121
	if (colorPerVertex)
	{
		m_vboColor = vbo;
		bind();
		unsigned int id = bindVA_VBO("VertexColor", vbo);
		unbind();
		return id;
	}
	return 0;
122
123
}

124
void PointSprite::setSize(float size)
125
{
126
	m_size = size;
127
	bind();
128
129
	glUniform1f(*m_uniform_size, size);
	unbind();
130
131
}

132
void PointSprite::setColor(const Geom::Vec4f& color)
133
{
134
135
136
137
138
139
140
	if (!colorPerVertex)
	{
		m_color = color;
		bind();
		glUniform4fv(*m_uniform_color, 1, color.data());
		unbind();
	}
141
142
}

143
void PointSprite::setLightPosition(const Geom::Vec3f& pos)
144
{
145
	m_lightPos = pos;
146
	bind();
147
	glUniform3fv(*m_uniform_lightPos, 1, pos.data());
148
149
150
	unbind();
}

151
void PointSprite::setAmbiantColor(const Geom::Vec3f& amb)
152
{
153
	m_ambiant = amb;
154
	bind();
155
	glUniform3fv(*m_uniform_ambiant, 1, amb.data());
156
157
158
	unbind();
}

159
void PointSprite::setEyePosition(const Geom::Vec3f& ep)
160
{
161
	if (plane)
162
	{
163
164
165
166
		m_eyePos = ep;
		bind();
		glUniform3fv(*m_uniform_eyePos, 1, ep.data());
		unbind();
167
168
169
	}
}

170
171
172
173
174
175
176
177
178
void PointSprite::setClippingPlane(const Geom::Vec4f& plane)
{
	m_planeClip = plane;
	bind();
	glUniform4fv(*m_unif_planeClip, 1, plane.data());
	unbind();
}


179
void PointSprite::restoreUniformsAttribs()
180
{
181
182
	getLocations();
	sendParams();
183
184

	bind();
185
186
187
	bindVA_VBO("VertexPosition", m_vboPos);
	if (colorPerVertex)
		bindVA_VBO("VertexColor", m_vboColor);
188
189
190
	unbind();
}

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

} // namespace CGoGN