tuto5.cpp 9.26 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/


26
#include "tuto5.h"
Sylvain Thery's avatar
Sylvain Thery committed
27
28
#include <iostream>

Sylvain Thery's avatar
Sylvain Thery committed
29

Sylvain Thery's avatar
Sylvain Thery committed
30
31
#include "Algo/Modelisation/primitives3d.h"
#include "Algo/Modelisation/polyhedron.h"
Sylvain Thery's avatar
Sylvain Thery committed
32
33
34
35
#include "Algo/Modelisation/subdivision.h"

#include "Algo/Render/GL2/topo3Render.h"
#include "Algo/Render/SVG/mapSVGRender.h"
Sylvain Thery's avatar
Sylvain Thery committed
36
37
38
39


PFP::MAP myMap;
PFP::TVEC3 position ;
Sylvain Thery's avatar
Sylvain Thery committed
40
41
42
43
44
45
46
47
Dart dglobal;

void MyQT::balls_onoff(bool x)
{
	render_balls = !render_balls;
	updateGL();
	CGoGNerr << " balls_onoff  "<< CGoGNendl;
}
48

Sylvain Thery's avatar
Sylvain Thery committed
49
void MyQT::vectors_onoff(bool x)
50
{
Sylvain Thery's avatar
Sylvain Thery committed
51
	render_vectors = !render_vectors;
Sylvain Thery's avatar
Sylvain Thery committed
52
	updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
53
	CGoGNerr << " vectors_onoff  "<< CGoGNflush;
54
55
}

Sylvain Thery's avatar
Sylvain Thery committed
56
void MyQT::text_onoff(bool x)
Pierre Kraemer's avatar
Pierre Kraemer committed
57
{
Sylvain Thery's avatar
Sylvain Thery committed
58
	render_text = !render_text;
Sylvain Thery's avatar
Sylvain Thery committed
59
	updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
60
	CGoGNerr << " text_onoff  " << CGoGNflush;
61
}
Pierre Kraemer's avatar
Pierre Kraemer committed
62

Sylvain Thery's avatar
Sylvain Thery committed
63

Sylvain Thery's avatar
Sylvain Thery committed
64
65
66
67
void MyQT::topo_onoff(bool x)
{
	render_topo = !render_topo;
	updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
68
	CGoGNerr << " topo_onoff  " << CGoGNflush;
Pierre Kraemer's avatar
Pierre Kraemer committed
69
70
}

Sylvain Thery's avatar
Sylvain Thery committed
71
void MyQT::slider_balls(int x)
Sylvain Thery's avatar
Sylvain Thery committed
72
{
Sylvain Thery's avatar
Sylvain Thery committed
73
	m_sprite->setSize(0.05f*x);
Sylvain Thery's avatar
Sylvain Thery committed
74
	updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
75
76
}

Sylvain Thery's avatar
Sylvain Thery committed
77
void MyQT::slider_vectors(int x)
78
{
Sylvain Thery's avatar
Sylvain Thery committed
79
	m_lines->setScale(0.02*x);
Sylvain Thery's avatar
Sylvain Thery committed
80
	updateGL();
81
82
}

Sylvain Thery's avatar
Sylvain Thery committed
83
void MyQT::slider_text(int x)
84
{
Sylvain Thery's avatar
Sylvain Thery committed
85
	m_strings->setScale(0.02f*x);
Sylvain Thery's avatar
Sylvain Thery committed
86
	updateGL();
87
}
88

Sylvain Thery's avatar
Sylvain Thery committed
89

Sylvain Thery's avatar
Sylvain Thery committed
90
91

void MyQT::animate()
92
{
Sylvain Thery's avatar
Sylvain Thery committed
93
94
95
//	transfoMatrix() = glm::rotate(transfoMatrix(), 0.5f, glm::vec3(0.5773f,0.5773f,0.5773f));
	transfoRotate( 0.5f, 0.5773f,0.5773f,0.5773f);
	updateGLMatrices();
untereiner's avatar
untereiner committed
96
97
98
}


Sylvain Thery's avatar
Sylvain Thery committed
99
void MyQT::storeVerticesInfo()
untereiner's avatar
untereiner committed
100
{
101

Sylvain Thery's avatar
Sylvain Thery committed
102
103
	CellMarker mv(myMap,VERTEX);
	for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
Sylvain Thery's avatar
Sylvain Thery committed
104
	{
Sylvain Thery's avatar
Sylvain Thery committed
105
106
107
108
109
110
111
		if (!mv.isMarked(d))
		{
			mv.mark(d);
			std::stringstream ss;
			ss << d << " : "<< position[d];
			m_strings->addString(ss.str(),position[d]);
		}
Sylvain Thery's avatar
Sylvain Thery committed
112
	}
113
}
114

