scene.cpp 9.43 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 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"
36
#include "Algo/Geometry/normal.h"
37 38 39

#include "Algo/Render/vbo_MapRender.h"
#include "Algo/Modelisation/polyhedron.h"
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;

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 ;

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

111 112 113 114
	void exitCallback();

	~myGlutWin() {}

115 116
};

117
void myGlutWin::exitCallback()
118
{
119
	CGoGNout << "DESTRUCTOR"<< CGoGNendl;
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
}

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()
{
	// create the renderer (first call only)
	if (m_render == NULL)
Sylvain Thery's avatar
Sylvain Thery committed
145
		m_render = new Algo::Render::GL2::MapRender_VBO() ;
146 147

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

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

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
}


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
175
//	m_render->draw(Algo::Render::GL2::LINES) ;
176 177 178 179 180 181 182 183 184 185
//
//	//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
186
//	m_render->draw(Algo::Render::GL2::TRIANGLES) ;
187 188 189
//
//	glDisable( GL_POLYGON_OFFSET_FILL );

190
	Utils::SceneGraph::renderGraph(root);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

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

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

Sylvain Thery's avatar
Sylvain Thery committed
214 215 216
	glewInit();
	if (! Utils::GLSLShader::areShadersSupported())
	{
217
		CGoGNerr << "Shaders not supported"<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
218 219 220 221 222 223 224 225 226 227 228
		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);

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

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

237
	CellMarker mark2(myMap,VERTEX);
Sylvain Thery's avatar
Sylvain Thery committed
238 239 240 241 242 243 244
	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);

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



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

Sylvain Thery's avatar
Sylvain Thery committed
252 253 254
	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
255 256

	SelectorCellMarked cm1(mark1);
Sylvain Thery's avatar
Sylvain Thery committed
257 258 259
	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);
260 261


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

Sylvain Thery's avatar
Sylvain Thery committed
264
	SelectorCellMarked cm2(mark2);
Sylvain Thery's avatar
Sylvain Thery committed
265 266 267
	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
268

Sylvain Thery's avatar
Sylvain Thery committed
269
	Algo::Render::GL2::MapRender_VBO* render3 = new Algo::Render::GL2::MapRender_VBO(*render1) ;
Sylvain Thery's avatar
Sylvain Thery committed
270
	SelectorCellMarked cm3(mark3);
Sylvain Thery's avatar
Sylvain Thery committed
271 272 273
	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);
274 275


276 277 278 279
	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
280 281 282
	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);
283
	mater->setColor(Geom::Vec4f(0.0f, 0.0f, 0.0f, 1.0f));
Sylvain Thery's avatar
Sylvain Thery committed
284
	mater->setShader(phong);
285

286 287
	mgw.root->setMaterial(mater);

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

292
	mgw.root->addChild(vbon);
293

294
	Utils::SceneGraph::Group_Node* group1 = new Utils::SceneGraph::Group_Node();
295 296 297
	Geom::Matrix44f mat;
	mat.identity();
	Geom::translate(80.0f, 0.0f, 0.0f,mat);
298
	group1->setMatrixTransfo(mat);
299

300
	mgw.root->addChild(group1);
301

Sylvain Thery's avatar
Sylvain Thery committed
302 303 304
	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
305
	group1->addChild(vbon2);
306 307


308
	Utils::SceneGraph::Group_Node* group2 = new Utils::SceneGraph::Group_Node();
309 310
	mat.identity();
	Geom::translate(100.0f, 50.0f, 20.0f,mat);
311
	group2->setMatrixTransfo(mat);
312

313
	mgw.root->addChild(group2);
314 315


316 317
	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
318 319 320
	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);
321
	mater->setColor(Geom::Vec4f(1.0f, 1.0f, 0.0f, 1.0f));
Sylvain Thery's avatar
Sylvain Thery committed
322
	mater->setNoShader();
323
	group2->setMaterial(mater);
324

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

329
	group2->addChild(vbon);
330 331


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

	mgw.mainLoop();

	return 0;
}