GLUTCallbackFunctions.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------- 00004 00005 // Last Change: 2004/06/14 00006 // Version: 0.2 00007 00008 //--------------------------------------------------------------------includes- 00009 #include "../src/main.h" 00010 #include "../src/Graph.h" 00011 #include "../src/RadialLayout.h" 00012 #include "../src/GLUTCallbackFunctions.h" 00013 00014 //----------------------------------------------------------------file globals- 00015 int main_window; 00016 00017 Graph *graph_; 00018 RadialLayout *radial_layout_; 00019 00020 GLuint screen_width_; 00021 GLuint screen_height_; 00022 00023 GLuint selected_animation_speed_; 00024 GLint selected_render_mode_; 00025 00026 GLuint clicked_object_id_ = 0; 00027 GLuint motion_object_id_ = 0; 00028 00029 bool is_animation_mode_active_ = false; 00030 bool is_fullscreen_mode_on_ = false; 00031 00032 bool is_animation_running_ = false; 00033 GLuint num_iterations_ = 0; 00034 00035 GLint last_x_pos_; 00036 GLint last_y_pos_; 00037 00038 bool is_reset_rotation_ = true; 00039 GLfloat x_rotation_ = 0.0; 00040 GLfloat y_rotation_ = 0.0; 00041 GLfloat z_rotation_ = 0.0; 00042 00043 bool is_left_mouse_button_down_ = true; 00044 int is_caption_enabled_ = true; 00045 00046 //--------------------------------------------------------------myGLUTInitFunc- 00047 // 00048 // This is the callback function for the GLUT initializations. 00049 // @param graph The graph to draw. 00050 // @param radial_layout The radiallayout to draw. 00051 // @param screen_width The screen width. 00052 // @param screen_height The screen height. 00053 // 00054 void createGLUTWindow(Graph *graph, RadialLayout *radial_layout, 00055 GLuint screen_width, GLuint screen_height) 00056 { 00057 screen_width_ = screen_width <= 0 ? 100 : screen_width; 00058 screen_height_ = screen_height <= 0 ? 100 : screen_height; 00059 if (graph == NULL) 00060 { 00061 cerr << "Graph is NULL." << endl; 00062 exit(0); 00063 } 00064 graph_ = graph; 00065 00066 if (radial_layout == NULL) 00067 { 00068 cerr << "Radial Layout is NULL." << endl; 00069 exit(0); 00070 } 00071 radial_layout_ = radial_layout; 00072 00073 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 00074 glutInitWindowPosition(50, 50); 00075 glutInitWindowSize(800, 600); 00076 main_window = glutCreateWindow("InfoViz: Animated Radial Tree Exploration"); 00077 00078 // assign the GLUT callback functions 00079 glutDisplayFunc(renderScene); 00080 00081 glutReshapeFunc(myGLUTReshapeFunc); 00082 glutKeyboardFunc(myGLUTKeyboardEventFunc); 00083 glutMouseFunc(myGLUTMouseEventFunc); 00084 glutMotionFunc(myGlutMotion); 00085 00086 // create graph 00087 graph_->createGraph("Hallo"); 00088 // create the GLUI 00089 createGLUI(); 00090 // run the GLUT main moop and wait for events 00091 glutMainLoop(); 00092 } 00093 00094 //------------------------------------------------------------------createGLUI- 00095 // 00096 // This creates the GLUI for the application. 00097 // 00098 void createGLUI() 00099 { 00100 //exit(3434); 00101 GLUI *glui_window = GLUI_Master.create_glui("Menu"); 00102 // for he animation speed 00103 00104 GLUI_Spinner *animation_speed_spinner = 00105 glui_window->add_spinner("Speed:", GLUI_SPINNER_INT, 00106 &selected_animation_speed_); 00107 00108 animation_speed_spinner->set_int_limits(100, 2000); 00109 00110 // for he render mode 00111 GLUI_Panel *radio_group_panel = glui_window->add_panel("Render Mode"); 00112 GLUI_RadioGroup *render_mode_radio_group = 00113 glui_window->add_radiogroup_to_panel(radio_group_panel, 00114 &selected_render_mode_, 1, 00115 (GLUI_Update_CB)updateRenderMode); 00116 00117 // put on the GLUI 00118 glui_window->add_checkbox("Enable Caption", &is_caption_enabled_, 2, 00119 (GLUI_Update_CB)toggleCaptionEnabling); 00120 glui_window->add_radiobutton_to_group(render_mode_radio_group, 00121 "Ivan's Version"); 00122 glui_window->add_radiobutton_to_group(render_mode_radio_group, 00123 "Laura's Version"); 00124 glui_window->add_button("Demo", 0, (GLUI_Update_CB)startDemoMode); 00125 glui_window->add_button("Reset Rotation", 0, 00126 (GLUI_Update_CB)resetSceneRotation); 00127 glui_window->add_separator(); 00128 glui_window->add_button("Quit", 0, (GLUI_Update_CB)exit); 00129 00130 glui_window->set_main_gfx_window(main_window); 00131 00132 // register the idle callback with GLUI (not with GLUT!) 00133 GLUI_Master.set_glutIdleFunc(myGLUTIdleFunc); 00134 } 00135 00136 //-----------------------------------------------------------------renderScene- 00137 // 00138 // This renders the entire scene into OpenGL. 00139 // 00140 void renderScene() 00141 { 00142 if ((is_animation_running_ == false) 00143 && (is_animation_mode_active_ == true)) 00144 { 00145 num_iterations_ = 1; 00146 is_animation_running_ = true; 00147 graph_->setAnimationMode(true); 00148 } 00149 if (num_iterations_ <= selected_animation_speed_) 00150 { 00151 ++num_iterations_; 00152 } 00153 00154 graph_->setAnimationSpeed(&selected_animation_speed_); 00155 00156 GLfloat width_height_aspect = 00157 (GLfloat)screen_width_ / (GLfloat)screen_height_; 00158 00159 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 00160 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00161 00162 //glMatrixMode(GL_PROJECTION); 00163 //glLoadIdentity(); 00164 glFrustum(-width_height_aspect * 0.04, width_height_aspect * 0.04, -0.04, 00165 0.04, 0.1, 15.0); 00166 00167 glMatrixMode( GL_MODELVIEW ); 00168 glLoadIdentity(); // reset the view 00169 00170 // position view up vector 00171 gluLookAt(0, 0, 6, 0, 0, 0, 0, 1, 0); 00172 00173 // number of circles 00174 GLint num_circles = 10; 00175 00176 // radius of the starting circle 00177 GLfloat radius = -0.1f; 00178 GLUquadricObj *qobj; 00179 qobj = gluNewQuadric(); 00180 00182 if (is_reset_rotation_ == true) 00183 { 00184 glRotatef(0.0f, 0.0f, 1.0f, 0.0f); 00185 glRotatef(0.0f, 1.0f, 0.0f, 0.0f); 00186 glRotatef(0.0f, 0.0f, 0.0f, 1.0f); 00187 is_reset_rotation_ = false; 00188 y_rotation_ = 0.0f; 00189 x_rotation_ = 0.0f; 00190 last_y_pos_ = 0.0f; 00191 last_x_pos_ = 0.0f; 00192 myGlutMotion(last_x_pos_, last_y_pos_); 00193 } 00194 else 00195 { 00196 glRotatef(y_rotation_, 0.0f, 1.0f, 0.0f); 00197 glRotatef(x_rotation_, 1.0f, 0.0f, 0.0f); 00198 glRotatef(0.0f, 0.0f, 0.0f, 1.0f); 00199 } 00201 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); 00202 glEnable(GL_LINE_SMOOTH); 00204 gluQuadricNormals(qobj, GLU_SMOOTH); 00205 glDisable(GL_BLEND); 00206 radial_layout_ -> setNumberDisplayedRings(num_circles); 00207 radial_layout_ -> setRadiusOfRing(1, radius); 00208 radial_layout_ -> renderRadialLayout(qobj); 00209 00210 glInitNames(); 00211 glPushName(0); 00212 graph_->setSceneRotation(x_rotation_, y_rotation_, z_rotation_); 00213 graph_->drawGraph(qobj); 00214 glColor3f(1.0f, 1.0f, 1.0f); 00215 glDisable(GL_LINE_SMOOTH); 00216 00217 // swap buffers 00218 glutSwapBuffers(); 00219 gluDeleteQuadric(qobj); 00220 } 00221 00222 //--------------------------------------------------------------myGLUTIdleFunc- 00223 // 00224 // This creates the GLUI for the application. 00225 // 00226 void myGLUTIdleFunc() 00227 { 00228 // the current window is undefined during an idle callback; therefore we 00229 // need to explicitly change it if necessary 00230 if (glutGetWindow() != main_window) 00231 { 00232 glutSetWindow(main_window); 00233 } 00234 glutPostRedisplay(); 00235 } 00236 00237 //-----------------------------------------------------------myGLUTReshapeFunc- 00238 // 00239 // This reshapes the OpenGL window to specified resolution. 00240 // 00241 void myGLUTReshapeFunc(GLint screen_width, GLint screen_height) 00242 { 00243 GLfloat width_height_aspect = 00244 (GLfloat)screen_width / (GLfloat)screen_height; 00245 00246 glViewport(0, 0, screen_width, screen_height); 00247 00248 glMatrixMode(GL_PROJECTION); 00249 glLoadIdentity(); 00250 00251 glutPostRedisplay(); 00252 } 00253 00254 //------------------------------------------------------------updateRenderMode- 00255 // 00256 // This updates the render mode. 00257 // 00258 void updateRenderMode() 00259 { 00260 graph_->toggleRenderMode(); 00261 } 00262 00263 //------------------------------------------------------------updateRenderMode- 00264 // 00265 // This starts the demo mode. 00266 // 00267 void startDemoMode() 00268 { 00269 if (is_animation_mode_active_ == false) 00270 { 00271 num_iterations_ = 1; 00272 is_animation_mode_active_ = true; 00273 is_animation_running_ = true; 00274 graph_->setAnimationMode(true); 00275 } 00276 else 00277 { 00278 is_animation_mode_active_ = false; 00279 } 00280 } 00281 00282 //-----------------------------------------------------myGLUTKeyboardEventFunc- 00283 // 00284 // This handles the key board events. 00285 // @param key The user-typed key. 00286 // @param x_pos The keyboard's x position. 00287 // @param y_pos The keyboard's y position. 00288 // 00289 void myGLUTKeyboardEventFunc(unsigned char key, int x_pos, int y_pos) 00290 { 00291 switch(key) 00292 { 00293 case 27 : 00294 case 'q' : 00295 exit(0); 00296 case GLUT_KEY_F1 : 00297 if (!is_fullscreen_mode_on_) 00298 { 00299 glutFullScreen(); 00300 is_fullscreen_mode_on_ = true; 00301 } 00302 else 00303 { 00304 glutReshapeWindow(800, 600); 00305 is_fullscreen_mode_on_ = false; 00306 } 00307 break; 00308 case 'a' : 00309 startDemoMode(); 00310 break; 00311 case 'm' : 00312 graph_->toggleRenderMode(); 00313 break; 00314 default : 00315 break; 00316 }; 00317 } 00318 00319 //--------------------------------------------------------myGLUTMouseEventFunc- 00320 // 00321 // This handles the mouse events. 00322 // @param button Holds the flag which mouse button has been clicked. 00323 // @param state Indicates whether the mouse button is up or down. 00324 // @param x_pos The mouse cursor's x position. 00325 // @param y_pos The mouse cursor's y position. 00326 // 00327 void myGLUTMouseEventFunc(int button, int state, int x_pos, int y_pos) 00328 { 00329 if (button == GLUT_LEFT_BUTTON) 00330 { 00331 cout << x_pos << " " << y_pos << endl; 00332 // Here we pass in the cursors X and Y co-ordinates to test 00333 // an object under the mouse. 00334 clicked_object_id_ = retrieveObjectID(x_pos, y_pos); 00335 graph_->highlightNodeIfSelected(clicked_object_id_); 00336 00337 if ((graph_->isANodeClicked(clicked_object_id_)) 00338 && (is_animation_running_ == false)) 00339 { 00340 num_iterations_ = 1; 00341 is_animation_running_ = true; 00342 graph_->setCenterNode(clicked_object_id_); 00343 } 00344 } 00345 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) 00346 { 00347 is_left_mouse_button_down_ = true; 00348 } 00349 else// (button == GLUT_LEFT_BUTTON && state == GLUT_UP) 00350 { 00351 is_left_mouse_button_down_ = false; 00352 } 00353 } 00354 00355 //------------------------------------------------------------retrieveObjectID- 00356 // 00357 // This finds the object which was clicked by the user given the x, y 00358 // coordinates. 00359 // @param x_coord The x coordinate of the viewport. 00360 // @param y_coord The y coordinate of the viewport. 00361 // 00362 int retrieveObjectID(int x_coord, int y_coord) 00363 { 00364 int objectsFound = 0; 00365 int viewportCoords[4] = {0}; 00366 00367 unsigned int selectBuffer[1024] = {0}; 00368 00369 glSelectBuffer(1024, selectBuffer); 00370 glGetIntegerv(GL_VIEWPORT, viewportCoords); 00371 00372 glMatrixMode(GL_PROJECTION); 00373 glPushMatrix(); 00374 glRenderMode(GL_SELECT); 00375 glLoadIdentity(); 00376 gluPickMatrix(x_coord, viewportCoords[3] - y_coord, 2, 2, viewportCoords); 00377 00378 gluPerspective(45.0f,(float)screen_width_/(float)screen_height_,0.1f,150.0f); 00379 glMatrixMode(GL_MODELVIEW); 00380 00381 renderScene(); 00382 00383 objectsFound = glRenderMode(GL_RENDER); 00384 glMatrixMode(GL_PROJECTION); 00385 glPopMatrix(); 00386 00387 glMatrixMode(GL_MODELVIEW); 00388 00389 if (objectsFound > 0) 00390 { 00391 unsigned int lowestDepth = selectBuffer[1]; 00392 00393 int selectedObject = selectBuffer[3]; 00394 00395 // Return the selected object 00396 return selectedObject; 00397 } 00398 return 0; 00399 } 00400 00401 //----------------------------------------------------------------myGlutMotion- 00402 // 00403 // This is the callback function for the motions in the scene. 00404 // @param x_pos The x position of the viewport. 00405 // @param y_pos The y position of the viewport. 00406 // 00407 void myGlutMotion(int x_pos, int y_pos) 00408 { 00409 if (!is_left_mouse_button_down_) 00410 { 00411 x_rotation_ += (GLfloat) (y_pos - last_y_pos_); 00412 y_rotation_ += (GLfloat) (x_pos - last_x_pos_); 00413 00414 last_x_pos_ = x_pos; 00415 last_y_pos_ = y_pos; 00416 00417 if ((last_x_pos_ == screen_width_ / 2) || 00418 (last_y_pos_ == screen_height_ / 2)) 00419 { 00420 } 00421 else 00422 { 00423 if (((x_pos - screen_width_ / 2) * (last_y_pos_ - screen_height_ / 2) 00424 - (last_x_pos_ - screen_width_ / 2) * (y_pos - screen_height_ / 2)) 00425 > 0) 00426 { 00427 z_rotation_ += (GLfloat) ((abs(x_pos - last_x_pos_) 00428 + abs(y_pos - last_y_pos_))); 00429 } 00430 else 00431 { 00432 z_rotation_ += (GLfloat) (-1*(abs(x_pos - last_x_pos_) 00433 + abs(y_pos - last_y_pos_))); 00434 } 00435 } 00436 00437 glutPostRedisplay(); 00438 } 00439 } 00440 00441 //----------------------------------------------------------resetSceneRotation- 00442 // 00443 // This resets the rotation of the entire scene. 00444 // 00445 void resetSceneRotation() 00446 { 00447 is_reset_rotation_ = true; 00448 } 00449 00450 //--------------------------------------------------------toggleCaptionEnabling- 00451 // 00452 // This toggles the caption of the nodes. If enablen the caption's will be 00453 // displayed. 00454 // 00455 void toggleCaptionEnabling() 00456 { 00457 graph_->toggleCaptionMode(); 00458 } 00459 00460 // eof

Generated on Mon Jun 14 12:48:56 2004 for InfoVis by doxygen 1.3.7