Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

vpscene.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 // ChangeLog
00006 // Jun 07, 2005 - Bruno de Oliveira Schneider
00007 // - DrawOGL now checks for existence of a current camera.
00008 // - Added description attribute and related Get/Set methods.
00009 // Dec 15, 2004 - Bruno de Oliveira Schneider
00010 // - DrawOGL is now virtual, allowing use of derived classes in the viewers.
00011 // - The destructor now deletes objects and lights marked as "auto delete".
00012 // - Added SetAllCamerasAspectRatio, ComputeBoundingBox and GetBoundingBox.
00013 // - Removed lines of old commented coded to control scene changes.
00014 // - AddLight(const VPLight&) now makes a copy of its parameter.
00015 // - Added GetNumLights and MakeCameraViewAll.
00016 // Oct 04, 2004 - Bruno de Oliveira Schneider
00017 // - Code typing has been cleaned. DoxyGen documentation started. Indentation fixed.
00018 // - Moved "using namespace std" from header file to implementation file.
00019 // - Removed "vp" prefix from every method name.
00020 // - Changed the prototypes of GetLight, GetCamera and GetObject to avoid passing
00021 //   a string by copy.
00022 // - Added "AddLight(const VPLight&)" method because it was not possible to add a
00023 //   constant light to a scene. Changed the lists "lights" and "objects" so
00024 //   that they now keep const pointers.
00025 // - Changed cameras, lights and objects to "protected" because derived classes need
00026 //   access to them in order to call draw routines.
00027 // - Added "DrawOGL" method.
00028 // - Added "currentCamera" atribute. Added "GetCurrentCamera", "UseNextCamera"
00029 //   and "UsePreviousCamera" methods.
00030 // - Changed the type of objects from VPGraphicObj to VPSceneNode.
00031 // - Removed methods and attributes to mark changed children.
00032 // January 04, 2001 - Isabel Harb Manssour
00033 // - Addition of new methods, constants and the "typeOfChange" attribute.
00034 // - New methods implementation.
00035 // September 27, 2000 - Anderson Maciel
00036 // - New methods and attributes addition.
00037 // June 22, 2000 - Anderson Maciel
00038 // - Methods implementation.
00039 // June 08, 2000 - Luciana Porcher Nedel
00040 // - Class declaration.
00041 // May 30, 2000 - Isabel Harb Manssour
00042 // - Class declaration.
00043 
00044 #include "vpscene.h"
00045 #include "vpgraphicobj.h"
00046 #include <cassert>
00047 
00048 #ifdef VP_OGL
00049 #include <GL/gl.h>
00050 #endif
00051 
00052 using namespace std;
00053 
00054 VPScene::VPScene() {
00055     background = VPColor::BLACK();
00056     currentCamera = cameras.end();
00057 }
00058 
00059 VPScene::~VPScene() {
00060     list<const VPSceneNode*>::const_iterator objItr;
00061     list<const VPLight*>::const_iterator lightItr;
00062     //~ cout << "VPScene::~VPScene()" << endl;
00063     for (objItr = objects.begin(); objItr != objects.end(); ++objItr)
00064     {
00065         (*objItr)->AutoDeleteChildren();
00066         if ((*objItr)->autoDelete)
00067             delete (*objItr);
00068     }
00069     for (lightItr = lights.begin(); lightItr != lights.end(); ++lightItr)
00070     {
00071         if ((*lightItr)->autoDelete)
00072             delete (*lightItr);
00073     }
00074 }
00075 
00076 list<const VPLight*> VPScene::GetLights() {
00077     return lights;
00078 }
00079 
00080 list<VPCamera*> VPScene::GetCameras() {
00081     return cameras;
00082 }
00083 
00084 list<const VPSceneNode*> VPScene::GetObjects() {
00085     return objects;
00086 }
00087 
00088 void VPScene::AddLight(VPLight* newLight) {
00089     lights.push_back(newLight);
00090 }
00091 
00092 void VPScene::AddLight(const VPLight& newLight) {
00093     VPLight* lightPtr = new VPLight(newLight);
00094     lightPtr->autoDelete = true;
00095     lights.push_back(lightPtr);
00096 }
00097 
00098 void VPScene::AddCamera(VPCamera* newCamera) {
00099     cameras.push_back( newCamera );
00100     if (currentCamera == cameras.end())
00101         currentCamera = cameras.begin();
00102 }
00103 
00104 void VPScene::AddObject( VPSceneNode* newObjectPtr ) {
00105     objects.push_back( newObjectPtr );
00106 }
00107 
00108 const VPLight* VPScene::GetLight(const string& lightName) {
00109     list<const VPLight*>::const_iterator iter;
00110     for (iter = lights.begin(); iter != lights.end(); ++iter) {
00111         if( (*iter)->GetDescription() == lightName ) {
00112             return (*iter);
00113         }
00114     }
00115     return NULL;
00116 }
00117 
00118 const VPCamera* VPScene::GetCamera(const string& cameraName) {
00119     list<VPCamera*>::const_iterator iter;
00120     for (iter = cameras.begin(); iter != cameras.end(); ++iter) {
00121         if( (*iter)->GetDescription() == cameraName ) {
00122             return (*iter);
00123         }
00124     }
00125     return NULL;
00126 }
00127 
00128 const VPSceneNode* VPScene::GetObject(const string& objectName) {
00129     list<const VPSceneNode*>::const_iterator iter;
00130     for (iter = objects.begin(); iter != objects.end(); ++iter) {
00131         if( (*iter)->GetDescription() == objectName ) {
00132             return (*iter);
00133         }
00134     }
00135     return NULL;
00136 }
00137 
00138 VPColor VPScene::GetBackgroundColor() {
00139     return background;
00140 }
00141 
00142 void VPScene::SetBackgroundColor(VPColor color) {
00143     background = color;
00144 }
00145 
00146 bool VPScene::DrawOGL() const {
00147 #ifdef VP_OGL
00148     assert(*currentCamera); // make sure there is a current camera
00149     static float bgColor[4]; // static for better performance
00150     background.GetScaled(1.0f, bgColor); // convert color components to float
00151     glClearColor(bgColor[0], bgColor[1], bgColor[2], bgColor[3]);
00152     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00153     // Lights need not be drawn every rendering cicle. They are should be drawn
00154     // by a different method.
00155     DrawLightsOGL();
00156 
00157     // LookAT
00158     (*currentCamera)->DrawOGL();
00159 
00160     // Draw graphical objects
00161     list<const VPSceneNode*>::const_iterator iter;
00162     for (iter = objects.begin(); iter != objects.end(); ++iter)
00163     {
00164         (*iter)->DrawOGL();
00165     }
00166     return true;
00167 #else
00168     return false;
00169 #endif
00170 }
00171 
00172 bool VPScene::DrawLightsOGL() const {
00173 #ifdef VP_OGL
00174     unsigned int lightID = 0;
00175     list<const VPLight*>::const_iterator iter;
00176     for (iter = lights.begin(); iter != lights.end(); ++iter)
00177     {
00178         (*iter)->DrawOGL(lightID);
00179         ++lightID;
00180     }
00181     return true;
00182 #else
00183     return false;
00184 #endif
00185 }
00186 
00187 VPCamera* VPScene::GetCurrentCamera() const {
00188     if (currentCamera == cameras.end())
00189         return NULL;
00190     else
00191         return *currentCamera;
00192 }
00193 
00194 void VPScene::UseNextCamera() {
00195     ++currentCamera;
00196     if (currentCamera == cameras.end())
00197         currentCamera = cameras.begin();
00198 }
00199 
00200 void VPScene::UsePreviousCamera() {
00201     --currentCamera;
00202     if (currentCamera == cameras.end())
00203         --currentCamera;
00204 }
00205 
00206 void VPScene::SetAllCamerasAspectRatio(float newAspectRatio) {
00207     list<VPCamera*>::const_iterator iter;
00208     for (iter = cameras.begin(); iter != cameras.end(); ++iter) {
00209         (*iter)->SetAspectRatio(newAspectRatio);
00210     }
00211 }
00212 
00213 void VPScene::ComputeBoundingBox() {
00214     list<const VPSceneNode*>::const_iterator iter = objects.begin();
00215     const VPGraphicObj* objPtr;
00216     bool notInitialized = true;
00217 
00218     // Initialize the bounding box
00219     for (; (iter != objects.end()) && notInitialized; ++iter) {
00220         objPtr = dynamic_cast<const VPGraphicObj*>(*iter);
00221         if (objPtr) // objPtr != NULL
00222         {
00223             bbox = objPtr->GetRecursiveBoundingBox();
00224             notInitialized = false;
00225         }
00226     }
00227 
00228     // Merge the bounding box with other graphical objects
00229     for (; iter != objects.end(); ++iter) {
00230         objPtr = dynamic_cast<const VPGraphicObj*>(*iter);
00231         if (objPtr) // objPtr != NULL
00232             bbox.MergeWith(objPtr->GetRecursiveBoundingBox());
00233     }
00234 }
00235 
00236 void VPScene::MakeCameraViewAll()
00237 {
00238     assert(cameras.size() > 0);
00239     VPPoint4D bboxCenter;
00240     double distance, cameraDistance;
00241 
00242     bbox.ProcessCenter();
00243     bboxCenter = bbox.GetCenter();
00244     (*currentCamera)->SetTarget(bboxCenter);
00245     cameraDistance = bbox.GetGreaterX()-bbox.GetSmallerX();
00246     distance = bbox.GetGreaterY()-bbox.GetSmallerY();
00247     if (distance > cameraDistance)
00248         cameraDistance = distance;
00249     distance = bbox.GetGreaterZ()-bbox.GetSmallerZ();
00250     if (distance > cameraDistance)
00251         cameraDistance = distance;
00252 
00253     (*currentCamera)->SetLocation(bboxCenter+VPPoint4D(0,0,cameraDistance*1.2,0));
00254     (*currentCamera)->SetUp(VPPoint4D::Y());
00255     // FixMe: Not sure what to do with near plane distance
00256     //~ (*currentCamera)->SetNearPlaneDistance();
00257     (*currentCamera)->SetFarPlaneDistance(cameraDistance*2.4);
00258 }

Generated on Tue Sep 6 10:00:06 2005 for VPAT by  doxygen 1.4.4