drawer.cpp 6.86 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
25
#include "GL/glew.h"
26
#include "Utils/drawer.h"
27
28
#include "Utils/Shaders/shaderColorPerVertex.h"

29
#include "Utils/vbo.h"
Sylvain Thery's avatar
Sylvain Thery committed
30
#include "Utils/svg.h"
31
32
33

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

35
36
37
namespace Utils
{

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
38
Drawer::Drawer() : m_currentWidth(1.0f)
39
40
41
42
43
44
45
46
47
48
49
50
{
	m_vboPos = new Utils::VBO();
	m_vboPos->setDataSize(3);

	m_vboCol = new Utils::VBO();
	m_vboCol->setDataSize(3);

	m_shader = new Utils::ShaderColorPerVertex();

	m_shader->setAttributePosition(m_vboPos);
	m_shader->setAttributeColor(m_vboCol);

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
51
	Utils::GLSLShader::registerShader(NULL, m_shader);
52
53
54
55
56
57
58
59
60

	m_dataPos.reserve(128);
	m_dataCol.reserve(128);
	m_begins.reserve(16);
//	m_modes.reserve(16);
}

Drawer::~Drawer()
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
61
	Utils::GLSLShader::unregisterShader(NULL, m_shader);
62
63
	delete m_vboPos;
	delete m_vboCol;
64
	delete m_shader;
65
66
67
68
}

void Drawer::lineWidth(float lw)
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
69
	m_currentWidth = lw;
70
71
72
73
}

void Drawer::pointSize(float ps)
{
74
	m_currentSize = ps;
75
76
77
78
}

void Drawer::begin(GLenum mode)
{
79
80
81
82
	if (mode == GL_POINTS)
		m_begins.push_back(PrimParam(m_dataPos.size(), mode, m_currentSize));
	else
		m_begins.push_back(PrimParam(m_dataPos.size(), mode, m_currentWidth));
83
84
85
86
}

void Drawer::end()
{
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
87
88
89
	if (m_begins.empty())
		return;

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	m_begins.back().nb = m_dataPos.size() - m_begins.back().begin;
}

void Drawer::color(const Geom::Vec3f& col)
{
	if (m_dataPos.size() == m_dataCol.size())
		m_dataCol.push_back(col);
	else
		m_dataCol.back() = col;
}

void Drawer::color3f(float r, float g, float b)
{
	color(Geom::Vec3f(r,g,b));
}

Sylvain Thery's avatar
Sylvain Thery committed
106
unsigned int Drawer::vertex(const Geom::Vec3f& v)
107
108
109
110
{
	if (m_dataPos.size() == m_dataCol.size())
	{
		if (m_dataCol.empty())
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
111
			m_dataCol.push_back(Geom::Vec3f(1.0f, 1.0f, 1.0f));
112
113
114
115
		else
			m_dataCol.push_back( m_dataCol.back());
	}
	m_dataPos.push_back(v);
Sylvain Thery's avatar
Sylvain Thery committed
116
	return  m_dataPos.size()-1;
117
118
}

Sylvain Thery's avatar
Sylvain Thery committed
119
unsigned int Drawer::vertex3f(float r, float g, float b)
120
{
Sylvain Thery's avatar
Sylvain Thery committed
121
	return vertex(Geom::Vec3f(r,g,b));
122
123
124
125
126
127
128
129
130
131
132
133
134
}

void Drawer::newList(GLenum comp)
{
	m_compile = comp;
	m_dataPos.clear();
	m_dataCol.clear();
	m_begins.clear();
}

void Drawer::endList()
{
	unsigned int nbElts = m_dataPos.size();
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
135
136
137
	
	if (nbElts == 0)
		return;
138
139

	m_vboPos->bind();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
140
	glBufferData(GL_ARRAY_BUFFER, nbElts * sizeof(Geom::Vec3f), &(m_dataPos[0]), GL_STREAM_DRAW);
141
142

	m_vboCol->bind();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
143
	glBufferData(GL_ARRAY_BUFFER, nbElts * sizeof(Geom::Vec3f), &(m_dataCol[0]), GL_STREAM_DRAW);
144
145
146
147
148
149
150
151
152
153
154

	// free memory
	std::vector<Geom::Vec3f> tempo;
	tempo.swap(m_dataPos);
	std::vector<Geom::Vec3f> tempo2;
	tempo2.swap(m_dataCol);

	if (m_compile != GL_COMPILE)
		callList();
}

