#include <Display.h>
Public Member Functions | |
virtual | ~Display () |
Empty Display destructor. | |
void | init () |
Initialize the OpenGL parameters for 3D rendering. | |
void | quit () |
Actions to do before exiting the program. | |
void | timer (int value) |
The timer fonction that handle periodic iteration and redisplay. | |
void | draw () |
Render the scene in OpenGL. | |
void | reshape (int width, int height) |
Recompute projection matrix after reshaping the window. | |
void | inputKey (unsigned char key, int x, int y) |
Associate treatments to the keyboard keys. | |
void | inputMouse (int button, int state, int x, int y) |
Associate treatments to the mouse. | |
Static Public Member Functions | |
static Display * | instance () |
Return an instance of Display (use of singleton design pattern). | |
static void | destroy () |
Destroy the instance of Display. | |
Protected Member Functions | |
Display () | |
Empty Display constructor. | |
GLfloat | focal_distance () |
Compute the focal distance (e.g. the distance to the object we are looking at). | |
void | translate_camera (GLfloat inc_x, GLfloat inc_z) |
Translate the camera. | |
void | move_camera (GLfloat inc_y) |
Move the camera up and down. | |
void | zoom_camera (GLfloat inc) |
Zoom or un-zoom to the center of the screen. | |
void | rotate_camera (GLfloat inc) |
Rotate the camera around the center of the screen point. | |
Protected Attributes | |
GLfloat | _camera_x |
GLfloat | _camera_y |
GLfloat | _camera_z |
GLfloat | _center_x |
GLfloat | _center_y |
GLfloat | _center_z |
GLfloat | _near |
GLfloat | _far |
GLfloat | _fovy |
World | _world |
bool | _timer |
Static Protected Attributes | |
static Display * | _instance = NULL |
Definition at line 35 of file Display.h.
GLfloat Display::focal_distance | ( | ) | [protected] |
Compute the focal distance (e.g. the distance to the object we are looking at).
Definition at line 68 of file Display.cpp.
References _camera_x, _camera_y, _center_x, and _center_y.
Referenced by zoom_camera().
00068 { 00069 return sqrt((_camera_x - _center_x) * (_camera_x - _center_x) + 00070 (_camera_y - _center_y) * (_camera_y - _center_y) + 00071 (_camera_y - _center_y) * (_camera_y - _center_y)); 00072 }
void Display::translate_camera | ( | GLfloat | inc_x, | |
GLfloat | inc_z | |||
) | [protected] |
Translate the camera.
inc_x | the relative movement of the camera in the x direction | |
inc_z | the relative movement of the camera in the z direction |
Definition at line 74 of file Display.cpp.
References _camera_x, _camera_z, _center_x, _center_z, _world, CELL_SIZE, World::getHeight(), and World::getWidth().
Referenced by inputKey().
00074 { 00075 GLfloat tmp_x = _center_x + inc_x; 00076 GLfloat tmp_z = _center_z + inc_z; 00077 00078 if(tmp_x > 0.0 && tmp_x < (_world.getWidth() - 1.0) * CELL_SIZE && 00079 tmp_z > 0.0 && tmp_z < (_world.getHeight() - 1.0) * CELL_SIZE) { 00080 _camera_x += inc_x; 00081 _center_x = tmp_x; 00082 _camera_z += inc_z; 00083 _center_z = tmp_z; 00084 glutPostRedisplay(); 00085 } 00086 }
void Display::move_camera | ( | GLfloat | inc_y | ) | [protected] |
Move the camera up and down.
inc_y | the relative movement of the camera in the y direction |
Definition at line 88 of file Display.cpp.
References _camera_y, HEIGHT_MAX, and HEIGHT_MIN.
Referenced by inputKey(), and inputMouse().
00088 { 00089 GLfloat tmp_y = _camera_y + inc_y; 00090 00091 if(tmp_y >= HEIGHT_MIN && tmp_y <= HEIGHT_MAX) { 00092 _camera_y += inc_y; 00093 glutPostRedisplay(); 00094 } 00095 }
void Display::zoom_camera | ( | GLfloat | inc | ) | [protected] |
Zoom or un-zoom to the center of the screen.
inc | the relative movement of the camera in the center of the screen direction |
Definition at line 97 of file Display.cpp.
References _camera_x, _camera_y, _camera_z, _center_x, _center_y, _center_z, focal_distance(), HEIGHT_MAX, and HEIGHT_MIN.
Referenced by inputKey(), and inputMouse().
00097 { 00098 GLfloat tmp = focal_distance(); 00099 00100 if(_camera_y + inc >= HEIGHT_MIN && _camera_y + inc <= HEIGHT_MAX) { 00101 _camera_x = ((_camera_x - _center_x) * (1 + inc / tmp)) + _center_x; 00102 _camera_y = ((_camera_y - _center_y) * (1 + inc / tmp)) + _center_y; 00103 _camera_z = ((_camera_z - _center_z) * (1 + inc / tmp)) + _center_z; 00104 glutPostRedisplay(); 00105 } 00106 }
void Display::rotate_camera | ( | GLfloat | inc | ) | [protected] |
Rotate the camera around the center of the screen point.
inc | the angle of the rotation to do |
Definition at line 108 of file Display.cpp.
References _camera_x, _camera_z, _center_x, and _center_z.
Referenced by inputKey(), and inputMouse().
00108 { 00109 GLfloat tmp = _camera_x; 00110 _camera_x = ((_camera_x - _center_x) * cos(inc) - 00111 (_camera_z - _center_z) * sin(inc)) + _center_x; 00112 _camera_z = ((tmp - _center_x) * sin(inc) + 00113 (_camera_z - _center_z) * cos(inc)) + _center_z; 00114 glutPostRedisplay(); 00115 }
Display * Display::instance | ( | ) | [static] |
Return an instance of Display (use of singleton design pattern).
Definition at line 121 of file Display.cpp.
References _instance, and Display().
Referenced by display_draw(), display_init(), display_inputKey(), display_inputMouse(), display_quit(), display_reshape(), and display_timer().
00121 { 00122 if(_instance == NULL) { 00123 _instance = new Display(); 00124 } 00125 00126 return _instance; 00127 }
void Display::timer | ( | int | value | ) |
The timer fonction that handle periodic iteration and redisplay.
value | (unused) |
Definition at line 188 of file Display.cpp.
References _timer, _world, display_timer(), World::iteration(), and REDISPLAY_NB_MILLI_SEC.
Referenced by display_timer().
00188 { 00189 _world.iteration(); 00190 glutPostRedisplay(); 00191 if(_timer) { 00192 // on relance le timer pour ré-appeler automatiquement la fonction plus tard 00193 glutTimerFunc(REDISPLAY_NB_MILLI_SEC, display_timer, 0); 00194 } 00195 }
void Display::reshape | ( | int | width, | |
int | height | |||
) |
Recompute projection matrix after reshaping the window.
width | the new width of the window | |
height | the new height of the window |
Definition at line 254 of file Display.cpp.
References _far, _fovy, and _near.
Referenced by display_reshape().
00254 { 00255 // division par 0 : pas bô, alors autant l'éviter... 00256 if (height == 0) 00257 height = 1; 00258 00259 // modification de la taille de la zone d'affichage 00260 glViewport(0, 0, width, height); 00261 00262 // passage en mode projection 00263 glMatrixMode(GL_PROJECTION); 00264 00265 // création de la nouvelle transformation de projection 00266 glLoadIdentity(); 00267 gluPerspective(_fovy, (float) width / height, _near, _far); 00268 00269 // réactivation du mode dessin 00270 glMatrixMode(GL_MODELVIEW); 00271 }
void Display::inputKey | ( | unsigned char | key, | |
int | x, | |||
int | y | |||
) |
Associate treatments to the keyboard keys.
key | the key pressed | |
x | the x mouse coordinate | |
y | the y mouse coordinate |
Definition at line 273 of file Display.cpp.
References _center_x, _center_z, _timer, _world, World::addGodsGift(), CELL_SIZE, display_timer(), move_camera(), MOVE_UP_SPEED, PAUSE, REDISPLAY_NB_MILLI_SEC, ROTATE_ANGLE, rotate_camera(), ROTATE_LEFT, ROTATE_RIGHT, translate_camera(), TRANSLATE_DOWN, TRANSLATE_LEFT, TRANSLATE_RIGHT, TRANSLATE_UP, VALIDATE, zoom_camera(), ZOOM_IN, ZOOM_OUT, and ZOOM_SPEED.
Referenced by display_inputKey().
00273 { 00274 GLint specialKey = glutGetModifiers(); 00275 00276 switch(key) { 00277 case TRANSLATE_LEFT : 00278 translate_camera(-CELL_SIZE, 0.0); 00279 break; 00280 case TRANSLATE_RIGHT : 00281 translate_camera(CELL_SIZE, 0.0);; 00282 break; 00283 case TRANSLATE_UP : 00284 translate_camera(0.0, -CELL_SIZE); 00285 break; 00286 case TRANSLATE_DOWN : 00287 translate_camera(0.0, CELL_SIZE); 00288 break; 00289 case ZOOM_IN : 00290 if(specialKey == GLUT_ACTIVE_CTRL) { 00291 move_camera(MOVE_UP_SPEED); 00292 } else { 00293 zoom_camera(-ZOOM_SPEED); 00294 } 00295 break; 00296 case ZOOM_OUT : 00297 if(specialKey == GLUT_ACTIVE_CTRL) { 00298 move_camera(-MOVE_UP_SPEED); 00299 } else { 00300 zoom_camera(ZOOM_SPEED); 00301 } 00302 break; 00303 case ROTATE_LEFT : 00304 rotate_camera(-ROTATE_ANGLE); 00305 break; 00306 case ROTATE_RIGHT : 00307 rotate_camera(ROTATE_ANGLE); 00308 break; 00309 case PAUSE : 00310 if(_timer) { 00311 _timer = false; 00312 } else { 00313 _timer = true; 00314 // relance le timer 00315 glutTimerFunc(REDISPLAY_NB_MILLI_SEC, display_timer, 0); 00316 } 00317 break; 00318 case VALIDATE : 00319 _world.addGodsGift((unsigned int) _center_z / CELL_SIZE, 00320 (unsigned int) _center_x / CELL_SIZE); 00321 glutPostRedisplay(); 00322 break; 00323 } 00324 }
void Display::inputMouse | ( | int | button, | |
int | state, | |||
int | x, | |||
int | y | |||
) |
Associate treatments to the mouse.
button | the button pressed | |
state | the state of the button (up or down) | |
x | the x mouse coordinate | |
y | the y mouse coordinate |
Definition at line 326 of file Display.cpp.
References _world, GLUT_DOWN_BUTTON, GLUT_UP_BUTTON, World::iteration(), move_camera(), MOVE_UP_SPEED, ROTATE_ANGLE, rotate_camera(), zoom_camera(), and ZOOM_SPEED.
Referenced by display_inputMouse().
00326 { 00327 GLint specialKey = glutGetModifiers(); 00328 00329 /* 00330 GLint viewport[4]; 00331 GLdouble modelview[16]; 00332 GLdouble projection[16]; 00333 GLfloat win_x, win_y, win_z; 00334 GLdouble obj_x, obj_y, obj_z; 00335 00336 glGetIntegerv(GL_VIEWPORT, viewport); 00337 glGetDoublev(GL_MODELVIEW_MATRIX, modelview); 00338 glGetDoublev(GL_PROJECTION_MATRIX, projection); 00339 00340 win_x = (GLfloat) x; 00341 win_y = (GLfloat) viewport[3] - (GLfloat) y; 00342 glReadPixels(win_x, win_y , 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &win_z); 00343 00344 gluUnProject((GLdouble) win_x, (GLdouble) win_y, 0.0, modelview, projection, 00345 viewport, &obj_x, &obj_y, &obj_z); 00346 */ 00347 00348 if(state == GLUT_DOWN) { 00349 switch(button) { 00350 case GLUT_UP_BUTTON : 00351 if(specialKey == GLUT_ACTIVE_CTRL) { 00352 move_camera(MOVE_UP_SPEED); 00353 } else { 00354 zoom_camera(-ZOOM_SPEED); 00355 } 00356 break; 00357 case GLUT_DOWN_BUTTON : 00358 if(specialKey == GLUT_ACTIVE_CTRL) { 00359 move_camera(-MOVE_UP_SPEED); 00360 } else { 00361 zoom_camera(ZOOM_SPEED); 00362 } 00363 break; 00364 case GLUT_LEFT_BUTTON : 00365 if(specialKey == GLUT_ACTIVE_CTRL) { 00366 rotate_camera(-ROTATE_ANGLE); 00367 } else { 00368 _world.iteration(); 00369 glutPostRedisplay(); 00370 } 00371 break; 00372 case GLUT_RIGHT_BUTTON : 00373 if(specialKey == GLUT_ACTIVE_CTRL) { 00374 rotate_camera(ROTATE_ANGLE); 00375 } 00376 break; 00377 } 00378 } 00379 }
Display * Display::_instance = NULL [static, protected] |
Instance d'affichage
Definition at line 40 of file Display.h.
Referenced by destroy(), and instance().
GLfloat Display::_camera_x [protected] |
x position of the camera
Definition at line 43 of file Display.h.
Referenced by Display(), draw(), focal_distance(), rotate_camera(), translate_camera(), and zoom_camera().
GLfloat Display::_camera_y [protected] |
y position of the camera
Definition at line 45 of file Display.h.
Referenced by Display(), draw(), focal_distance(), move_camera(), and zoom_camera().
GLfloat Display::_camera_z [protected] |
z position of the camera
Definition at line 47 of file Display.h.
Referenced by Display(), draw(), rotate_camera(), translate_camera(), and zoom_camera().
GLfloat Display::_center_x [protected] |
x position of the center of screen
Definition at line 49 of file Display.h.
Referenced by Display(), draw(), focal_distance(), inputKey(), rotate_camera(), translate_camera(), and zoom_camera().
GLfloat Display::_center_y [protected] |
y position of the center of screen
Definition at line 51 of file Display.h.
Referenced by Display(), draw(), focal_distance(), and zoom_camera().
GLfloat Display::_center_z [protected] |
y position of the center of screen
Definition at line 53 of file Display.h.
Referenced by Display(), draw(), inputKey(), rotate_camera(), translate_camera(), and zoom_camera().
GLfloat Display::_near [protected] |
GLfloat Display::_far [protected] |
GLfloat Display::_fovy [protected] |
World Display::_world [protected] |
the world to display
Definition at line 62 of file Display.h.
Referenced by Display(), draw(), init(), inputKey(), inputMouse(), timer(), and translate_camera().
bool Display::_timer [protected] |
timer status : activate or not
Definition at line 65 of file Display.h.
Referenced by Display(), inputKey(), and timer().