src/opengl/prospect_opengl.cc

00001 
00002 #if __linux
00003 #define  GL_GLEXT_PROTOTYPES
00004 #include <GL/gl.h>
00005 #include <GL/glu.h>
00006 #include <GL/glext.h>
00007 #include <GL/glut.h>
00008 #else
00009 #if __APPLE__
00010 #include <opengl/gl.h>
00011 #include <opengl/glu.h>
00012 #include <glut/glut.h>
00013 #endif
00014 #endif
00015 #include <stdio.h>
00016 
00017 
00018 #include "TargetStructGL.h"
00019 #include "sm_matrix.h"
00020 #include "ArcBall.h"
00021 
00022 
00023 //GLUT interface stuff
00024 
00025 
00026 
00027 ArcBall_t *my_arcball;
00028 Matrix4fT Transform = { 1.0f, 0.0f, 0.0f, 0.0f,
00029   0.0f, 1.0f, 0.0f, 0.0f,
00030   0.0f, 0.0f, 1.0f, 0.0f,
00031   0.0f, 0.0f, 0.0f, 1.0f
00032 };
00033 
00034 Matrix3fT LastRot = { 1.0f, 0.0f, 0.0f, // Last Rotation
00035   0.0f, 1.0f, 0.0f,
00036   0.0f, 0.0f, 1.0f
00037 };
00038 
00039 Matrix3fT ThisRot = { 1.0f, 0.0f, 0.0f, // This Rotation
00040   0.0f, 1.0f, 0.0f,
00041   0.0f, 0.0f, 1.0f
00042 };
00043 
00044 
00045 
00046 
00047 vec3 eye = {0.0f, 0.0f, 140.0f};
00048 vec3 rot = {45.0f, 45.0f, 0.0f};
00049 int screenHeight, screenWidth;
00050 int last_x, last_y;
00051 
00052 TargetStructGL *the_target;
00053 
00054 int modifer_keys = 0;
00055 
00056 
00057 void my_draw_function (void) {
00058   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00059 
00060   glLoadIdentity ();
00061   glPushMatrix ();
00062 
00063 
00064   glTranslatef (-eye[0], -eye[1], -eye[2]);       
00065   
00066   glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
00067   glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
00068   glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
00069   
00070   glMultMatrixf (Transform.M);  // Apply Dynamic Transform
00071   {
00072     the_target->DrawBackBoneLine ();
00073     the_target->DrawAtomSphere ();
00074   }
00075   glPopMatrix ();
00076 
00077   glFlush ();
00078   glutSwapBuffers ();
00079 }
00080 
00081 
00082 
00083 void
00084 glutResize (int width, int height)
00085 {
00086   glViewport (0, 0, width, height);
00087   glMatrixMode (GL_PROJECTION);
00088   glLoadIdentity ();
00089   gluPerspective (55.0, (float) width / (float) height, 1.0, 3000.0);
00090   glMatrixMode (GL_MODELVIEW);
00091   screenHeight = height; screenWidth = width;
00092   my_arcball->setBounds ((GLfloat) width, (GLfloat) height);    // Update Mouse Bounds For ArcBall
00093 }
00094 
00095 
00096 void glutKeyboard (unsigned char key, int x, int y) {
00097         
00098         modifer_keys = glutGetModifiers();
00099         
00100         switch (key)
00101     {
00102                 case 27:
00103                         exit (0);
00104                         break;
00105     }
00106         
00107 }
00108 
00109 
00110 
00111 
00112 enum
00113 {
00114   LEFT = 0,
00115   RIGHT,
00116 };
00117 
00118 
00119 #if !defined(GLUT_WHEEL_UP)
00120 #  define GLUT_WHEEL_UP   3
00121 #  define GLUT_WHEEL_DOWN 4
00122 #endif
00123 
00124 
00125 
00126 
00127 int clickdown_select;
00128 int mButton = -1;
00129 
00130 void glutMotion (int x, int y) {
00131         
00132         if ( modifer_keys & GLUT_ACTIVE_CTRL ) {
00133                 eye[3] += last_x - x;
00134         } else {
00135                 
00136                 if (mButton == LEFT) {
00137                         Point2fT mouse_point;
00138                         mouse_point.s.X = x;
00139                         mouse_point.s.Y = y;
00140                         //mouse_point.s.X  =  ((mouse_point.s.X / (((float)screenWidth  - 1) / 2.0)) - 1);
00141                         //mouse_point.s.Y  = -((mouse_point.s.Y / (((float)screenHeight - 1) / 2.0)) - 1);
00142                         Quat4fT ThisQuat;
00143                         
00144                         my_arcball->drag (&mouse_point, &ThisQuat);     // Update End Vector And Get Rotation As Quaternion
00145                         Matrix3fSetRotationFromQuat4f (&ThisRot, &ThisQuat);    // Convert Quaternion Into Matrix3fT
00146                         Matrix3fMulMatrix3f (&ThisRot, &LastRot);       // Accumulate Last Rotation Into This One
00147                         Matrix4fSetRotationFromMatrix3f (&Transform, &ThisRot); // Set Our Final Transform's Rotation From This One
00148                         
00149                         
00150                         my_draw_function();
00151                         clickdown_select = -1;
00152                 } else if (mButton == RIGHT) {
00153                         if ( clickdown_select != -1 ) {
00154                                 /*
00155                                  //Example code to calculate the x,y,z co-ordinates of a selected point
00156                                 GLint viewport[4];
00157                                 GLdouble modelview[16];
00158                                 GLdouble projection[16];
00159                                 GLfloat winX, winY, winZ;
00160                                 GLdouble posX, posY, posZ;
00161                                 
00162                                 glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
00163                                 glGetDoublev( GL_PROJECTION_MATRIX, projection );
00164                                 glGetIntegerv( GL_VIEWPORT, viewport );
00165                                 
00166                                 winX = (float)x;
00167                                 winY = (float)viewport[3] - (float)y;
00168                                 //glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
00169                                 // calculate the Z distance as the center of the selected object
00170                                 
00171                                 gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
00172                                 */
00173                         }                       
00174                 }
00175         }
00176         last_x = x; last_y = y;
00177 }
00178 
00179 void glutMouse (int button, int state, int x, int y) {
00180         int num_pickable_objects = 1000;
00181         
00182         if (state == GLUT_DOWN) {
00183                 printf("x: %d y%d\n", x, y);
00184                 Point2fT  mouse_point;
00185                 mouse_point.s.X = x;
00186                 mouse_point.s.Y = y;
00187                 //mouse_point.s.X  =  ((mouse_point.s.X / (((float)screenWidth  - 1) / 2)) - 1);
00188                 //mouse_point.s.Y  = -((mouse_point.s.Y / (((float)screenHeight - 1) / 2)) - 1);
00189                 my_arcball->click (&mouse_point);
00190                 switch (button) {
00191                         case GLUT_LEFT_BUTTON: {
00192                                 mButton = LEFT;
00193                                 
00194                                 
00195                                 //Hold the ID of the objects clicked. Each object takes 4 places in the array.
00196                                 GLuint *selectBuffer = new GLuint[num_pickable_objects * 4];
00197                                 //Register our buffer to write in it.
00198                                 glSelectBuffer (num_pickable_objects * 4, selectBuffer);
00199                                 
00200                                 glRenderMode (GL_SELECT);
00201                                 glMatrixMode (GL_PROJECTION);
00202                                 //Restrict region to pick object only in this region
00203                                 
00204                                 GLint viewport[4];      // Where The Viewport Values Will Be Stored
00205                                 glGetIntegerv (GL_VIEWPORT, viewport);  // Retrieves The Viewport Values (X, Y, Width, Height)
00206                                 
00207                                 glLoadIdentity ();
00208                                 gluPickMatrix (x, viewport[3] - y, 2, 2, viewport);     //viewport[3]-y because 0 is at top for the mouse
00209                                 gluPerspective (55.0, (float) viewport[2] / (float) viewport[3],
00210                                                                 1.0, 3000.0);
00211                                 
00212                                 glPushMatrix ();
00213                                 
00214                                 
00215                                 my_draw_function ();
00216                                 
00217                                 
00218                                 glPopMatrix ();
00219                                 
00220                                 int num_objects_picked = glRenderMode (GL_RENDER);
00221                                 if (num_objects_picked > 0) {
00222                                         
00223                                         GLuint hit_dist = selectBuffer[1];
00224                                         GLuint hit_name = selectBuffer[3];
00225                                         int offset = 0;
00226                                         
00227                                         for (int i = 0; i < num_objects_picked; i++) {          // for each hit 
00228                                                 if (hit_dist >= selectBuffer[offset + 1]) {
00229                                                         hit_dist = selectBuffer[offset + 1];
00230                                                         hit_name = selectBuffer[offset + 3];
00231                                                 }
00232                                                 offset += 3 + selectBuffer[offset];
00233                                         }
00234                                         clickdown_select = hit_name;
00235                                 } else {
00236                                         clickdown_select = -1;
00237                                 }
00238                                 
00239                                 glMatrixMode (GL_PROJECTION);
00240                                 glLoadIdentity ();
00241                                 gluPerspective (55.0, (float) viewport[2] / (float) viewport[3],
00242                                                                 1.0, 3000.0);
00243                                 glMatrixMode (GL_MODELVIEW);
00244                                 
00245                                 
00246                                 break;
00247                         }
00248                         case GLUT_RIGHT_BUTTON:
00249                                 mButton = RIGHT;
00250                                 break;
00251                         case GLUT_WHEEL_UP:
00252                                 eye[2]+=5;
00253                                 break;
00254                         case GLUT_WHEEL_DOWN:
00255                                 eye[2]-=5;
00256                                 break;
00257                 }
00258         } else if (state == GLUT_UP) {
00259                 mButton = -1;
00260                 if (clickdown_select != -1) {
00261                         printf ("Selection %d\n", clickdown_select);
00262                         the_target->SetColor (clickdown_select, 0.4, 1.0, 0.4);
00263                         the_target->SphereList_Init ();
00264                         
00265                 }
00266         }
00267         last_x = x; last_y = y;
00268         my_draw_function();
00269 }
00270 
00271 
00272 
00273 void glutMenu (int value) {
00274   switch (value) {
00275     case 1:
00276       glutFullScreen ();
00277       return;
00278 
00279     case 2:
00280       exit (0);
00281     }
00282 }
00283 
00284 void my_glut_init (int argc, char **argv) {
00285   GLfloat ambient[] = { .3, .3, .3, .3 };
00286   GLfloat specular[] = { 1, 1, 1, 1 };
00287 
00288   my_arcball = new ArcBall_t (400, 300);
00289   // Reset Rotation
00290   Matrix3fSetIdentity (&LastRot);
00291   // Reset Rotation
00292   Matrix3fSetIdentity (&ThisRot);
00293   // Reset Rotation
00294   Matrix4fSetRotationFromMatrix3f (&Transform, &ThisRot);
00295 
00296   screenHeight = 300; screenWidth = 400;
00297 
00298   glutInit (&argc, argv);
00299   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00300   glutInitWindowSize ( screenWidth, screenHeight );
00301   glutInitWindowPosition (100, 100);
00302 
00303   glutCreateWindow ("local align");
00304   glutReshapeFunc (glutResize);
00305   glutDisplayFunc (my_draw_function);
00306   //glutIdleFunc(glutDisplay);
00307   glutKeyboardFunc (glutKeyboard);
00308   glutMouseFunc (glutMouse);
00309   glutMotionFunc (glutMotion);
00310   glEnable (GL_TEXTURE_2D);
00311   glEnable (GL_BLEND);
00312   //    glEnable(GL_LIGHTING);
00313   //    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient );
00314   //    glLightfv(GL_LIGHT0, GL_SPECULAR, specular );
00315   //    glEnable(GL_LIGHT0);
00316   glEnable (GL_COLOR_MATERIAL);
00317   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00318   glEnable (GL_DEPTH_TEST);
00319 
00320   glutCreateMenu (glutMenu);
00321   glutAttachMenu (GLUT_MIDDLE_BUTTON);
00322   glutAddMenuEntry ("Full Screen", 1);
00323   glutAddMenuEntry ("Exit", 2);
00324 
00325 
00326 }
00327 
00328 
00329 
00330 int
00331 main (int argc, char **argv)
00332 {
00333   my_glut_init (argc, argv);
00334 
00335   TargetStructGL target;
00336   target.LoadPDB (argv[1]);
00337   the_target = &target;
00338   the_target->MoveMeanToOrigin ();
00339 
00340   glEnableClientState (GL_VERTEX_ARRAY);
00341 
00342   glShadeModel (GL_SMOOTH);     // Enable Smooth Shading
00343   glClearColor (0.2f, 0.5f, 1.0f, 1.0f);        // Background
00344   glClearDepth (1.0f);          // Depth Buffer Setup
00345   glClearStencil (0);           // Clear The Stencil Buffer To 0
00346   glEnable (GL_DEPTH_TEST);     // Enables Depth Testing
00347   glDepthFunc (GL_LEQUAL);      // The Type Of Depth Testing To Do
00348   glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   // Really Nice Perspective Calculations
00349   glEnable (GL_TEXTURE_2D);     // Enable 2D Texture Mapping
00350   glEnable (GL_COLOR_MATERIAL);
00351 
00352 
00353   // Light Parameters
00354   static GLfloat LightAmb[] = { 0.4f, 0.4f, 0.4f, 1.0f };       // Ambient Light
00355   static GLfloat LightDif[] = { 0.7f, 0.7f, 0.7f, 1.0f };       // Diffuse Light
00356   static GLfloat LightSpc[] = { 1.0f, 1.0f, 1.0f, 1.0f };       // Diffuse Light
00357   static GLfloat LightPos[] = { 4.0f, 4.0f, 6.0f, 1.0f };       // Light Position
00358 
00359   {
00360     double max_dist = 0;
00361     for (int i = 0; i < the_target->len; i++)
00362       {
00363         ResidueAtoms *the_res = &the_target->structure[i];
00364         for (int j = 0; j < the_res->atom_count; j++)
00365           {
00366             double dist = smNorm (the_res->atom[j]);
00367             if (dist > max_dist)
00368               max_dist = dist;
00369           }
00370       }
00371     double ratio = max_dist / smNorm (LightPos);
00372     LightPos[0] *= ratio;
00373     LightPos[1] *= ratio;
00374     LightPos[2] *= ratio;
00375   }
00376 
00377 
00378   glLightfv (GL_LIGHT0, GL_AMBIENT, LightAmb);  // Set The Ambient Lighting For Light0
00379   glLightfv (GL_LIGHT0, GL_DIFFUSE, LightDif);  // Set The Diffuse Lighting For Light0
00380   glLightfv (GL_LIGHT0, GL_SPECULAR, LightSpc); // Set The Diffuse Lighting For Light0
00381   glLightfv (GL_LIGHT0, GL_POSITION, LightPos); // Set The Position For Light0
00382 
00383   glEnable (GL_LIGHT0);         // Enable Light 0
00384   glEnable (GL_LIGHTING);       // Enable Lighting
00385 
00386   the_target->SphereList_Init ();
00387   the_target->VOB_Init ();
00388 
00389   glutMainLoop ();
00390 
00391   return 0;
00392 }

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