placement.cpp

This is an example of how to position objects in a scene. Using both primitives and CIvfComposite classes.

00001 // ------------------------------------------------------------
00002 //
00003 // Ivf++ Object placement/rotation sample
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/IvfCube.h>
00019 #include <ivf/IvfSphere.h>
00020 #include <ivf/IvfCylinder.h>
00021 #include <ivf/IvfCone.h>
00022 #include <ivf/IvfAxis.h>
00023 #include <ivf/IvfComposite.h>
00024 #include <ivf/IvfLighting.h>
00025 #include <ivf/IvfLight.h>
00026 #include <ivf/IvfMaterial.h>
00027 
00028 // ------------------------------------------------------------
00029 // Window class definition
00030 // ------------------------------------------------------------
00031 
00032 IvfSmartPointer(CExampleWindow);
00033 
00034 class CExampleWindow: public CIvfWindow {
00035 private:
00036 
00037         // Camera movement state variables
00038 
00039         int m_beginX;
00040         int m_beginY;
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         CIvfCameraPtr           m_camera;
00050         CIvfCompositePtr        m_scene;
00051         CIvfLightPtr            m_light;
00052 public:
00053         CExampleWindow(int X, int Y, int W, int H)
00054                 :CIvfWindow(X, Y, W, H) {};
00055 
00056         virtual void onInit(int width, int height);
00057         virtual void onResize(int width, int height);
00058         virtual void onRender();
00059 
00060         // Mouse event methods
00061 
00062         virtual void onMouseDown(int x, int y);
00063         virtual void onMouseMove(int x, int y);
00064         virtual void onMouseUp(int x, int y);
00065 };
00066 
00067 // ------------------------------------------------------------
00068 // Window class implementation
00069 // ------------------------------------------------------------
00070 
00071 void CExampleWindow::onInit(int width, int height)
00072 {
00073     // State variables
00074 
00075     m_angleX = 0.0f;
00076     m_angleY = 0.0f;
00077     m_moveX = 0.0f;
00078     m_moveY = 0.0f;
00079     m_zoomX = 0.0f;
00080     m_zoomY = 0.0f;
00081 
00082         // Initialize Ivf++ camera
00083 
00084         m_camera = new CIvfCamera();
00085         m_camera->setPosition(0.0, 4.0, 9.0);
00086         m_camera->setPerspective(45.0, 0.1, 100.0);
00087 
00088         // Create a materials
00089 
00090         CIvfMaterialPtr redMaterial = new CIvfMaterial();
00091         redMaterial->setDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f);
00092         redMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
00093         redMaterial->setAmbientColor(0.5f, 0.0f, 0.0f, 1.0f);
00094 
00095         CIvfMaterialPtr greenMaterial = new CIvfMaterial();
00096         greenMaterial->setDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f);
00097         greenMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
00098         greenMaterial->setAmbientColor(0.0f, 0.5f, 0.0f, 1.0f);
00099         
00100         CIvfMaterialPtr blueMaterial = new CIvfMaterial();
00101         blueMaterial->setDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f);
00102         blueMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
00103         blueMaterial->setAmbientColor(0.0f, 0.0f, 0.5f, 1.0f);
00104 
00105         CIvfMaterialPtr yellowMaterial = new CIvfMaterial();
00106         yellowMaterial->setDiffuseColor(1.0f, 1.0f, 0.0f, 1.0f);
00107         yellowMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
00108         yellowMaterial->setAmbientColor(0.5f, 0.5f, 0.0f, 1.0f);
00109 
00110         // Create scene composite
00111 
00112         m_scene = new CIvfComposite();
00113 
00114         // Create objects
00115         
00116         CIvfCubePtr cube = new CIvfCube();
00117         cube->setMaterial(redMaterial);
00118         cube->setPosition(2.0, 0.0, 2.0);
00119         m_scene->addChild(cube);
00120 
00121         CIvfSpherePtr sphere = new CIvfSphere();
00122         sphere->setMaterial(greenMaterial);
00123         sphere->setPosition(-2.0, 0.0, 2.0);
00124         m_scene->addChild(sphere);
00125 
00126         CIvfCylinderPtr cylinder = new CIvfCylinder();
00127         cylinder->setMaterial(blueMaterial);
00128         cylinder->setPosition(-2.0, 0.0, -2.0);
00129         m_scene->addChild(cylinder);
00130 
00131         CIvfConePtr cone = new CIvfCone();
00132         cone->setMaterial(yellowMaterial);
00133         cone->setPosition(2.0, 0.0, -2.0);
00134         cone->setRotationQuat(0.0, 0.0, 1.0, 45.0);
00135         m_scene->addChild(cone);
00136 
00137         CIvfAxisPtr axis = new CIvfAxis();
00138         m_scene->addChild(axis);
00139         
00140         // Create a light
00141 
00142         CIvfLightingPtr lighting = CIvfLighting::getInstance();
00143 
00144         m_light = lighting->getLight(0);
00145         m_light->setLightPosition(1.0, 1.0, 1.0, 0.0);
00146         m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 
00147         m_light->enable();
00148 }
00149 
00150 // ------------------------------------------------------------
00151 void CExampleWindow::onResize(int width, int height)
00152 {
00153         m_camera->setViewPort(width, height);
00154         m_camera->initialize();
00155 }
00156 
00157 // ------------------------------------------------------------
00158 void CExampleWindow::onRender()
00159 {
00160         m_light->render();
00161         m_camera->render();
00162         m_scene->render();
00163 }
00164 
00165 // ------------------------------------------------------------
00166 void CExampleWindow::onMouseDown(int x, int y)
00167 {
00168         m_beginX = x;
00169         m_beginY = y;
00170 }
00171 
00172 // ------------------------------------------------------------
00173 void CExampleWindow::onMouseMove(int x, int y)
00174 {
00175     m_angleX = 0.0;
00176     m_angleY = 0.0;
00177     m_moveX = 0.0;
00178     m_moveY = 0.0;
00179     m_zoomX = 0.0;
00180     m_zoomY = 0.0;
00181         
00182     if (this->isLeftButtonDown())
00183     {
00184         m_angleX = (x - m_beginX);
00185         m_angleY = (y - m_beginY);
00186         m_beginX = x;
00187         m_beginY = y;
00188                 
00189                 m_camera->rotatePositionY(m_angleX/100.0);
00190                 m_camera->rotatePositionX(m_angleY/100.0);
00191                 
00192         this->redraw();
00193     }
00194 
00195     if (this->isRightButtonDown())
00196     {
00197                 if (this->getModifierKey()==CIvfWidgetBase::MT_SHIFT)
00198                 {
00199             m_zoomX = (x - m_beginX);
00200             m_zoomY = (y - m_beginY);
00201         }
00202         else
00203         {
00204             m_moveX = (x - m_beginX);
00205             m_moveY = (y - m_beginY);
00206         }
00207 
00208         m_beginX = x;
00209         m_beginY = y;
00210 
00211                 m_camera->moveSideways(m_moveX/100.0);
00212                 m_camera->moveVertical(m_moveY/100.0);
00213                 m_camera->moveDepth(m_zoomY/50.0);
00214                 
00215         this->redraw();
00216     }
00217 }
00218 
00219 // ------------------------------------------------------------
00220 void CExampleWindow::onMouseUp(int x, int y)
00221 {
00222         m_angleX = 0.0;
00223         m_angleY = 0.0;
00224         m_moveX = 0.0;
00225         m_moveY = 0.0;
00226         m_zoomX = 0.0;
00227         m_zoomY = 0.0;
00228 }
00229 
00230 // ------------------------------------------------------------
00231 // Main program
00232 // ------------------------------------------------------------
00233 
00234 int main(int argc, char **argv) 
00235 {
00236         // Create Ivf++ application object.
00237 
00238         CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB);
00239 
00240         // Create a window
00241 
00242         CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512);
00243 
00244         // Set window title and show window
00245 
00246         window->setWindowTitle("Ivf++ Object placement example");
00247         window->show();
00248 
00249         // Enter main application loop
00250 
00251     app->run();
00252 
00253         return 0;
00254 }

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