topoRender.cpp 7.13 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Render/GL2/topoRender.h"
26 27 28
#include "Utils/Shaders/shaderSimpleColor.h"
#include "Utils/Shaders/shaderColorPerVertex.h"

Sylvain Thery's avatar
Sylvain Thery committed
29 30 31 32 33 34 35 36 37 38 39 40 41

namespace CGoGN
{

namespace Algo
{

namespace Render
{

namespace GL2
{

42
TopoRender::TopoRender():
Sylvain Thery's avatar
Sylvain Thery committed
43 44
m_topo_dart_width(2.0f), m_topo_relation_width(3.0f)
{
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	m_vbo0 = new Utils::VBO();
	m_vbo1 = new Utils::VBO();
	m_vbo2 = new Utils::VBO();
	m_vbo3 = new Utils::VBO();

	m_vbo0->setDataSize(3);
	m_vbo1->setDataSize(3);
	m_vbo2->setDataSize(3);
	m_vbo3->setDataSize(3);

	m_shader1 = new Utils::ShaderSimpleColor();
	m_shader2 = new Utils::ShaderColorPerVertex();


	// binding VBO - VA
	m_vaId = m_shader1->setAttributePosition(m_vbo1);

	m_shader2->setAttributePosition(m_vbo0);
	m_shader2->setAttributeColor(m_vbo3);

	// registering for auto matrices update
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
66 67
	Utils::GLSLShader::registerShader(NULL, m_shader1);
	Utils::GLSLShader::registerShader(NULL, m_shader2);
Sylvain Thery's avatar
Sylvain Thery committed
68 69
}

70
TopoRender::~TopoRender()
Sylvain Thery's avatar
Sylvain Thery committed
71
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
72 73
	Utils::GLSLShader::unregisterShader(NULL, m_shader2);
	Utils::GLSLShader::unregisterShader(NULL, m_shader1);
74 75 76 77 78 79 80 81

