scene.cpp 9.45 KB
Newer Older
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           *
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/                                         *
21
22
23
24
25
26
27
28
29
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

#include "Utils/glutwin.h"

#include "Topology/generic/parameters.h"
30
#include "Topology/map/embeddedMap2.h"
31
32
33
34
35

#include "Geometry/vector_gen.h"
#include "Geometry/matrix.h"
#include "Geometry/transfo.h"
#include "Algo/Geometry/boundingbox.h"
Sylvain Thery's avatar
Sylvain Thery committed
36
#include "Algo/Geometry/normal.h"
37
38
39

#include "Algo/Render/vbo_MapRender.h"
#include "Algo/Modelisation/polyhedron.h"
Sylvain Thery's avatar
Sylvain Thery committed
40
#include "Topology/generic/cellmarker.h"
41
42
43
44
45
46
47
48
49
50

#include "Utils/scene_graph.h"

using namespace CGoGN;



struct PFP: public PFP_STANDARD
{
	// definition de la carte
51
	typedef EmbeddedMap2 MAP;
52
53
54
55
56
};


PFP::MAP myMap;

Sylvain Thery's avatar
Sylvain Thery committed
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

class myGlutWin: public Utils::SimpleGlutWin
{
public:
	/**
	 * position of object
	 */
	Geom::Vec3f gPosObj;

	/**
	 * width of object
	 */
	float gWidthObj;

	/**
	 * redraw CB
	 */
	void myRedraw();

	/**
	 * keyboard CB
	 */
	void myKeyboard(unsigned char keycode, int x, int y);

	/**
	 * Update VBO buffers for map
	 */
	void updateRender();

	/**
	 * GL initialization
	 */
	void init();

	// position handler
	PFP::TVEC3 position ;

Sylvain Thery's avatar
Sylvain Thery committed
95
96
97
	PFP::TVEC3 normal ;

	Utils::SceneGraph::Group_Node* root;
98
99
100
101

	/**
	 * render object
	 */
Sylvain Thery's avatar
Sylvain Thery committed
102
	Algo::Render::GL2::MapRender_VBO* m_render;
103
104
105
106
107
108
109
110



	myGlutWin(	int* argc, char **argv, int winX, int winY) :
				SimpleGlutWin(argc,argv,winX,winY),
				m_render(NULL)
				 {	}

Sylvain Thery's avatar
Sylvain Thery committed
111
112
113
114
	void exitCallback();

	~myGlutWin() {}

115
116
};

Sylvain Thery's avatar
Sylvain Thery committed
117
void myGlutWin::exitCallback()
118
{
119
	CGoGNout << "DESTRUCTOR"<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
120
121
122
123
	eraseGraph(root);
//	if (m_render)
//		delete m_render;

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
}

void myGlutWin::init()
{

	glClearColor(0.1f,0.1f,0.1f,0.0f);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);

	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
	glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
	glEnable(GL_LIGHT0);
}


void myGlutWin::updateRender()
{
	SelectorTrue allDarts;

	// create the renderer (first call only)
	if (m_render == NULL)
Sylvain Thery's avatar
Sylvain Thery committed
147
		m_render = new Algo::Render::GL2::MapRender_VBO() ;
148
149

	// update buffer op vertices positions
Sylvain Thery's avatar
Sylvain Thery committed
150
	m_render->updateData(Algo::Render::GL2::POSITIONS, position);
151
	// update flat faces primtives (warning need position buffer)
Sylvain Thery's avatar
Sylvain Thery committed
152
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::FLAT_TRIANGLES);
Sylvain Thery's avatar
Sylvain Thery committed
153
	// update smooth faces primtives
Sylvain Thery's avatar
Sylvain Thery committed
154
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
155
	// update lines primitives
Sylvain Thery's avatar
Sylvain Thery committed
156
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
157

Sylvain Thery's avatar
Sylvain Thery committed
158
	Algo::Geometry::computeNormalVertices<PFP>(myMap, position, normal) ;
Sylvain Thery's avatar
Sylvain Thery committed
159
	m_render->updateData(Algo::Render::GL2::NORMALS, normal);
Sylvain Thery's avatar
Sylvain Thery committed
160

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
}


void myGlutWin::myRedraw(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();

	// center the scene
	float sc = 50./gWidthObj;
	glScalef(sc,sc,sc);
	glTranslatef(-gPosObj[0],-gPosObj[1],-gPosObj[2]);

//	// DRAW LINES OF OBJECTS
//	glDisable(GL_LIGHTING);
//	glColor3f(1.0f,1.0f,0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
177
//	m_render->draw(Algo::Render::GL2::LINES) ;
178
179
180
181
182
183
184
185
186
187
//
//	//shit for nice line rendering
//	glEnable( GL_POLYGON_OFFSET_FILL );
//	glPolygonOffset( 1.0f, 1.0f );
//
//	// DRAW FACES OF OBJECTS
//	glEnable(GL_LIGHTING);
//	glEnable(GL_COLOR_MATERIAL);
//	glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);
//	glColor3f(0.0f,0.0f,0.9f);
Sylvain Thery's avatar
Sylvain Thery committed
188
//	m_render->draw(Algo::Render::GL2::TRIANGLES) ;
189
190
191
//
//	glDisable( GL_POLYGON_OFFSET_FILL );

Sylvain Thery's avatar
Sylvain Thery committed
192
	Utils::SceneGraph::renderGraph(root);
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

	glPopMatrix();
}

