actionctl.cpp

This is an example of how to use the CIvfAction class, using the CIvfActionController class

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 #include <ivf/IvfCube.h>
00023 
00024 #include <ivffile/IvfAc3DReader.h>
00025 
00026 #include <ivfctl/IvfControllerGroup.h>
00027 #include <ivfctl/IvfActionController.h>
00028 #include <ivfctl/IvfRotateController.h>
00029 #include <ivfctl/IvfCameraController.h>
00030 #include <ivfctl/IvfSlerpController.h>
00031 #include <ivfctl/IvfPathController.h>
00032 
00033 // ------------------------------------------------------------
00034 // Window class definition
00035 // ------------------------------------------------------------
00036 
00037 IvfSmartPointer(CExampleWindow);
00038 
00039 class CExampleWindow: public CIvfWindow {
00040 private:
00041         CIvfCameraPtr           m_camera;
00042         CIvfCompositePtr        m_scene;
00043         CIvfLightPtr            m_light;
00044 
00045         CIvfActionControllerPtr m_controllers;
00046 
00047         double m_angleX;
00048         double m_angleY;
00049         double m_moveX;
00050         double m_moveY;
00051         double m_zoomX;
00052         double m_zoomY;
00053 
00054         int m_beginX;
00055         int m_beginY;
00056 
00057         bool m_rotating;
00058 
00059 public:
00060         CExampleWindow(int X, int Y, int W, int H)
00061                 :CIvfWindow(X, Y, W, H) {};
00062 
00063         virtual void onInit(int width, int height);
00064         virtual void onResize(int width, int height);
00065         virtual void onRender();
00066         virtual void onMouseDown(int x, int y);
00067         virtual void onMouseMove(int x, int y);
00068         virtual void onMouseUp(int x, int y);
00069         virtual bool onTimeout0();
00070         virtual void onKeyboard(int key, int x, int y);
00071 };
00072 
00073 // ------------------------------------------------------------
00074 // Window class implementation
00075 // ------------------------------------------------------------
00076 
00077 void CExampleWindow::onInit(int width, int height)
00078 {
00079         // Initialize variables
00080 
00081         m_angleX = 0.0;
00082         m_angleY = 0.0;
00083         m_moveX = 0.0;
00084         m_moveY = 0.0;
00085         m_zoomX = 0.0;
00086         m_zoomY = 0.0;
00087 
00088         m_rotating = false;
00089 
00090         int i, j;
00091 
00092         // Initialize Ivf++ camera
00093 
00094         m_camera = new CIvfCamera();
00095         m_camera->setPosition(8.0, 8.0, 8.0);
00096         m_camera->setTarget(0.0, 0.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         CIvfCubePtr cube = new CIvfCube();
00110         cube->setMaterial(material);
00111 
00112         CIvfAxisPtr axis = new CIvfAxis();
00113         axis->setPosition(0.0, 0.0, 0.0);
00114         axis->setSize(2.0);
00115         m_scene->addChild(axis);
00116 
00117         // Create controllers
00118 
00119         m_controllers = new CIvfActionController();
00120         m_controllers->activate();
00121 
00122         for (i=0; i<5; i++)
00123                 for (j=0; j<5; j++)
00124                 {
00125                         CIvfTransformPtr tf = new CIvfTransform();
00126                         tf->addChild(cube);
00127                         tf->setPosition(-5.0+2.5*(double)i, 0.0, -5.0+2.5*(double)j);
00128                         m_scene->addChild(tf);
00129 
00130                         CIvfRotateControllerPtr ctl = new CIvfRotateController();
00131                         ctl->setShape(tf);
00132                         ctl->setRotationSpeed(45.0*3.0/10.0);
00133                         ctl->activate();
00134 
00135                         CIvfSpline3dPtr path = new CIvfSpline3d();
00136                         path->setSize(2);
00137                         path->getPoint(0)->setComponents(-5.0+2.5*(double)i, 0.0, -5.0+2.5*(double)j);
00138                         path->getPoint(1)->setComponents(-5.0+2.5*(double)i, 3.0, -5.0+2.5*(double)j);
00139                         path->update();
00140 
00141                         CIvfPathControllerPtr pathCtl = new CIvfPathController();
00142                         pathCtl->setPath(path);
00143                         pathCtl->setShape(tf);
00144                         pathCtl->setInitialSpeed(0.1);
00145                         pathCtl->activate();
00146 
00147                         CIvfControllerGroupPtr groupCtl = new CIvfControllerGroup();
00148                         groupCtl->addChild(ctl);
00149                         groupCtl->addChild(pathCtl);
00150                         //groupCtl->activate();
00151 
00152                         m_controllers->addChild(groupCtl);
00153 
00154                         CIvfActionPtr action = new CIvfAction();
00155                         action->setActionType(IVF_ACTION_ACTIVATE);
00156                         action->setTarget(groupCtl);
00157                         action->setTime(1.0+1.0*(double)(i*5+j));
00158 
00159                         m_controllers->addAction(action);
00160                         
00161                         action = new CIvfAction();
00162                         action->setActionType(IVF_ACTION_DEACTIVATE);
00163                         action->setTarget(ctl);
00164                         action->setTime(1.0+1.0*(double)(i*5+j)+10.0);
00165 
00166                         m_controllers->addAction(action);
00167                 }
00168 
00169         // Create a light
00170 
00171         CIvfLightingPtr lighting = CIvfLighting::getInstance();
00172         
00173         m_light = lighting->getLight(0);;
00174         m_light->setLightPosition(1.0, 1.0, 1.0, 0.0);
00175         m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 
00176         m_light->enable();
00177 
00178         enableTimeout(0.01, 0);
00179 }
00180 
00181 // ------------------------------------------------------------
00182 void CExampleWindow::onResize(int width, int height)
00183 {
00184         m_camera->setPerspective(45.0, 0.1, 100.0);
00185         m_camera->setViewPort(width, height);
00186         m_camera->initialize();
00187 }
00188 
00189 // ------------------------------------------------------------
00190 void CExampleWindow::onRender()
00191 {
00192         m_light->render();
00193 
00194         m_camera->rotatePositionY(m_angleX/100.0);
00195         m_camera->rotatePositionX(m_angleY/100.0);
00196         m_camera->moveSideways(m_moveX/100.0);
00197         m_camera->moveVertical(m_moveY/100.0);
00198         m_camera->moveDepth(m_zoomY/50.0);
00199         m_camera->render();
00200 
00201         m_scene->render();
00202 }
00203 
00204 // ------------------------------------------------------------
00205 void CExampleWindow::onMouseDown(int x, int y)
00206 {
00207         m_beginX = x;
00208         m_beginY = y;
00209 }
00210 
00211 // ------------------------------------------------------------
00212 void CExampleWindow::onMouseMove(int x, int y)
00213 {
00214         m_angleX = 0.0;
00215         m_angleY = 0.0;
00216         m_moveX = 0.0;
00217         m_moveY = 0.0;
00218         m_zoomX = 0.0;
00219         m_zoomY = 0.0;
00220 
00221         if (isLeftButtonDown())
00222         {
00223                 m_angleX = (x - m_beginX);
00224                 m_angleY = (y - m_beginY);
00225                 m_beginX = x;
00226                 m_beginY = y;
00227                 redraw();
00228         }
00229 
00230         if (isRightButtonDown())
00231         {
00232                 if (getModifierKey() == CIvfWidgetBase::MT_SHIFT)
00233                 {
00234                         m_zoomX = (x - m_beginX);
00235                         m_zoomY = (y - m_beginY);
00236                 }
00237                 else
00238                 {
00239                         m_moveX = (x - m_beginX);
00240                         m_moveY = (y - m_beginY);
00241                 }
00242                 m_beginX = x;
00243                 m_beginY = y;
00244 
00245                 redraw();
00246         }
00247 }
00248 
00249 // ------------------------------------------------------------
00250 void CExampleWindow::onMouseUp(int x, int y)
00251 {
00252         m_angleX = 0.0;
00253         m_angleY = 0.0;
00254         m_moveX = 0.0;
00255         m_moveY = 0.0;
00256         m_zoomX = 0.0;
00257         m_zoomY = 0.0;
00258 }
00259 
00260 // ------------------------------------------------------------
00261 bool CExampleWindow::onTimeout0()
00262 {
00263         m_controllers->update(0.01);
00264         redraw();
00265         return true;
00266 }
00267 
00268 // ------------------------------------------------------------
00269 void CExampleWindow::onKeyboard(int key, int x, int y)
00270 {
00271 }
00272 
00273 // ------------------------------------------------------------
00274 // Main program
00275 // ------------------------------------------------------------
00276 
00277 int main(int argc, char **argv) 
00278 {
00279         // Create Ivf++ application object.
00280 
00281         CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB);
00282 
00283         // Create a window
00284 
00285         CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512);
00286 
00287         // Set window title and show window
00288 
00289         window->setWindowTitle("Ivf++ Controller example");
00290         window->show();
00291 
00292         // Enter main application loop
00293 
00294     app->run();
00295 
00296         return 0;
00297 }

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