Debian Codegraph @

src/graphicmanager.cpp

raw symbols SCIP
1 /***************************************************************************
2 graphicmanager.cpp - description
3 -------------------
4 begin : september 21th, 2003
5 copyright : (C) 2003-2008 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: graphicmanager.cpp 450 2010-11-21 19:11:43Z neoneurone $
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * any later version. *
17 * *
18 ***************************************************************************/
19
20 // Useful enumerations
21 #include "opencity_direction.h"
22 #include "opencity_structure_type.h"
23
24 // OpenCity headers
25 #include "graphicmanager.h"
26 #include "structure.h"
27 #include "model.h"
28 #include "modelloader.h"
29 #include "movement.h"
30 #include "agent.h"
31 #include "conf.h" // Parser for .conf file
32
33 // Global settings
34 #include "globalvar.h"
35 extern GlobalVar gVars;
36
37 // Standard headers
38 #include <cmath> // For floorf
39 #include <sstream> // We use std::stringstream for data conversion
40
41
42 static GLfloat red, green, blue;
43
44
45 /*======================================================================*/
46 GraphicManager::GraphicManager():
47 _uiDisplayListMask( OC_OPAQUE_ONESIDE_LIST | OC_OPAQUE_TWOSIDE_LIST | OC_ALPHA_LIST )
48 {
49 OPENCITY_DEBUG( "GraphicManager ctor" );
50
51 // Initialize the model table
52 int i;
53 for (i = 0; i < OC_GRAPHIC_CODE_MAX; i++ ) {
54 _tabpModel[i] = NULL;
55 }
56
57 // Try to open the graphism config file
58 Conf* pConf = new Conf();
59 if (pConf->Open( ocConfigDirPrefix(OC_GRAPHISM_FILE_FILENAME) ) != OC_ERR_FREE) {
60 OPENCITY_FATAL( "Failed to open the graphism config file" );
61 delete pConf;
62 abort();
63 }
64
65 // Load all the specified models from the config file
66 string strPath;
67 std::stringstream ss;
68 for (i = 0; i < OC_GRAPHIC_CODE_MAX; i++ ) {
69 ss.str("");
70 ss << i;
71 strPath = pConf->GetValue( ss.str() );
72 if (strPath == "")
73 continue;
74
75 _tabpModel[i] = ModelLoader::Load( ocDataDirPrefix(strPath) );
76 if (_tabpModel[i] == NULL) {
77 OPENCITY_FATAL( "Failed to load the file: " << ocDataDirPrefix(strPath) );
78 abort();
79 }
80 } // for
81
82 delete pConf;
83 }
84
85
86 /*======================================================================*/
87 GraphicManager::~GraphicManager()
88 {
89 OPENCITY_DEBUG( "GraphicManager dtor" );
90
91 for (int i = 0; i < OC_GRAPHIC_CODE_MAX; i++ ) {
92 delete _tabpModel[i]; // delete NULL won't hurt
93 _tabpModel[i] = NULL; // safe
94 }
95 }
96
97
98 /*======================================================================*/
99 const uint
100 GraphicManager::GetSelection(
101 const uint & rcuiWinX,
102 const uint & rcuiWinY ) const
103 {
104 static GLubyte rgbTab[3];
105 static GLshort uiValue = 0;
106 static GLfloat fValue = .0;
107
108 //---- read the back buffer, double-buffering must be supported !
109 // glReadBuffer( GL_BACK );
110 // 32 bits
111 glReadPixels( rcuiWinX, rcuiWinY, 1, 1, GL_RGB, GL_FLOAT, &fValue );
112 glReadPixels( rcuiWinX, rcuiWinY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, rgbTab );
113 glReadPixels( rcuiWinX, rcuiWinY, 1, 1, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, &uiValue );
114
115 //debug begins
116 cout << "WinX: " << rcuiWinX << "/ WinY: " << rcuiWinY << endl;
117 cout << "RGB got: " << (int)rgbTab[0] << "/" << (int)rgbTab[1] << "/" << (int)rgbTab[2]
118 << "/ Float code: " << fValue
119 << "/ Uint code: " << uiValue;
120 if (glGetError() != GL_NO_ERROR) {
121 cout << "OMG: bug !" << endl;
122 }
123
124 //debug ends
125
126 // 32 bpp
127 return ((rgbTab[0] << 16) + (rgbTab[1] << 8) + rgbTab[2]);
128 // 16 bpp
129 // return ((rgbTab[0] << 11) + (rgbTab[1] << 5) + rgbTab[2]);
130 // return uiValue;
131 }
132
133
134 /*======================================================================*/
135 void
136 GraphicManager::DisplayTerrain(
137 const uint & rcuiW,
138 const uint & rcuiL ) const
139 {
140 static signed char tabH [4];
141 static GLfloat ax, ay, az;
142 static GLfloat bx, by, bz;
143 static GLfloat nx, ny, nz;
144
145 gVars.gpMapMgr->GetSquareHeight( rcuiW, rcuiL, tabH );
146
147 //WARNING: GL_COLOR_MATERIAL is enabled
148 glColor3f( .25, .2, .15 );
149
150 // do we need to calculate the new normal ?
151 if ((tabH[0] == tabH[1]) && (tabH[1] == tabH[2]) && (tabH[2] == tabH[3])) {
152 glVertex3i( rcuiW, tabH[0], rcuiL );
153 glVertex3i( rcuiW, tabH[1], rcuiL+1 );
154 glVertex3i( rcuiW+1, tabH[2], rcuiL+1 );
155 glVertex3i( rcuiW+1, tabH[3], rcuiL );
156 }
157 else {
158 // calculate the new normal (the cross product)
159 ax = .0; ay = (GLfloat)tabH[2]; az = 1.;
160 bx = 1.; by = (GLfloat)tabH[1]; bz = .0;
161
162 nx = by;
163 ny = 1.;
164 nz = ay;
165 glNormal3f( nx, ny, nz );
166 /*
167 cx = ay * bz - by * az;
168 cy = bx * az - ax * bz;
169 cz = ax * by - bx * ay;
170 */
171
172 // draw the stuff
173 glVertex3i( rcuiW, tabH[0], rcuiL );
174 glVertex3i( rcuiW, tabH[1], rcuiL+1 );
175 glVertex3i( rcuiW+1, tabH[2], rcuiL+1 );
176 glVertex3i( rcuiW+1, tabH[3], rcuiL );
177
178 // then restore the default normal
179 glNormal3f( 0., 0., 1. );
180 }
181 }
182
183
184 /*======================================================================*/
185 void
186 GraphicManager::DisplayStructure
187 (
188 const Structure* pcStructure,
189 const uint & rcuiW,
190 const uint & rcuiL
191 ) const
192 {
193 static OPENCITY_GRAPHIC_CODE enumGC;
194 static signed char tabH [4];
195 static GLfloat delta;
196
197
198 assert( pcStructure != NULL );
199
200 // Get the heights from the map manager
201 gVars.gpMapMgr->GetSquareHeight( rcuiW, rcuiL, tabH );
202
203 // get the level of the structure
204 delta = (GLfloat)(pcStructure->GetLevel() + 1) / 20;
205
206 // Get the graphic code
207 enumGC = pcStructure->GetGraphicCode();
208 assert( (enumGC == OC_EMPTY) || (_tabpModel[enumGC] != NULL) );
209
210 // The "switch" instruction will be removed once we have enough models
211 switch (enumGC) {
212 case OC_RES_ZONE0:
213 case OC_RES_ZONE1:
214 case OC_RES_ZONE2:
215 case OC_RES_ZONE3:
216 case OC_RES_ZONE4:
217 case OC_RES_ZONE5:
218 case OC_RES_ZONE6:
219 case OC_RES_ZONE7:
220 case OC_RES_ZONE8:
221 case OC_RES_ZONE9:
222
223 case OC_COM_ZONE0:
224 case OC_COM_ZONE1:
225 case OC_COM_ZONE2:
226 case OC_COM_ZONE3:
227 case OC_COM_ZONE4:
228 case OC_COM_ZONE5:
229 case OC_COM_ZONE6:
230 case OC_COM_ZONE7:
231 case OC_COM_ZONE8:
232 case OC_COM_ZONE9:
233
234 case OC_IND_ZONE0:
235 case OC_IND_ZONE1:
236 case OC_IND_ZONE2:
237 case OC_IND_ZONE3:
238 case OC_IND_ZONE4:
239 case OC_IND_ZONE5:
240 case OC_IND_ZONE6:
241 case OC_IND_ZONE7:
242 case OC_IND_ZONE8:
243 case OC_IND_ZONE9:
244
245 case OC_PARK0:
246 case OC_TREE_FIR:
247 case OC_TREE_007:
248 case OC_TREE_PEKINGWILLOW:
249 case OC_TREE_PINE1:
250 case OC_TREE_PINE2:
251 _tabpModel[enumGC]->DisplayList( rcuiW, rcuiL, tabH, _uiDisplayListMask );
252 break;
253
254 // Road part
255 case OC_ROAD_O_N:
256 case OC_ROAD_O_E:
257 case OC_ROAD_O_S:
258 case OC_ROAD_O_W:
259 case OC_ROAD_S_N:
260 case OC_ROAD_W_E:
261 case OC_ROAD_N_E:
262 case OC_ROAD_N_W:
263 case OC_ROAD_S_E:
264 case OC_ROAD_S_W:
265 case OC_ROAD_S_N_E:
266 case OC_ROAD_S_W_E:
267 case OC_ROAD_S_N_W:
268 case OC_ROAD_N_W_E:
269 case OC_ROAD_S_N_W_E:
270 _tabpModel[enumGC]->DisplayList( rcuiW, rcuiL, tabH, _uiDisplayListMask );
271 break;
272
273 // Electric lines part
274 case OC_ELINE_O_N:
275 case OC_ELINE_O_E:
276 case OC_ELINE_O_S:
277 case OC_ELINE_O_W:
278 case OC_ELINE_S_N:
279 case OC_ELINE_W_E:
280 case OC_ELINE_N_E:
281 case OC_ELINE_N_W:
282 case OC_ELINE_S_E:
283 case OC_ELINE_S_W:
284 case OC_ELINE_S_N_E:
285 case OC_ELINE_S_W_E:
286 case OC_ELINE_S_N_W:
287 case OC_ELINE_N_W_E:
288 case OC_ELINE_S_N_W_E:
289 _tabpModel[enumGC]->DisplayList( rcuiW, rcuiL, tabH, _uiDisplayListMask );
290 break;
291
292 case OC_EPLANT_COAL:
293 case OC_EPLANT_NUCLEAR:
294 case OC_FIRE_DEPT:
295 case OC_POLICE_DEPT:
296 case OC_HOSPITAL_DEPT:
297 case OC_EDUCATION_DEPT:
298 _tabpModel[enumGC]->DisplayList( rcuiW, rcuiL, tabH, _uiDisplayListMask );
299 break;
300
301 case OC_EMPTY:
302 //debug cout << "empty" << endl;
303 break;
304
305 // Test codes used by graphists
306 case OC_TEST_BUILDING:
307 if (_tabpModel[OC_TEST_BUILDING] != NULL) {
308 _tabpModel[OC_TEST_BUILDING]->DisplayList( rcuiW, rcuiL, tabH, _uiDisplayListMask );
309 }
310 break;
311
312 default:
313 red = .0; green = .0; blue = .0;
314 // Display_draw_polygon:
315 glBegin( GL_QUADS );
316 glColor3f( red, green, blue );
317 glVertex3i( rcuiW, tabH[0], rcuiL );
318 glVertex3i( rcuiW , tabH[1], (rcuiL+1) );
319 glVertex3i( rcuiW+1 , tabH[2], (rcuiL+1) );
320 glVertex3i( rcuiW+1 , tabH[3], rcuiL );
321 glEnd();
322 break;
323 }
324 }
325
326
327 /*======================================================================*/
328 void
329 GraphicManager::DisplayGC(
330 const OPENCITY_GRAPHIC_CODE & enumGC,
331 const uint & rcuiW,
332 const uint & rcuiL ) const
333 {
334 static signed char tabH [4];
335
336 // Get the heights from the map manager
337 gVars.gpMapMgr->GetSquareHeight( rcuiW, rcuiL, tabH );
338
339 assert( (enumGC == OC_EMPTY) || (_tabpModel[enumGC] != NULL) );
340
341 _tabpModel[enumGC]->DisplayList( rcuiW, rcuiL, tabH );
342 }
343
344
345 /*======================================================================*/
346 void
347 GraphicManager::DisplayTerrainHighlight(
348 const uint & rcuiW,
349 const uint & rcuiL,
350 const OPENCITY_TOOL_CODE & enumTool ) const
351 {
352 signed char tabH[4];
353
354 // warning: see warning in DisplayTerrain
355 gVars.gpMapMgr->GetSquareHeight( rcuiW, rcuiL, tabH );
356
357 // compare the tool code
358 switch (enumTool) {
359 case OC_TOOL_ZONE_RES:
360 red = .2; green = .5; blue = .2;
361 break;
362
363 case OC_TOOL_ZONE_COM:
364 red = .2; green = .2; blue = .5;
365 break;
366
367 case OC_TOOL_ZONE_IND:
368 red = .5; green = .5; blue = .2;
369 break;
370
371 case OC_TOOL_ROAD:
372 red = .5; green = .65; blue = .5;
373 break;
374
375 case OC_TOOL_ELINE:
376 red = .55; green = .55; blue = .45;
377 break;
378
379 default:
380 red = .1; green = .1; blue = .1;
381 }
382
383
384 glBegin( GL_QUADS );
385 glColor3f( red, green, blue );
386 glVertex3f( .1 + rcuiW, tabH[0], 0.1 + rcuiL);
387 glVertex3f( .1 + rcuiW, tabH[1], 0.9 + rcuiL );
388 glVertex3f( .9 + rcuiW, tabH[2], 0.9 + rcuiL );
389 glVertex3f( .9 + rcuiW, tabH[3], 0.1 + rcuiL);
390 glEnd();
391 }
392
393
394 /*======================================================================*/
395 void
396 GraphicManager::DisplayStructureHighlight(
397 const Structure* pcStructure,
398 const uint & rcuiW,
399 const uint & rcuiL,
400 const OPENCITY_TOOL_CODE & enumTool ) const
401 {
402 //TODO: check the pcStructure graphic code
403 DisplayTerrainHighlight(
404 rcuiW,
405 rcuiL,
406 enumTool );
407 }
408
409
410 /*======================================================================*/
411 void
412 GraphicManager::DisplayTerrainSelection
413 (
414 const uint & rcuiW,
415 const uint & rcuiL,
416 const uint & rcuiID
417 ) const
418 {
419 static signed char tabH [4];
420
421 // Warning: we draw the polygon counter-clock wise
422 // however, the polygon OY heights are
423 // stored as the left,right heights of the first line
424 // then left, right heights of the second line
425 gVars.gpMapMgr->GetSquareHeight( rcuiW, rcuiL, tabH );
426
427 glLoadName( rcuiID );
428 glBegin( GL_QUADS );
429 glVertex3i( rcuiW, tabH[0], rcuiL);
430 glVertex3i( rcuiW, tabH[1], rcuiL+1 );
431 glVertex3i( rcuiW+1, tabH[2], rcuiL+1 );
432 glVertex3i( rcuiW+1, tabH[3], rcuiL);
433 glEnd();
434 }
435
436
437 /*======================================================================*/
438 void
439 GraphicManager::DisplayStructureSelection
440 (
441 const Structure* pcStructure,
442 const uint & rcuiW,
443 const uint & rcuiL,
444 const uint & rcuiID
445 ) const
446 {
447 glLoadName( rcuiID );
448
449 this->DisplayStructure( pcStructure, rcuiW, rcuiL );
450 }
451
452
453 /*======================================================================*/
454 void
455 GraphicManager::Display
456 (
457 const OC_FLOAT & rcfW,
458 const OC_FLOAT & rcfL,
459 const OC_FLOAT & rcfH,
460 const Movement* const pm
461 ) const
462 {
463 assert( pm != NULL );
464 // static OC_BYTE tabH [4];
465
466 // warning: see warning in DisplayTerrain
467 // gVars.gpMapMgr->GetSquareHeight( (uint)floorf(rcfW), (uint)floorf(rcfH), tabH );
468
469 glPushMatrix();
470 // Rotation translation compensation
471 // The 0.05 value is the display translation vector of the other structures
472 glTranslatef( rcfW+pm->_fTX, rcfH+pm->_fTY+0.05, rcfL+pm->_fTZ );
473 glRotatef( pm->_fRY, 0, 1, 0 ); // Model rotation
474 glRotatef( pm->_fRX, 1, 0, 0 ); // Slope
475 glRotatef( pm->_fRZ, 0, 0, 1 );
476 _tabpModel[pm->GetGraphicCode()]->DisplayList();
477 glPopMatrix();
478 }
479
480
481 /*======================================================================*/
482 void
483 GraphicManager::SetLOD( const uint lod )
484 {
485 switch (lod) {
486 case OC_LOD_HIGH:
487 _uiDisplayListMask = OC_OPAQUE_ONESIDE_LIST | OC_OPAQUE_TWOSIDE_LIST | OC_ALPHA_LIST;
488 break;
489 case OC_LOD_MEDIUM:
490 _uiDisplayListMask = OC_OPAQUE_ONESIDE_LIST | OC_OPAQUE_TWOSIDE_LIST;
491 break;
492 case OC_LOD_LOW:
493 _uiDisplayListMask = OC_OPAQUE_ONESIDE_LIST;
494 break;
495 default:
496 break;
497 }
498 }
499
500 /*======================================================================*/
501 uint
502 GraphicManager::GetListMask() const
503 {
504 return _uiDisplayListMask;
505 }
506
507
508 /*======================================================================*/
509 void
510 GraphicManager::SetListMask( const uint mask )
511 {
512 _uiDisplayListMask = mask;
513 }
514
515
516 /*======================================================================*/
517
518 /* WARNING: this is a special function used for agent rendering */
519 void
520 GraphicManager::DisplayAgent(float x, float y, const Agent* const pAgent) const
521 {
522 static signed char tabH[4];
523
524 assert(pAgent != NULL);
525
526 // warning: see warning in DisplayTerrain
527 gVars.gpMapMgr->GetSquareHeight( (uint)floorf(x), (uint)floorf(y), tabH );
528 _tabpModel[pAgent->GetGraphicCode()]->DisplayList( x, y, tabH );
529 }
530
531
532
533
534
535
536
537
538
539
540
541
542