Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

rendergl.C

Go to the documentation of this file.
00001 //----------------------------------------------------------------------
00002 //               The Motion Strategy Library (MSL)
00003 //----------------------------------------------------------------------
00004 //
00005 // Copyright (c) 1998-2000 Iowa State University and Steve LaValle.  
00006 // All Rights Reserved.
00007 // 
00008 // Permission to use, copy, and distribute this software and its 
00009 // documentation is hereby granted free of charge, provided that 
00010 // (1) it is not a component of a commercial product, and 
00011 // (2) this notice appears in all copies of the software and
00012 //     related documentation. 
00013 // 
00014 // Iowa State University and the author make no representations
00015 // about the suitability or fitness of this software for any purpose.  
00016 // It is provided "as is" without express or implied warranty.
00017 //----------------------------------------------------------------------
00018 
00019 #include <math.h>
00020 #include <stdlib.h>
00021 
00022 #include "rendergl.h"
00023 #include "defs.h"
00024 
00025 // Because the callback methods are static members that are not allowed by
00026 // glut to take an arguments, we have to recover the class data.  There 
00027 // must be a better way...
00028 RenderGL *RGL;
00029 
00030 
00032 //
00033 // RenderGL Class
00034 //
00036 
00037 RenderGL::RenderGL(): Render()
00038 {
00039   WindowX = 600.0; 
00040   WindowY = 600.0;
00041   WindowZ = 600.0;
00042 
00043   NumberOfObject = 0;
00044   NumberOfBody = 0;
00045   NumberOfEnvObj = 0;
00046 
00047   SceneBodyLib = new (mslGLObject *);
00048   SceneEnvObjLib = new (mslGLObject *); 
00049 
00050   RGL = this;
00051   ControlFreak = true;
00052 }
00053 
00054 
00055 RenderGL::RenderGL(string filepath=""): Render(filepath)
00056 {
00057   WindowX = 600.0; 
00058   WindowY = 600.0;
00059   WindowZ = 600.0;
00060 
00061   NumberOfObject = 0;
00062   NumberOfBody = 0;
00063   NumberOfEnvObj = 0;
00064 
00065   SceneBodyLib = new (mslGLObject *);
00066   SceneEnvObjLib = new (mslGLObject *); 
00067 
00068   RGL = this;
00069   ControlFreak = true;
00070 }
00071 
00072 
00073 RenderGL::RenderGL(Scene *s, string filepath): Render(s,filepath)
00074 {
00075   WindowX = 600.0; 
00076   WindowY = 600.0;
00077   WindowZ = 600.0;
00078 
00079   NumberOfObject = 0;
00080   NumberOfBody = 0;
00081   NumberOfEnvObj = 0;
00082 
00083   SceneBodyLib = new (mslGLObject *);
00084   SceneEnvObjLib = new (mslGLObject *); 
00085 
00086   RGL = this;
00087   ControlFreak = true;
00088 }
00089 
00090 
00091 RenderGL::~RenderGL()
00092 {
00093   // Delete a bunch of GL stuff
00094 }
00095 
00096 
00097 void RenderGL::AddBodyObject(mslGLObject * obj)
00098 {
00099   
00100   if(SceneBodyLib == NULL)
00101     {
00102       if((SceneBodyLib = (mslGLObject **) malloc(sizeof(mslGLObject *))) == 0)
00103         {
00104           printf("Error reallocating mem\n");
00105           exit(-1);       
00106         }      
00107     }
00108   else
00109     if((SceneBodyLib = (mslGLObject **) 
00110         realloc(SceneBodyLib, sizeof(mslGLObject *)*(NumberOfBody+1))) == 0) 
00111       {
00112         printf("Error reallocating mem\n");
00113         exit(-1);
00114       }
00115   
00116   SceneBodyLib[NumberOfBody] = obj;
00117   SceneBodyLib[NumberOfBody]->ID = NumberOfObject;
00118   
00119   NumberOfBody++;
00120   NumberOfObject++;
00121 }
00122 
00123 void RenderGL::AddEnvObject(mslGLObject * obj)
00124 {
00125   
00126   if(SceneEnvObjLib == NULL)
00127     {
00128       if((SceneEnvObjLib = (mslGLObject **) malloc(sizeof(mslGLObject *))) == 0)
00129         {
00130           printf("Error reallocating mem\n");
00131           exit(-1);       
00132         }      
00133     }
00134   else
00135     if((SceneEnvObjLib = (mslGLObject **) 
00136         realloc(SceneEnvObjLib, sizeof(mslGLObject *) * (NumberOfEnvObj+1))) == 0) 
00137       {
00138         printf("Error reallocating mem\n");
00139         exit(-1);
00140       }
00141 
00142   SceneEnvObjLib[NumberOfEnvObj] = obj;
00143   SceneEnvObjLib[NumberOfEnvObj]->ID = NumberOfObject;
00144 
00145   NumberOfEnvObj++;
00146   NumberOfObject++;
00147 }
00148 
00149 
00150 mslGLObject* RenderGL::WhichObject(int id)
00151 {
00152   int i;
00153 
00154   for(i=0; i<NumberOfBody; i++)
00155     if(SceneBodyLib[i]->ID == id) return SceneBodyLib[i];
00156 
00157   for(i=0; i<NumberOfEnvObj; i++)
00158     if(SceneEnvObjLib[i]->ID == id) return SceneEnvObjLib[i];
00159 
00160   cout << "Object ID allocation error" << endl;
00161   exit(-1);  
00162 }
00163 
00164 
00165 void RenderGL::Reset()
00166 {
00167   // Use reset from base class
00168   Render::Reset();
00169 
00170   Fov = 45.0;      AspectRatio = 1.0;
00171   Near = 10.0;     Far = 100000.0;
00172   VupX = 0.0,      VupY = 1.0,          VupZ = 0.0;
00173 
00174   EyeX = (BoundingBoxMax[0] + BoundingBoxMin[0])/2.0;
00175   EyeY = (BoundingBoxMax[1] + BoundingBoxMin[1])/2.0;  
00176   EyeZ = BoundingBoxMax[2] + (BoundingBoxMax[1]-BoundingBoxMin[1])/tan(38.0/180.0*PI);
00177 
00178   VpX = (BoundingBoxMax[0] + BoundingBoxMin[0])/2.0;
00179   VpY = (BoundingBoxMax[1] + BoundingBoxMin[1])/2.0;
00180   VpZ = (BoundingBoxMax[2] + BoundingBoxMin[2])/2.0;
00181 
00182   LightPosX = EyeX;
00183   LightPosY = EyeY + (BoundingBoxMax[1] - BoundingBoxMin[1]);
00184   LightPosZ = BoundingBoxMax[2] + (BoundingBoxMax[1]-BoundingBoxMin[1])/tan(38.0/180.0*PI)/2.0;
00185 
00186   Orientation[0] = 0.0;   Orientation[1] = 0.0;          Orientation[2] = 0.0;
00187   Position[0] = 0.0;    Position[1] = 0.0;           Position[2] = 0.0; 
00188 }
00189 
00190 
00191 void RenderGL::ShowCoordinateFrame()
00192 {
00193   MSLVector pos(3);
00194   MSLVector scenecenter(3);
00195 
00196   glDisable(GL_TEXTURE_2D);
00197   glDisable(GL_LIGHTING);
00198 
00199   glPushMatrix();
00200 
00201   scenecenter[0] = (RGL->BoundingBoxMin[0] + RGL->BoundingBoxMax[0])/2.0;
00202   scenecenter[1] = (RGL->BoundingBoxMin[1] + RGL->BoundingBoxMax[1])/2.0;
00203   scenecenter[2] = (RGL->BoundingBoxMin[2] + RGL->BoundingBoxMax[2])/2.0;
00204   
00205   glTranslatef(scenecenter[0], scenecenter[1], scenecenter[2]);
00206 
00207   GLfloat Diffuse[] = {1.0, 0.0, 0.0};
00208   GLfloat Ambient[] = {1.0, 0.0, 0.0};
00209   GLfloat Specular[] = {1.0, 0.0, 0.0};
00210   GLfloat Shininess[] = {1.0, 0.0, 0.0};
00211 
00212   glLineWidth(0.2);
00213 
00214   glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
00215   glMaterialfv(GL_FRONT, GL_AMBIENT, Ambient);
00216   glMaterialfv(GL_FRONT, GL_SPECULAR, Specular);
00217   glMaterialfv(GL_FRONT, GL_SHININESS, Shininess);
00218 
00219   glBegin(GL_LINES);
00220 
00221   // x axis
00222   glColor4f(1.0, 0.0, 0.0, 1.0);
00223   glVertex3f(0.0, 0.0, 0.0);
00224   glVertex3f(10.0, 0.0, 0.0);
00225   glVertex3f(10.0, 1.0, 0.0);
00226   glVertex3f(12.0, -1.0, 0.0);
00227   glVertex3f(10.0, -1.0, 0.0);
00228   glVertex3f(12.0, 1.0, 0.0);
00229 
00230   // y axis
00231   glColor4f(0.0, 1.0, 0.0, 1.0);
00232   glVertex3f(0.0, 0.0, 0.0);
00233   glVertex3f(0.0, 10.0, 0.0);
00234   glVertex3f(0.707, 12.707, 0.0);
00235   glVertex3f(0.0, 12.0, 0.0);
00236   glVertex3f(-0.707, 12.707, 0.0);
00237   glVertex3f(0.0, 12.0, 0.0);
00238   glVertex3f(0.0, 11.0, 0.0);
00239   glVertex3f(0.0, 12.0, 0.0);
00240 
00241   // z axis
00242   glColor4f(0.0, 0.0, 1.0, 1.0);
00243   glVertex3f(0.0, 0.0, 0.0);
00244   glVertex3f(0.0, 0.0, 10.0);
00245   glVertex3f(-1.0, 1.0, 10.0);
00246   glVertex3f(1.0, 1.0, 10.0);
00247   glVertex3f(-1.0, -1.0, 10.0);
00248   glVertex3f(1.0, -1.0, 10.0);
00249   glVertex3f(1.0, 1.0, 10.0);
00250   glVertex3f(-1.0, -1.0, 10.0);
00251 
00252   glEnd();
00253 
00254   glLineWidth(1.0);
00255   glEnable(GL_TEXTURE_2D);
00256   glEnable(GL_LIGHTING);
00257 
00258   glPopMatrix();
00259 
00260 }
00261 
00262 
00263 
00264 void RenderGL::Init(){
00265   Render::Init();
00266 
00267   int argc = 2;
00268   char *argv[] = {"Dummy1","Dummy2"};
00269 
00270   glutInit(&argc,  argv);
00271   glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
00272   glutInitWindowSize((GLint) WindowX, (GLint) WindowY);
00273   glutInitWindowPosition(200,0);
00274   MainWindow = glutCreateWindow("MSL Library   Iowa State University");
00275   
00276   glEnable(GL_DEPTH_TEST);
00277   glShadeModel(GL_SMOOTH);
00278   glEnable(GL_NORMALIZE);
00279 
00280   GLfloat light_direction []= {0.5, 0.5, -1.5}; 
00281   GLfloat light_diffuse[] = {0.6, 0.4, 0.5, 1.0};
00282   GLfloat light_specular[] = {0.3, 0.6, 0.5, 1.0};
00283   GLfloat light_ambient[] = {AmbientLight, AmbientLight, AmbientLight, 1.0};
00284   glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light_direction);
00285   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
00286   glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
00287   glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00288   glEnable(GL_LIGHTING);
00289   glEnable(GL_LIGHT0);
00290   
00291   // setting the material properties
00292   GLfloat amb_diff [] = {0.3, 0.5, 0.7, 1.0};
00293   GLfloat specular [] = { 0.8, 0.5, 0.5, 1.0 };
00294   GLfloat shininess [] = { 60.0 };
00295   glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, amb_diff); 
00296   glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
00297   glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
00298   glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
00299   glEnable(GL_COLOR_MATERIAL);
00300   
00301   glDepthFunc(GL_LEQUAL);
00302   glEnable(GL_DEPTH_TEST);
00303   glEnable(GL_TEXTURE_2D);
00304 
00305   // read the configuration file to read appropriate model file
00306   InitData();
00307 
00308   glClearColor(0.0,  0.0,  0.0,  0.0); 
00309   glViewport((GLint)0.0, (GLint)0.0, (GLsizei)WindowX, (GLsizei)WindowY);
00310   glMatrixMode( GL_PROJECTION );
00311   glLoadIdentity();
00312   gluPerspective(Fov, AspectRatio, Near, Far);
00313   glMatrixMode(GL_MODELVIEW);
00314   glLoadIdentity();
00315   gluLookAt(EyeX, EyeY, EyeZ, VpX, VpY, VpZ, VupX, VupY, VupZ);
00316 
00317   glutReshapeFunc( GlutReshape );
00318   glutMouseFunc( GlutMouse );
00319   glutMotionFunc( GlutMouseMove );
00320   glutDisplayFunc( GlutDrawEnvironment );
00321   glutIdleFunc( GlutIdleProcessing );
00322   glutKeyboardFunc( GlutKeyboard );  
00323   
00324 }
00325 
00326 
00327 void RenderGL::LoadConfig()
00328 {
00329 
00330   int i;
00331 
00332   BodyTransform = MSLVector(9 * NumberOfBody);
00333   for(i=0; i<NumberOfBody; i++) 
00334     {
00335       BodyTransform[9*i+0] = 0.0;  BodyTransform[9*i+1] = 0.0;  
00336       BodyTransform[9*i+2] = 0.0;
00337       BodyTransform[9*i+3] = 0.0;  BodyTransform[9*i+4] = 0.0;  
00338       BodyTransform[9*i+5] = 0.0;
00339       BodyTransform[9*i+6] = 1.0;  BodyTransform[9*i+7] = 1.0;  
00340       BodyTransform[9*i+8] = 1.0;
00341     }
00342   READ_OPTIONAL_PARAMETER(BodyTransform);
00343   
00344   SetBodyState(BodyTransform);
00345   
00346   EnvTransform = MSLVector(9 * NumberOfEnvObj);
00347   for(i=0; i<NumberOfEnvObj; i++)
00348     {
00349       EnvTransform[9*i+0] = 0.0;  EnvTransform[9*i+1] = 0.0;  
00350       EnvTransform[9*i+2] = 0.0;
00351       EnvTransform[9*i+3] = 0.0;  EnvTransform[9*i+4] = 0.0;  
00352       EnvTransform[9*i+5] = 0.0;
00353       EnvTransform[9*i+6] = 1.0;  EnvTransform[9*i+7] = 1.0;  
00354       EnvTransform[9*i+8] = 1.0;
00355     }
00356   READ_OPTIONAL_PARAMETER(EnvTransform);
00357 
00358   SetEnvState(EnvTransform);
00359 }
00360 
00361 
00362 void RenderGL::SetBodyState(const MSLVector& state)
00363 {
00364   int i;
00365   MSLVector vpos(3), vori(3), sca(3);
00366 
00367   if(state.dim() - NumberOfBody * 6 == 0)
00368     {
00369       for(i=0; i<NumberOfBody; i++)
00370         {
00371           vpos[0] = state[i*6];
00372           vpos[1] = state[i*6+1];
00373           vpos[2] = state[i*6+2];
00374           vori[0] = state[i*6+3];
00375           vori[1] = state[i*6+4];
00376           vori[2] = state[i*6+5];
00377           
00378           SceneBodyLib[i]->SetObjectPosition(vpos);
00379           SceneBodyLib[i]->SetObjectOrientation(vori);      
00380         }
00381     }
00382   else
00383     if(state.dim() - 9 * NumberOfBody == 0)
00384       {
00385         for(i=0; i<NumberOfBody; i++)
00386           {
00387             vpos[0] = state[i*9];
00388             vpos[1] = state[i*9+1];
00389             vpos[2] = state[i*9+2];
00390             vori[0] = state[i*9+3];
00391             vori[1] = state[i*9+4];
00392             vori[2] = state[i*9+5];
00393             sca[0] = state[i*9+6];
00394             sca[1] = state[i*9+7];
00395             sca[2] = state[i*9+8];
00396       
00397             SceneBodyLib[i]->SetObjectPosition(vpos);
00398             SceneBodyLib[i]->SetObjectOrientation(vori);     
00399             SceneBodyLib[i]->SetObjectScale(sca);   
00400           }
00401       }
00402   else
00403     cout << "BodyEnf File information error" << endl;
00404 }
00405 
00406 
00407 void RenderGL::SetEnvState(const MSLVector& state)
00408 {
00409   int i;
00410   MSLVector vpos(3), vori(3), sca(3);
00411 
00412   if(state.dim() / 9 == NumberOfEnvObj)
00413     {
00414       for(i=0; i<NumberOfEnvObj; i++)
00415         {
00416           vpos[0] = state[i*9];
00417           vpos[1] = state[i*9+1];
00418           vpos[2] = state[i*9+2];
00419           vori[0] = state[i*9+3];
00420           vori[1] = state[i*9+4];
00421           vori[2] = state[i*9+5];
00422           sca[0] = state[i*9+6];
00423           sca[1] = state[i*9+7];
00424           sca[2] = state[i*9+8];
00425           
00426           SceneEnvObjLib[i]->SetObjectPosition(vpos);
00427           SceneEnvObjLib[i]->SetObjectOrientation(vori);     
00428           SceneEnvObjLib[i]->SetObjectScale(sca);   
00429         }
00430     }
00431   else
00432     cout << "EnvTransform file information error" << endl;
00433 }
00434 
00435 
00436 
00437 void RenderGL::InitData(){
00438   list<MSLTriangle> trlist;
00439   list<MSLPolygon> plist;
00440   list<string>::iterator fname;
00441   int i;
00442   mslGLObject * tobj;
00443   MSLMatrix R(3,3);
00444 
00445   AnimationActive = false;
00446   CurrentObject = -1;
00447 
00448   CurrentKeyboard = ORI_MOVE_MODE;
00449 
00450   Fov = 45.0;      AspectRatio = 1.0;
00451   Near = 1.0;     Far = 100000.0;
00452   VupX = 0.0,      VupY = 1.0,          VupZ = 0.0;
00453 
00454   LightPosX = 1.0; LightPosY = 1.0;     LightPosZ = 3.0;
00455 
00456   ChangeRate = 4.0;
00457 
00458   AnimationTimeScaleTmp = 0.0;
00459 
00460   Orientation[0] = 0.0;   Orientation[1] = 0.0;          Orientation[2] = 0.0;
00461   Position[0] = 0.0;      Position[1] = 0.0;             Position[2] = 0.0; 
00462 
00463   // set the upperworld and lowerworld
00464   BoundingBoxMin[0] = S->LowerWorld[0];
00465   BoundingBoxMin[1] = S->LowerWorld[1];
00466   BoundingBoxMin[2] = S->LowerWorld[2];
00467 
00468   BoundingBoxMax[0] = S->UpperWorld[0];
00469   BoundingBoxMax[1] = S->UpperWorld[1];
00470   BoundingBoxMax[2] = S->UpperWorld[2];
00471 
00472   // set the eye position and view reference point accoring to the scene configuration
00473   EyeX = (BoundingBoxMax[0] + BoundingBoxMin[0])/2.0;  
00474   EyeY = (BoundingBoxMax[1] + BoundingBoxMin[1])/2.0;  
00475   EyeZ = BoundingBoxMax[2] + (BoundingBoxMax[1]-BoundingBoxMin[1])/tan(38.0/180.0*PI);
00476 
00477   VpX = (BoundingBoxMax[0] + BoundingBoxMin[0])/2.0;
00478   VpY = (BoundingBoxMax[1] + BoundingBoxMin[1])/2.0; 
00479   VpZ = (BoundingBoxMax[2] + BoundingBoxMin[2])/2.0;
00480 
00481   // set the view length
00482   ViewLength = EyeZ - VpZ;
00483 
00484   // set light position accoring to the viewer position
00485   LightPosX = EyeX;
00486   LightPosY = EyeY + (BoundingBoxMax[1] - BoundingBoxMin[1]);
00487   LightPosZ = BoundingBoxMax[2] + (BoundingBoxMax[1]-BoundingBoxMin[1])/tan(38.0/180.0*PI)/2.0;
00488 
00489   // set the view coordinate system by the viewer parameters
00490   VCoordX = VCoordY = VCoordZ = MSLVector(3);
00491   VCoordZ = S->GlobalCameraDirection.norm();
00492   normalMSLVector(S->GlobalCameraZenith, S->GlobalCameraDirection, VCoordX);
00493   normalMSLVector(VCoordZ, VCoordX, VCoordY);
00494 
00495   /*
00496   R(0, 0) = VCoordX[0];   R(1, 0) = VCoordX[1];   R(2, 0) = VCoordX[2];
00497   R(0, 1) = VCoordY[0];   R(1, 1) = VCoordY[1];   R(2, 1) = VCoordY[2];
00498   R(0, 2) = VCoordZ[0];   R(1, 2) = VCoordZ[1];   R(2, 2) = VCoordZ[2];
00499   
00500   cout << "viewer orientation matrix: " << R << endl;
00501 
00502   VRpy = irpy(R);
00503 
00504   DefVCoordX = DefVCoordY = DefVCoordZ = MSLVector(3);
00505   DefVCoordZ[0] = 0.0;  DefVCoordZ[1] = 0.0;  DefVCoordZ[2] = -1.0;
00506   DefVCoordY[0] = 0.0;  DefVCoordY[1] = 1.0;  DefVCoordY[2] = 0.0;
00507   DefVCoordX[0] = -1.0;  DefVCoordX[1] = 0.0;  DefVCoordX[2] = 0.0;
00508 
00509   R(0, 0) = DefVCoordX[0];   R(1, 0) = DefVCoordX[1];   R(2, 0) = DefVCoordX[2];
00510   R(0, 1) = DefVCoordY[0];   R(1, 1) = DefVCoordY[1];   R(2, 1) = DefVCoordY[2];
00511   R(0, 2) = DefVCoordZ[0];   R(1, 2) = DefVCoordZ[1];   R(2, 2) = DefVCoordZ[2];
00512   
00513   cout << "Default viewer orientation matrix: " << R << endl;
00514 
00515   DefVRpy = irpy(R);
00516 
00517   RpyModification = (VRpy - DefVRpy) * 180 / PI;
00518 
00519   cout << "rpy angle: " << RpyModification << endl;
00520 
00521   SCoordZ[0] = 0.0;  SCoordZ[1] = 0.0;  SCoordZ[2] = 1.0;
00522   SCoordY[0] = 0.0;  SCoordY[1] = 1.0;  SCoordY[2] = 0.0;
00523   SCoordX[0] = 1.0;  SCoordX[1] = 0.0;  SCoordX[2] = 0.0;
00524   */
00525 
00526   // EnvList was initialized by Init in Render base class
00527   EnvIndex = vector<int>(EnvList.size());
00528 
00529   i = 0;
00530   forall(fname,EnvList) {    
00531 
00532     if (fname->substr(fname->length()-3,3) == "obj")
00533       {
00534         
00535         cout << "model file name: " << *fname << endl;
00536         tobj = new mslGLObject;
00537         tobj->ReadModelFile(FilePath, *fname);
00538         AddEnvObject(tobj);
00539         EnvIndex[i] = -1;
00540       }
00541     else
00542       {
00543         std::ifstream fin((FilePath + *fname).c_str());
00544         if (S->GeomDim == 2) 
00545           {
00546             fin >> plist;
00547             trlist = PolygonsToTriangles(plist,5.0); // Defined in triangle.C
00548           }
00549         else
00550           fin >> trlist;
00551         fin.close();
00552         EnvIndex[i] = glGenLists(1);
00553         glNewList(EnvIndex[i], GL_COMPILE);
00554         InitGeometry(trlist);
00555         glEndList();
00556         trlist.clear();
00557         plist.clear();
00558       }
00559     i++;
00560   }
00561 
00562   // Bodies
00563   BodyIndex = vector<int>(BodyList.size());
00564   i = 0;
00565   forall(fname,BodyList) {
00566     if (fname->substr(fname->length()-3,3) == "obj")
00567       {
00568         cout << "model file name: " << *fname << endl;
00569         
00570         tobj = new mslGLObject;
00571         tobj->ReadModelFile(FilePath, *fname);
00572         AddBodyObject(tobj);
00573         BodyIndex[i] = -1;
00574       }
00575     else
00576       {
00577         std::ifstream fin2((FilePath + *fname).c_str());
00578         if (S->GeomDim == 2) 
00579           {
00580             fin2 >> plist;
00581             trlist = PolygonsToTriangles(plist,3.0); // Defined in 3Dtriangle.C
00582           }
00583         else
00584           fin2 >> trlist;
00585         fin2.close();
00586         BodyIndex[i] = glGenLists(1);
00587         glNewList(BodyIndex[i], GL_COMPILE);
00588         InitGeometry(trlist);
00589         glEndList();
00590         trlist.clear();
00591       }
00592     i++;
00593   }
00594 
00595   LoadConfig();
00596 
00597 }
00598 
00599 
00600 
00601 void RenderGL::InitGeometry(list<MSLTriangle> triangles){
00602   mslGLObject tobj;
00603 
00604   list<MSLTriangle>::iterator t;
00605   GLfloat d1[3],d2[3],norm[3];
00606   glPushMatrix();
00607   glBegin(GL_TRIANGLES);
00608   forall(t, triangles){
00609     d1[0] = (GLfloat)t->p2.xcoord() - (GLfloat)t->p1.xcoord();
00610     d1[1] = (GLfloat)t->p2.ycoord() - (GLfloat)t->p1.ycoord();
00611     d1[2] = (GLfloat)t->p2.zcoord() - (GLfloat)t->p1.zcoord();
00612 
00613     d2[0] = (GLfloat)t->p3.xcoord() - (GLfloat)t->p1.xcoord();
00614     d2[1] = (GLfloat)t->p3.ycoord() - (GLfloat)t->p1.ycoord();
00615     d2[2] = (GLfloat)t->p3.zcoord() - (GLfloat)t->p1.zcoord();
00616 
00617     NormCrossProduct(d1, d2, norm);
00618 
00619     glNormal3fv(norm);
00620 
00621     glVertex3f(t->p1.xcoord(), t->p1.ycoord(), t->p1.zcoord());
00622     glVertex3f(t->p2.xcoord(), t->p2.ycoord(), t->p2.zcoord());
00623     glVertex3f(t->p3.xcoord(), t->p3.ycoord(), t->p3.zcoord());
00624   }
00625   glEnd();
00626   glPopMatrix();
00627 }
00628 
00629 
00630 
00631 
00632 void RenderGL::DrawBodies(const MSLVector &x)
00633 {
00634 
00635   int i, j;
00636   MSLVector q(6),mq;
00637   MSLVector tv(3);
00638 
00639   // Use the forward kinematics to place all body configs in a big MSLVector
00640   mq = x;
00641   
00642   j = 0;
00643 
00644   for (i = 0; i < S->NumBodies; i++) {
00645     // Get the configuration
00646     q[0] = mq[i*6]; q[1] = mq[i*6+1]; q[2] = mq[i*6+2]; 
00647     q[3] = mq[i*6+3]; q[4] = mq[i*6+4]; q[5] = mq[i*6+5]; 
00648     
00649     if(BodyIndex[i] != -1)
00650       {
00651         // draw the simple model objects
00652 
00653         // Change the colors for different bodies
00654         glColor3f(RGBRed[(i+1) % RENDERCOLORS],
00655                   RGBGreen[(i+1) % RENDERCOLORS],
00656                   RGBBlue[(i+1) % RENDERCOLORS]);
00657         
00658         // Dump the gl stuff
00659         glPushMatrix();
00660 
00661         glTranslatef((GLfloat)q[0],(GLfloat)q[1],(GLfloat)q[2]);
00662         glRotatef((GLfloat)(q[5]*57.286),0,0,1.0);
00663         glRotatef((GLfloat)(q[4]*57.296),0,1.0,0);
00664         glRotatef((GLfloat)(q[3]*57.296),1.0,0,0);
00665         
00666         glCallList(BodyIndex[i]);
00667         
00668         glPopMatrix();
00669       }
00670     else
00671       {
00672         // draw complex 3d model objects
00673 
00674         // if it is in the animation process, continously change the state, 
00675         // otherwise, keep the current state
00676   
00677         if (AnimationActive)
00678           {
00679             tv[0] = q[0]; tv[1] = q[1]; tv[2] = q[2];
00680             SceneBodyLib[j]->SetObjectPosition(tv);
00681             tv[0] = q[3]*57.286; tv[1] = q[4]*57.286; tv[2] = q[5]*57.286;
00682             SceneBodyLib[j]->SetObjectOrientation(tv);
00683           }
00684 
00685         //  comment the object bounding box drawing
00686         if (BoundingBoxOn)
00687           SceneBodyLib[j]->ObjectBoundingBoxDraw();
00688         SceneBodyLib[j]->ObjectDraw();
00689         j++;
00690       }
00691   }
00692   
00693 }
00694 
00695 
00696 void RenderGL::DrawEnv(){
00697   unsigned int i, j;
00698 
00699   j = 0;
00700 
00701   for (i = 0; i < EnvIndex.size(); i++)
00702     {
00703       if(EnvIndex[i] != -1)
00704         {
00705           glPushMatrix();
00706           glColor3f(RGBRed[0],RGBGreen[0],RGBBlue[0]);  
00707           glCallList(EnvIndex[i]);
00708           glPopMatrix();
00709         }
00710       else
00711         {
00712           if (BoundingBoxOn)
00713             SceneEnvObjLib[j]->ObjectBoundingBoxDraw();
00714           SceneEnvObjLib[j]->ObjectDraw();
00715           j++;
00716         }
00717     }
00718 }
00719 
00720 
00721 void RenderGL::NormCrossProduct(float v1[3], float v2[3], float out[3])
00722 {
00723   out[0] = v1[1]*v2[2] - v1[2]*v2[1];
00724   out[1] = v1[2]*v2[0] - v1[0]*v2[2];
00725   out[2] = v1[0]*v2[1] - v1[1]*v2[0];
00726 
00727   Normalize(out);
00728 }
00729 
00730 
00731 void RenderGL::Normalize(float v[3]) 
00732 {
00733   GLfloat d = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
00734 
00735   v[0] /=d;
00736   v[1] /=d;
00737   v[2] /=d;
00738 }
00739 
00740 
00741 
00742 void RenderGL::MainLoop(Gui *g) {
00743 
00744   g->Finished = false;
00745   
00746   // Store this in the gui pointer G (for callbacks)
00747   G = g;
00748 
00749   glutMainLoop();
00750 
00751 }
00752 
00753 
00754 
00755 
00756 void RenderGL::DrawPath()
00757 { 
00758   int i, j;
00759   int BodyNum;
00760   MSLVector state1;
00761   list<MSLVector>::iterator state2;
00762 
00763   glPushMatrix();
00764   glDisable(GL_TEXTURE_2D);
00765 
00766   GLfloat Diffuse[] = {1.0, 0.0, 0.0};
00767   GLfloat Ambient[] = {1.0, 0.0, 0.0};
00768   GLfloat Specular[] = {1.0, 0.0, 0.0};
00769   GLfloat Shininess[] = {1.0, 0.0, 0.0};
00770 
00771   glDisable(GL_LIGHTING);
00772   glLineWidth(0.5);
00773 
00774   glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
00775   glMaterialfv(GL_FRONT, GL_AMBIENT, Ambient);
00776   glMaterialfv(GL_FRONT, GL_SPECULAR, Specular);
00777   glMaterialfv(GL_FRONT, GL_SHININESS, Shininess);
00778 
00779   glBegin(GL_LINES);
00780 
00781   state1 = FrameList.front();
00782   BodyNum = state1.dim() / 6;
00783   i = 0;
00784   forall(state2, FrameList)
00785     {
00786       if(i != 0 && i<NumFrames)
00787         {
00788           for(j=0; j<BodyNum; j++)
00789             {
00790               glColor3f(RGBRed[(j+1) % RENDERCOLORS],
00791                         RGBGreen[(j+1) % RENDERCOLORS],
00792                         RGBBlue[(j+1) % RENDERCOLORS]);
00793 
00794               glVertex3f(state1[6*j], state1[6*j+1], state1[6*j+2]);
00795               glVertex3f(state2->operator[](6*j), 
00796                          state2->operator[](6*j+1), 
00797                          state2->operator[](6*j+2));
00798             }
00799           state1 = *state2;
00800         }
00801       i++;
00802     }
00803 
00804   glEnd();
00805 
00806   glLineWidth(1.0);
00807 
00808   glEnable(GL_TEXTURE_2D);
00809   glEnable(GL_LIGHTING);
00810 
00811   glPopMatrix();
00812 }
00813 
00814 
00815 
00816 void RenderGL::GlutDrawEnvironment() {
00817 
00818   int k, j;
00819   MSLVector c, conf(6);
00820   float vsca;
00821   MSLVector vt1(3);
00822   MSLVector scenecenter(3);
00823 
00824   scenecenter[0] = (RGL->BoundingBoxMin[0] + RGL->BoundingBoxMax[0])/2.0;
00825   scenecenter[1] = (RGL->BoundingBoxMin[1] + RGL->BoundingBoxMax[1])/2.0;
00826   scenecenter[2] = (RGL->BoundingBoxMin[2] + RGL->BoundingBoxMax[2])/2.0;
00827  
00828   RGL->SetLightPos();
00829 
00830   // LaValle added these three lines 1/8/2001
00831   GLfloat light_ambient[] = {RGL->AmbientLight, RGL->AmbientLight, 
00832                              RGL->AmbientLight, 1.0};
00833   glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00834 
00835   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00836   
00837   // Advance the frame
00838   if (RGL->AnimationActive)
00839     RGL->SetCurrentAnimationFrame();
00840   
00841   if(RGL->AttachedCameraOn)
00842     {
00843       // get the current frameinformation
00844       list<MSLVector>::iterator fi;
00845       fi = RGL->FrameList.begin();
00846       for (k = 0; k < RGL->AnimationFrameIndex - 1; k++)
00847         fi++;
00848       c = *fi;
00849 
00850       for (j = 0; j < 6; j++)
00851         conf[j] = c[ 6 * RGL->S->AttachedCameraBody + j];
00852       
00853       // get the camera position and orientation in the scene coordinate
00854       vt1 = RGL->S->AttachedCameraPosition;
00855       vt1 = point_x_rotation(conf[3], vt1);
00856       vt1 = point_y_rotation(conf[4], vt1);
00857       vt1 = point_z_rotation(conf[5], vt1);      
00858 
00859       vt1[0] = vt1[0] + conf[0];
00860       vt1[1] = vt1[1] + conf[1];
00861       vt1[2] = vt1[2] + conf[2];
00862 
00863       // get the camera position and orientation in the global coordinate
00864       vt1[0] = vt1[0] - scenecenter[0];
00865       vt1[1] = vt1[1] - scenecenter[1];
00866       vt1[2] = vt1[2] - scenecenter[2];
00867       
00868       vt1 = point_x_rotation(RGL->Orientation[0]*(PI/180.0), vt1);
00869       vt1 = point_y_rotation(RGL->Orientation[1]*(PI/180.0), vt1);
00870       vt1 = point_z_rotation(RGL->Orientation[2]*(PI/180.0), vt1);      
00871 
00872       vt1[0] = vt1[0] + scenecenter[0];
00873       vt1[1] = vt1[1] + scenecenter[1];
00874       vt1[2] = vt1[2] + scenecenter[2];
00875            
00876       vt1[0] = vt1[0] + RGL->Position[0];
00877       vt1[1] = vt1[1] + RGL->Position[1];
00878       vt1[2] = vt1[2] + RGL->Position[2];
00879 
00880       RGL->EyeX = vt1[0];
00881       RGL->EyeY = vt1[1];
00882       RGL->EyeZ = vt1[2];
00883 
00884       vt1 = RGL->S->AttachedCameraDirection;
00885 
00886       // get the direction in the scene coordinate
00887       vt1 = point_x_rotation(conf[3], vt1);
00888       vt1 = point_y_rotation(conf[4], vt1);
00889       vt1 = point_z_rotation(conf[5], vt1);      
00890 
00891       // get the direction in the global coordinate
00892       vt1 = point_x_rotation(RGL->Orientation[0]*(PI/180.0), vt1);
00893       vt1 = point_y_rotation(RGL->Orientation[1]*(PI/180.0), vt1);
00894       vt1 = point_z_rotation(RGL->Orientation[2]*(PI/180.0), vt1);      
00895 
00896       vsca = RGL->ViewLength / vt1.length();
00897 
00898       RGL->VpX = RGL->EyeX + vt1[0]*vsca;
00899       RGL->VpY = RGL->EyeY + vt1[1]*vsca;
00900       RGL->VpZ = RGL->EyeZ + vt1[2]*vsca;
00901 
00902       vt1 = RGL->S->AttachedCameraZenith;
00903       // get the zenith in the scene coordinate
00904       vt1 = point_x_rotation(conf[3], vt1);
00905       vt1 = point_y_rotation(conf[4], vt1);
00906       vt1 = point_z_rotation(conf[5], vt1);
00907 
00908       // get the direction in the global coordinate
00909       vt1 = point_x_rotation(RGL->Orientation[0]*(PI/180.0), vt1);
00910       vt1 = point_y_rotation(RGL->Orientation[1]*(PI/180.0), vt1);
00911       vt1 = point_z_rotation(RGL->Orientation[2]*(PI/180.0), vt1);      
00912 
00913       RGL->VupX = vt1[0];
00914       RGL->VupY = vt1[1];
00915       RGL->VupZ = vt1[2];
00916 
00917       RGL->Fov = 70.0;
00918     }
00919   else
00920     {
00921       vsca = RGL->ViewLength / RGL->S->GlobalCameraDirection.length();
00922 
00923       RGL->EyeX = RGL->S->GlobalCameraPosition[0];
00924       RGL->EyeY = RGL->S->GlobalCameraPosition[1];
00925       RGL->EyeZ = RGL->S->GlobalCameraPosition[2];
00926 
00927       RGL->VpX = RGL->EyeX + RGL->S->GlobalCameraDirection[0] * vsca;
00928       RGL->VpY = RGL->EyeY + RGL->S->GlobalCameraDirection[1] * vsca;
00929       RGL->VpZ = RGL->EyeZ + RGL->S->GlobalCameraDirection[2] * vsca;
00930 
00931       RGL->VupX = RGL->S->GlobalCameraZenith[0];
00932       RGL->VupY = RGL->S->GlobalCameraZenith[1];
00933       RGL->VupZ = RGL->S->GlobalCameraZenith[2];
00934 
00935       RGL->Fov = 45.0;      
00936     }
00937 
00938   if (!RGL->MultipleViewsOn)
00939     {
00940       glViewport((GLint)0.0, (GLint)0.0, (GLsizei)RGL->WindowX, (GLsizei)RGL->WindowY);
00941       glMatrixMode(GL_PROJECTION);
00942       glLoadIdentity();
00943       gluPerspective(RGL->Fov,(GLfloat)RGL->WindowX / (GLfloat)RGL->WindowY, 
00944                      RGL->Near, RGL->Far);
00945       glMatrixMode(GL_MODELVIEW);
00946       glLoadIdentity();  
00947       
00948       gluLookAt(RGL->EyeX, RGL->EyeY, RGL->EyeZ, RGL->VpX, 
00949                 RGL->VpY, RGL->VpZ, RGL->VupX, RGL->VupY, RGL->VupZ);
00950       
00951       RGL->SceneRender();
00952     }
00953   else
00954     {
00955 
00956       glViewport((GLint)0.0, (GLint)0.0, (GLsizei)RGL->WindowX/2, (GLsizei)RGL->WindowY/2);
00957       glMatrixMode( GL_PROJECTION );
00958       glLoadIdentity();
00959       glOrtho(RGL->BoundingBoxMin[2]*1.2-scenecenter[2]*1.2,  
00960               RGL->BoundingBoxMax[2]*1.2-scenecenter[2]*1.2, 
00961               RGL->BoundingBoxMin[1]*1.2-scenecenter[1]*1.2, 
00962               RGL->BoundingBoxMax[1]*1.2-scenecenter[1]*1.2, 0.1, 100000.0);
00963       glMatrixMode(GL_MODELVIEW);
00964       glLoadIdentity();
00965       gluLookAt(scenecenter[0]-1000.0, scenecenter[1], scenecenter[2], 
00966                 scenecenter[0], scenecenter[1], scenecenter[2], 0.0, 1.0, 0.0);      
00967  
00968       RGL->SceneRender();
00969       
00970       glViewport((GLint)RGL->WindowX/2, (GLint)RGL->WindowY/2, 
00971                  (GLsizei)RGL->WindowX/2, (GLsizei)RGL->WindowY/2);
00972       glMatrixMode( GL_PROJECTION );
00973       glLoadIdentity();
00974       glOrtho(RGL->BoundingBoxMin[0]*1.2-scenecenter[0]*1.2, 
00975               RGL->BoundingBoxMax[0]*1.2-scenecenter[0]*1.2, 
00976               RGL->BoundingBoxMin[2]*1.2-scenecenter[2]*1.2, 
00977               RGL->BoundingBoxMax[2]*1.2-scenecenter[2]*1.2, 0.1, 100000.0);
00978       glMatrixMode(GL_MODELVIEW);
00979       glLoadIdentity();
00980       gluLookAt(scenecenter[0], scenecenter[1]+1000.0, scenecenter[2], 
00981                 scenecenter[0], scenecenter[1], scenecenter[2], 0.0, 0.0, -1.0);      
00982 
00983       RGL->SceneRender();
00984 
00985       glViewport((GLint)0.0, (GLint)RGL->WindowY/2, 
00986                  (GLsizei)RGL->WindowX/2, (GLsizei)RGL->WindowY/2);
00987       glMatrixMode( GL_PROJECTION );
00988       glLoadIdentity();
00989       glOrtho(RGL->BoundingBoxMin[0]*1.2-scenecenter[0]*1.2, 
00990               RGL->BoundingBoxMax[0]*1.2-scenecenter[0]*1.2, 
00991               RGL->BoundingBoxMin[1]*1.2-scenecenter[1]*1.2, 
00992               RGL->BoundingBoxMax[1]*1.2-scenecenter[1]*1.2, 0.1, 100000.0);
00993       glMatrixMode(GL_MODELVIEW);
00994       glLoadIdentity();
00995       gluLookAt(scenecenter[0], scenecenter[1], scenecenter[2]+1000.0, 
00996                 scenecenter[0], scenecenter[1], scenecenter[2], 0.0, 1.0, 0.0);      
00997 
00998       RGL->SceneRender();
00999 
01000       glViewport((GLint)RGL->WindowX/2, (GLint)0.0, 
01001                  (GLsizei)RGL->WindowX/2, (GLsizei)RGL->WindowY/2);
01002       glMatrixMode(GL_PROJECTION);
01003       glLoadIdentity();
01004       gluPerspective(RGL->Fov,(GLfloat)RGL->WindowX / (GLfloat)RGL->WindowY, 
01005                      RGL->Near, RGL->Far);
01006       glMatrixMode(GL_MODELVIEW);
01007       glLoadIdentity();  
01008       gluLookAt(RGL->EyeX, RGL->EyeY, RGL->EyeZ, RGL->VpX, 
01009                 RGL->VpY, RGL->VpZ, RGL->VupX, RGL->VupY, RGL->VupZ);
01010       
01011       RGL->SceneRender();
01012 
01013     }
01014     
01015   glutSwapBuffers();
01016 }
01017 
01018 
01019 void RenderGL::SceneRender()
01020 {
01021   MSLVector scenecenter(3);
01022 
01023   glPushMatrix();
01024 
01025   scenecenter[0] = (BoundingBoxMin[0] + BoundingBoxMax[0])/2.0;
01026   scenecenter[1] = (BoundingBoxMin[1] + BoundingBoxMax[1])/2.0;
01027   scenecenter[2] = (BoundingBoxMin[2] + BoundingBoxMax[2])/2.0;
01028 
01029   // set the position and orientation of the scene
01030   glTranslatef(Position[0], Position[1], Position[2]);
01031 
01032   // rotate around the position the viewer looks at
01033   glTranslatef(scenecenter[0], scenecenter[1], scenecenter[2]);
01034   glRotatef(Orientation[2], 0.0, 0.0, 1.0);
01035   glRotatef(Orientation[1], 0.0, 1.0, 0.0);
01036   glRotatef(Orientation[0], 1.0, 0.0, 0.0);    
01037   glTranslatef(-scenecenter[0], -scenecenter[1], -scenecenter[2]);
01038 
01039   // draw the coordinate
01040   //ShowCoordinateFrame();
01041      
01042   // Draw environment
01043   DrawEnv();
01044 
01045   DrawBodies(CurrentAnimationFrame);
01046 
01047   if (ShowPathOn)
01048     DrawPath();
01049 
01050   if (CurrentObject != -1)
01051     WhichObject(CurrentObject)->ObjectHighlight();
01052 
01053   if (BoundingBoxOn)
01054     DrawBoundingBox();
01055 
01056   glPopMatrix();
01057   
01058 }
01059 
01060 
01061 void RenderGL::SetLightPos()
01062 {
01063   GLfloat lightpos[4];
01064 
01065   lightpos[0] = LightPosX;
01066   lightpos[1] = LightPosY;
01067   lightpos[2] = LightPosZ;
01068   lightpos[3] = 1.0;
01069   
01070   glLightfv(GL_LIGHT0, GL_POSITION, lightpos);  
01071 }
01072 
01073 
01074 void RenderGL::SetSceneOrientationChange(const MSLVector& oric)
01075 {
01076   
01077   Orientation[0] = Orientation[0] + oric[0];
01078   Orientation[1] = Orientation[1] + oric[1];
01079   Orientation[2] = Orientation[2] + oric[2];
01080 
01081   for(int i=0; i<3; i++)
01082     {
01083       while(Orientation[i]>=2*PI) Orientation[i] = Orientation[i] - 360.0;
01084       while(Orientation[i]<0) Orientation[i] = Orientation[i] + 360.0;
01085     }
01086 }
01087 
01088 
01089 
01090 
01091 void RenderGL::SetScenePositionChange(const MSLVector& posc)
01092 {
01093   
01094   Position[0] = Position[0] + posc[0];
01095   Position[1] = Position[1] + posc[1];
01096   Position[2] = Position[2] + posc[2];
01097 }
01098 
01099 
01100 
01101 void RenderGL::DrawBoundingBox()
01102 {
01103   
01104   glDisable(GL_TEXTURE_2D);
01105   glDisable(GL_LIGHTING);
01106 
01107   GLfloat Diffuse[] = {1.0, 0.0, 0.0};
01108   GLfloat Ambient[] = {1.0, 0.0, 0.0};
01109   GLfloat Specular[] = {1.0, 0.0, 0.0};
01110   GLfloat Shininess[] = {1.0, 0.0, 0.0};
01111 
01112   glPushMatrix();
01113 
01114   glLineWidth(1.0);
01115 
01116   glColor4f(0.41, 0.55, 0.137, 1.0);
01117   glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
01118   glMaterialfv(GL_FRONT, GL_AMBIENT, Ambient);
01119   glMaterialfv(GL_FRONT, GL_SPECULAR, Specular);
01120   glMaterialfv(GL_FRONT, GL_SHININESS, Shininess);
01121 
01122   glBegin(GL_LINES);
01123   
01124   glNormal3f(-1.0, -1.0, 1.0);
01125   glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01126   glNormal3f(1.0, -1.0, 1.0);
01127   glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01128 
01129   glNormal3f(-1.0, -1.0, 1.0);
01130   glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01131   glNormal3f(-1.0, 1.0, 1.0);
01132   glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01133 
01134   glNormal3f(1.0, 1.0, 1.0);
01135   glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01136   glNormal3f(-1.0, 1.0, 1.0);
01137   glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01138 
01139   glNormal3f(1.0, 1.0, 1.0);
01140   glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01141   glNormal3f(1.0, -1.0, 1.0);
01142   glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01143 
01144 
01145 
01146   glNormal3f(1.0, 1.0, -1.0);
01147   glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01148   glNormal3f(-1.0, 1.0, -1.0);
01149   glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01150 
01151   glNormal3f(1.0, 1.0, -1.0);
01152   glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01153   glNormal3f(1.0, -1.0, -1.0);
01154   glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01155 
01156   glNormal3f(-1.0, -1.0, -1.0);
01157   glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01158   glNormal3f(1.0, -1.0, -1.0);
01159   glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01160 
01161   glNormal3f(-1.0, -1.0, -1.0);
01162   glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01163   glNormal3f(-1.0, 1.0, -1.0);
01164   glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01165 
01166 
01167 
01168   glNormal3f(1.0, -1.0, 1.0);
01169   glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01170   glNormal3f(1.0, -1.0, -1.0);
01171   glVertex3f(BoundingBoxMax[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01172 
01173   glNormal3f(1.0, 1.0, -1.0);
01174   glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01175   glNormal3f(1.0, 1.0, 1.0);
01176   glVertex3f(BoundingBoxMax[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01177 
01178   glNormal3f(-1.0, 1.0, 1.0);
01179   glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMin[2]);
01180   glNormal3f(-1.0, 1.0, -1.0);
01181   glVertex3f(BoundingBoxMin[0], BoundingBoxMax[1], BoundingBoxMax[2]);
01182 
01183   glNormal3f(-1.0, -1.0, 1.0);
01184   glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMin[2]);
01185   glNormal3f(-1.0, -1.0, 1.0);
01186   glVertex3f(BoundingBoxMin[0], BoundingBoxMin[1], BoundingBoxMax[2]);
01187 
01188   glEnd();
01189   
01190   glPopMatrix();
01191   
01192   glLineWidth(1.0);
01193 
01194   glEnable(GL_TEXTURE_2D);
01195   glEnable(GL_LIGHTING);
01196 }
01197 
01198 
01199 
01200 void RenderGL::GlutReshape(int w, int h)
01201 {
01202   RGL->WindowX = w;
01203   RGL->WindowY = h;
01204   RGL->WindowZ = ((float) w + (float) h)/2.0;
01205 
01206   glViewport(0,0,(GLsizei) RGL->WindowX, (GLsizei) RGL->WindowY);
01207   glMatrixMode(GL_PROJECTION);
01208   glLoadIdentity();
01209   gluPerspective(RGL->Fov,(GLfloat) RGL->WindowX / (GLfloat) RGL->WindowY, RGL->Near, RGL->Far);
01210   glMatrixMode(GL_MODELVIEW);
01211   glLoadIdentity();  
01212   gluLookAt(RGL->EyeX, RGL->EyeY, RGL->EyeZ, RGL->VpX, 
01213             RGL->VpY, RGL->VpZ, RGL->VupX, RGL->VupY, RGL->VupZ);
01214 }
01215 
01216 
01217 void RenderGL::GlutMouse(int button, int state, int x, int y)
01218 {
01219   float x1, y1;
01220 
01221   x1 = ((float)x-RGL->WindowX/2.0)*(MaxX-MinX)/RGL->WindowX;
01222   y1 = ((float)y-RGL->WindowY/2.0)*(MaxY-MinY)/RGL->WindowY*-1.0;
01223 
01224   RGL->LastX = x1;
01225   RGL->LastY = y1;
01226 
01227   RGL->CurrentMouseButton = button;
01228   RGL->CurrentMouseState = state;
01229 }
01230 
01231 
01232 void RenderGL::GlutMouseMove( int x, int y )
01233 {
01234   int i;
01235   float x1, y1;
01236   float delta_x, delta_y;
01237   MSLVector oric(3), posc(3);
01238   MSLVector sori(3);
01239   MSLMatrix mt;
01240 
01241   oric[0] = 0.0;  oric[1] = 0.0;  oric[2] = 0.0;
01242   posc[0] = 0.0;  posc[1] = 0.0;  posc[2] = 0.0;
01243 
01244   x1 = ((float)x-RGL->WindowX/2.0)*(MaxX-MinX)/RGL->WindowX;
01245   y1 = ((float)y-RGL->WindowY/2.0)*(MaxY-MinY)/RGL->WindowY*-1.0;
01246 
01247   delta_x = (x1 - RGL->LastX);
01248   delta_y = (y1 - RGL->LastY);
01249 
01250   if(RGL->CurrentKeyboard == ORI_MOVE_MODE)
01251     {
01252       // change the position and orientation of the whole scene
01253       
01254       // change the orientation around y and z axis
01255       if(RGL->CurrentMouseButton==GLUT_LEFT_BUTTON && RGL->CurrentMouseState==GLUT_DOWN)
01256         {
01257 
01258           sori[0] = RGL->Orientation[0];
01259           sori[1] = RGL->Orientation[1];
01260           sori[2] = RGL->Orientation[2];
01261 
01262           mt = free_rotate(RGL->VCoordY, rpy(sori*(PI/180.0)), 
01263                            delta_x / (MaxX - MinX) * PI / 5.0);
01264 
01265 
01266           mt = free_rotate(RGL->VCoordX, mt, 
01267                            delta_y / (MaxY - MinY) * PI / 5.0);
01268           
01269           oric = irpy(mt)*180.0/PI - sori;
01270           RGL->SetSceneOrientationChange(oric);
01271           
01272         }
01273 
01274       // change the position along the x and y axis
01275       if(RGL->CurrentMouseButton==GLUT_MIDDLE_BUTTON && RGL->CurrentMouseState==GLUT_DOWN)
01276         {
01277           posc = -delta_x * RGL->ChangeRate * RGL->VCoordX + 
01278             delta_y * RGL->ChangeRate * RGL->VCoordY;
01279 
01280           RGL->SetScenePositionChange(posc*0.1);
01281         }
01282 
01283       // change the orientation around x axis and position along the z axis
01284       if(RGL->CurrentMouseButton==GLUT_RIGHT_BUTTON && RGL->CurrentMouseState==GLUT_DOWN)
01285         {
01286           sori[0] = RGL->Orientation[0];
01287           sori[1] = RGL->Orientation[1];
01288           sori[2] = RGL->Orientation[2];
01289 
01290           mt = free_rotate(RGL->VCoordZ, rpy(sori*(PI/180.0)), 
01291                            delta_x / (MaxX - MinX) * PI / 5.0);
01292 
01293           oric = irpy(mt)*180.0/PI - sori;
01294           RGL->SetSceneOrientationChange(oric);
01295 
01296           posc = 5.0 * delta_y / (MaxY - MinY) * PI * RGL->ChangeRate * RGL->VCoordZ;
01297           RGL->SetScenePositionChange(posc);
01298         }
01299     }
01300   else
01301     {
01302       // change the scale, orientation and position of the current object
01303 
01304       // change the orientation around y and z axis 
01305       if(RGL->CurrentMouseButton==GLUT_LEFT_BUTTON && RGL->CurrentMouseState==GLUT_DOWN)
01306         {
01307           if(RGL->CurrentObject!=-1)
01308             {
01309               oric[0] = 0.0;
01310               oric[1] = delta_x / (MaxX - MinX) * PI * RGL->ChangeRate; 
01311               oric[2] = delta_y / (MaxY - MinY) * PI * RGL->ChangeRate;
01312               
01313               RGL->WhichObject(RGL->CurrentObject)->SetBodyOrientationChange(oric);
01314             }
01315         }
01316 
01317       // change the position along x and y
01318       if(RGL->CurrentMouseButton==GLUT_MIDDLE_BUTTON && RGL->CurrentMouseState==GLUT_DOWN)
01319         {
01320           if(RGL->CurrentObject!=-1)
01321             {
01322               RGL->WhichObject(RGL->CurrentObject)->Position[0] =
01323                 RGL->WhichObject(RGL->CurrentObject)->Position[0] + delta_x * RGL->ChangeRate;
01324               RGL->WhichObject(RGL->CurrentObject)->Position[1] = 
01325                 RGL->WhichObject(RGL->CurrentObject)->Position[1] + delta_y * RGL->ChangeRate;
01326             }
01327         }
01328       
01329       // change the orientation around x or scale of the object or position along z
01330       if(RGL->CurrentMouseButton==GLUT_RIGHT_BUTTON && RGL->CurrentMouseState==GLUT_DOWN)
01331         {
01332           if(RGL->CurrentObject!=-1)
01333             {
01334               oric[0] = delta_x / (MaxX - MinX) * PI * RGL->ChangeRate;
01335               oric[1] = 0.0;             oric[2] = 0.0;
01336               RGL->WhichObject(RGL->CurrentObject)->SetBodyOrientationChange(oric);
01337 
01338               if(RGL->CurrentKeyboard == SCALE_MOVE_MODE)
01339                 {
01340                   for(i=0; i<3; i++)
01341                     {
01342                       RGL->WhichObject(RGL->CurrentObject)->Scale[i] = 
01343                         RGL->WhichObject(RGL->CurrentObject)->Scale[i] * 
01344                         (1+delta_y/(MaxY - MinY));
01345                     }
01346                 }
01347               else
01348                 {
01349                   RGL->WhichObject(RGL->CurrentObject)->Position[2] = 
01350                     RGL->WhichObject(RGL->CurrentObject)->Position[2] + delta_y * RGL->ChangeRate;
01351                 }
01352             }
01353         }
01354     }
01355 
01356   RGL->LastX = x1;
01357   RGL->LastY = y1;
01358 
01359   glutPostRedisplay();
01360 }
01361 
01362 
01363 
01364 void RenderGL::GlutIdleProcessing()
01365 {
01366   int i;
01367 
01368   // Handle the window events for the Gui
01369   for (i = 0; i < 20; i++) // This is a hack!!!
01370     RGL->G->HandleEvents();
01371 
01372   RGL->HandleEvents();
01373 
01374   // Allow exiting by pressing Exit button in Gui
01375   if (RGL->G->Finished)
01376     exit(-1);
01377 
01378   if ( glutGetWindow() != RGL->MainWindow ) 
01379     glutSetWindow(RGL->MainWindow);  
01380 
01381   glutPostRedisplay();
01382 
01383 }
01384 
01385 
01386 void RenderGL::GlutKeyboard(unsigned char Key, int x, int y)
01387 {
01388   switch(Key) {
01389   case 'a':
01390     RGL->ButtonHandle(74);
01391     break;
01392   case 'b':
01393     RGL->BoundingBoxOn = !(RGL->BoundingBoxOn);
01394     break;
01395   case 'd':
01396     RGL->ButtonHandle(73);
01397     break;
01398   case 'h':
01399     cout << endl;
01400     cout << "HELP FOR RENDERGL KEYBOARD CONTROL:" << endl;
01401     cout << "'a': Accelerate Animation Rate" << endl;
01402     cout << "'b': Show and Hide the Bounding Box" << endl;
01403     cout << "'d': Decelerate Animation Rate" << endl;
01404     cout << "'h': Show the Keyboard Control Help" << endl;
01405     cout << "'m': Change to Multiview Mode" << endl;
01406     cout << "'p': Pause or Continue the Animation" << endl;
01407     cout << "'q': Quit" << endl;
01408     cout << "'r': Reset the Rendering Options\n" << endl;
01409     break;
01410   case 'm':
01411     RGL->MultipleViewsOn = !(RGL->MultipleViewsOn);
01412     break;
01413   case 'p':
01414     RGL->ButtonHandle(72);
01415     break;
01416   case 'q':
01417     exit(0);
01418     break;
01419   case 'r':
01420     RGL->ButtonHandle(75);
01421     break;
01422   case 'v':
01423     RGL->AttachedCameraOn = !(RGL->AttachedCameraOn);
01424     break;
01425   };
01426   
01427   glutPostRedisplay();   
01428 }
01429 
01430 
01431 
01432 
01433 
01434 
01435 
01436 
Motion Strategy Library


Web page maintained by Steve LaValle
Partial support provided by NSF CAREER Award IRI-970228 (LaValle), Honda Research, and Iowa State University.
Contributors: Anna Atramentov, Peng Cheng, James Kuffner, Steve LaValle, and Libo Yang.