Sylvain Thery's avatar
Sylvain Thery committed
115
void MyQT::cb_initGL()
Pierre Kraemer's avatar
Pierre Kraemer committed
116
{
Sylvain Thery's avatar
Sylvain Thery committed
117
118
119
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);

Sylvain Thery's avatar
Sylvain Thery committed
120
121
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
Sylvain Thery's avatar
Sylvain Thery committed
122

Sylvain Thery's avatar
Sylvain Thery committed
123
    m_render_topo = new Algo::Render::GL2::Topo3Render();
Sylvain Thery's avatar
Sylvain Thery committed
124

Sylvain Thery's avatar
Sylvain Thery committed
125
126
127
 	// create VBO for position
	m_positionVBO = new Utils::VBO();
	m_positionVBO->updateData(position);
Sylvain Thery's avatar
Sylvain Thery committed
128

Sylvain Thery's avatar
Sylvain Thery committed
129
130
131
132
	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
	m_shader->setAttributePosition(m_positionVBO);
	m_shader->setColor(Geom::Vec4f(0.,1.,0.,0.));
Sylvain Thery's avatar
Sylvain Thery committed
133

Sylvain Thery's avatar
Sylvain Thery committed
134
135
	m_sprite = new Utils::PointSprite();
	m_sprite->setAttributePosition(m_positionVBO);
Sylvain Thery's avatar
Sylvain Thery committed
136

Sylvain Thery's avatar
Sylvain Thery committed
137
138
139
    m_strings = new Utils::Strings3D(true, Geom::Vec3f(0.1f,0.0f,0.3f));
    storeVerticesInfo();
    m_strings->sendToVBO();
140

Sylvain Thery's avatar
Sylvain Thery committed
141
142
    // copy de contenu de VBO a la creation
	m_dataVBO = new Utils::VBO(*m_positionVBO);
143

Sylvain Thery's avatar
Sylvain Thery committed
144
145
146
147
148
	m_lines = new Utils::ShaderVectorPerVertex();
	m_lines->setAttributePosition(m_positionVBO);
	m_lines->setAttributeVector(m_dataVBO);
	m_lines->setScale(0.2f);
	m_lines->setColor(Geom::Vec4f(0.0f, 1.0f, 0.2f, 0.0f));
149

Sylvain Thery's avatar
Sylvain Thery committed
150
151
152
153
154
155
156
157
158
159
160
161
	// accede au buffer du VBO pour modification
	PFP::VEC3* data = static_cast<PFP::VEC3*>(m_dataVBO->lockPtr());
	for (unsigned int i=0; i< m_dataVBO->nbElts(); ++i)
	{
		data[i].normalize();
	}
	m_dataVBO->releasePtr();

	registerShader(m_shader);
	registerShader(m_strings);
	registerShader(m_sprite);
	registerShader(m_lines);
162

Sylvain Thery's avatar
Sylvain Thery committed
163
	SelectorTrue allDarts;
164

Sylvain Thery's avatar
Sylvain Thery committed
165
166
167
168
169
170
171
172
173
174
175
176
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::POINTS);

	SelectorDartNoBoundary<PFP::MAP> nb(myMap);
	m_render_topo->updateData<PFP>(myMap, position,  0.9f, 0.9f, 0.9f, nb);


	// timer example for animation
	m_timer = new QTimer( this );
	connect( m_timer, SIGNAL(timeout()), SLOT(animate()) );
}
177

