pointSprite.cpp 5.57 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
41
42
43
44
PointSprite::PointSprite(bool withColorPerVertex, bool withPlane) :
	colorPerVertex(withColorPerVertex),
	plane(withPlane),
	m_size(1.0f),
	m_color(Geom::Vec4f(0.0f, 0.0f, 1.0f, 1.0f)),
	m_lightPos(Geom::Vec3f(100.0f, 100.0f, 100.0f)),
	m_ambiant(Geom::Vec3f(0.1f, 0.1f, 0.1f)),
	m_eyePos(Geom::Vec3f(0.0f, 0.0f, 0.0f))
45
46
{
	std::string glxvert(*GLSLShader::DEFINES_GL);
47
48
	if (withColorPerVertex)
		glxvert.append("#define WITH_COLOR_PER_VERTEX 1\n");
49
50
	glxvert.append(vertexShaderText);

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

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

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

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

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

86
void PointSprite::sendParams()
87
{
88
89
90
91
92
93
94
95
	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());
96
97
	if (*m_unif_planeClip > 0)
		glUniform4fv(*m_unif_planeClip, 1, m_planeClip.data());
98
	unbind();
99
100
101
102
}

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

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

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

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

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

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

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

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


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

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

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

} // namespace CGoGN