void myGlutWin::myKeyboard(unsigned char keycode, int x, int y)
{
	switch(keycode)
	{
	default:
		break;
	}
}


int main(int argc, char **argv)
{
	/// init glut interface and
	myGlutWin mgw(&argc,argv,800,800);
	mgw.init();

213
214
	mgw.position = myMap.addAttribute<PFP::VEC3>(VERTEX, "position") ;
	mgw.normal = myMap.addAttribute<PFP::VEC3>(VERTEX, "normal") ;
215

Sylvain Thery's avatar
Sylvain Thery committed
216
217
218
	glewInit();
	if (! Utils::GLSLShader::areShadersSupported())
	{
219
		CGoGNerr << "Shaders not supported"<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
220
221
222
223
224
225
226
227
228
229
230
		exit(1);
	}

	Utils::GLSLShader* phong = new Utils::GLSLShader() ;
	phong->loadShaders("phong_vs.txt", "phong_ps.txt") ;


	Algo::Modelisation::Polyhedron<PFP> prim1(myMap, mgw.position);
	prim1.cylinder_topo(16,15, true, true);
	prim1.embedCylinder(30.0f,20.0f,50.0f);

231
	CellMarker mark1(myMap,VERTEX);
Sylvain Thery's avatar
Sylvain Thery committed
232
	prim1.mark(mark1);
233
234
235

	// create a sphere
	Algo::Modelisation::Polyhedron<PFP> prim2(myMap, mgw.position);
Sylvain Thery's avatar
Sylvain Thery committed
236
	prim2.cylinder_topo(16,16, true, true);
237
	prim2.embedSphere(40.0f);
Sylvain Thery's avatar
Sylvain Thery committed
238

239
	CellMarker mark2(myMap,VERTEX);
Sylvain Thery's avatar
Sylvain Thery committed
240
241
242
243
244
245
246
	prim2.mark(mark2);

	// create a sphere
	Algo::Modelisation::Polyhedron<PFP> prim3(myMap, mgw.position);
	prim3.tore_topo(12,48);
	prim3.embedTore(50.0f,10.0f);

247
	CellMarker mark3(myMap,VERTEX);
Sylvain Thery's avatar
Sylvain Thery committed
248
249
250
251
252
253
	prim3.mark(mark3);



	Algo::Geometry::computeNormalVertices<PFP>(myMap, mgw.position, mgw.normal) ;

Sylvain Thery's avatar
Sylvain Thery committed
254
255
256
	Algo::Render::GL2::MapRender_VBO* render1 = new Algo::Render::GL2::MapRender_VBO() ;
	render1->updateData(Algo::Render::GL2::POSITIONS, mgw.position);
	render1->updateData(Algo::Render::GL2::NORMALS, mgw.normal);
Sylvain Thery's avatar
Sylvain Thery committed
257
258

	SelectorCellMarked cm1(mark1);
Sylvain Thery's avatar
Sylvain Thery committed
259
260
261
	render1->initPrimitives<PFP>(myMap, cm1, Algo::Render::GL2::TRIANGLES);
	render1->initPrimitives<PFP>(myMap, cm1, Algo::Render::GL2::FLAT_TRIANGLES);
	render1->initPrimitives<PFP>(myMap, cm1, Algo::Render::GL2::LINES);
Sylvain Thery's avatar
Sylvain Thery committed
262
263


Sylvain Thery's avatar
Sylvain Thery committed
264
	Algo::Render::GL2::MapRender_VBO* render2 = new Algo::Render::GL2::MapRender_VBO(*render1) ;
Sylvain Thery's avatar
Sylvain Thery committed
265

Sylvain Thery's avatar
Sylvain Thery committed
266
	SelectorCellMarked cm2(mark2);
Sylvain Thery's avatar
Sylvain Thery committed
267
268
269
	render2->initPrimitives<PFP>(myMap, cm2, Algo::Render::GL2::TRIANGLES);
	render2->initPrimitives<PFP>(myMap, cm2, Algo::Render::GL2::FLAT_TRIANGLES);
	render2->initPrimitives<PFP>(myMap, cm2, Algo::Render::GL2::POINTS);
Sylvain Thery's avatar
Sylvain Thery committed
270

Sylvain Thery's avatar
Sylvain Thery committed
271
	Algo::Render::GL2::MapRender_VBO* render3 = new Algo::Render::GL2::MapRender_VBO(*render1) ;
Sylvain Thery's avatar
Sylvain Thery committed
272
	SelectorCellMarked cm3(mark3);
Sylvain Thery's avatar
Sylvain Thery committed
273
274
275
	render3->initPrimitives<PFP>(myMap, cm3, Algo::Render::GL2::FLAT_TRIANGLES);
	render3->initPrimitives<PFP>(myMap, cm3, Algo::Render::GL2::TRIANGLES);
	render3->initPrimitives<PFP>(myMap, cm3, Algo::Render::GL2::LINES);
276
277


Sylvain Thery's avatar
Sylvain Thery committed
278
279
280
281
	mgw.root = new Utils::SceneGraph::Group_Node();

	Utils::SceneGraph::Material_Node* mater = new Utils::SceneGraph::Material_Node();
	mater->setDiffuse(Geom::Vec4f(0.3f, 1.0f, 0.0f, 1.0f));
Sylvain Thery's avatar
Sylvain Thery committed
282
283
284
	mater->setAmbient(Geom::Vec4f(0.1f, 0.1f, 0.1f, 1.0f));
	mater->setSpecular(Geom::Vec4f(1.0f, 1.0f, 1.0f, 1.0f));
	mater->setShininess(100.0f);
Sylvain Thery's avatar
Sylvain Thery committed
285
	mater->setColor(Geom::Vec4f(0.0f, 0.0f, 0.0f, 1.0f));
Sylvain Thery's avatar
Sylvain Thery committed
286
	mater->setShader(phong);
287

Sylvain Thery's avatar
Sylvain Thery committed
288
289
	mgw.root->setMaterial(mater);

Sylvain Thery's avatar
Sylvain Thery committed
290
291
292
	Utils::SceneGraph::GL2_Node* vbon = new Utils::SceneGraph::GL2_Node(render1);
	vbon->setPrimitives(Algo::Render::GL2::FLAT_TRIANGLES);
	vbon->setPrimitives(Algo::Render::GL2::LINES);
293

Sylvain Thery's avatar
Sylvain Thery committed
294
	mgw.root->addChild(vbon);
295

Sylvain Thery's avatar
Sylvain Thery committed
296
	Utils::SceneGraph::Group_Node* group1 = new Utils::SceneGraph::Group_Node();
297
298
299
	Geom::Matrix44f mat;
	mat.identity();
	Geom::translate(80.0f, 0.0f, 0.0f,mat);
Sylvain Thery's avatar
Sylvain Thery committed
300
	group1->setMatrixTransfo(mat);
301

Sylvain Thery's avatar
Sylvain Thery committed
302
	mgw.root->addChild(group1);
303

Sylvain Thery's avatar
Sylvain Thery committed
304
305
306
	Utils::SceneGraph::GL2_Node* vbon2 = new Utils::SceneGraph::GL2_Node(render2);
	vbon2->setPrimitives(Algo::Render::GL2::TRIANGLES);
	vbon2->setPrimitives(Algo::Render::GL2::POINTS);
Sylvain Thery's avatar
Sylvain Thery committed
307
	group1->addChild(vbon2);
308
309


Sylvain Thery's avatar
Sylvain Thery committed
310
	Utils::SceneGraph::Group_Node* group2 = new Utils::SceneGraph::Group_Node();
311
312
	mat.identity();
	Geom::translate(100.0f, 50.0f, 20.0f,mat);
Sylvain Thery's avatar
Sylvain Thery committed
313
	group2->setMatrixTransfo(mat);
314

Sylvain Thery's avatar
Sylvain Thery committed
315
	mgw.root->addChild(group2);
316
317


Sylvain Thery's avatar
Sylvain Thery committed
318
319
	mater = new Utils::SceneGraph::Material_Node();
	mater->setDiffuse(Geom::Vec4f(1.0f, 0.0f, 0.0f, 1.0f));
Sylvain Thery's avatar
Sylvain Thery committed
320
321
322
	mater->setAmbient(Geom::Vec4f(0.1f, 0.1f, 0.1f, 1.0f));
	mater->setSpecular(Geom::Vec4f(1.0f, 1.0f, 1.0f, 1.0f));
	mater->setShininess(100.0f);
Sylvain Thery's avatar
Sylvain Thery committed
323
	mater->setColor(Geom::Vec4f(1.0f, 1.0f, 0.0f, 1.0f));
Sylvain Thery's avatar
Sylvain Thery committed
324
	mater->setNoShader();
Sylvain Thery's avatar
Sylvain Thery committed
325
	group2->setMaterial(mater);
326

Sylvain Thery's avatar
Sylvain Thery committed
327
328
329
	vbon = new Utils::SceneGraph::GL2_Node(render3);
	vbon->setPrimitives(Algo::Render::GL2::TRIANGLES);
	vbon->setPrimitives(Algo::Render::GL2::LINES);
330

Sylvain Thery's avatar
Sylvain Thery committed
331
	group2->addChild(vbon);
332
333


Sylvain Thery's avatar
Sylvain Thery committed
334
// not possible here to compute a BB !!
335
336
337
338
339
340
341
342
343
	mgw.gWidthObj = 200.0f;
	mgw.gPosObj = Geom::Vec3f(100.0f,0.0f,0.0f);

	mgw.mainLoop();

	return 0;
}