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

rendergl.C

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