lod.cpp

This is an example of how to use the CIvfLOD/CIvfSwitch classes.

00001 // ------------------------------------------------------------
00002 //
00003 // Ivf++ LOD/Switch example
00004 //
00005 // ------------------------------------------------------------
00006 //
00007 // Author: Jonas Lindemann
00008 //
00009 
00010 // ------------------------------------------------------------
00011 // Include files
00012 // ------------------------------------------------------------
00013 
00014 #include <ivfui/IvfApplication.h>
00015 #include <ivfui/IvfWindow.h>
00016 
00017 #include <ivf/IvfCamera.h>
00018 #include <ivf/IvfAxis.h>
00019 #include <ivf/IvfComposite.h>
00020 #include <ivf/IvfTransform.h>
00021 #include <ivf/IvfLighting.h>
00022 #include <ivf/IvfLight.h>
00023 #include <ivf/IvfMaterial.h>
00024 #include <ivf/IvfSphere.h>
00025 #include <ivf/IvfLOD.h>
00026 #include <ivf/IvfCube.h>
00027 #include <ivf/IvfCylinder.h>
00028 
00029 using namespace std;
00030 
00031 // ------------------------------------------------------------
00032 // Window class definition
00033 // ------------------------------------------------------------
00034 
00035 IvfSmartPointer(CExampleWindow);
00036 
00037 class CExampleWindow: public CIvfWindow {
00038 private:
00039 
00040         // Camera movement state variables
00041 
00042         double m_angleX;
00043         double m_angleY;
00044         double m_moveX;
00045         double m_moveY;
00046         double m_zoomX;
00047         double m_zoomY;
00048 
00049         int m_beginX;
00050         int m_beginY;
00051 
00052         // Ivf++ object declarations
00053 
00054         CIvfCameraPtr           m_camera;
00055         CIvfLightPtr            m_light;
00056         CIvfCompositePtr        m_scene;
00057 
00058         CIvfLODPtr                      m_lod1;
00059         CIvfLODPtr                      m_lod2;
00060         CIvfLODPtr                      m_lod3;
00061         CIvfLODPtr                      m_lod4;
00062         CIvfSwitchPtr           m_switch;
00063 
00064         double m_lodNear;
00065         double m_lodFar;
00066 
00067         void updateLOD();
00068 public:
00069         CExampleWindow(int X, int Y, int W, int H)
00070                 :CIvfWindow(X, Y, W, H) {};
00071 
00072         virtual void onInit(int width, int height);
00073         virtual void onResize(int width, int height);
00074         virtual void onRender();
00075         virtual void onMouseDown(int x, int y);
00076         virtual void onMouseMove(int x, int y);
00077         virtual void onMouseUp(int x, int y);
00078         virtual void onKeyboard(int key, int x, int y);
00079 };
00080 
00081 // ------------------------------------------------------------
00082 // Window class implementation
00083 // ------------------------------------------------------------
00084 
00085 void CExampleWindow::onInit(int width, int height)
00086 {
00087         // Initialize variables
00088 
00089         m_angleX = 0.0;
00090         m_angleY = 0.0;
00091         m_moveX = 0.0;
00092         m_moveY = 0.0;
00093         m_zoomX = 0.0;
00094         m_zoomY = 0.0;
00095 
00096         // Initialise LOD limits
00097 
00098         m_lodNear = 2.0;
00099         m_lodFar = 20.0;
00100 
00101         // Initialize Ivf++ camera
00102 
00103         m_camera = new CIvfCamera();
00104         m_camera->setPosition(0.0, 5.0, 20.0);
00105 
00106         // Create a materials
00107 
00108         CIvfMaterialPtr material = new CIvfMaterial();
00109         material->setDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f);
00110         material->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
00111         material->setAmbientColor(0.0f, 0.5f, 0.0f, 1.0f);
00112 
00113         // Create scene
00114 
00115         m_scene = new CIvfComposite();
00116 
00117         // Create four spheres with different complexity
00118 
00119         CIvfSpherePtr sphereDetailed = new CIvfSphere();
00120         sphereDetailed->setSlices(12);
00121         sphereDetailed->setStacks(12);
00122         
00123         CIvfSpherePtr sphereNormal = new CIvfSphere();
00124         sphereNormal->setSlices(10);
00125         sphereNormal->setStacks(8);
00126 
00127         CIvfSpherePtr sphereSmall = new CIvfSphere();
00128         sphereSmall->setSlices(8);
00129         sphereSmall->setStacks(6);
00130         
00131         CIvfSpherePtr sphereTiny = new CIvfSphere();
00132         sphereTiny->setSlices(6);
00133         sphereTiny->setStacks(4);
00134 
00135         // Create LOD objects
00136 
00137         m_lod1 = new CIvfLOD();
00138         m_lod1->addChild(sphereDetailed);
00139         m_lod1->addChild(sphereNormal);
00140         m_lod1->addChild(sphereSmall);
00141         m_lod1->addChild(sphereTiny);
00142         m_lod1->setPosition(-5.0, 0.0, -5.0);
00143         m_lod1->setMaterial(material);
00144         m_lod1->setCamera(m_camera);
00145         m_lod1->setLimits(m_lodNear, m_lodFar);
00146         m_scene->addChild(m_lod1);
00147         
00148         m_lod2 = new CIvfLOD();
00149         m_lod2->addChild(sphereDetailed);
00150         m_lod2->addChild(sphereNormal);
00151         m_lod2->addChild(sphereSmall);
00152         m_lod2->addChild(sphereTiny);
00153         m_lod2->setPosition(5.0, 0.0, -5.0);
00154         m_lod2->setMaterial(material);
00155         m_lod2->setCamera(m_camera);
00156         m_lod2->setLimits(m_lodNear, m_lodFar);
00157         m_scene->addChild(m_lod2);
00158 
00159         m_lod3 = new CIvfLOD();
00160         m_lod3->addChild(sphereDetailed);
00161         m_lod3->addChild(sphereNormal);
00162         m_lod3->addChild(sphereSmall);
00163         m_lod3->addChild(sphereTiny);
00164         m_lod3->setPosition(-5.0, 0.0, 5.0);
00165         m_lod3->setMaterial(material);
00166         m_lod3->setCamera(m_camera);
00167         m_lod3->setLimits(m_lodNear, m_lodFar);
00168         m_scene->addChild(m_lod3);
00169 
00170         m_lod4 = new CIvfLOD();
00171         m_lod4->addChild(sphereDetailed);
00172         m_lod4->addChild(sphereNormal);
00173         m_lod4->addChild(sphereSmall);
00174         m_lod4->addChild(sphereTiny);
00175         m_lod4->setPosition(5.0, 0.0, 5.0);
00176         m_lod4->setCamera(m_camera);
00177         m_lod4->setMaterial(material);
00178         m_lod4->setLimits(m_lodNear, m_lodFar);
00179         m_scene->addChild(m_lod4);
00180 
00181         // Create a switch object
00182 
00183         CIvfCubePtr cube = new CIvfCube();
00184         cube->setMaterial(material);
00185         CIvfCylinderPtr cylinder = new CIvfCylinder();
00186         cylinder->setMaterial(material);
00187         
00188         m_switch = new CIvfSwitch();
00189         m_switch->addChild(cube);
00190         m_switch->addChild(cylinder);
00191 
00192         m_scene->addChild(m_switch);
00193         
00194         // Create a light
00195 
00196         CIvfLightingPtr lighting = CIvfLighting::getInstance();
00197         
00198         m_light = lighting->getLight(0);
00199         m_light->setLightPosition(1.0, 1.0, 1.0, 0.0);
00200         m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 
00201         m_light->enable();
00202 }
00203 
00204 // ------------------------------------------------------------
00205 void CExampleWindow::onResize(int width, int height)
00206 {
00207         m_camera->setPerspective(45.0, 0.1, 100.0);
00208         m_camera->setViewPort(width, height);
00209         m_camera->initialize();
00210 }
00211 
00212 // ------------------------------------------------------------
00213 void CExampleWindow::onRender()
00214 {
00215         m_light->render();
00216         m_camera->render();
00217         m_scene->render();
00218 }
00219 
00220 // ------------------------------------------------------------
00221 void CExampleWindow::onMouseDown(int x, int y)
00222 {
00223         m_beginX = x;
00224         m_beginY = y;
00225 }
00226 
00227 // ------------------------------------------------------------
00228 void CExampleWindow::onMouseMove(int x, int y)
00229 {
00230         m_angleX = 0.0;
00231         m_angleY = 0.0;
00232         m_moveX = 0.0;
00233         m_moveY = 0.0;
00234         m_zoomX = 0.0;
00235         m_zoomY = 0.0;
00236 
00237         if (isLeftButtonDown())
00238         {
00239                 m_angleX = (x - m_beginX);
00240                 m_angleY = (y - m_beginY);
00241                 m_beginX = x;
00242                 m_beginY = y;
00243 
00244                 m_camera->rotatePositionY(m_angleX/100.0);
00245                 m_camera->rotatePositionX(m_angleY/100.0);
00246 
00247                 redraw();
00248         }
00249 
00250         if (isRightButtonDown())
00251         {
00252                 if (getModifierKey() == CIvfWidgetBase::MT_SHIFT)
00253                 {
00254                         m_zoomX = (x - m_beginX);
00255                         m_zoomY = (y - m_beginY);
00256                 }
00257                 else
00258                 {
00259                         m_moveX = (x - m_beginX);
00260                         m_moveY = (y - m_beginY);
00261                 }
00262                 m_beginX = x;
00263                 m_beginY = y;
00264 
00265                 m_camera->moveSideways(m_moveX/100.0);
00266                 m_camera->moveVertical(m_moveY/100.0);
00267                 m_camera->moveDepth(m_zoomY/50.0);
00268 
00269                 redraw();
00270         }
00271 }
00272 
00273 // ------------------------------------------------------------
00274 void CExampleWindow::onMouseUp(int x, int y)
00275 {
00276         m_angleX = 0.0;
00277         m_angleY = 0.0;
00278         m_moveX = 0.0;
00279         m_moveY = 0.0;
00280         m_zoomX = 0.0;
00281         m_zoomY = 0.0;
00282 }
00283 
00284 // ------------------------------------------------------------
00285 void CExampleWindow::onKeyboard(int key, int x, int y)
00286 {
00287         switch (key) {
00288         case 'a':
00289                 m_lodNear += 0.5;
00290                 updateLOD();
00291                 redraw();
00292                 break;
00293         case 'z':
00294                 m_lodNear -= 0.5;
00295                 updateLOD();
00296                 redraw();
00297                 break;
00298         case 's':
00299                 m_lodFar += 0.5;
00300                 updateLOD();
00301                 redraw();
00302                 break;
00303         case 'x':
00304                 m_lodFar -= 0.5;
00305                 updateLOD();
00306                 redraw();
00307                 break;
00308         case 'd':
00309                 m_switch->cycleForward();
00310                 redraw();
00311                 break;
00312         default:
00313                 break;
00314         }
00315 }
00316 
00317 // ------------------------------------------------------------
00318 void CExampleWindow::updateLOD()
00319 {
00320         m_lod1->setLimits(m_lodNear, m_lodFar);
00321         m_lod2->setLimits(m_lodNear, m_lodFar);
00322         m_lod3->setLimits(m_lodNear, m_lodFar);
00323         m_lod4->setLimits(m_lodNear, m_lodFar);
00324 
00325         cout << "Lod limits set to: " << m_lodNear << ", " << m_lodFar << endl;
00326 }
00327 
00328 // ------------------------------------------------------------
00329 // Main program
00330 // ------------------------------------------------------------
00331 
00332 int main(int argc, char **argv) 
00333 {
00334         // Create Ivf++ application object.
00335 
00336         CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB);
00337 
00338         // Create a window
00339 
00340         CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512);
00341 
00342         // Set window title and show window
00343 
00344         window->setWindowTitle("Ivf++ LOD/Switch example");
00345         window->show();
00346 
00347         // Enter main application loop
00348 
00349     app->run();
00350 
00351         return 0;
00352 }

Generated on Fri Sep 1 15:36:44 2006 for Interactive Visualisation Framework - Ivf++ by  doxygen 1.4.6-NO