00001 // ------------------------------------------------------------ 00002 // 00003 // Ivf++ Robot arm 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/IvfSphere.h> 00019 #include <ivf/IvfCylinder.h> 00020 #include <ivf/IvfAxis.h> 00021 #include <ivf/IvfComposite.h> 00022 #include <ivf/IvfTransform.h> 00023 #include <ivf/IvfLighting.h> 00024 #include <ivf/IvfLight.h> 00025 #include <ivf/IvfMaterial.h> 00026 00027 // ------------------------------------------------------------ 00028 // Window class definition 00029 // ------------------------------------------------------------ 00030 00031 IvfSmartPointer(CExampleWindow); 00032 00033 class CExampleWindow: public CIvfWindow { 00034 private: 00035 CIvfCameraPtr m_camera; 00036 CIvfCompositePtr m_scene; 00037 CIvfLightPtr m_light; 00038 00039 CIvfTransformPtr m_part1; 00040 CIvfTransformPtr m_part2; 00041 CIvfTransformPtr m_part3; 00042 CIvfTransformPtr m_arm; 00043 00044 double m_alfa; 00045 double m_beta; 00046 double m_gamma; 00047 double m_delta; 00048 00049 void updateArm(); 00050 public: 00051 CExampleWindow(int X, int Y, int W, int H) 00052 :CIvfWindow(X, Y, W, H) {}; 00053 00054 00055 virtual void onInit(int width, int height); 00056 virtual void onResize(int width, int height); 00057 virtual void onRender(); 00058 virtual void onDestroy(); 00059 virtual void onKeyboard(int key, int x, int y); 00060 }; 00061 00062 // ------------------------------------------------------------ 00063 // Window class implementation 00064 // ------------------------------------------------------------ 00065 00066 void CExampleWindow::onInit(int width, int height) 00067 { 00068 // Initialize variables 00069 00070 m_alfa = 0.0; 00071 m_beta = -45.0; 00072 m_gamma = 90.0; 00073 m_delta = 75.0; 00074 00075 // Initialize Ivf++ camera 00076 00077 m_camera = new CIvfCamera(); 00078 m_camera->setPosition(0.0, 5.0, 5.0); 00079 00080 // Create a materials 00081 00082 CIvfMaterialPtr redMaterial = new CIvfMaterial(); 00083 redMaterial->setDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f); 00084 redMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00085 redMaterial->setAmbientColor(0.5f, 0.0f, 0.0f, 1.0f); 00086 00087 CIvfMaterialPtr greenMaterial = new CIvfMaterial(); 00088 greenMaterial->setDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f); 00089 greenMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00090 greenMaterial->setAmbientColor(0.0f, 0.5f, 0.0f, 1.0f); 00091 00092 CIvfMaterialPtr blueMaterial = new CIvfMaterial(); 00093 blueMaterial->setDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f); 00094 blueMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00095 blueMaterial->setAmbientColor(0.0f, 0.0f, 0.5f, 1.0f); 00096 00097 CIvfMaterialPtr yellowMaterial = new CIvfMaterial(); 00098 yellowMaterial->setDiffuseColor(1.0f, 1.0f, 0.0f, 1.0f); 00099 yellowMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00100 yellowMaterial->setAmbientColor(0.5f, 0.5f, 0.0f, 1.0f); 00101 00102 // Create scene composite 00103 00104 m_scene = new CIvfComposite(); 00105 00106 // Create robot base 00107 00108 CIvfCylinderPtr lowerBase = new CIvfCylinder(); 00109 lowerBase->setHeight(0.2); 00110 lowerBase->setMaterial(yellowMaterial); 00111 m_scene->addChild(lowerBase); 00112 00113 CIvfCylinderPtr upperBase = new CIvfCylinder(); 00114 upperBase->setHeight(0.3); 00115 upperBase->setRadius(0.5); 00116 upperBase->setPosition(0.0, 0.1 + 0.15, 0.0); 00117 upperBase->setMaterial(blueMaterial); 00118 m_scene->addChild(upperBase); 00119 00120 // Create part1 00121 00122 m_part1 = new CIvfTransform(); 00123 00124 CIvfSpherePtr sphere = new CIvfSphere(); 00125 sphere->setRadius(0.1); 00126 sphere->setMaterial(redMaterial); 00127 m_part1->addChild(sphere); 00128 00129 CIvfCylinderPtr cylinder = new CIvfCylinder(); 00130 cylinder->setRadius(0.1); 00131 cylinder->setHeight(1.0); 00132 cylinder->setMaterial(greenMaterial); 00133 cylinder->setPosition(0.0, 0.1 + 0.5, 0.0); 00134 00135 m_part1->addChild(cylinder); 00136 m_part1->setPosition(0.0, 1.2, 0.0); 00137 m_part1->setRotationQuat(0.0, 0.0, 1.0, m_delta); 00138 00139 // Create part2 00140 00141 m_part2 = new CIvfTransform(); 00142 00143 sphere = new CIvfSphere(); 00144 sphere->setRadius(0.1); 00145 sphere->setMaterial(redMaterial); 00146 m_part2->addChild(sphere); 00147 00148 cylinder = new CIvfCylinder(); 00149 cylinder->setRadius(0.1); 00150 cylinder->setHeight(1.0); 00151 cylinder->setMaterial(greenMaterial); 00152 cylinder->setPosition(0.0, 0.1 + 0.5, 0.0); 00153 m_part2->addChild(cylinder); 00154 m_part2->addChild(m_part1); 00155 m_part2->setPosition(0.0, 1.2, 0.0); 00156 m_part2->setRotationQuat(0.0, 0.0, 1.0, m_gamma); 00157 00158 // Create part3 00159 00160 m_part3 = new CIvfTransform(); 00161 00162 sphere = new CIvfSphere(); 00163 sphere->setRadius(0.1); 00164 sphere->setMaterial(redMaterial); 00165 m_part3->addChild(sphere); 00166 00167 cylinder = new CIvfCylinder(); 00168 cylinder->setRadius(0.1); 00169 cylinder->setHeight(1.0); 00170 cylinder->setMaterial(greenMaterial); 00171 cylinder->setPosition(0.0, 0.1 + 0.5, 0.0); 00172 m_part3->addChild(cylinder); 00173 m_part3->addChild(m_part2); 00174 m_part3->setPosition(0.0, 0.0, 0.0); 00175 m_part3->setRotationQuat(0.0, 0.0, 1.0, m_beta); 00176 00177 // Create complete arm 00178 00179 m_arm = new CIvfTransform(); 00180 m_arm->addChild(m_part3); 00181 m_arm->setPosition(0.0, 0.1+0.4, 0.0); 00182 m_arm->setRotationQuat(0.0, 1.0, 0.0, m_alfa); 00183 00184 m_scene->addChild(m_arm); 00185 00186 CIvfAxisPtr axis = new CIvfAxis(); 00187 axis->setSize(1.5); 00188 m_scene->addChild(axis); 00189 00190 // Create a light 00191 00192 CIvfLightingPtr lighting = CIvfLighting::getInstance(); 00193 00194 m_light = lighting->getLight(0); 00195 m_light->setLightPosition(1.0, 1.0, 1.0, 0.0); 00196 m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 00197 m_light->enable(); 00198 } 00199 00200 // ------------------------------------------------------------ 00201 void CExampleWindow::onResize(int width, int height) 00202 { 00203 m_camera->setPerspective(45.0, 0.1, 100.0); 00204 m_camera->setViewPort(width, height); 00205 m_camera->initialize(); 00206 } 00207 00208 // ------------------------------------------------------------ 00209 void CExampleWindow::onRender() 00210 { 00211 m_light->render(); 00212 m_camera->render(); 00213 m_scene->render(); 00214 } 00215 00216 // ------------------------------------------------------------ 00217 void CExampleWindow::onDestroy() 00218 { 00219 delete m_camera; 00220 delete m_light; 00221 delete m_scene; 00222 } 00223 00224 // ------------------------------------------------------------ 00225 void CExampleWindow::onKeyboard(int key, int x, int y) 00226 { 00227 switch (key) { 00228 case 'a': 00229 m_alfa += 5.0; 00230 updateArm(); 00231 break; 00232 case 'z': 00233 m_alfa -= 5.0; 00234 updateArm(); 00235 break; 00236 case 's': 00237 m_beta += 5.0; 00238 updateArm(); 00239 break; 00240 case 'x': 00241 m_beta -= 5.0; 00242 updateArm(); 00243 break; 00244 case 'd': 00245 m_gamma += 5.0; 00246 updateArm(); 00247 break; 00248 case 'c': 00249 m_gamma -= 5.0; 00250 updateArm(); 00251 break; 00252 case 'f': 00253 m_delta += 5.0; 00254 updateArm(); 00255 break; 00256 case 'v': 00257 m_delta -= 5.0; 00258 updateArm(); 00259 break; 00260 default: 00261 break; 00262 } 00263 } 00264 00265 // ------------------------------------------------------------ 00266 void CExampleWindow::updateArm() 00267 { 00268 m_arm->setRotationQuat(0.0, 1.0, 0.0, m_alfa); 00269 m_part1->setRotationQuat(0.0, 0.0, 1.0, m_delta); 00270 m_part2->setRotationQuat(0.0, 0.0, 1.0, m_gamma); 00271 m_part3->setRotationQuat(0.0, 0.0, 1.0, m_beta); 00272 00273 redraw(); 00274 } 00275 00276 // ------------------------------------------------------------ 00277 // Main program 00278 // ------------------------------------------------------------ 00279 00280 int main(int argc, char **argv) 00281 { 00282 // Create Ivf++ application object. 00283 00284 CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB); 00285 00286 // Create a window 00287 00288 CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512); 00289 00290 // Set window title and show window 00291 00292 window->setWindowTitle("Ivf++ Robot example"); 00293 window->show(); 00294 00295 // Enter main application loop 00296 00297 app->run(); 00298 00299 return 0; 00300 }