controllers.cpp

This is an example of how to use controllers

00001 // ------------------------------------------------------------
00002 //
00003 // Ivf++ Controller 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 
00023 #include <ivffile/IvfAc3DReader.h>
00024 
00025 #include <ivfctl/IvfControllerGroup.h>
00026 #include <ivfctl/IvfRotateController.h>
00027 #include <ivfctl/IvfCameraController.h>
00028 #include <ivfctl/IvfSlerpController.h>
00029 
00030 // ------------------------------------------------------------
00031 // Window class definition
00032 // ------------------------------------------------------------
00033 
00034 IvfSmartPointer(CExampleWindow);
00035 
00036 class CExampleWindow: public CIvfWindow {
00037 private:
00038         CIvfCameraPtr           m_camera;
00039         CIvfCompositePtr        m_scene;
00040         CIvfLightPtr            m_light;
00041 
00042         CIvfControllerGroupPtr m_controllers;
00043         CIvfRotateControllerPtr m_propCtl;
00044         CIvfRotateControllerPtr m_headCtl;
00045         CIvfCameraControllerPtr m_cameraCtl;
00046 
00047         CIvfSlerpControllerPtr m_slerpCtl;
00048 
00049         double m_angleX;
00050         double m_angleY;
00051         double m_moveX;
00052         double m_moveY;
00053         double m_zoomX;
00054         double m_zoomY;
00055 
00056         int m_beginX;
00057         int m_beginY;
00058 
00059         bool m_rotating;
00060 
00061 public:
00062         CExampleWindow(int X, int Y, int W, int H)
00063                 :CIvfWindow(X, Y, W, H) {};
00064 
00065         virtual void onInit(int width, int height);
00066         virtual void onResize(int width, int height);
00067         virtual void onRender();
00068         virtual void onMouseDown(int x, int y);
00069         virtual void onMouseMove(int x, int y);
00070         virtual void onMouseUp(int x, int y);
00071         virtual bool onTimeout0();
00072         virtual void onKeyboard(int key, int x, int y);
00073 };
00074 
00075 // ------------------------------------------------------------
00076 // Window class implementation
00077 // ------------------------------------------------------------
00078 
00079 void CExampleWindow::onInit(int width, int height)
00080 {
00081         // Initialize variables
00082 
00083         m_angleX = 0.0;
00084         m_angleY = 0.0;
00085         m_moveX = 0.0;
00086         m_moveY = 0.0;
00087         m_zoomX = 0.0;
00088         m_zoomY = 0.0;
00089 
00090         m_rotating = false;
00091 
00092         // Initialize Ivf++ camera
00093 
00094         m_camera = new CIvfCamera();
00095         m_camera->setPosition(10.0, 20.0, 20.0);
00096         m_camera->setTarget(0.0, 8.0, 0.0);
00097 
00098         // Create a materials
00099 
00100         CIvfMaterialPtr material = new CIvfMaterial();
00101         material->setDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f);
00102         material->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
00103         material->setAmbientColor(0.0f, 0.5f, 0.0f, 1.0f);
00104 
00105         // Create scene
00106 
00107         m_scene = new CIvfComposite();
00108 
00109         // Create a file reader
00110 
00111         CIvfAc3DReaderPtr acReader = new CIvfAc3DReader();
00112 
00113         // Set parameters
00114 
00115         acReader->setFileName("models/controllers.ac");
00116         acReader->setScaling(1.0);
00117 
00118         // Read file
00119 
00120         acReader->read();
00121 
00122         // Retrieve poly set
00123 
00124         CIvfCompositePtr model = (CIvfComposite*)acReader->getShape();
00125 
00126         CIvfShapePtr propeller = model->findShape("propeller");
00127         propeller->setRotationQuat(0.0, 0.0, 1.0, 0.0);
00128         CIvfShapePtr head = model->findShape("head");
00129         head->setRotationQuat(0.0, 1.0, 0.0, 0.0);
00130 
00131 
00132         m_propCtl = new CIvfRotateController();
00133         m_propCtl->setShape(propeller);
00134         m_propCtl->setRotationSpeed(100.0);
00135         m_propCtl->activate();
00136 
00137         m_headCtl = new CIvfRotateController();
00138         m_headCtl->setShape(head);
00139         m_headCtl->setRotationSpeed(10.0);
00140         m_headCtl->activate();
00141 
00142         CIvfSpline3dPtr positionSpline = new CIvfSpline3d();
00143         positionSpline->setSize(5);
00144         positionSpline->getPoint(0)->setComponents(15.0, 10.0, 15.0);
00145         positionSpline->getPoint(1)->setComponents(-15.0,  5.0, 15.0);
00146         positionSpline->getPoint(2)->setComponents(-15.0, 10.0, -15.0);
00147         positionSpline->getPoint(3)->setComponents( 15.0, 5.0, -15.0);
00148         positionSpline->getPoint(4)->setComponents( 15.0, 10.0, 15.0);
00149         positionSpline->update();
00150 
00151         /*
00152         CIvfSpline3d* targetSpline = new CIvfSpline3d();
00153         targetSpline->setSize(5);
00154         targetSpline->getPoint(0)->setComponents(15.0, 10.0, 15.0);
00155         targetSpline->getPoint(1)->setComponents(-15.0,  5.0, 15.0);
00156         targetSpline->getPoint(2)->setComponents(-15.0, 10.0, -15.0);
00157         targetSpline->getPoint(3)->setComponents( 15.0, 5.0, -15.0);
00158         targetSpline->getPoint(4)->setComponents( 15.0, 10.0, 15.0);
00159         targetSpline->update();
00160         */
00161 
00162         m_cameraCtl = new CIvfCameraController();
00163         m_cameraCtl->setCamera(m_camera);
00164         m_cameraCtl->setPath(positionSpline);
00165         m_cameraCtl->setFollowPath(false);
00166         //m_cameraCtl->setTargetPath(targetSpline);
00167         m_cameraCtl->setInitialSpeed(0.2);
00168         //m_cameraCtl->setInitialTargetSpeed(0.2);
00169         m_cameraCtl->setEndActionType(CIvfCameraController::EA_RESET);
00170         m_cameraCtl->setStartActionType(CIvfCameraController::SA_RESET);
00171         //m_cameraCtl->setTargetEndAction(CIvfCameraController::EA_REVERSE);
00172         //m_cameraCtl->setTargetStartAction(CIvfCameraController::SA_REVERSE);
00173         m_cameraCtl->deactivate();
00174 
00175         CIvfAxisPtr axis = new CIvfAxis();
00176         axis->setPosition(3.0, 3.0, 3.0);
00177         axis->setSize(2.0);
00178         m_scene->addChild(axis);
00179 
00180         CIvfSlerpPtr slerp = new CIvfSlerp();
00181         slerp->setSize(4);
00182         slerp->setQuatAxisAngle(0, 0.0, 0.0, 1.0, 10.0);
00183         slerp->setQuatAxisAngle(1, 0.0, 0.0, 1.0, 90.0);
00184         slerp->setQuatAxisAngle(2, 1.0, 0.0, 0.0, 45.0);
00185         slerp->setQuatAxisAngle(3, 0.0, 1.0, 0.0, 30.0);
00186         slerp->update();
00187 
00188         CIvfActionPtr action1 = new CIvfAction();
00189         action1->setActionType(IVF_ACTION_ACTIVATE);
00190         action1->setTarget(m_cameraCtl);
00191 
00192         CIvfActionPtr action2 = new CIvfAction();
00193         action2->setActionType(IVF_ACTION_DEACTIVATE);
00194         action2->setTarget(m_cameraCtl);
00195 
00196         m_slerpCtl = new CIvfSlerpController();
00197         m_slerpCtl->setSlerp(slerp);
00198         m_slerpCtl->setShape(axis);
00199         m_slerpCtl->setInitialSpeed(1.0);
00200         m_slerpCtl->setEndActionType(CIvfSlerpController::EA_REVERSE);
00201         m_slerpCtl->setEndAction(action1);
00202         m_slerpCtl->setStartActionType(CIvfSlerpController::SA_REVERSE);
00203         m_slerpCtl->setStartAction(action2);
00204         m_slerpCtl->activate();
00205 
00206         m_controllers = new CIvfControllerGroup();
00207         m_controllers->addChild(m_propCtl);
00208         m_controllers->addChild(m_headCtl);
00209         m_controllers->addChild(m_cameraCtl);
00210         m_controllers->addChild(m_slerpCtl);
00211         m_controllers->activate();
00212 
00213         m_scene->addChild(model);
00214 
00215         // Create a light
00216 
00217         CIvfLightingPtr lighting = CIvfLighting::getInstance();
00218 
00219         m_light = lighting->getLight(0);
00220         m_light->setLightPosition(1.0, 1.0, 1.0, 0.0);
00221         m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 
00222         m_light->enable();
00223 
00224         enableTimeout(0.01, 0);
00225 }
00226 
00227 // ------------------------------------------------------------
00228 void CExampleWindow::onResize(int width, int height)
00229 {
00230         m_camera->setPerspective(45.0, 0.1, 100.0);
00231         m_camera->setViewPort(width, height);
00232         m_camera->initialize();
00233 }
00234 
00235 // ------------------------------------------------------------
00236 void CExampleWindow::onRender()
00237 {
00238         m_light->render();
00239 
00240         m_camera->rotatePositionY(m_angleX/100.0);
00241         m_camera->rotatePositionX(m_angleY/100.0);
00242         m_camera->moveSideways(m_moveX/100.0);
00243         m_camera->moveVertical(m_moveY/100.0);
00244         m_camera->moveDepth(m_zoomY/50.0);
00245         m_camera->render();
00246 
00247         m_scene->render();
00248 }
00249 
00250 // ------------------------------------------------------------
00251 void CExampleWindow::onMouseDown(int x, int y)
00252 {
00253         m_beginX = x;
00254         m_beginY = y;
00255 }
00256 
00257 // ------------------------------------------------------------
00258 void CExampleWindow::onMouseMove(int x, int y)
00259 {
00260         m_angleX = 0.0;
00261         m_angleY = 0.0;
00262         m_moveX = 0.0;
00263         m_moveY = 0.0;
00264         m_zoomX = 0.0;
00265         m_zoomY = 0.0;
00266 
00267         if (isLeftButtonDown())
00268         {
00269                 m_angleX = (x - m_beginX);
00270                 m_angleY = (y - m_beginY);
00271                 m_beginX = x;
00272                 m_beginY = y;
00273                 redraw();
00274         }
00275 
00276         if (isRightButtonDown())
00277         {
00278                 if (getModifierKey() == CIvfWidgetBase::MT_SHIFT)
00279                 {
00280                         m_zoomX = (x - m_beginX);
00281                         m_zoomY = (y - m_beginY);
00282                 }
00283                 else
00284                 {
00285                         m_moveX = (x - m_beginX);
00286                         m_moveY = (y - m_beginY);
00287                 }
00288                 m_beginX = x;
00289                 m_beginY = y;
00290 
00291                 redraw();
00292         }
00293 }
00294 
00295 // ------------------------------------------------------------
00296 void CExampleWindow::onMouseUp(int x, int y)
00297 {
00298         m_angleX = 0.0;
00299         m_angleY = 0.0;
00300         m_moveX = 0.0;
00301         m_moveY = 0.0;
00302         m_zoomX = 0.0;
00303         m_zoomY = 0.0;
00304 }
00305 
00306 // ------------------------------------------------------------
00307 bool CExampleWindow::onTimeout0()
00308 {
00309         m_controllers->update(0.01);
00310         redraw();
00311         return true;
00312 }
00313 
00314 // ------------------------------------------------------------
00315 void CExampleWindow::onKeyboard(int key, int x, int y)
00316 {
00317         switch (key) {
00318         case 'p' :
00319                 if (m_propCtl->isActive())
00320                         m_propCtl->deactivate();
00321                 else
00322                         m_propCtl->activate();
00323                 break;
00324         case 'h' :
00325                 if (m_headCtl->isActive())
00326                         m_headCtl->deactivate();
00327                 else
00328                         m_headCtl->activate();
00329                 break;
00330         case 'c' :
00331                 if (m_cameraCtl->isActive())
00332                         m_cameraCtl->deactivate();
00333                 else
00334                         m_cameraCtl->activate();
00335                 break;
00336         default:
00337                 break;
00338         }
00339 }
00340 
00341 // ------------------------------------------------------------
00342 // Main program
00343 // ------------------------------------------------------------
00344 
00345 int main(int argc, char **argv) 
00346 {
00347         // Create Ivf++ application object.
00348 
00349         CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB);
00350 
00351         // Create a window
00352 
00353         CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512);
00354 
00355         // Set window title and show window
00356 
00357         window->setWindowTitle("Ivf++ Controller example");
00358         window->show();
00359 
00360         // Enter main application loop
00361 
00362     app->run();
00363 
00364         return 0;
00365 }

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