	delete m_shader2;
	delete m_shader1;
	delete m_vbo3;
	delete m_vbo2;
	delete m_vbo1;
	delete m_vbo0;

Sylvain Thery's avatar
Sylvain Thery committed
82 83
	if (m_attIndex.map() != NULL)
		static_cast<AttribMap*>(m_attIndex.map())->removeAttribute(m_attIndex);
84

Sylvain Thery's avatar
Sylvain Thery committed
85 86
}

87
void TopoRender::setDartWidth(float dw)
Sylvain Thery's avatar
Sylvain Thery committed
88 89 90 91
{
	m_topo_dart_width = dw;
}

92
void TopoRender::setRelationWidth(float pw)
Sylvain Thery's avatar
Sylvain Thery committed
93 94 95 96
{
	m_topo_relation_width = pw;
}

97
void TopoRender::setDartColor(Dart d, float r, float g, float b)
Sylvain Thery's avatar
Sylvain Thery committed
98 99 100 101
{
	float RGB[6];
	RGB[0]=r; RGB[1]=g; RGB[2]=b;
	RGB[3]=r; RGB[4]=g; RGB[5]=b;
102
	m_vbo3->bind();
Sylvain Thery's avatar
Sylvain Thery committed
103 104 105
	glBufferSubData(GL_ARRAY_BUFFER, m_attIndex[d]*3*sizeof(float), 6*sizeof(float),RGB);
}

106
void TopoRender::setAllDartsColor(float r, float g, float b)
Sylvain Thery's avatar
Sylvain Thery committed
107
{
108 109
	m_vbo3->bind();
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Sylvain Thery's avatar
Sylvain Thery committed
110 111 112 113 114 115 116 117
	float* colorDartBuf = reinterpret_cast<float*>(ColorDartsBuffer);
	for (unsigned int i=0; i < 2*m_nbDarts; ++i)
	{
		*colorDartBuf++ = r;
		*colorDartBuf++ = g;
		*colorDartBuf++ = b;
	}

118 119
	m_vbo3->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Sylvain Thery's avatar
Sylvain Thery committed
120 121
}

122
void TopoRender::drawDarts()
Sylvain Thery's avatar
Sylvain Thery committed
123
{
124
	m_shader2->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
125

126
	glLineWidth(m_topo_dart_width);
Sylvain Thery's avatar
Sylvain Thery committed
127 128
	glDrawArrays(GL_LINES, 0, m_nbDarts*2);

129 130
	// change the stride to take 1/2 vertices
	m_shader2->enableVertexAttribs(6*sizeof(GL_FLOAT));
Sylvain Thery's avatar
Sylvain Thery committed
131

132
	glPointSize(2.0f*m_topo_dart_width);
Sylvain Thery's avatar
Sylvain Thery committed
133
 	glDrawArrays(GL_POINTS, 0, m_nbDarts);
134 135

 	m_shader2->disableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
136 137
}

138
void TopoRender::drawRelation1()
Sylvain Thery's avatar
Sylvain Thery committed
139 140 141
{
	glLineWidth(m_topo_relation_width);

142 143 144 145
	m_shader1->changeVA_VBO(m_vaId, m_vbo1);
	m_shader1->setColor(Geom::Vec4f(0.0f,1.0f,1.0f,0.0f));
	m_shader1->enableVertexAttribs();

Sylvain Thery's avatar
Sylvain Thery committed
146
	glDrawArrays(GL_LINES, 0, m_nbDarts*2);
147 148

	m_shader1->disableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
149 150
}

151
void TopoRender::drawRelation2()
Sylvain Thery's avatar
Sylvain Thery committed
152 153 154
{
	glLineWidth(m_topo_relation_width);

155 156 157 158 159 160 161
	m_shader1->changeVA_VBO(m_vaId, m_vbo2);
	m_shader1->setColor(Geom::Vec4f(1.0f,0.0f,0.0f,0.0f));
	m_shader1->enableVertexAttribs();

	glDrawArrays(GL_LINES, 0, m_nbDarts*2);

	m_shader1->disableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
162 163
}

164
void TopoRender::drawTopo()
Sylvain Thery's avatar
Sylvain Thery committed
165 166 167 168 169 170
{
	drawDarts();
	drawRelation1();
	drawRelation2();
}

171
void TopoRender::overdrawDart(Dart d, float width, float r, float g, float b)
Sylvain Thery's avatar
Sylvain Thery committed
172 173 174
{
	unsigned int indexDart =  m_attIndex[d];

175 176 177 178
	m_shader1->changeVA_VBO(m_vaId, m_vbo0);
	m_shader1->setColor(Geom::Vec4f(r,g,b,0.0f));
	m_shader1->enableVertexAttribs();

Sylvain Thery's avatar
Sylvain Thery committed
179
	glLineWidth(width);
180 181
	glDrawArrays(GL_LINES, indexDart, 2);

Sylvain Thery's avatar
Sylvain Thery committed
182
	glPointSize(2.0f*width);
183
 	glDrawArrays(GL_POINTS, indexDart, 1);
Sylvain Thery's avatar
Sylvain Thery committed
184

185 186
 	m_shader2->disableVertexAttribs();
}
Sylvain Thery's avatar
Sylvain Thery committed
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
Dart TopoRender::colToDart(float* color)
{
	unsigned int r = (unsigned int)(color[0]*255.0f);
	unsigned int g = (unsigned int)(color[1]*255.0f);
	unsigned int b = (unsigned int)(color[2]*255.0f);

	unsigned int id = r + 255*g +255*255*b;

	if (id==0)
		return Dart::nil();
	return  Dart(id-1);

}

void TopoRender::dartToCol(Dart d, float& r, float& g, float& b)
{
	unsigned int lab = d.index + 1; // add one to avoid picking the black of screen

	r = float(lab%255) / 255.0f; lab = lab/255;
	g = float(lab%255) / 255.0f; lab = lab/255;
	b = float(lab%255) / 255.0f; lab = lab/255;
	if (lab!=0)
210
		CGoGNerr << "Error picking color, too many darts"<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
211 212
}

Sylvain Thery's avatar
Sylvain Thery committed
213
Dart TopoRender::pickColor(unsigned int x, unsigned int y)
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
{
	//more easy picking for
	unsigned int dw = m_topo_dart_width;
	m_topo_dart_width+=2;

	// save clear color and set to zero
	float cc[4];
	glGetFloatv(GL_COLOR_CLEAR_VALUE,cc);

	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glDisable(GL_LIGHTING);
	// draw in back buffer (not shown on screen)
	drawDarts();

	// restore dart with
	m_topo_dart_width = dw;

	// read the pixel under the mouse in back buffer
	glReadBuffer(GL_BACK);
	float color[3];
	glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,color);

	glClearColor(cc[0], cc[1], cc[2], cc[3]);

	return colToDart(color);
}

Sylvain Thery's avatar
Sylvain Thery committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
void TopoRender::pushColors()
{
	m_color_save = new float[6*m_nbDarts];
	m_vbo3->bind();
	void* colorBuffer = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);

	memcpy(m_color_save, colorBuffer, 6*m_nbDarts*sizeof(float));
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

void TopoRender::popColors()
{
	m_vbo3->bind();
	void* colorBuffer = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);

	memcpy(colorBuffer, m_color_save, 6*m_nbDarts*sizeof(float));
	glUnmapBuffer(GL_ARRAY_BUFFER);

	delete[] m_color_save;
	m_color_save=NULL;
}
264

Sylvain Thery's avatar
Sylvain Thery committed
265 266 267 268 269 270 271
}//end namespace GL2

}//end namespace Render

}//end namespace Algo

}//end namespace CGoGN