Sylvain Thery's avatar
Sylvain Thery committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168

void Drawer::updatePositions(unsigned int first, unsigned int nb, const Geom::Vec3f* P)
{
	m_vboPos->bind();
	glBufferSubData(GL_ARRAY_BUFFER, first * sizeof(Geom::Vec3f), nb * sizeof(Geom::Vec3f), P);
}

void Drawer::updatePositions(unsigned int first, unsigned int nb, const float* P)
{
	m_vboPos->bind();
	glBufferSubData(GL_ARRAY_BUFFER, first * 3 * sizeof(float), nb * 3 * sizeof(float), P);
}


169
void Drawer::callList(float opacity)
170
171
172
173
{
	if (m_begins.empty())
		return;

Sylvain Thery's avatar
Sylvain Thery committed
174
175
	m_shader->setOpacity(opacity);

176
177
178
179
180
181
182
183
184
185
186
187
	m_shader->enableVertexAttribs();
	for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
	{
		if (pp->mode == GL_POINTS)
			glPointSize(pp->width);
		if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP))
			glLineWidth(pp->width);
		glDrawArrays(pp->mode, pp->begin, pp->nb);
	}
 	m_shader->disableVertexAttribs();
}

Sylvain Thery's avatar
Sylvain Thery committed
188

Sylvain Thery's avatar
Sylvain Thery committed
189

Sylvain Thery's avatar
Sylvain Thery committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
void Drawer::toSVG(Utils::SVG::SVGOut& svg)
{
	const Geom::Vec3f* ptrP = reinterpret_cast<Geom::Vec3f*>(m_vboPos->lockPtr());
	const Geom::Vec3f* ptrC = reinterpret_cast<Geom::Vec3f*>(m_vboCol->lockPtr());

	for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
	{
		svg.setWidth(pp->width);
		if (pp->mode == GL_POINTS)
		{
			unsigned int end = pp->begin + pp->nb;
			svg.beginPoints();
			for (unsigned int i=pp->begin; i<end; ++i)
				svg.addPoint(ptrP[i], ptrC[i]);
			svg.endPoints();
		}

		if (pp->mode == GL_LINES)
		{
			unsigned int end = pp->begin + pp->nb;
			svg.beginLines();
			for (unsigned int i=pp->begin; i<end; i+=2)
				svg.addLine(ptrP[i], ptrP[i+1], ptrC[i]);
			svg.endLines();
		}

		if ((pp->mode == GL_LINE_LOOP) || (pp->mode == GL_POLYGON))
		{
			unsigned int end = pp->begin + pp->nb-1;
			svg.beginLines();
			for (unsigned int i=pp->begin; i<=end; ++i)
				svg.addLine(ptrP[i], ptrP[i+1], ptrC[i]);
			svg.addLine(ptrP[end], ptrP[pp->begin], ptrC[end]);
			svg.endLines();
		}
		if (pp->mode == GL_TRIANGLES)
		{
			unsigned int end = pp->begin + pp->nb;
			svg.beginLines();
			for (unsigned int i=pp->begin; i<end; i+=3)
			{
				svg.addLine(ptrP[i],   ptrP[i+1], ptrC[i]);
				svg.addLine(ptrP[i+1], ptrP[i+2], ptrC[i+1]);
				svg.addLine(ptrP[i+2], ptrP[i],   ptrC[i+2]);
			}
			svg.endLines();
		}
		if (pp->mode == GL_QUADS)
		{
			unsigned int end = pp->begin + pp->nb;
			svg.beginLines();
			for (unsigned int i=pp->begin; i<end; i+=4)
			{
				svg.addLine(ptrP[i],   ptrP[i+1], ptrC[i]);
				svg.addLine(ptrP[i+1], ptrP[i+2], ptrC[i+1]);
				svg.addLine(ptrP[i+2], ptrP[i+3], ptrC[i+2]);
				svg.addLine(ptrP[i+3], ptrP[i],   ptrC[i+3]);
			}
			svg.endLines();
		}
	}

	m_vboPos->releasePtr();
	m_vboCol->releasePtr();
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
256
} // namespace Utils
257

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
258
} // namespace CGoGN