Scene.cpp

00001 #include "Scene.h"
00002 
00003 #include "SimpleArrow.h"
00004 #include "TerminalObject.h"
00005 
00006 
00007 Scene::Scene ()
00008 {
00009         // init members
00010         camera = new Camera ();
00011         first_object = NULL;
00012         first_light = NULL;
00013         current_light_id = GL_LIGHT0;
00014 
00015         // create visualization for the camera
00016         RawObject * arrow = new SimpleArrow (4.0f);
00017         arrow->SetColor (0.5f, 0.5f, 1.0f, 1.0f);
00018         camera_object = new TerminalObject (arrow);
00019 }
00020 
00021 Scene::~Scene ()
00022 {
00023         // do nothing   
00024 }
00025 
00026 void Scene::Render (int time, int culling_mode, bool level_of_detail)
00027 {
00028         GraphicalObjectElem * current_object;
00029 
00030         glPushMatrix ();
00031         glLoadIdentity ();
00032 
00033         // set up camera
00034         camera->ApplyAttributes ();
00035         camera->ApplyPosition (time);
00036         camera->ComputeInverseView (time);
00037         
00038         // could be done only when attributes are applied (since the planes do not depend on position).
00039         camera->BuildPlanes2 ();
00040 
00041         // apply lights
00042         ApplyLights (time);
00043 
00044         // draw all objects that belong to the scene
00045         current_object = first_object;
00046         while (current_object != NULL)
00047         {
00048                 current_object->graphical_object->Draw (time, camera, NULL, false, culling_mode, level_of_detail);
00049                 current_object = current_object->next;
00050         }
00051 
00052         glPopMatrix ();
00053 }
00054 
00055 void Scene::Monitor (int time, Camera * monitor, int culling_mode, bool level_of_detail)
00056 {
00057         GraphicalObjectElem * current_object;
00058 
00059         glPushMatrix ();
00060         glLoadIdentity ();
00061 
00062         // set up camera
00063         monitor->ApplyAttributes ();
00064         monitor->ApplyPosition (time);
00065         monitor->ComputeInverseView (time);
00066 
00067         // build planes each time anew (since they change with the position).
00068         camera->BuildPlanes (time);
00069 
00070         // apply the lights
00071         ApplyLights (time);
00072 
00073         // draw all objects that belong to the scene
00074         current_object = first_object;
00075         while (current_object != NULL)
00076         {
00077                 // objects appear as if the would be rendered by the original camera
00078                 current_object->graphical_object->Draw (time, camera, monitor, true, culling_mode, level_of_detail);
00079                 current_object = current_object->next;
00080         }
00081 
00082         // finally, also draw the camera
00083         camera_object->position = camera->position;
00084         camera_object->Draw (time, monitor, NULL, false, C_NONE, false);        
00085 
00086         // and its frustum (this can done in world coordinates)
00087         camera->BuildPlanes (time);
00088         camera->DrawFrustum ();
00089 
00090         glPopMatrix ();
00091 }
00092 
00093 void Scene::CopyPosition (int time, Camera * destination)
00094 {
00095         Position position;
00096 
00097         // copy a constant position...
00098         position = camera->position->GetPosition (time);
00099         destination->position->SetConstantPosition (position.x, position.y, position.z, position.roll, position.pitch, position.yaw);
00100 }
00101 
00102 void Scene::AddObject (GraphicalObject * obj)
00103 {
00104         GraphicalObjectElem * current_object;
00105 
00106         if (first_object == NULL)
00107         {
00108                 first_object = new GraphicalObjectElem;
00109                 first_object->next = NULL;
00110                 first_object->graphical_object = obj;
00111         }
00112         else
00113         {
00114                 current_object = first_object;
00115                 while (current_object->next != NULL) current_object = current_object->next;
00116 
00117                 current_object->next = new GraphicalObjectElem;
00118                 current_object->next->next = NULL;
00119                 current_object->next->graphical_object = obj;
00120         }
00121 }
00122 
00123 void Scene::AddLight (Light * light)
00124 {
00125         LightElem * current_light;
00126         int light_id;
00127 
00128         light_id = current_light_id;
00129         NextLightId ();
00130         
00131         if (first_light == NULL)
00132         {
00133                 first_light = new LightElem;
00134                 first_light->next = NULL;
00135                 first_light->light = light;
00136         }
00137         else
00138         {
00139                 current_light = first_light;
00140                 while (current_light->next != NULL) current_light = current_light->next;
00141 
00142                 current_light->next = new LightElem;
00143                 current_light = current_light->next;
00144                 current_light->next = NULL;
00145                 current_light->light = light;
00146         }
00147 
00148         // Bind to OpenGL
00149         light->Bind (light_id);
00150 }
00151 
00152 void Scene::ApplyLights (int time)
00153 {
00154         LightElem * current_light;
00155 
00156         TurnOffLights ();
00157 
00158         current_light = first_light;
00159         while (current_light != NULL)
00160         {
00161                 current_light->light->Apply (time);
00162                 current_light = current_light->next;
00163         }
00164 }
00165 
00166 void Scene::NextLightId ()
00167 {
00168         // get next OpenGL id
00169         switch (current_light_id)
00170         {
00171                 case    GL_LIGHT0:      current_light_id = GL_LIGHT1;
00172                                                         break;
00173                 case    GL_LIGHT1:      current_light_id = GL_LIGHT2;
00174                                                         break;
00175                 case    GL_LIGHT2:      current_light_id = GL_LIGHT3;
00176                                                         break;
00177                 case    GL_LIGHT3:      current_light_id = GL_LIGHT4;
00178                                                         break;
00179                 case    GL_LIGHT4:      current_light_id = GL_LIGHT5;
00180                                                         break;
00181                 case    GL_LIGHT5:      current_light_id = GL_LIGHT6;
00182                                                         break;
00183                 case    GL_LIGHT6:      current_light_id = GL_LIGHT7;
00184                                                         break;
00185                 case    GL_LIGHT7:      current_light_id = GL_LIGHT7;
00186                                                         break;
00187         }
00188 }
00189 
00190 void Scene::TurnOffLights ()
00191 {
00192         switch (current_light_id)
00193         {
00194                 case GL_LIGHT0: glDisable (GL_LIGHT0);
00195                 case GL_LIGHT1: glDisable (GL_LIGHT1);
00196                 case GL_LIGHT2: glDisable (GL_LIGHT2);
00197                 case GL_LIGHT3: glDisable (GL_LIGHT3);
00198                 case GL_LIGHT4: glDisable (GL_LIGHT4);
00199                 case GL_LIGHT5: glDisable (GL_LIGHT5);
00200                 case GL_LIGHT6: glDisable (GL_LIGHT6);
00201                                                 break;
00202                 default:                glDisable (GL_LIGHT0);
00203                                                 glDisable (GL_LIGHT1);
00204                                                 glDisable (GL_LIGHT2);
00205                                                 glDisable (GL_LIGHT3);
00206                                                 glDisable (GL_LIGHT4);
00207                                                 glDisable (GL_LIGHT5);
00208                                                 glDisable (GL_LIGHT6);
00209                                                 glDisable (GL_LIGHT7);
00210         }
00211 }
00212 
00213 void Scene::PrintToConsole ()
00214 {
00215         GraphicalObjectElem * current_elem;
00216         double mod[16];
00217         double inv[16];
00218 
00219         printf ("----------- Scene Information --------------\n");
00220 
00221         current_elem = first_object;
00222         while (current_elem != NULL)
00223         {
00224                 current_elem->graphical_object->PrintToConsole ();
00225                 current_elem = current_elem->next;
00226         }
00227 
00228         if (camera != NULL)
00229         {
00230                 glPushMatrix ();
00231                 glLoadIdentity ();
00232                         camera->ApplyPosition (0);
00233                         glGetDoublev (GL_MODELVIEW_MATRIX, mod);
00234                         printf ("ModelView Matrix:\n[%g, %g, %g, %g,\n %g, %g, %g, %g,\n %g, %g, %g, %g,\n %g, %g, %g, %g]\n",
00235                                          mod[0], mod[4], mod[8], mod[12], 
00236                                          mod[1], mod[5], mod[9], mod[13],
00237                                          mod[2], mod[6], mod[10], mod[14],
00238                                          mod[3], mod[7], mod[11], mod[15]);
00239 
00240                         Geometry::InvertHomogenousMatrix (mod, inv);
00241                         printf ("Inverse Matrix:\n[%g, %g, %g, %g,\n %g, %g, %g, %g,\n %g, %g, %g, %g,\n %g, %g, %g, %g]\n",
00242                                          inv[0], inv[4], inv[8], inv[12], 
00243                                          inv[1], inv[5], inv[9], inv[13],
00244                                          inv[2], inv[6], inv[10], inv[14],
00245                                          inv[3], inv[7], inv[11], inv[15]);
00246 
00247                 glPopMatrix ();
00248         }
00249 
00250         printf ("-------- End Of Scene Information ----------\n");
00251 }

Generated on Sun Jul 2 13:20:39 2006 for Demo by  doxygen 1.4.6-NO