src/opengl/glut_util.cc

00001 
00002 #if __linux
00003 #include <GL/gl.h>
00004 #include <GL/glut.h>
00005 #else
00006 #include <opengl/gl.h>
00007 #include <glut/glut.h>
00008 #endif
00009 
00010 #include <unistd.h>
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 
00014 
00015 #include "glut_util.h"
00016 
00017 typedef float vec3_t[3];
00018 
00019 drawing_function user_draw_function;
00020 input_function user_input_function;
00021 selection_function user_select_function;
00022 
00023 int clickdown_select;
00024 
00025 
00026 void glutDisplay(void);
00027 
00028 
00029 void my_glut_set_selectfunc(selection_function sel_func) {
00030         user_select_function = sel_func;        
00031         clickdown_select = -1;
00032 }
00033 
00034 
00035 void clamp(vec3_t v)
00036 {
00037     int i;
00038     
00039     for (i = 0; i < 3; i ++)
00040         if (v[i] > 360 || v[i] < -360)
00041             v[i] = 0;
00042 }
00043 
00044 //Generic GLUT functions
00045 
00046 
00047 enum {
00048     LEFT = 0, 
00049     RIGHT,
00050 };
00051 
00052 
00053 int mButton = -1;
00054 int mOldY, mOldX; 
00055 
00056 vec3_t eye = {0.0f, 0.0f, 140.0f};
00057 vec3_t rot = {45.0f, 45.0f, 0.0f};
00058 
00059 
00060 void glutResize(int width, int height)
00061 {   
00062         glViewport(0, 0, width, height);
00063         glMatrixMode(GL_PROJECTION);
00064         glLoadIdentity();
00065         gluPerspective(55.0, (float)width/(float)height, 1.0, 3000.0);
00066         glMatrixMode(GL_MODELVIEW);
00067 }
00068 
00069 
00070 void glutKeyboard(unsigned char key, int x, int y)
00071 {
00072         switch (key) {
00073                 case 27:
00074                         exit(0);
00075                 break;
00076         }
00077         user_input_function(key);
00078         
00079 }
00080 
00081 
00082 void glutMotion(int x, int y) 
00083 {
00084     if (mButton == LEFT) 
00085     {
00086         rot[0] -= ((mOldY - y) * 180.0f) / 200.0f;
00087         rot[1] -= ((mOldX - x) * 180.0f) / 200.0f;
00088         clamp (rot);
00089                 clickdown_select = -1;
00090     }
00091         else if (mButton == RIGHT) 
00092     {
00093         eye[2] -= ((mOldY - y) * 180.0f) / 200.0f;
00094         clamp (rot);
00095     } 
00096 
00097     mOldX = x; 
00098     mOldY = y;
00099 }
00100 
00101 void glutMouse(int button, int state, int x, int y) 
00102 {
00103   int num_pickable_objects = 1000;
00104   
00105   if(state == GLUT_DOWN) {
00106     mOldX = x;
00107     mOldY = y;
00108     switch(button) {
00109     case GLUT_LEFT_BUTTON: {
00110       mButton = LEFT;
00111       
00112       
00113       //Hold the ID of the objects clicked. Each object takes 4 places in the array.
00114       GLuint *selectBuffer = new GLuint[ num_pickable_objects*4 ];
00115       //Register our buffer to write in it.
00116       glSelectBuffer(num_pickable_objects * 4, selectBuffer);
00117       
00118       glRenderMode( GL_SELECT );
00119       glMatrixMode( GL_PROJECTION );
00120       //Restrict region to pick object only in this region
00121       
00122       GLint viewport[4];                                        // Where The Viewport Values Will Be Stored
00123       glGetIntegerv(GL_VIEWPORT, viewport);                     // Retrieves The Viewport Values (X, Y, Width, Height)
00124       
00125       glLoadIdentity();
00126       gluPickMatrix(x, viewport[3]-y, 2, 2, viewport);    //viewport[3]-y because 0 is at top for the mouse
00127       gluPerspective(55.0, (float)viewport[2]/(float)viewport[3], 1.0, 3000.0);
00128       
00129       glPushMatrix();
00130       
00131       glTranslatef (-eye[0], -eye[1], -eye[2]);   
00132       
00133       glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
00134       glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
00135       glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
00136       (*user_draw_function)();
00137       glPopMatrix();
00138       
00139       int num_objects_picked = glRenderMode( GL_RENDER );                               
00140       if ( num_objects_picked > 0 ) {
00141         
00142         GLuint hit_dist = selectBuffer[1];
00143         GLuint hit_name = selectBuffer[3];
00144         int offset = 0;
00145         
00146         for (int i = 0; i < num_objects_picked; i++) {  // for each hit 
00147           if ( hit_dist >= selectBuffer[ offset + 1 ] ) {
00148             hit_dist = selectBuffer[ offset + 1 ];
00149             hit_name = selectBuffer[ offset + 3 ];
00150           }
00151           offset += 3 + selectBuffer[ offset ];
00152         }
00153         clickdown_select = hit_name;
00154       } else {
00155         clickdown_select = -1;
00156       }
00157       
00158       glMatrixMode(GL_PROJECTION);
00159       glLoadIdentity();
00160       gluPerspective(55.0, (float)viewport[2]/(float)viewport[3], 1.0, 3000.0);
00161       glMatrixMode(GL_MODELVIEW);
00162       
00163       
00164       break;
00165     }
00166     case GLUT_RIGHT_BUTTON:
00167       mButton = RIGHT;
00168       break;
00169     }
00170   } else if (state == GLUT_UP) {
00171     mButton = -1;
00172     if ( user_select_function != NULL && clickdown_select != -1 ) {
00173       user_select_function( clickdown_select );
00174       glutDisplay();
00175     }
00176   }
00177 }
00178 
00179 void glutMenu(int value)
00180 {
00181         switch (value)
00182         {
00183                 case 1:
00184                         glutFullScreen();
00185                         return;
00186                 
00187                 case 2:
00188                         exit(0);
00189         }
00190 }
00191 
00192 
00193 void glutDisplay(void) {
00194         long i,j,res_start,res_end;   
00195         bool draw;
00196         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00197         float alpha;
00198 
00199     glPushMatrix();
00200 
00201                 glTranslatef (-eye[0], -eye[1], -eye[2]);         
00202 
00203                 glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
00204             glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
00205             glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
00206             
00207                 glBindTexture(GL_TEXTURE_2D, 1);
00208                 
00209                 
00210                 (*user_draw_function)();
00211                 
00212         glPopMatrix();
00213 
00214         glFlush();
00215         glutSwapBuffers();
00216 }
00217 
00218                 
00219 
00220 void my_glut_init(int argc, char **argv, drawing_function draw, input_function input) {
00221         GLfloat ambient[] = {.3,.3,.3,.3};
00222         GLfloat specular[]  = {1,1,1,1};
00223         
00224         glutInit(&argc, argv);
00225         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00226         glutInitWindowSize(400, 300);
00227         glutInitWindowPosition(100, 100);
00228         
00229         glutCreateWindow("local align");
00230         glutReshapeFunc(glutResize);
00231         glutDisplayFunc(glutDisplay);
00232         glutIdleFunc(glutDisplay);
00233     glutKeyboardFunc(glutKeyboard);
00234         glutMouseFunc(glutMouse);
00235     glutMotionFunc(glutMotion);
00236 
00237         glEnable(GL_TEXTURE_2D);
00238         glEnable(GL_BLEND);
00239 //      glEnable(GL_LIGHTING);
00240 //      glLightfv(GL_LIGHT0, GL_AMBIENT, ambient );
00241 //      glLightfv(GL_LIGHT0, GL_SPECULAR, specular );
00242 //      glEnable(GL_LIGHT0);
00243         glEnable(GL_COLOR_MATERIAL);
00244         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00245     glEnable(GL_DEPTH_TEST);
00246 
00247         glutCreateMenu(glutMenu);
00248         glutAttachMenu(GLUT_MIDDLE_BUTTON);
00249         glutAddMenuEntry("Full Screen", 1);
00250         glutAddMenuEntry("Exit", 2);
00251 
00252         user_draw_function = draw;
00253         user_input_function = input;
00254 }
00255 
00256 
00257 
00258 void my_glut_main_loop(void) {
00259         glutMainLoop();
00260 }

Generated on Wed Apr 11 16:50:50 2007 for open_prospect by  doxygen 1.4.6