simpleGMap2.cpp 10.1 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
/*******************************************************************************
* 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/os_spec.h"

#include "Utils/GLSLShader.h"
#include "Utils/glutwin.h"

#include "Topology/gmap/gmap2.h"
#include "Topology/generic/embeddedMap2.h"

#include "Geometry/matrix.h"
#include "Geometry/vector_gen.h"
#include "Algo/Import/import.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Render/map_glRender.h"
#include "Algo/Render/topo_vboRender.h"

using namespace CGoGN;


// definition des parametres de la carte
struct PFP
{
	typedef EmbeddedMap2<GMap2> MAP;

	// definition du type de reel utilise
	typedef float REAL;
	typedef Geom::Vector<3,REAL> VEC3;
	typedef Geom::Vector<6,REAL> VEC6;
	typedef Geom::Matrix<3,3,REAL> MATRIX33;
	typedef Geom::Matrix<4,4,REAL> MATRIX44;
	typedef Geom::Matrix<3,6,REAL> MATRIX36;

	typedef AttributeHandler<VEC3> TVEC3;
	typedef AttributeHandler<REAL> TREAL;
	typedef AttributeHandler<MATRIX33> TFRAME;
	typedef AttributeHandler<MATRIX36> TRGBFUNCS;
};

INIT_STATICS_MAP() ;

PFP::MAP myMap;

// interface
class myGlutWin: public Utils::SimpleGlutWin
{
public:
        Geom::Vec4f colDif;

        Geom::Vec4f colSpec;

        Geom::Vec4f colClear;

        Geom::Vec4f colNormal;

        float shininess;

    	PFP::TVEC3 position ;
    	PFP::TVEC3 normal ;

        /**
         * position of object
         */
        PFP::VEC3 gPosObj;

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

        /**
         * factor to apply to normal drawing
         */
        float normalScaleFactor;

        /**
         * mesh normals
         */
        std::vector<PFP::VEC3> vnormals;

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

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

        /**
         * Display list init
         */
        void initDL();

        /**
         * Display list init only for normal drawing
         */
        void initDLNormals(void);

        /**
         * Display list init only for lines drawing
         */
        void initDLLines(void);

         /**
         * GL (old school) rendering function (called by init DL)
         */
        void render(int renderMode);

         /**
         * inverse the normal when computing normal
         */
        bool invertedNormals;

        /**
         * inverse object for culling
         */
        bool invertedObject;

        /**
         * rendering normals ?
         */
        bool renderNormal;

        /**
         * rendering lines ?
         */
        bool renderLines;

        /**
        * aide affichee
        */
        bool aff_help;

        /**
         * style of rendering
         */
        int renderStyle;

        /**
         * Display List for object
         */
        GLuint dl_obj;

        /**
         * Display List for object (second)
         */
        GLuint dl_obj2;

        /**
         * Display List for normal
         */
        GLuint dl_norm;

        /**
         * render topology
         */
        Algo::Render::VBO::topo_VBORenderGMap* m_render_topo;

        /**
         * render mode enum
         */
        enum { CLEAR=1, LINE, FLAT, GOURAUD, PHONG, NORMAL,NONE };

	myGlutWin(	int* argc, char **argv, int winX, int winY) :
		SimpleGlutWin(argc,argv,winX,winY),
		invertedNormals(true),
		invertedObject(true),
		renderNormal(false),
		renderLines(true),
		aff_help(false),
		renderStyle(FLAT),
		dl_obj(-1),
		dl_obj2(-1),
		dl_norm(-1)
    {
		if (this->shaderOk) shaders[0].loadShaders("phong_vs.txt","phong_ps.txt");
		m_render_topo = new Algo::Render::VBO::topo_VBORenderGMap();
	}
};

void myGlutWin::render(int renderMode)
{
	switch (renderMode)
	{
	case FLAT:
			glEnable(GL_LIGHTING);
			glDisable(GL_CULL_FACE);
			glPolygonMode(GL_FRONT,GL_FILL);
			glShadeModel(GL_FLAT);
			Algo::Geometry::computeNormalVertices<PFP>(myMap, position, normal) ;

			if (invertedObject)
					glFrontFace(GL_CW);
			else
					glFrontFace(GL_CCW);

			Algo::Render::Direct::renderTriQuadPoly<PFP>(myMap, Algo::Render::Direct::FLAT, 1.0f, position, normal);
			break;

	case LINE:
			glDisable(GL_LIGHTING);
			glEnable(GL_CULL_FACE);
			glPolygonMode(GL_FRONT,GL_LINE);

			if (invertedObject)
					glFrontFace(GL_CW);
			else
					glFrontFace(GL_CCW);

			if (renderStyle!=CLEAR)
					glColor3f(1.0f,1.0f,0.0f);
			else
					glColor4fv(colDif.data());

			Algo::Render::Direct::renderTriQuadPoly<PFP>(myMap, Algo::Render::Direct::LINE, 1.0f, position, normal);

			break;

	case NONE:
			break;

	default:
			break;
	}
}

void myGlutWin::initDL(void)
{
	if (glIsList(dl_obj))
			glDeleteLists(dl_obj,1);

	dl_obj = glGenLists(1);

	glNewList(dl_obj,GL_COMPILE);

	if (renderStyle==FLAT)
			render(FLAT);

	glEndList();
}

