viewer.cpp 15.5 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
/*******************************************************************************
* 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 "viewer.h"
#include <string>

/**********************************************************************************************
 *                                 MyGlutWin IMPLEMENTATION                                   *
 **********************************************************************************************/

MyGlutWin::MyGlutWin(int* argc, char **argv, int winX, int winY) :
	SimpleGlutWin(argc, argv, winX, winY)
{}

void MyGlutWin::init()
{
	explode = 0.9f;

	b_animate = false;
	render_anim = false;
	nbGenerated = 0;

	displayFps = false;
	nextUpdate = clock() + 1 * CLOCKS_PER_SEC;
	fps = 0;
	
	glEnable( GL_POINT_SMOOTH );

	visu = 0;
}

void MyGlutWin::myRedraw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	if(b_animate)
		animate();

	glDisable(GL_LIGHTING);
Pierre Kraemer's avatar
Pierre Kraemer committed
61
/*
Pierre Kraemer's avatar
Pierre Kraemer committed
62
63
64
65
66
67
68
69
70
71
72
	glColor3f(0.5f,0.5f,0.5f);
	glBegin(GL_LINES);
		glVertex3f(0.,0.,0.);
		glVertex3f(1.,0.,0.);

		glVertex3f(0.,0.,0.);
		glVertex3f(0.,1.,0.);

		glVertex3f(0.,0.,0.);
		glVertex3f(0.,0.,1.);
	glEnd();
Pierre Kraemer's avatar
Pierre Kraemer committed
73
*/
Pierre Kraemer's avatar
Pierre Kraemer committed
74
75
76
	for(std::vector<Agent*>::iterator it = sim.agents_.begin(); it != sim.agents_.end(); ++ it)
	{	
		renderAgent(*it);
Pierre Kraemer's avatar
Pierre Kraemer committed
77
78
/*
		for(std::vector<std::pair<float, Dart> >::iterator obst = (*it)->obstacleNeighbors_.begin();
Pierre Kraemer's avatar
Pierre Kraemer committed
79
80
81
82
83
84
85
			obst != (*it)->obstacleNeighbors_.end() && obst->first < (*it)->rangeSq_;
			++obst)
		{
			glColor3f(0.0f, 1.0f, 1.0f);
			glLineWidth(10.0f);
			renderDart(sim.envMap_, obst->second);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
86
*/
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		if(visu == 1)
		{
			// draw prediction triangle
 			glColor3f((*it)->part_.state / 3.0f, (*it)->part_.state % 2, 0.0f);
 			glLineWidth(5.0f);
 			renderPredictionTriangle(sim.envMap_, (*it)->part_.d, (*it)->part_.m_position);
		}
	}

	//draw the environment
	if(visu == 0)
	{
		glEnable(GL_LIGHTING);
		glColor3f(1.0f ,1.0f ,1.0f);
 		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		Algo::Render::GL1::renderTriQuadPoly<PFP>(sim.envMap_.map, Algo::Render::GL1::FLAT, 1.0, sim.envMap_.position, sim.envMap_.normal);
 		glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);

		glDisable(GL_LIGHTING);
 		glColor3f(0.0f, 0.0f, 0.0f);
		glLineWidth(1.0f);
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 		Algo::Render::GL1::renderTriQuadPoly<PFP>(sim.envMap_.map, Algo::Render::GL1::LINE, 1.0, sim.envMap_.position, sim.envMap_.normal);
 		glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
	}
	else if(visu == 1)
	{
		glDisable(GL_LIGHTING);
		glColor3f(1.0f, 1.0f, 1.0f);
		glLineWidth(1.0f);
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		Algo::Render::GL1::renderTriQuadPoly<PFP>(sim.envMap_.map, Algo::Render::GL1::LINE, 1.0, sim.envMap_.position, sim.envMap_.normal);
		glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
	}
	else if(visu == 2)
	{
		glDisable(GL_LIGHTING);
		glLineWidth(1.0f);
		Algo::Render::GL1::renderTopoMD2<PFP>(sim.envMap_.map, sim.envMap_.position, true, true, 0.9f, 0.9f);
	}
	else if(visu == 3)
	{}
	
	glColor3f(1.0f, 0.0f, 0.0f);
	glLineWidth(5.0f);
	CellMarker dmo(sim.envMap_.map, EDGE_CELL);
	for(Dart d = sim.envMap_.map.begin(); d != sim.envMap_.map.end(); sim.envMap_.map.next(d))
	{
		if(!dmo.isMarked(d))
		{
			dmo.mark(d);
			if(sim.envMap_.obstacleMark.isMarked(d))
				renderDart(sim.envMap_, d);
		}
	}
	/*
	glColor3f(0.0f, 1.0f, 0.0f);
	glLineWidth(5.0f);
	CellMarker dmb(sim.envMap_.map, FACE_CELL);
	for(Dart d = sim.envMap_.map.begin(); d != sim.envMap_.map.end(); sim.envMap_.map.next(d))
	{
		if(!dmb.isMarked(d))
		{
			dmb.mark(d);
			if(sim.envMap_.buildingMark.isMarked(d))
			{
				VEC3 c = Algo::Geometry::faceCentroid<PFP>(sim.envMap_.map, d, sim.envMap_.position);
				glBegin(GL_LINES);
				Dart dd = d;
				do
				{
					VEC3 p1 = sim.envMap_.position[dd];
					VEC3 p2 = sim.envMap_.position[sim.envMap_.map.phi1(dd)];
					VEC3 vec = c - p1;
					p1 = p1 + vec*0.25f;
					vec = c - p2;
					p2 = p2 + vec*0.25f;

					glVertex3f(p1[0],p1[1],p1[2]);
					glVertex3f(p2[0],p2[1],p2[2]);
					
					dd = sim.envMap_.map.phi1(dd);
				} while(dd != d);
				glEnd();
			}
		}
	}
	*/
	//count fps
	if(displayFps)
	{
		++frames;
		clock_t overtime = clock() - nextUpdate;
		if (overtime > 0)
		{
			fps = frames / (float)(1+ (float)overtime/(float)CLOCKS_PER_SEC);
			frames = 0;
			nextUpdate = clock() + 1 * CLOCKS_PER_SEC;
		}
		std::string s("fps: ");
		std::ostringstream oss;
		oss << s << fps;
		glColor3f(1.0f, 1.0f, 1.0f);
        printString2D(10, 20, oss.str());
	}
}

