00001 // ------------------------------------------------------------ 00002 // 00003 // Ivf++ 3D UI interaction 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/IvfBrick.h> 00019 #include <ivf/IvfCube.h> 00020 #include <ivf/IvfSphere.h> 00021 #include <ivf/IvfCylinder.h> 00022 #include <ivf/IvfCone.h> 00023 #include <ivf/IvfAxis.h> 00024 #include <ivf/IvfComposite.h> 00025 #include <ivf/IvfTransform.h> 00026 #include <ivf/IvfLight.h> 00027 #include <ivf/IvfMaterial.h> 00028 #include <ivf/IvfLighting.h> 00029 00030 #include <ivfwidget/IvfMouseViewHandler.h> 00031 #include <ivfwidget/IvfSceneHandler.h> 00032 #include <ivfwidget/IvfInteractionHandler.h> 00033 00034 #include <ivf3dui/IvfUIButton.h> 00035 #include <ivf3dui/IvfUISwitch.h> 00036 #include <ivf3dui/IvfUIButtonGroup.h> 00037 #include <ivf3dui/IvfUIComposite.h> 00038 00039 using namespace std; 00040 00041 #define VIEW_MODE 100 00042 #define INTERACTION_MODE 101 00043 00044 // ------------------------------------------------------------ 00045 // Window class definition 00046 // ------------------------------------------------------------ 00047 00048 IvfSmartPointer(CExampleWindow); 00049 00050 class CExampleWindow: public CIvfWindow, 00051 CIvfInitEvent, 00052 CIvfShapeOverEvent, 00053 CIvfShapeLeaveEvent, 00054 CIvfShapeEnterEvent, 00055 CIvfShapeDownEvent, 00056 CIvfShapeUpEvent, 00057 CIvfShapeClickEvent, 00058 CIvfControlOverEvent, 00059 CIvfControlLeaveEvent, 00060 CIvfControlEnterEvent, 00061 CIvfControlDownEvent, 00062 CIvfControlUpEvent, 00063 CIvfControlClickEvent, 00064 CIvfTimeoutEvent 00065 { 00066 private: 00067 00068 // Camera movement state variables 00069 00070 CIvfCameraPtr m_camera; 00071 CIvfScenePtr m_scene; 00072 CIvfLightPtr m_light; 00073 00074 CIvfCubePtr m_cube; 00075 CIvfSpherePtr m_sphere; 00076 CIvfCylinderPtr m_cylinder; 00077 CIvfConePtr m_cone; 00078 00079 CIvfTransformPtr m_xfm; 00080 00081 CIvfShapePtr m_rotateShape; 00082 00083 CIvfMouseViewHandlerPtr m_mouseHandler; 00084 CIvfSceneHandlerPtr m_sceneHandler; 00085 CIvfInteractionHandlerPtr m_interactionHandler; 00086 public: 00087 CExampleWindow(int X, int Y, int W, int H); 00088 00089 virtual void onInit(int width, int height); 00090 virtual bool onTimeout(); 00091 00092 virtual void onShapeDown(CIvfShape* shape); 00093 virtual void onShapeClick(CIvfShape* shape); 00094 virtual void onShapeUp(CIvfShape* shape); 00095 virtual void onShapeEnter(CIvfShape* shape); 00096 virtual void onShapeOver(CIvfShape* shape); 00097 virtual void onShapeLeave(CIvfShape* shape); 00098 virtual void onShapeDrag(CIvfShape* shape); 00099 00100 virtual void onControlClick(CIvfUIInteractiveBase* uiControl); 00101 virtual void onControlDrag(CIvfUIInteractiveBase* uiControl); 00102 virtual void onControlUp(CIvfUIInteractiveBase* uiControl); 00103 virtual void onControlDown(CIvfUIInteractiveBase* uiControl); 00104 virtual void onControlEnter(CIvfUIInteractiveBase* uiControl); 00105 virtual void onControlOver(CIvfUIInteractiveBase* uiControl); 00106 virtual void onControlLeave(CIvfUIInteractiveBase* uiControl); 00107 }; 00108 00109 // ------------------------------------------------------------ 00110 // Window class implementation 00111 // ------------------------------------------------------------ 00112 00113 CExampleWindow::CExampleWindow(int X, int Y, int W, int H) 00114 :CIvfWindow(X, Y, W, H) 00115 { 00116 addInitEvent(this); 00117 assignTimeoutEvent(0, this); 00118 } 00119 00120 void CExampleWindow::onInit(int width, int height) 00121 { 00122 // Initialize Ivf++ camera 00123 00124 m_camera = new CIvfCamera(); 00125 m_camera->setPerspective(45.0, 0.1, 100.0); 00126 m_camera->setPosition(-0.0, 6.0, 10.0); 00127 m_camera->setTarget(-0.0, 0.0, 0.0); 00128 00129 // Create scene 00130 00131 m_scene = new CIvfScene(); 00132 m_scene->setView(m_camera); 00133 00134 // Create a materials 00135 00136 CIvfMaterialPtr redMaterial = new CIvfMaterial(); 00137 redMaterial->setDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f); 00138 redMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00139 redMaterial->setAmbientColor(0.5f, 0.0f, 0.0f, 1.0f); 00140 00141 CIvfMaterialPtr greenMaterial = new CIvfMaterial(); 00142 greenMaterial->setDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f); 00143 greenMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00144 greenMaterial->setAmbientColor(0.0f, 0.5f, 0.0f, 1.0f); 00145 00146 CIvfMaterialPtr blueMaterial = new CIvfMaterial(); 00147 blueMaterial->setDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f); 00148 blueMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00149 blueMaterial->setAmbientColor(0.0f, 0.0f, 0.5f, 1.0f); 00150 00151 CIvfMaterialPtr yellowMaterial = new CIvfMaterial(); 00152 yellowMaterial->setDiffuseColor(1.0f, 1.0f, 0.0f, 1.0f); 00153 yellowMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00154 yellowMaterial->setAmbientColor(0.5f, 0.5f, 0.0f, 1.0f); 00155 00156 // Create objects 00157 00158 m_xfm = new CIvfTransform(); 00159 00160 m_scene->addChild(m_xfm); 00161 00162 m_cube = new CIvfCube(); 00163 m_cube->setMaterial(redMaterial); 00164 m_cube->setPosition(2.0, 0.0, 2.0); 00165 m_xfm->addChild(m_cube); 00166 00167 m_sphere = new CIvfSphere(); 00168 m_sphere->setMaterial(greenMaterial); 00169 m_sphere->setPosition(-2.0, 0.0, 2.0); 00170 m_xfm->addChild(m_sphere); 00171 00172 m_cylinder = new CIvfCylinder(); 00173 m_cylinder->setMaterial(blueMaterial); 00174 m_cylinder->setPosition(-2.0, 0.0, -2.0); 00175 m_xfm->addChild(m_cylinder); 00176 00177 m_cone = new CIvfCone(); 00178 m_cone->setMaterial(yellowMaterial); 00179 m_cone->setPosition(2.0, 0.0, -2.0); 00180 m_cone->setRotationQuat(0.0, 0.0, 1.0, 45.0); 00181 m_xfm->addChild(m_cone); 00182 00183 CIvfBrickPtr buttonShape = new CIvfBrick(); 00184 buttonShape->setSize(0.3, 0.1, 0.3); 00185 buttonShape->setMaterial(yellowMaterial); 00186 00187 // Create a light 00188 00189 CIvfLightingPtr lighting = CIvfLighting::getInstance(); 00190 lighting->enable(); 00191 00192 m_light = lighting->getLight(0); 00193 m_light->setLightPosition(1.0, 1.0, 1.0, 0.0); 00194 m_light->setDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); 00195 m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 00196 m_light->enable(); 00197 00198 CIvfUIButtonGroupPtr buttonGroup = new CIvfUIButtonGroup(); 00199 buttonGroup->setPosition(0.0, 0.0, 0.0); 00200 //buttonGroup->setRotationQuat(1.0, 0.0, 0.0, 0.0); 00201 00202 int i, j; 00203 00204 CIvfUIButtonPtr button; 00205 00206 for (i=0; i<2; i++) 00207 { 00208 for (j=0; j<2; j++) 00209 { 00210 button = new CIvfUIButton(); 00211 button->setVisualAction(CIvfUIButton::VA_MOVING); 00212 button->setId(1001+j+i*2); 00213 button->setMovementDirection(0.0, -1.0, 0.0); 00214 button->setMovementDistance(0.2); 00215 button->setPosition(i*0.4 - 0.2, 0.0, j*0.4 - 0.2); 00216 button->setShape(buttonShape); 00217 buttonGroup->addChild(button); 00218 } 00219 } 00220 00221 m_scene->addChild(buttonGroup); 00222 00223 button = new CIvfUIButton(); 00224 button->setVisualAction(CIvfUIButton::VA_MOVING); 00225 button->setId(1005); 00226 button->setMovementDirection(0.0, -1.0, 0.0); 00227 button->setMovementDistance(0.2); 00228 button->setPosition(0.0, 0.0, 1.0); 00229 button->setShape(buttonShape); 00230 00231 m_scene->addChild(button); 00232 00233 // Create event handlers 00234 00235 m_mouseHandler = new CIvfMouseViewHandler(this, m_camera); 00236 m_mouseHandler->activate(); 00237 00238 m_interactionHandler = new CIvfInteractionHandler(this, m_scene); 00239 m_interactionHandler->activate(); 00240 m_interactionHandler->setShapeEnterEvent(this); 00241 m_interactionHandler->setShapeOverEvent(this); 00242 m_interactionHandler->setShapeLeaveEvent(this); 00243 m_interactionHandler->setShapeDownEvent(this); 00244 m_interactionHandler->setShapeUpEvent(this); 00245 m_interactionHandler->setShapeClickEvent(this); 00246 m_interactionHandler->setControlEnterEvent(this); 00247 m_interactionHandler->setControlOverEvent(this); 00248 m_interactionHandler->setControlLeaveEvent(this); 00249 m_interactionHandler->setControlDownEvent(this); 00250 m_interactionHandler->setControlUpEvent(this); 00251 m_interactionHandler->setControlClickEvent(this); 00252 00253 m_sceneHandler = new CIvfSceneHandler(this, m_scene); 00254 m_sceneHandler->activate(); 00255 00256 enableTimeout(0.01, 0); 00257 } 00258 00259 bool CExampleWindow::onTimeout() 00260 { 00261 if (m_rotateShape!=NULL) 00262 { 00263 double v1, v2, v3, angle; 00264 m_rotateShape->getRotationQuat(v1, v2, v3, angle); 00265 angle+=0.1; 00266 m_rotateShape->setRotationQuat(0.0, 0.0, 1.0, angle); 00267 00268 redraw(); 00269 } 00270 return true; 00271 } 00272 00273 void CExampleWindow::onShapeDown(CIvfShape* shape) 00274 { 00275 if (shape!=NULL) 00276 cout << "onShapeDown: " << shape->getClassName() << endl; 00277 } 00278 00279 void CExampleWindow::onShapeClick(CIvfShape* shape) 00280 { 00281 if (shape!=NULL) 00282 cout << "onShapeClick: " << shape->getClassName() << endl; 00283 } 00284 00285 void CExampleWindow::onShapeUp(CIvfShape* shape) 00286 { 00287 if (shape!=NULL) 00288 cout << "onShapeUp: " << shape->getClassName() << endl; 00289 } 00290 00291 void CExampleWindow::onShapeEnter(CIvfShape* shape) 00292 { 00293 if (shape!=NULL) 00294 cout << "onShapeEnter: " << shape->getClassName() << endl; 00295 } 00296 00297 void CExampleWindow::onShapeOver(CIvfShape* shape) 00298 { 00299 if (shape!=NULL) 00300 cout << "onShapeOver: " << shape->getClassName() << endl; 00301 } 00302 00303 void CExampleWindow::onShapeLeave(CIvfShape* shape) 00304 { 00305 if (shape!=NULL) 00306 cout << "onShapeLeave: " << shape->getClassName() << endl; 00307 } 00308 00309 void CExampleWindow::onShapeDrag(CIvfShape* shape) 00310 { 00311 if (shape!=NULL) 00312 cout << "onShapeDrag: " << shape->getClassName() << endl; 00313 } 00314 00315 void CExampleWindow::onControlClick(CIvfUIInteractiveBase *uiControl) 00316 { 00317 if (uiControl!=NULL) 00318 { 00319 switch (uiControl->getId()) { 00320 case 1001: 00321 m_rotateShape = m_cylinder; 00322 break; 00323 case 1002: 00324 m_rotateShape = m_sphere; 00325 break; 00326 case 1003: 00327 m_rotateShape = m_cone; 00328 break; 00329 case 1004: 00330 m_rotateShape = m_cube; 00331 break; 00332 default: 00333 break; 00334 } 00335 cout << "onControlClick: " << uiControl->getClassName() 00336 << ", " << uiControl->getId() << endl; 00337 } 00338 } 00339 00340 void CExampleWindow::onControlOver(CIvfUIInteractiveBase *uiControl) 00341 { 00342 if (uiControl!=NULL) 00343 cout << "onControlOver: " << uiControl->getClassName() 00344 << ", " << uiControl->getId() << endl; 00345 } 00346 00347 void CExampleWindow::onControlDown(CIvfUIInteractiveBase *uiControl) 00348 { 00349 if (uiControl!=NULL) 00350 { 00351 if (uiControl->getId()==1005) 00352 m_rotateShape = m_xfm; 00353 00354 cout << "onControlDown: " << uiControl->getClassName() 00355 << ", " << uiControl->getId() << endl; 00356 } 00357 } 00358 00359 void CExampleWindow::onControlUp(CIvfUIInteractiveBase *uiControl) 00360 { 00361 if (uiControl!=NULL) 00362 { 00363 if (uiControl->getId()==1005) 00364 m_rotateShape = NULL; 00365 00366 cout << "onControlUp: " << uiControl->getClassName() 00367 << ", " << uiControl->getId() << endl; 00368 } 00369 } 00370 00371 void CExampleWindow::onControlDrag(CIvfUIInteractiveBase *uiControl) 00372 { 00373 if (uiControl!=NULL) 00374 cout << "onControlDrag: " << uiControl->getClassName() << endl; 00375 } 00376 00377 void CExampleWindow::onControlLeave(CIvfUIInteractiveBase *uiControl) 00378 { 00379 if (uiControl!=NULL) 00380 cout << "onControlLeave: " << uiControl->getClassName() << endl; 00381 } 00382 00383 void CExampleWindow::onControlEnter(CIvfUIInteractiveBase *uiControl) 00384 { 00385 if (uiControl!=NULL) 00386 cout << "onControlEnter: " << uiControl->getClassName() << endl; 00387 } 00388 00389 // ------------------------------------------------------------ 00390 // Main program 00391 // ------------------------------------------------------------ 00392 00393 00394 int main(int argc, char **argv) 00395 { 00396 // Create Ivf++ application object. 00397 00398 CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB); 00399 00400 // Create a window 00401 00402 CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512); 00403 00404 // Set window title and show window 00405 00406 window->setWindowTitle("Ivf++ 3D user interface interaction"); 00407 window->show(); 00408 00409 // Enter main application loop 00410 00411 app->run(); 00412 00413 cout << "window refcount = " << window->getRefCount() << endl; 00414 00415 return 0; 00416 } 00417 00418