void myGlutWin::initDLLines(void)
{
	std::vector<PFP::VEC3> normals;

	if (glIsList(dl_obj2))
			glDeleteLists(dl_obj2,1);

	dl_obj2 = glGenLists(1);

	glNewList(dl_obj2,GL_COMPILE);

	render(LINE);

	glEndList();
}

void myGlutWin::myRedraw(void)
{
	GLfloat black[4]= {0.0f,0.0f,0.0f,1.0f};
	GLfloat amb[4]= {0.2f,0.1f,0.1f,1.0f};

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();

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

	if (glIsList(dl_obj2))
			glCallList(dl_obj2);

	m_render_topo->drawTopo();

	glEnable( GL_POLYGON_OFFSET_FILL );
	glPolygonOffset( 1.0f, 1.0f );

	if (glIsList(dl_obj))
	{
			glMaterialfv(GL_FRONT, GL_DIFFUSE, colDif.data());
			glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
			glMaterialfv(GL_FRONT,GL_SPECULAR,black);
			glMaterialf( GL_FRONT, GL_SHININESS, 1.0f );
			glCallList(dl_obj);
	}

	glDisable( GL_POLYGON_OFFSET_FILL );


	glPopMatrix();
}

void myGlutWin::myKeyboard(unsigned char keycode, int, int)
{
//	GLint t1,t2;
//	GLfloat seconds;
	switch (keycode) {

	case 'd':
			setFoc(getFoc()/1.05f);
			reshape(-1,-1);

			glutPostRedisplay();
			break;
	case 'D':
			setFoc(getFoc()*1.05f);
			reshape(-1,-1);

			glutPostRedisplay();
			break;

	case 'i':
			invertedNormals = !invertedNormals;
			initDL();
			glutPostRedisplay();
			break;

	case 'o':
			invertedObject = !invertedObject;
			initDL();
			glutPostRedisplay();
			break;

	case 'O':
			invertedObject = !invertedObject;
			invertedNormals = !invertedNormals;
			initDL();
			glutPostRedisplay();
			break;

	case 'f':
			renderStyle = FLAT;
			initDL();
			glutPostRedisplay();
			break;


	case 'l':
			renderLines = !renderLines;

			if (renderLines)
					initDLLines();
			else {
					if (glIsList(dl_obj2))
							glDeleteLists(dl_obj2,1);
			}
			initDL();
			glutPostRedisplay();
			break;

	case 'z':
			shininess /= 1.1f;
			glutPostRedisplay();
			break;
	case 'Z':
			shininess *= 1.1f;
			glutPostRedisplay();
			break;

	}
}


int main(int argc, char **argv)
{
	myGlutWin mgw(&argc,argv,800,800);

	char* filename = argv[1] ;

	mgw.position = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "position") ;
	mgw.normal = myMap.addAttribute<PFP::VEC3>(VERTEX_ORBIT, "normal") ;

	PFP::VEC3 gMax;
	PFP::VEC3 gMin;

	if(argc < 2)
	{
		Dart d = myMap.newFace(4);
		Dart e = myMap.newFace(3);

		myMap.sewFaces(d, e);

		// plonge les 4 sommets du carré
		mgw.position[d] = PFP::VEC3(-1,1,0);

		d = myMap.phi1(d);
		mgw.position[d] = PFP::VEC3(1,1,0);

		d = myMap.phi1(d);
		mgw.position[d] = PFP::VEC3(1,-1,0);

		d = myMap.phi1(d);
		mgw.position[d] = PFP::VEC3(-1,-1,0);

		// plonge le dernier sommet du triangle
		d = myMap.phi_1(e);
		mgw.position[d] = PFP::VEC3(0,2,0);
	}
	else
	{
		bool success = Algo::Import::importMesh<PFP>(myMap, filename, mgw.position, Algo::Import::ImportSurfacique::UNKNOWNSURFACE) ;
		if (!success)
		{
			std::cerr <<"Import fail"<< std::endl;
			exit(1);
		}
	}

	// pour le rendu
	Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, mgw.position) ;

	mgw.gPosObj = bb.center() ;
	float tailleX = bb.size(0) ;
	float tailleY = bb.size(1) ;
	float tailleZ = bb.size(2) ;

	mgw.gWidthObj = std::max<float>(std::max<float>(tailleX, tailleY), tailleZ) ;
	mgw.normalScaleFactor = std::min<float>(std::min<float>(tailleX,tailleY),tailleZ) / 50.0f ;

	mgw.colClear = Geom::Vec4f(0.2f,0.2f,0.2f,0.1);
	mgw.colDif = Geom::Vec4f(0.8f,0.9f,0.7f,1.0f);
	mgw.colSpec = Geom::Vec4f(0.9f,0.9f,0.9f,1.0f);
	mgw.colNormal = Geom::Vec4f(1.0f,0.0f,0.0f,1.0f);
	mgw.shininess=80.0f;

	mgw.m_render_topo->updateData<PFP>(myMap,mgw.position,0.9f, 0.9f);

	mgw.initDL();
	mgw.initDLLines();

	mgw.mainLoop();

	return 0;
}