00001 // ------------------------------------------------------------ 00002 // 00003 // Ivf++ Object selection 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/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/IvfBufferSelection.h> 00025 #include <ivf/IvfLighting.h> 00026 #include <ivf/IvfLight.h> 00027 #include <ivf/IvfMaterial.h> 00028 00029 // ------------------------------------------------------------ 00030 // Window class definition 00031 // ------------------------------------------------------------ 00032 00033 IvfSmartPointer(CExampleWindow); 00034 00035 class CExampleWindow: public CIvfWindow { 00036 private: 00037 00038 // Ivf++ objects 00039 00040 CIvfCameraPtr m_camera; 00041 CIvfCompositePtr m_scene; 00042 CIvfLightPtr m_light; 00043 00044 CIvfBufferSelectionPtr m_selection; 00045 public: 00046 CExampleWindow(int X, int Y, int W, int H) 00047 :CIvfWindow(X, Y, W, H) {}; 00048 00049 virtual void onInit(int width, int height); 00050 virtual void onResize(int width, int height); 00051 virtual void onRender(); 00052 virtual void onMouseDown(int x, int y); 00053 }; 00054 00055 // ------------------------------------------------------------ 00056 // Window class implementation 00057 // ------------------------------------------------------------ 00058 00059 void CExampleWindow::onInit(int width, int height) 00060 { 00061 // Initialize Ivf++ camera 00062 00063 m_camera = new CIvfCamera(); 00064 m_camera->setPosition(0.0, 4.0, 9.0); 00065 00066 // Create a materials 00067 00068 CIvfMaterialPtr redMaterial = new CIvfMaterial(); 00069 redMaterial->setDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f); 00070 redMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00071 redMaterial->setAmbientColor(0.5f, 0.0f, 0.0f, 1.0f); 00072 00073 CIvfMaterialPtr greenMaterial = new CIvfMaterial(); 00074 greenMaterial->setDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f); 00075 greenMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00076 greenMaterial->setAmbientColor(0.0f, 0.5f, 0.0f, 1.0f); 00077 00078 CIvfMaterialPtr blueMaterial = new CIvfMaterial(); 00079 blueMaterial->setDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f); 00080 blueMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00081 blueMaterial->setAmbientColor(0.0f, 0.0f, 0.5f, 1.0f); 00082 00083 CIvfMaterialPtr yellowMaterial = new CIvfMaterial(); 00084 yellowMaterial->setDiffuseColor(1.0f, 1.0f, 0.0f, 1.0f); 00085 yellowMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00086 yellowMaterial->setAmbientColor(0.5f, 0.5f, 0.0f, 1.0f); 00087 00088 // Create scene composite 00089 00090 m_scene = new CIvfComposite(); 00091 00092 // Create objects 00093 00094 CIvfCubePtr cube = new CIvfCube(); 00095 cube->setMaterial(redMaterial); 00096 cube->setPosition(2.0, 0.0, 2.0); 00097 m_scene->addChild(cube); 00098 00099 CIvfSpherePtr sphere = new CIvfSphere(); 00100 sphere->setMaterial(greenMaterial); 00101 sphere->setPosition(-2.0, 0.0, 2.0); 00102 m_scene->addChild(sphere); 00103 00104 CIvfCylinderPtr cylinder = new CIvfCylinder(); 00105 cylinder->setMaterial(blueMaterial); 00106 cylinder->setPosition(-2.0, 0.0, -2.0); 00107 m_scene->addChild(cylinder); 00108 00109 CIvfConePtr cone = new CIvfCone(); 00110 cone->setMaterial(yellowMaterial); 00111 cone->setPosition(2.0, 0.0, -2.0); 00112 cone->setRotationQuat(0.0, 0.0, 1.0, 45.0); 00113 m_scene->addChild(cone); 00114 00115 CIvfAxisPtr axis = new CIvfAxis(); 00116 m_scene->addChild(axis); 00117 00118 // Setup the selection algorithm, 00119 00120 m_selection = new CIvfBufferSelection(); 00121 m_selection->setView(m_camera); 00122 m_selection->setComposite(m_scene); 00123 00124 // Create a light 00125 00126 CIvfLightingPtr lighting = CIvfLighting::getInstance(); 00127 00128 m_light = lighting->getLight(0); 00129 m_light->setType(CIvfLight::LT_DIRECTIONAL); 00130 m_light->setDirection(1.0, 1.0, 1.0); 00131 m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 00132 m_light->enable(); 00133 } 00134 00135 // ------------------------------------------------------------ 00136 void CExampleWindow::onResize(int width, int height) 00137 { 00138 m_camera->setPerspective(45.0, 0.1, 100.0); 00139 m_camera->setViewPort(width, height); 00140 m_camera->initialize(); 00141 } 00142 00143 // ------------------------------------------------------------ 00144 void CExampleWindow::onRender() 00145 { 00146 m_camera->render(); 00147 m_light->render(); 00148 m_scene->render(); 00149 } 00150 00151 // ------------------------------------------------------------ 00152 void CExampleWindow::onMouseDown(int x, int y) 00153 { 00154 int i; 00155 00156 if (isLeftButtonDown()) 00157 { 00158 m_selection->pick(x, y); 00159 if (m_selection->getSelectedShape()!=NULL) 00160 { 00161 m_selection->getSelectedShape()->setHighlight(CIvfShape::HS_ON); 00162 redraw(); 00163 } 00164 else 00165 { 00166 m_scene->setHighlightChildren(CIvfShape::HS_OFF); 00167 redraw(); 00168 } 00169 } 00170 00171 if (isRightButtonDown()) 00172 { 00173 m_selection->pick(x, y); 00174 if (m_selection->getSize()!=0) 00175 { 00176 for (i=0; i<m_selection->getSize(); i++) 00177 m_selection->getSelectedShape(i)->setHighlight(CIvfShape::HS_ON); 00178 00179 redraw(); 00180 } 00181 else 00182 { 00183 m_scene->setHighlightChildren(CIvfShape::HS_OFF); 00184 redraw(); 00185 } 00186 } 00187 } 00188 00189 // ------------------------------------------------------------ 00190 // Main program 00191 // ------------------------------------------------------------ 00192 00193 int main(int argc, char **argv) 00194 { 00195 // Create Ivf++ application object. 00196 00197 CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB); 00198 00199 // Create a window 00200 00201 CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512); 00202 00203 // Set window title and show window 00204 00205 window->setWindowTitle("Ivf++ Selection example"); 00206 window->show(); 00207 00208 // Enter main application loop 00209 00210 app->run(); 00211 00212 return 0; 00213 }