tuto5.cpp 9.83 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-2012, 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.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

untereiner's avatar
untereiner committed
28
#include "Algo/Tiling/Volume/cubic.h"
Sylvain Thery's avatar
Sylvain Thery committed
29 30
#include "Algo/Modelisation/polyhedron.h"
#include "Algo/Modelisation/subdivision.h"
untereiner's avatar
untereiner committed
31
#include "Algo/Modelisation/subdivision3.h"
Sylvain Thery's avatar
Sylvain Thery committed
32 33 34 35

#include "Algo/Render/GL2/topo3Render.h"
#include "Algo/Render/SVG/mapSVGRender.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
36
MAP myMap;
Pierre Kraemer's avatar
Pierre Kraemer committed
37

38
VertexAttribute<VEC3, MAP> position ;
Sylvain Thery's avatar
Sylvain Thery committed
39 40
Dart dglobal;

41
void MyQT::balls_onoff(bool /*x*/)
Sylvain Thery's avatar
Sylvain Thery committed
42 43
{
	render_balls = !render_balls;
44
	updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
45
	CGoGNerr << " balls_onoff  "<< CGoGNendl;
46
}
47

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

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


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

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

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

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

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

Sylvain Thery's avatar
Sylvain Thery committed
96
void MyQT::storeVerticesInfo()
untereiner's avatar
untereiner committed
97
{
Pierre Kraemer's avatar
Pierre Kraemer committed
98
	CellMarker<MAP, VERTEX> mv(myMap);
Pierre Kraemer's avatar
Pierre Kraemer committed
99
	for (Dart d = myMap.begin(); d != myMap.end(); myMap.next(d))
Sylvain Thery's avatar
Sylvain Thery committed
100 101 102 103 104 105
	{
		if (!mv.isMarked(d))
		{
			mv.mark(d);
			std::stringstream ss;
			ss << d << " : "<< position[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
106
			m_strings->addString(ss.str(), position[d]);
Sylvain Thery's avatar
Sylvain Thery committed
107 108
		}
	}
109
}
110

Sylvain Thery's avatar
Sylvain Thery committed
111
void MyQT::cb_initGL()
Pierre Kraemer's avatar
Pierre Kraemer committed
112
{
Sylvain Thery's avatar
Sylvain Thery committed
113 114 115
	// create the render
	m_render = new Algo::Render::GL2::MapRender();

Pierre Kraemer's avatar
Pierre Kraemer committed
116
	m_render_topo = new Algo::Render::GL2::Topo3RenderMap<PFP>();
Sylvain Thery's avatar
Sylvain Thery committed
117 118 119 120 121 122 123 124 125 126 127 128

 	// create VBO for position
	m_positionVBO = new Utils::VBO();
	m_positionVBO->updateData(position);

	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
	m_shader->setAttributePosition(m_positionVBO);
	m_shader->setColor(Geom::Vec4f(0.,1.,0.,0.));

	m_sprite = new Utils::PointSprite();
	m_sprite->setAttributePosition(m_positionVBO);
129
	m_sprite->setColor(Geom::Vec4f(1.0f, 0.0f , 0.0f, 1.0f));
Sylvain Thery's avatar
Sylvain Thery committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

    m_strings = new Utils::Strings3D(true, Geom::Vec3f(0.1f,0.0f,0.3f));
    storeVerticesInfo();
    m_strings->sendToVBO();

    // copy de contenu de VBO a la creation
	m_dataVBO = new Utils::VBO(*m_positionVBO);

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

	// 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);
156

157 158 159
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES);
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::LINES);
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::POINTS);
160

161
//	SelectorDartNoBoundary<PFP::MAP> nb(myMap);
Pierre Kraemer's avatar
Pierre Kraemer committed
162
	m_render_topo->updateData(myMap, position,  0.9f, 0.9f, 0.9f); // nb
163

Sylvain Thery's avatar
Sylvain Thery committed
164 165 166 167
	// timer example for animation
	m_timer = new QTimer( this );
	connect( m_timer, SIGNAL(timeout()), SLOT(animate()) );
}
168

Sylvain Thery's avatar
Sylvain Thery committed
169 170 171 172
void MyQT::cb_redraw()
{
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glEnable(GL_LIGHTING);
173

Sylvain Thery's avatar
Sylvain Thery committed
174
	m_render->draw(m_shader, Algo::Render::GL2::POINTS);
175

Sylvain Thery's avatar
Sylvain Thery committed
176 177 178
	glLineWidth(2.0f);
	m_shader->setColor(Geom::Vec4f(1.,1.,0.,0.));
	m_render->draw(m_shader, Algo::Render::GL2::LINES);
179

Sylvain Thery's avatar
Sylvain Thery committed
180 181
	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(1.0f, 1.0f);
182

Sylvain Thery's avatar
Sylvain Thery committed
183 184
	if (render_topo)
		m_render_topo->drawTopo();
185

186
/*	Dart d = myMap.phi2(myMap.begin());
Sylvain Thery's avatar
Sylvain Thery committed
187 188 189
	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);
190 191 192
*/	
	m_render_topo->overdrawDart(m_selected, 5, 1.0f, 0.0f, 1.0f);
	
Sylvain Thery's avatar
Sylvain Thery committed
193
	glDisable(GL_POLYGON_OFFSET_FILL);
194

Sylvain Thery's avatar
Sylvain Thery committed
195 196
	if (render_text)
		m_strings->drawAll(Geom::Vec3f(0.0f, 1.0f, 1.0f));
197

Sylvain Thery's avatar
Sylvain Thery committed
198 199 200 201
	if (render_balls)
	{
		m_render->draw(m_sprite, Algo::Render::GL2::POINTS);
	}
202

Sylvain Thery's avatar
Sylvain Thery committed
203 204 205 206 207 208
	if (render_vectors)
	{
		glLineWidth(1.0f);
		m_render->draw(m_lines, Algo::Render::GL2::POINTS);
	}
}
209