Sylvain Thery's avatar
Sylvain Thery committed
178
179
180
181
void MyQT::cb_redraw()
{
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glEnable(GL_LIGHTING);
182

Sylvain Thery's avatar
Sylvain Thery committed
183
184
185
186
187
188
	m_render->draw(m_shader, Algo::Render::GL2::POINTS);

	glLineWidth(2.0f);
	m_shader->setColor(Geom::Vec4f(1.,1.,0.,0.));
	m_render->draw(m_shader, Algo::Render::GL2::LINES);

Sylvain Thery's avatar
Sylvain Thery committed
189
190
	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(1.0f, 1.0f);
191

Sylvain Thery's avatar
Sylvain Thery committed
192
	if (render_topo)
Sylvain Thery's avatar
Sylvain Thery committed
193
		m_render_topo->drawTopo();
194

Sylvain Thery's avatar
Sylvain Thery committed
195
196
197
198
199
200
201
/*	Dart d = myMap.phi2(myMap.begin());
	m_render_topo->overdrawDart(d, 5, 1.0f, 0.0f, 1.0f);
	 d = myMap.phi1(myMap.begin());
	m_render_topo->overdrawDart(d, 5, 1.0f, 0.0f, 1.0f);
*/	
	m_render_topo->overdrawDart(m_selected, 5, 1.0f, 0.0f, 1.0f);
	
202

Sylvain Thery's avatar
Sylvain Thery committed
203
	glDisable(GL_POLYGON_OFFSET_FILL);
204

Sylvain Thery's avatar
Sylvain Thery committed
205
206
	if (render_text)
		m_strings->drawAll(Geom::Vec3f(0.0f, 1.0f, 1.0f));
207

Sylvain Thery's avatar
Sylvain Thery committed
208
	if (render_balls)
Sylvain Thery's avatar
Sylvain Thery committed
209
	{
Sylvain Thery's avatar
Sylvain Thery committed
210
211
212
		m_sprite->predraw(Geom::Vec3f(1.0f, 0.0f ,0.0f));
		m_render->draw(m_sprite, Algo::Render::GL2::POINTS);
		m_sprite->postdraw();
Sylvain Thery's avatar
Sylvain Thery committed
213
	}
214

Sylvain Thery's avatar
Sylvain Thery committed
215
	if (render_vectors)
Sylvain Thery's avatar
Sylvain Thery committed
216
	{
Sylvain Thery's avatar
Sylvain Thery committed
217
218
		glLineWidth(1.0f);
		m_render->draw(m_lines, Algo::Render::GL2::POINTS);
Sylvain Thery's avatar
Sylvain Thery committed
219
220
	}
}
221

Sylvain Thery's avatar
Sylvain Thery committed
222
void MyQT::cb_mousePress(int button, int x, int y)
Sylvain Thery's avatar
Sylvain Thery committed
223
{
Sylvain Thery's avatar
Sylvain Thery committed
224
	if (Shift())
Sylvain Thery's avatar
Sylvain Thery committed
225
	{
Sylvain Thery's avatar
Sylvain Thery committed
226
227
228
		SelectorDartNoBoundary<PFP::MAP> nb(myMap);	
		Dart d = m_render_topo->picking<PFP>(myMap, x,y, nb);
		if (d != Dart::nil())
229
		{
Sylvain Thery's avatar
Sylvain Thery committed
230
231
			CGoGNout << "Dart "<< d << " clicked" << CGoGNendl;
			m_selected = d;
232
		}
Sylvain Thery's avatar
Sylvain Thery committed
233
		else
234
		{
Sylvain Thery's avatar
Sylvain Thery committed
235
			statusMsg("");
236
		}
Sylvain Thery's avatar
Sylvain Thery committed
237
238
239
		updateGL();
	}
}
Sylvain Thery's avatar
bug    
Sylvain Thery committed
240

Sylvain Thery's avatar
Sylvain Thery committed
241
242
243
244
245
246
247
248
249
250
251
void MyQT::cb_keyPress(int code)
{
	if (code  == 's')
	{
		std::string filename = selectFileSave("Export SVG file ");
		CGoGNout << "Exporting "<<filename<<CGoGNendl;
		Algo::Render::SVG::SVGOut svg(filename,modelViewMatrix(),projectionMatrix());
//		svg.renderLinesToSVG<PFP>(myMap,position);
		svg.setColor(Geom::Vec3f(1.,0.,0.));
		svg.renderFacesToSVG<PFP>(myMap,position,0.8f);
		//svg destruction close the file
252
	}
Sylvain Thery's avatar
Sylvain Thery committed
253
	if (code  == 't')
254
	{
Sylvain Thery's avatar
Sylvain Thery committed
255
256
257
258
		if (m_timer->isActive())
			m_timer->stop();
		else
			m_timer->start(1000/30); // 30 fps
259
	}
Sylvain Thery's avatar
Sylvain Thery committed
260
}
Sylvain Thery's avatar
Sylvain Thery committed
261

untereiner's avatar
untereiner committed
262
263


