scene.cpp 9.52 KB
Newer Older
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 26 27 28 29 30 31 32 33 34 35 36
/*******************************************************************************
* 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 <iostream>

#include "Utils/glutwin.h"

#include "Topology/generic/parameters.h"
#include "Topology/map/map2.h"
#include "Topology/generic/embeddedMap2.h"

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

#include "Algo/Render/vbo_MapRender.h"
#include "Algo/Modelisation/polyhedron.h"
Sylvain Thery's avatar
Sylvain Thery committed
41
#include "Topology/generic/cellmarker.h"
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

#include "Utils/scene_graph.h"

using namespace CGoGN;



struct PFP: public PFP_STANDARD
{
	// definition de la carte
	typedef EmbeddedMap2<Map2> MAP;
};


PFP::MAP myMap;

Sylvain Thery's avatar
Sylvain Thery committed
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 95

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
96 97 98
	PFP::TVEC3 normal ;

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

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



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

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

	~myGlutWin() {}

116 117
};

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

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

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
148
		m_render = new Algo::Render::GL2::MapRender_VBO() ;
149 150

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

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

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


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

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

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

	mgw.position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position") ;
Sylvain Thery's avatar
Sylvain Thery committed
215
	mgw.normal = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "normal") ;
216

Sylvain Thery's avatar
Sylvain Thery committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	glewInit();
	if (! Utils::GLSLShader::areShadersSupported())
	{
		std::cerr << "Shaders not supported"<< std::endl;
		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);

	CellMarker mark1(myMap,VERTEX_CELL);
	prim1.mark(mark1);
234 235 236

	// create a sphere
	Algo::Modelisation::Polyhedron<PFP> prim2(myMap, mgw.position);
Sylvain Thery's avatar
Sylvain Thery committed
237
	prim2.cylinder_topo(16,16, true, true);
238
	prim2.embedSphere(40.0f);
Sylvain Thery's avatar
Sylvain Thery committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

	CellMarker mark2(myMap,VERTEX_CELL);
	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);

	CellMarker mark3(myMap,VERTEX_CELL);
	prim3.mark(mark3);



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

Sylvain Thery's avatar
Sylvain Thery committed
255 256 257
	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
258 259

	SelectorCellMarked cm1(mark1);
Sylvain Thery's avatar
Sylvain Thery committed
260 261 262
	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
263 264


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

Sylvain Thery's avatar
Sylvain Thery committed
267
	SelectorCellMarked cm2(mark2);
Sylvain Thery's avatar
Sylvain Thery committed
268 269 270
	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
271

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


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

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

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

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

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

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

Sylvain Thery's avatar
Sylvain Thery committed
305 306 307
	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
308
	group1->addChild(vbon2);
309 310


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

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


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

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

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


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

	mgw.mainLoop();

	return 0;
}