210
void MyQT::cb_mousePress(int /*button*/, int x, int y)
Sylvain Thery's avatar
Sylvain Thery committed
211 212 213
{
	if (Shift())
	{
214
//		SelectorDartNoBoundary<PFP::MAP> nb(myMap);
Pierre Kraemer's avatar
Pierre Kraemer committed
215
		Dart d = m_render_topo->picking(myMap, x,y); // nb
Sylvain Thery's avatar
Sylvain Thery committed
216 217 218
		if (d != Dart::nil())
		{
			CGoGNout << "Dart "<< d << " clicked" << CGoGNendl;
219
			m_selected = d;
Sylvain Thery's avatar
Sylvain Thery committed
220 221 222 223 224
		}
		else
		{
			statusMsg("");
		}
225
		updateGL();
Sylvain Thery's avatar
Sylvain Thery committed
226 227
	}
}
228

Sylvain Thery's avatar
Sylvain Thery committed
229 230 231 232
void MyQT::cb_keyPress(int code)
{
	if (code  == 's')
	{
233
		std::string filename = selectFileSave("Export SVG file ",".","(*.svg)");
Pierre Kraemer's avatar
Pierre Kraemer committed
234
		Utils::SVG::SVGOut svg(filename, modelViewMatrix(), projectionMatrix());
235 236
//		svg.setWidth(1.0f);
//		svg.setColor(Geom::Vec3f(0.0f,0.0f,0.5f));
Pierre Kraemer's avatar
Pierre Kraemer committed
237
		Algo::Render::SVG::renderEdges<PFP>(svg, myMap, position);
238 239
//		svg.setColor(Geom::Vec3f(0.0f,0.8f,0.0f));
//		svg.setWidth(5.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
240
		Algo::Render::SVG::renderVertices<PFP>(svg, myMap, position);
241
	//	svg.setColor(Geom::Vec3f(1.0f,0.0f,0.0f));
Sylvain Thery's avatar
Sylvain Thery committed
242
		m_strings->toSVG(svg);
Sylvain Thery's avatar
Sylvain Thery committed
243 244 245 246 247 248 249 250 251
		//svg destruction close the file
	}
	if (code  == 't')
	{
		if (m_timer->isActive())
			m_timer->stop();
		else
			m_timer->start(1000/30); // 30 fps
	}
untereiner's avatar
untereiner committed
252 253 254

	if(code == 'c')
	{
255
		//SelectorDartNoBoundary<PFP::MAP> nb(myMap);
256
		Algo::Volume::Modelisation::catmullClarkVol<PFP>(myMap, position);
untereiner's avatar
untereiner committed
257 258

		m_positionVBO->updateData(position);
259 260 261
		m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES);
		m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::LINES);
		m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::POINTS);
untereiner's avatar
untereiner committed
262

Pierre Kraemer's avatar
Pierre Kraemer committed
263
		m_render_topo->updateData(myMap, position, 0.9f, 0.9f, 0.9f);
untereiner's avatar
untereiner committed
264
	}
Sylvain Thery's avatar
Sylvain Thery committed
265
}
Sylvain Thery's avatar
bug  
Sylvain Thery committed
266

Sylvain Thery's avatar
Sylvain Thery committed
267 268
int main(int argc, char **argv)
{
269
	position = myMap.addAttribute<VEC3, VERTEX, MAP>("position");
untereiner's avatar
untereiner committed
270

Sylvain Thery's avatar
Sylvain Thery committed
271
	CGoGNout << 5.34 << " toto "<< Geom::Vec3f(2.5f, 2.2f, 4.3f) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
272
	CGoGNout << 3 << " tutu "<< 4 << CGoGNendl;
untereiner's avatar
untereiner committed
273

Sylvain Thery's avatar
Sylvain Thery committed
274 275 276
	int nb=3;
	if (argc>1)
		nb = atoi(argv[1]);
untereiner's avatar
untereiner committed
277 278 279
    Algo::Volume::Tilings::Cubic::Grid<PFP> cubic(myMap, nb, nb, nb);
    cubic.embedIntoGrid(position, 1.0f, 1.0f, 1.0f);
    dglobal = NIL;
untereiner's avatar
untereiner committed
280

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

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

Sylvain Thery's avatar
Sylvain Thery committed
289 290 291 292 293 294 295 296 297 298
 	// 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;

Sylvain Thery's avatar
Sylvain Thery committed
299
	CGoGNout.noStatusBar();
Sylvain Thery's avatar
Sylvain Thery committed
300

301
	//  bounding box
Sylvain Thery's avatar
Sylvain Thery committed
302
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
303 304
    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
305

306
    // envoit info BB a l'interface
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
307
	sqt.setParamObject(lWidthObj, lPosObj.data());
Pierre Kraemer's avatar
Pierre Kraemer committed
308

Sylvain Thery's avatar
Sylvain Thery committed
309 310 311
	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)) );
312
	sqt.setCallBack( dock.checkBox_topo, SIGNAL(toggled(bool)), SLOT(topo_onoff(bool)) );
Pierre Kraemer's avatar
Pierre Kraemer committed
313

Sylvain Thery's avatar
Sylvain Thery committed
314 315 316
	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)) );
Pierre Kraemer's avatar
Pierre Kraemer committed
317

318 319
	sqt.m_selected = myMap.begin();

Sylvain Thery's avatar
Sylvain Thery committed
320
	sqt.setGeometry(100,100,1024,1024);
321
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
322

Sylvain Thery's avatar
Sylvain Thery committed
323 324 325 326 327 328 329 330 331 332 333
	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;

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