264
265
int main(int argc, char **argv)
{
Sylvain Thery's avatar
Sylvain Thery committed
266
267
268
269
270
271
272
273
	position = myMap.addAttribute<PFP::VEC3>(VERTEX, "position");

	CGoGNout << 5.34 << " toto "<< Geom::Vec3f(2.5f, 2.2f, 4.3f) << CGoGNendl;
	CGoGNout << 3 << " tutu "<< 4 <<CGoGNendl;


	Algo::Modelisation::Primitive3D<PFP> prim(myMap, position);
	int nb=3;
Sylvain Thery's avatar
Sylvain Thery committed
274
	if (argc>1)
Sylvain Thery's avatar
Sylvain Thery committed
275
276
277
		nb = atoi(argv[1]);
	dglobal = prim.hexaGrid_topo(nb,nb,nb);
	prim.embedHexaGrid(1.0f,1.0f,1.0f);
untereiner's avatar
untereiner committed
278

Sylvain Thery's avatar
Sylvain Thery committed
279
280
281
    // un peu d'interface
	QApplication app(argc, argv);
	MyQT sqt;
Pierre Kraemer's avatar
Pierre Kraemer committed
282

283
284
285
	// interface de tuto5.ui
    Utils::QT::uiDockInterface dock;
    sqt.setDock(&dock);
Pierre Kraemer's avatar
Pierre Kraemer committed
286

Sylvain Thery's avatar
Sylvain Thery committed
287
288
289
290
291
292
293
294
295
296
297
 	// message d'aide
	sqt.setHelpMsg("Enter pour dock on/off\nShift Enter pour console on/off\nShift Click gauche pour selectionner un brin");

	CGoGNout.toStatusBar(&sqt);
	CGoGNout << "CGoGNOut StatusBar" << Geom::Vec3f(2.5f, 2.2f, 4.3f) << CGoGNendl;

	CGoGNout.toConsole(&sqt);

	CGoGNout << "CGoGNOut dans la console" << Geom::Vec3f(2.5f, 2.2f, 4.3f) << CGoGNendl;

	CGoGNout.toStatusBar(NULL);
Sylvain Thery's avatar
Sylvain Thery committed
298

299
	//  bounding box
Sylvain Thery's avatar
Sylvain Thery committed
300
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
Sylvain Thery's avatar
Sylvain Thery committed
301
302
    float lWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
    Geom::Vec3f lPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);
Pierre Kraemer's avatar
Pierre Kraemer committed
303

304
    // envoit info BB a l'interface
Sylvain Thery's avatar
Sylvain Thery committed
305
	sqt.setParamObject(lWidthObj, lPosObj.data());
Pierre Kraemer's avatar
Pierre Kraemer committed
306

Sylvain Thery's avatar
Sylvain Thery committed
307
308
309
	sqt.setCallBack( dock.checkBox_balls, SIGNAL(toggled(bool)), SLOT(balls_onoff(bool)) );
	sqt.setCallBack( dock.checkBox_vectors, SIGNAL(toggled(bool)), SLOT(vectors_onoff(bool)) );
	sqt.setCallBack( dock.checkBox_text, SIGNAL(toggled(bool)), SLOT(text_onoff(bool)) );
310
	sqt.setCallBack( dock.checkBox_topo, SIGNAL(toggled(bool)), SLOT(topo_onoff(bool)) );
Pierre Kraemer's avatar
Pierre Kraemer committed
311

Sylvain Thery's avatar
Sylvain Thery committed
312
313
314
	sqt.setCallBack( dock.slider_balls, SIGNAL(valueChanged(int)), SLOT(slider_balls(int)) );
	sqt.setCallBack( dock.slider_vectors, SIGNAL(valueChanged(int)), SLOT(slider_vectors(int)) );
	sqt.setCallBack( dock.slider_text, SIGNAL(valueChanged(int)), SLOT(slider_text(int)) );
315
316


Sylvain Thery's avatar
Sylvain Thery committed
317
	sqt.m_selected = myMap.begin();
Pierre Kraemer's avatar
Pierre Kraemer committed
318

Sylvain Thery's avatar
Sylvain Thery committed
319
320
321
322
323
324
325
326
327
328
329
330
	sqt.show();

	sqt.slider_balls(50);
	sqt.slider_vectors(50);
	sqt.slider_text(50);

	GLint texSize;
	glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
	CGoGNdbg.toConsole(&sqt);
	CGoGNerr.toConsole(&sqt);
	CGoGNdbg << " TextureSize " <<  texSize << CGoGNendl;
	CGoGNerr << " test ERROR  " <<  5*7 << CGoGNflush;
Sylvain Thery's avatar
Sylvain Thery committed
331

Pierre Kraemer's avatar
Pierre Kraemer committed
332
333


334
335
	// et on attend la fin.
	return app.exec();
Pierre Kraemer's avatar
Pierre Kraemer committed
336
}