void MyGlutWin::animate()
{
	if(sim.globalTime_ - int(sim.globalTime_) == 0.0f && int(sim.globalTime_) % 10 == 0)
		std::cout << sim.globalTime_ << " " << sim.envMap_.mapMemoryCost() << std::endl;
	
	sim.doStep();
	glutPostRedisplay();

	if(render_anim)
	{
		std::ostringstream oss;
		std::ostringstream tmpNb;
		tmpNb << std::setfill('0') << std::setw(4) << nbGenerated;
		nbGenerated++;
		oss << "./helicoid3/exportTransZoomHelicoid" << tmpNb.str() << ".pov";
		std::string chaine = oss.str();
// 		exportScenePov(sim.envMap_.map,sim.envMap_.position,chaine,VEC3(43,762,65),VEC3(0,762,0),VEC3(1.0f,0,0),0,0,0);
//		exportScenePov(sim.envMap_.map,sim.envMap_.position,chaine,VEC3(43,762,65+(1500.0f*float(nbGenerated)/400.0f)),VEC3(0,762,0),VEC3(1.0f,0,0),0,0,0);

		if(nbGenerated == 400)
		{
			std::cout << "enough .pov generated" << std::endl;
			exit(0);
		}
	}
}
/*
bool MyGlutWin::exportScenePov(PFP::MAP& map, PFP::TVEC3& position, const std::string& filename, PFP::VEC3 cameraPos, PFP::VEC3 cameraLook, PFP::VEC3 translate, float angle_X, float angle_Y, float angle_Z,const FunctorSelect& good)
{
	static const bool highlightAgent=false;
	static const bool wireDisplay=false;
	
	uint highlightAgentNo=0;
  
	std::ofstream out(filename.c_str(), std::ios::out);
	if (!out.good()) {
		std::cerr << "(export) Unable to open file " << filename << std::endl;
		return false;
	}

	float angleX = angle_X;
	float angleY = angle_Y;
	float angleZ = angle_Z;
	
	if(highlightAgent) {
	  cameraLook = sim.agents_[highlightAgentNo]->position_;
	   VEC3 goalV = (sim.goals[highlightAgentNo] - sim.agents_[highlightAgentNo]->position_);
	   goalV.normalize();
	  cameraPos = cameraLook - goalV*50.0f;
// 	  cameraPos[2]+= 100.0f;
	  float tmp = cameraPos[1];
	  cameraPos[1] = cameraPos[2]+400.0f;
	  cameraPos[2] = tmp;
	  tmp = cameraLook[1];
	  cameraLook[2] = cameraLook[1];
	  cameraLook[1] = tmp;
	}

	//define the camera position
	out << "camera { location <" << cameraPos[0] << "," << cameraPos[1] << "," << cameraPos[2]+100.0f << "> look_at <" << cameraLook[0] << "," << cameraLook[1] << "," << cameraLook[2] <<"> ";
// 	if(highlightAgent) {
// 	  out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << "> " << std::endl;
// 	  out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> }" << std::endl;
// 	}
	out << "}" << std::endl;
	
	//set a "infinite" plane 
// 	out << "plane { y, -1 pigment { color rgb 1 } }" << std::endl;
	//set a sky sphere
	out << "sphere { <0, 0, 0>, 5000";
	out << "texture{ pigment { color rgb <1, 1, 1>}	finish { ambient 1 diffuse 0 } } }" << std::endl;
	//put some lights
	out << "light_source { <" << cameraPos[0] << "," << cameraPos[1]+200 << "," << cameraPos[2] << "> color rgb 0.45}" << std::endl;
// 	out << "light_source { <-800, 800, -800> color rgb 0.25 }"<< std::endl;
	//set a high quality rendering
	out << "global_settings {" << std::endl;
	out << "radiosity {" << std::endl;
	out << "pretrace_start 0.08 pretrace_end 0.04" << std::endl;
	out << "count 500 nearest_count 10 error_bound 0.15 recursion_limit 1 low_error_factor 0.2 gray_threshold 0.0 minimum_reuse 0.015 brightness 1 adc_bailout 0.01/2 normal off media off} max_trace_level 255}" << std::endl;

	for(unsigned int i = 0; i< sim.agents_.size() ; ++i) {
		out << "cylinder {" << std::endl;
			Geom::Plane3D<PFP::REAL> pl = Algo::Geometry::trianglePlane<PFP>(sim.envMap_.map,sim.agents_[i]->part_.d,sim.envMap_.position);
			VEC3 pBase(sim.agents_[i]->position_);
			VEC3 posR;
			pl.normal() = -1.0f*pl.normal();
			VEC3 dir(0.0f,0.0f,-1.0f);
			pBase[2]=100000.0f;
			Geom::intersectPlaneRay<VEC3,Geom::Plane3D<PFP::REAL> >(pl,pBase,dir,posR);
// 			VEC3 posR(sim.agents_[i]->position_[0],sim.agents_[i]->position_[1],0);
			out << "<" << posR[0] << "," << posR[2]+0.05 << "," << posR[1] << ">, " << std::endl;
			out << "<" << posR[0] << "," << posR[2]+(sim.agents_[i]->radius_*5.0f) << "," << posR[1] << ">, " << std::endl;
			out << sim.agents_[i]->radius_ << std::endl;
			
			if(highlightAgent) {
			  if(i==highlightAgentNo) {
			    out << "texture{ pigment{ color rgb<0.5,1.0,0.5>} finish { ambient rgb 0.1 brilliance 0.5 } }" << std::endl;
			  }
			  else {
			    Agent * ag = sim.agents_[highlightAgentNo];
			    bool found = false;
			    for(std::vector<std::pair<float, size_t> >::iterator it = ag->agentNeighbors_.begin();!found && it != ag->agentNeighbors_.end();++it) {
				if((*it).second==i)
				    found=true;
			    }
			    if(found) {
// 				std::cout << "trouve voisin" << std::endl;
				out << "texture{ pigment{ color rgb<0.5.0,0.5,0.0>} finish { ambient rgb 0.1 brilliance 0.5 } }" << std::endl;
			    }
			    else {
				  out << "texture{ pigment{ color rgb<0.5,0.5.0,0.5>} finish { ambient rgb 0.1 brilliance 0.5 } }" << std::endl;
			    }
			  }
			}
			else {
				if(i<(sim.agents_.size()/2))
					out << "texture{ pigment{ color rgb<0.5,1.0,0.5>} finish { ambient rgb 0.1 brilliance 0.5 } }" << std::endl;
				else
					out << "texture{ pigment{ color rgb<0.5,0.5,1.0>} finish { ambient rgb 0.1 brilliance 0.5 } }" << std::endl;

			}
			
 			out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << ">" << std::endl;
 			out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl;
		out << "}" << std::endl;
	}

	if(highlightAgent) {
	  DartMarkerStore highlight(map);
	  Agent * ag = sim.agents_[highlightAgentNo];
	  for(Dart d=map.begin();d!=map.end();map.next(d)) {
	    if(!highlight.isMarked(d)) {
	      PFP::AGENTS listAgentInCell  = sim.envMap_.agentvect[d];
	      if(std::find(listAgentInCell.begin(), listAgentInCell.end(), ag)!=listAgentInCell.end()) {
		highlight.markOrbit(FACE_ORBIT,d);
	      }
	    }
	  }
	  
	  SelectorMarked sm(highlight);
	  SelectorUnmarked sum(highlight);
	  Algo::ExportPov::exportMeshPlain<PFP>(out,map,position,"facesHighlighted",sm);
	  Algo::ExportPov::exportMeshPlain<PFP>(out,map,position,"myMesh",sum);

	  out << "object {facesHighlighted" << std::endl;
	  out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << ">" << std::endl;
	  out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl;
	  out << "texture{ pigment{ color rgb<0.4667,0.709,0.996>} finish { ambient rgb 0.35 brilliance 0.5 } } }" << std::endl;
	}
	else {
	  Algo::ExportPov::exportMeshPlain<PFP>(out,map,position,"myMesh",good);
	}

	if(wireDisplay) {
		Algo::ExportPov::exportMeshWire<PFP>(out,map,position,"myMeshWire",good);
		out << "object {myMeshWire" << std::endl;
		out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << ">" << std::endl;
		out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl;
		out << "texture{ pigment{ color rgb<0,0,0>} } }" << std::endl;
	}
	
	out << "object {myMesh" << std::endl;
 	out << "translate <" << translate[0] << "," << translate[1] << "," << translate[2] << ">" << std::endl;
 	out << "rotate <" << angleX << "," << angleY << "," << angleZ << "> " << std::endl;
 	out << "texture{ pigment{ color rgbf<1.0,1.0,1,"<< 1.0f*(float(nbGenerated)/400.0f) <<">} finish { ambient rgb 0.3 brilliance 0.5 } } }" << std::endl;
//  	out << "texture{ pigment{ color rgb<1.0,1.0,1>} finish { ambient rgb 0.05 brilliance 0.5 } } }" << std::endl;

	out.close();
// 	for(Dart d = sim.envMap.map.begin() ; d != sim.envMap.map.end() ; sim.envMap.map.next(d)) {
// 		if(sim.envMap.closeMark.isMarked(d) && !sim.envMap.closeMark.isMarked(sim.envMap.map.phi1(d))) {
// 			std::cout << "probleme" << std::endl;
// 		}
// 	}
	return true;
}
*/
void MyGlutWin::myKeyboard(unsigned char keycode, int x, int y)
{
	switch (keycode)
	{
		case 'a': {
			sim.envMap_.map.check();
			b_animate = !b_animate;
			break;
		}
		case 'c': {
			sim.envMap_.map.check();
			break;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
383
384
385
386
		case 'e' : {
//			std::cout << "exporting obstacle to file myScene.obst" << std::endl;
//			std::string filename("myScene.obst");
//			CGoGN::ExportScene::exportSceneToFile<PFP>(sim.envMap_.map,sim.envMap_.position,sim.envMap_.closeMark,filename);
Pierre Kraemer's avatar
Pierre Kraemer committed
387
388
			std::string filename2("myAgents.pos");
			sim.exportAgents(filename2);
Pierre Kraemer's avatar
Pierre Kraemer committed
389
			break;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		}
		case 'f': {
			displayFps = ! displayFps;
			break;
		}
		case 'r' : {
// 			exportScenePov(sim.envMap_.map,sim.envMap_.position,"exportScene0002.pov",VEC3(1500,500,1500),VEC3(0,0,0),VEC3(1.0f,0,0),0,0,0);
//			exportScenePov(sim.envMap_.map,sim.envMap_.position,"exportScene0002.pov",VEC3(43,762,1565),VEC3(0,762,0),VEC3(1.0f,0,0),0,0,0);
// 			exportScenePov(sim.envMap_.map,sim.envMap_.position,"exportScene0002.pov",VEC3(43,762,65),VEC3(0,762,0),VEC3(1.0f,0,0),0,0,0);
			break;
		}
		case 's': {
			animate();
			break;
		}
		case 'v': {
			++visu;
			visu = visu % 4;
			break;
		}
		case 'x': {
			explode *= 0.9f;
			break;
		}
		case '9': {
			render_anim = !render_anim;
			break;
		}
	}

	glutPostRedisplay();
}

/**********************************************************************************************
 *                                      MAIN FUNCTION                                         *
 **********************************************************************************************/

int main(int argc, char** argv)
{
	MyGlutWin* mgw = new MyGlutWin(&argc, argv, 1200, 800);

	mgw->init();

	if(argc == 2)
	{
		GLint t1 = glutGet(GLUT_ELAPSED_TIME);
		for(unsigned int i = 0; i < 300; ++i)
			mgw->animate();
		GLint t2 = glutGet(GLUT_ELAPSED_TIME);
		GLfloat seconds = (t2 - t1) / 1000.0f;
		std::cout << "animation : "<< seconds << "sec" << std::endl;
	}
	else
		mgw->mainLoop() ;
	
	return 0 ;
}