pointSprite.cpp 5.78 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
merge..    
Pierre Kraemer committed
29

30
31
32
namespace Utils
{

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

37

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
38
unsigned char* PointSprite::m_ptrSphere = NULL;
39

40
41
42
PointSprite* PointSprite::m_instance0 = NULL;


43

Sylvain Thery's avatar
Sylvain Thery committed
44
PointSprite::PointSprite(bool withColorPervertex, float radius,  bool with_plane)
45
{
46
47
	std::string defineColor("#define WITH_COLOR_PER_VERTEX 1\n");

48
	std::string glxvert(*GLSLShader::DEFINES_GL);
49
50
	if (withColorPervertex)
		glxvert.append(defineColor);
51
52
53
	glxvert.append(vertexShaderText);

	std::string glxgeom = GLSLShader::defines_Geom("points","triangle_strip",4);
54
55
	if (withColorPervertex)
		glxgeom.append(defineColor);
56
57
58
	if (with_plane)
		glxgeom.append("#define WITH_PLANE 1\n");

59
60
61
	glxgeom.append(geometryShaderText);

	std::string glxfrag(*GLSLShader::DEFINES_GL);
62
63
	if (withColorPervertex)
		glxfrag.append(defineColor);
64
65
	if (with_plane)
		glxfrag.append("#define WITH_PLANE 1\n");
66
67
	glxfrag.append(fragmentShaderText);

Sylvain Thery's avatar
Sylvain Thery committed
68
	loadShadersFromMemory(glxvert.c_str(), glxfrag.c_str(), glxgeom.c_str(), GL_POINTS, GL_TRIANGLE_STRIP,4);
69
70

	bind();
71
72
73
	*m_uniform_size = glGetUniformLocation(program_handler(),"size");
	*m_uniform_color = glGetUniformLocation(program_handler(),"colorsprite");
	glUniform1f(*m_uniform_size, radius);
74
75
76
77
78
79
	unbind();

	// load texture
	if (m_ptrSphere == NULL)
	{
		computeSphere();
80
81
82
		m_instance0 = this;
		glGenTextures(1, &(*m_idTexture));
		glBindTexture(GL_TEXTURE_2D, *m_idTexture);
83
84
85
86
		glTexImage2D(GL_TEXTURE_2D, 0, 1, WIDTHSPRITE, WIDTHSPRITE, 0, GL_LUMINANCE,  GL_UNSIGNED_BYTE, (GLvoid*)m_ptrSphere);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}
87
88
89
90
91
92
	else
	{
		*m_idTexture = *(this->m_idTexture);
	}

	*m_uniform_texture = glGetUniformLocation(program_handler(),"SpriteTexture");
93
94
95
96
97
98

	if (with_plane)
	{
		*m_uniform_planeX = glGetUniformLocation(program_handler(),"planeX");
		*m_uniform_planeY = glGetUniformLocation(program_handler(),"planeY");
	}
99
100
101
102
103
104
105
}

PointSprite::~PointSprite()
{
	if (m_ptrSphere!=NULL)
	{
		delete[] m_ptrSphere;
106
		glDeleteTextures(1, &(*m_idTexture));
107
108
109
110
111
112
113
114
	}
}

unsigned int PointSprite::setAttributePosition(VBO* vbo)
{
	return bindVA_VBO("VertexPosition", vbo);
}

115
116
117
118
119
unsigned int PointSprite::setAttributeColor(VBO* vbo)
{
	return bindVA_VBO("VertexColor", vbo);
}

120
121
122
void PointSprite::predraw(const Geom::Vec3f& color)
{
	bind();
123
124
	glUniform1i(*m_uniform_texture, 0);
	glUniform3fv(*m_uniform_color, 1, color.data());
125
	glActiveTexture(GL_TEXTURE0);
126
	glBindTexture(GL_TEXTURE_2D, *m_idTexture);
127
128
129
	glEnable(GL_TEXTURE_2D);
}

130
131
132
void PointSprite::predraw()
{
	bind();
133
	glUniform1i(*m_uniform_texture, 0);
134
	glActiveTexture(GL_TEXTURE0);
135
	glBindTexture(GL_TEXTURE_2D, *m_idTexture);
136
137
138
	glEnable(GL_TEXTURE_2D);
}

139
140
141
142
143
144
145
146
147
void PointSprite::postdraw()
{
	glDisable(GL_TEXTURE_2D);
	unbind();
}

void PointSprite::setSize(float radius)
{
	bind();
148
	glUniform1f(*m_uniform_size, radius);
149
150
151
	unbind();
}

152
153
154
155
156
157
158
159
160
void PointSprite::setPlane(const Geom::Vec3f& ox, const Geom::Vec3f& oy)
{
	bind();
	glUniform3fv(*m_uniform_planeX, 1, ox.data());
	glUniform3fv(*m_uniform_planeY, 1, oy.data());
	unbind();
}


161
162
void PointSprite::computeSphere()
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
163
	if (m_ptrSphere == NULL) // normally useless
164
165
166
	{
		m_ptrSphere = new unsigned char[WIDTHSPRITE*WIDTHSPRITE];
	}
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
167

168
169
	unsigned char* ptr = m_ptrSphere;

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
170
171
	Geom::Vec3f eye(0.0f , 0.0f, -1.0f);
	Geom::Vec3f light(5.0f, 3.0f, 10.0f);
172
	light.normalize();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
173
174
	float r2 = float((WIDTHSPRITE - 1) * (WIDTHSPRITE - 1)) / 4.0f;
	float mid = 0.5f - float(WIDTHSPRITE / 2);
175

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
176
	for (unsigned int i = 0; i < WIDTHSPRITE; ++i)
177
	{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
178
		for (unsigned int j = 0; j < WIDTHSPRITE; ++j)
179
180
		{
			float x = float(i) + mid;
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
181
			float y = float(j) + mid;
182
183
184
			float a =  x*x + y*y;
			if (r2 >= a)
			{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
185
186
				float z = sqrt(r2 - a);
				Geom::Vec3f P(x, y, z);
187
				P.normalize();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
188
				float col = P * light;
189
				if (col < 0.0f)
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
190
					col = 0.02f; // ambiant
191

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
192
				*ptr++ = (unsigned char)(255.0f * col);
193
194
195
196
197
198
199
			}
			else
				*ptr++ = (unsigned char)0;
		}
	}
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
200
201
202
} // namespace Utils

} // namespace CGoGN