drawer.cpp 9.02 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"
Sylvain Thery's avatar
Sylvain Thery committed
26 27

#define CGoGN_UTILS_DLL_EXPORT 1
28
#include "Utils/drawer.h"
29 30
#include "Utils/Shaders/shaderColorPerVertex.h"

Thery Sylvain's avatar
Thery Sylvain committed
31
#include "Utils/vbo_base.h"
Sylvain Thery's avatar
Sylvain Thery committed
32
#include "Utils/svg.h"
33 34 35

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

37 38 39
namespace Utils
{

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
40
Drawer::Drawer() : m_currentWidth(1.0f)
41 42 43 44 45 46 47 48 49 50 51 52
{
	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
53
	Utils::GLSLShader::registerShader(NULL, m_shader);
54 55 56 57 58 59 60 61 62

	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
63
	Utils::GLSLShader::unregisterShader(NULL, m_shader);
64 65
	delete m_vboPos;
	delete m_vboCol;
66
	delete m_shader;
67 68
}

69 70 71 72 73
Utils::ShaderColorPerVertex* Drawer::getShader()
{
	return m_shader;
}

74 75
void Drawer::lineWidth(float lw)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
76
	m_currentWidth = lw;
77 78 79 80
}

void Drawer::pointSize(float ps)
{
81
	m_currentSize = ps;
82 83
}

84
int Drawer::begin(GLenum mode)
85
{
86
	int res = int(m_begins.size());
87
	if (mode == GL_POINTS)
88
		m_begins.push_back(PrimParam(uint32(m_dataPos.size()), mode, m_currentSize));
89
	else
90
		m_begins.push_back(PrimParam(uint32(m_dataPos.size()), mode, m_currentWidth));
91
	return res;
92 93 94 95
}

void Drawer::end()
{
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
96 97 98
	if (m_begins.empty())
		return;

99
	m_begins.back().nb = uint32(m_dataPos.size() - m_begins.back().begin);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
}

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
115
unsigned int Drawer::vertex(const Geom::Vec3f& v)
116 117 118 119
{
	if (m_dataPos.size() == m_dataCol.size())
	{
		if (m_dataCol.empty())
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
120
			m_dataCol.push_back(Geom::Vec3f(1.0f, 1.0f, 1.0f));
121 122 123 124
		else
			m_dataCol.push_back( m_dataCol.back());
	}
	m_dataPos.push_back(v);
125
	return  uint32(m_dataPos.size()-1);
126 127
}

Sylvain Thery's avatar
Sylvain Thery committed
128
unsigned int Drawer::vertex3f(float r, float g, float b)
129
{
Sylvain Thery's avatar
Sylvain Thery committed
130
	return vertex(Geom::Vec3f(r,g,b));
131 132 133 134 135 136 137 138 139 140 141 142
}

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

void Drawer::endList()
{
143
	unsigned int nbElts = uint32(m_dataPos.size());
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
144 145 146
	
	if (nbElts == 0)
		return;
147 148

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

	m_vboCol->bind();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
152
	glBufferData(GL_ARRAY_BUFFER, nbElts * sizeof(Geom::Vec3f), &(m_dataCol[0]), GL_STREAM_DRAW);
153 154 155 156 157 158 159 160 161 162 163

	// 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
164 165 166 167 168 169 170 171 172 173 174 175 176 177

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);
}


178
void Drawer::callList(float opacity)
179 180 181 182
{
	if (m_begins.empty())
		return;

Sylvain Thery's avatar
Sylvain Thery committed
183 184
	m_shader->setOpacity(opacity);

185 186 187 188 189
	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);
Sylvain Thery's avatar
Sylvain Thery committed
190
		if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP))
191 192 193 194 195 196
			glLineWidth(pp->width);
		glDrawArrays(pp->mode, pp->begin, pp->nb);
	}
 	m_shader->disableVertexAttribs();
}

Sylvain Thery's avatar
Sylvain Thery committed
197

198 199 200 201 202 203 204 205 206 207 208 209 210
void Drawer::callSubList(int index, float opacity)
{
	if (index >= int(m_begins.size()))
		return;

	m_shader->setOpacity(opacity);

	m_shader->enableVertexAttribs();

	PrimParam* pp = & (m_begins[index]);

	if (pp->mode == GL_POINTS)
		glPointSize(pp->width);
Sylvain Thery's avatar
Sylvain Thery committed
211
	if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP))
212 213 214 215 216 217
		glLineWidth(pp->width);
	glDrawArrays(pp->mode, pp->begin, pp->nb);

	m_shader->disableVertexAttribs();
}

218 219 220 221 222 223 224 225 226 227 228 229 230
void Drawer::callSubLists(int first, int nb, float opacity)
{
	m_shader->setOpacity(opacity);
	m_shader->enableVertexAttribs();

	int last = first+nb;
	for (int i = first; i< last; ++i)
		if (i < int(m_begins.size()))
		{
			PrimParam* pp = & (m_begins[i]);

			if (pp->mode == GL_POINTS)
				glPointSize(pp->width);
Sylvain Thery's avatar
Sylvain Thery committed
231
			if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP))
232 233 234 235 236 237 238
				glLineWidth(pp->width);
			glDrawArrays(pp->mode, pp->begin, pp->nb);
		}

	m_shader->disableVertexAttribs();
}

