pointSprite.cpp 5.22 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 24 25 26 27 28
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Utils/pointSprite.h"

namespace CGoGN
{
Pierre Kraemer's avatar
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 82
	if (plane)
		*m_uniform_eyePos = glGetUniformLocation(program_handler(),"eyePos");
	unbind();
83 84
}

85
void PointSprite::sendParams()
86
{
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());
	unbind();
96 97 98 99
}

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

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

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

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

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

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

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

166
void PointSprite::restoreUniformsAttribs()
167
{
168 169
	getLocations();
	sendParams();
170 171

	bind();
172 173 174
	bindVA_VBO("VertexPosition", m_vboPos);
	if (colorPerVertex)
		bindVA_VBO("VertexColor", m_vboColor);
175 176 177
	unbind();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
178 179 180
} // namespace Utils

} // namespace CGoGN