239 240 241 242 243 244 245 246 247 248 249 250 251
void Drawer::callSubLists(std::vector<int> indices, float opacity)
{
	m_shader->setOpacity(opacity);

	m_shader->enableVertexAttribs();

	for (std::vector<int>::iterator it = indices.begin(); it != indices.end(); ++it)
		if (*it < int(m_begins.size()))
		{
			PrimParam* pp = & (m_begins[*it]);

			if (pp->mode == GL_POINTS)
				glPointSize(pp->width);
Sylvain Thery's avatar
Sylvain Thery committed
252
			if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP))
253 254 255 256 257 258 259
				glLineWidth(pp->width);
			glDrawArrays(pp->mode, pp->begin, pp->nb);
		}

	m_shader->disableVertexAttribs();
}

Sylvain Thery's avatar
Sylvain Thery committed
260

Sylvain Thery's avatar
Sylvain Thery committed
261 262 263 264 265
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());

266 267 268 269
	Utils::SVG::SvgGroup* svg1 = new Utils::SVG::SvgGroup("points", svg.m_model, svg.m_proj);
	Utils::SVG::SvgGroup* svg2 = new Utils::SVG::SvgGroup("lines", svg.m_model, svg.m_proj);
	Utils::SVG::SvgGroup* svg3 = new Utils::SVG::SvgGroup("faces", svg.m_model, svg.m_proj);

Sylvain Thery's avatar
Sylvain Thery committed
270 271
	for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
	{
272
		svg1->setWidth(pp->width);
Sylvain Thery's avatar
Sylvain Thery committed
273 274 275
		if (pp->mode == GL_POINTS)
		{
			unsigned int end = pp->begin + pp->nb;
276
			svg1->beginPoints();
Sylvain Thery's avatar
Sylvain Thery committed
277
			for (unsigned int i=pp->begin; i<end; ++i)
278 279
				svg1->addPoint(ptrP[i], ptrC[i]);
			svg1->endPoints();
Sylvain Thery's avatar
Sylvain Thery committed
280 281
		}

282
		svg2->setWidth(pp->width);
Sylvain Thery's avatar
Sylvain Thery committed
283 284 285
		if (pp->mode == GL_LINES)
		{
			unsigned int end = pp->begin + pp->nb;
286
			svg2->beginLines();
Sylvain Thery's avatar
Sylvain Thery committed
287
			for (unsigned int i=pp->begin; i<end; i+=2)
288 289
				svg2->addLine(ptrP[i], ptrP[i+1], ptrC[i]);
			svg2->endLines();
Sylvain Thery's avatar
Sylvain Thery committed
290 291
		}

292
		svg3->setWidth(pp->width);
Sylvain Thery's avatar
Sylvain Thery committed
293 294 295
		if ((pp->mode == GL_LINE_LOOP) || (pp->mode == GL_POLYGON))
		{
			unsigned int end = pp->begin + pp->nb-1;
296
			svg3->beginLines();
Sylvain Thery's avatar
Sylvain Thery committed
297
			for (unsigned int i=pp->begin; i<=end; ++i)
298 299 300
				svg3->addLine(ptrP[i], ptrP[i+1], ptrC[i]);
			svg3->addLine(ptrP[end], ptrP[pp->begin], ptrC[end]);
			svg3->endLines();
Sylvain Thery's avatar
Sylvain Thery committed
301 302 303 304
		}
		if (pp->mode == GL_TRIANGLES)
		{
			unsigned int end = pp->begin + pp->nb;
305
			svg3->beginLines();
Sylvain Thery's avatar
Sylvain Thery committed
306 307
			for (unsigned int i=pp->begin; i<end; i+=3)
			{
308 309 310
				svg3->addLine(ptrP[i],   ptrP[i+1], ptrC[i]);
				svg3->addLine(ptrP[i+1], ptrP[i+2], ptrC[i+1]);
				svg3->addLine(ptrP[i+2], ptrP[i],   ptrC[i+2]);
Sylvain Thery's avatar
Sylvain Thery committed
311
			}
312
			svg3->endLines();
Sylvain Thery's avatar
Sylvain Thery committed
313 314 315 316
		}
		if (pp->mode == GL_QUADS)
		{
			unsigned int end = pp->begin + pp->nb;
317
			svg3->beginLines();
Sylvain Thery's avatar
Sylvain Thery committed
318 319
			for (unsigned int i=pp->begin; i<end; i+=4)
			{
320 321 322 323
				svg3->addLine(ptrP[i],   ptrP[i+1], ptrC[i]);
				svg3->addLine(ptrP[i+1], ptrP[i+2], ptrC[i+1]);
				svg3->addLine(ptrP[i+2], ptrP[i+3], ptrC[i+2]);
				svg3->addLine(ptrP[i+3], ptrP[i],   ptrC[i+3]);
Sylvain Thery's avatar
Sylvain Thery committed
324
			}
325
			svg3->endLines();
Sylvain Thery's avatar
Sylvain Thery committed
326 327 328
		}
	}

329 330 331 332
	svg.addGroup(svg1);
	svg.addGroup(svg2);
	svg.addGroup(svg3);

Sylvain Thery's avatar
Sylvain Thery committed
333 334 335 336
	m_vboPos->releasePtr();
	m_vboCol->releasePtr();
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
337
} // namespace Utils
338

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