00001 // ------------------------------------------------------------ 00002 // 00003 // Ivf++ Extrusion 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/IvfMaterial.h> 00023 #include <ivf/IvfExtrusion.h> 00024 00025 // ------------------------------------------------------------ 00026 // Window class definition 00027 // ------------------------------------------------------------ 00028 00029 IvfSmartPointer(CExampleWindow); 00030 00031 class CExampleWindow: public CIvfWindow { 00032 private: 00033 CIvfCameraPtr m_camera; 00034 CIvfCompositePtr m_scene; 00035 CIvfLightPtr m_light; 00036 00037 double m_angleX; 00038 double m_angleY; 00039 double m_moveX; 00040 double m_moveY; 00041 double m_zoomX; 00042 double m_zoomY; 00043 00044 int m_beginX; 00045 int m_beginY; 00046 00047 public: 00048 CExampleWindow(int X, int Y, int W, int H) 00049 :CIvfWindow(X, Y, W, H) {}; 00050 00051 virtual void onInit(int width, int height); 00052 virtual void onResize(int width, int height); 00053 virtual void onRender(); 00054 virtual void onMouseDown(int x, int y); 00055 virtual void onMouseMove(int x, int y); 00056 virtual void onMouseUp(int x, int y); 00057 }; 00058 00059 // ------------------------------------------------------------ 00060 // Window class implementation 00061 // ------------------------------------------------------------ 00062 00063 void CExampleWindow::onInit(int width, int height) 00064 { 00065 // Initialize variables 00066 00067 m_angleX = 0.0; 00068 m_angleY = 0.0; 00069 m_moveX = 0.0; 00070 m_moveY = 0.0; 00071 m_zoomX = 0.0; 00072 m_zoomY = 0.0; 00073 00074 int i, nSides; 00075 double r, angle, x, y; 00076 00077 // Initialize Ivf++ camera 00078 00079 m_camera = new CIvfCamera(); 00080 m_camera->setPosition(0.0, 5.0, 5.0); 00081 00082 // Create a materials 00083 00084 CIvfMaterialPtr yellowMaterial = new CIvfMaterial(); 00085 yellowMaterial->setDiffuseColor(1.0f, 1.0f, 0.0f, 1.0f); 00086 yellowMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f); 00087 yellowMaterial->setAmbientColor(0.5f, 0.5f, 0.0f, 1.0f); 00088 00089 // Create scene composite 00090 00091 m_scene = new CIvfComposite(); 00092 00093 // Create extrusion 00094 00095 CIvfExtrusionPtr extrusion = new CIvfExtrusion(); 00096 00097 // Create section 00098 00099 r = 0.5; 00100 nSides = 12; 00101 00102 extrusion->setSectionSize(nSides + 1); 00103 00104 for (i = 0; i<=nSides; i++) 00105 { 00106 angle = 2.0*M_PI*( ((double)i) / ((double)nSides) ); 00107 x = r * cos(angle); 00108 y = r * sin(angle); 00109 extrusion->setSectionCoord(i, x, y); 00110 extrusion->setSectionNormal(i, x/r, y/r); 00111 } 00112 00113 // Set spine 00114 00115 extrusion->setSpineSize(6); 00116 extrusion->setSpineCoord(0, 0.5, 0.0, 1.5); 00117 extrusion->setSpineCoord(1, 1.0, 0.0, 1.0); 00118 extrusion->setSpineCoord(2, 1.0, 0.0, -1.0); 00119 extrusion->setSpineCoord(3, -1.0, 0.0, -1.0); 00120 extrusion->setSpineCoord(4, -1.0, 0.0, 1.0); 00121 extrusion->setSpineCoord(5, -0.5, 0.0, 1.5); 00122 00123 // Set up-vector 00124 00125 extrusion->setUpVector(0.0, 1.0, 0.0); 00126 00127 // Set join style 00128 00129 extrusion->setJoinStyle(TUBE_NORM_EDGE|TUBE_JN_ANGLE|TUBE_JN_CAP); 00130 00131 // Set other properties 00132 00133 extrusion->setMaterial(yellowMaterial); 00134 m_scene->addChild(extrusion); 00135 00136 // First point 00137 00138 CIvfAxisPtr axis = new CIvfAxis(); 00139 m_scene->addChild(axis); 00140 00141 // Create a light 00142 00143 CIvfLightingPtr lighting = CIvfLighting::getInstance(); 00144 00145 m_light = new CIvfLight(); 00146 m_light->setLightPosition(1.0, 1.0, 1.0, 0.0); 00147 m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 00148 m_light->enable(); 00149 } 00150 00151 // ------------------------------------------------------------ 00152 void CExampleWindow::onResize(int width, int height) 00153 { 00154 m_camera->setPerspective(45.0, 0.1, 100.0); 00155 m_camera->setViewPort(width, height); 00156 m_camera->initialize(); 00157 } 00158 00159 // ------------------------------------------------------------ 00160 void CExampleWindow::onRender() 00161 { 00162 m_light->render(); 00163 m_camera->render(); 00164 m_scene->render(); 00165 } 00166 00167 // ------------------------------------------------------------ 00168 void CExampleWindow::onMouseDown(int x, int y) 00169 { 00170 m_beginX = x; 00171 m_beginY = y; 00172 } 00173 00174 // ------------------------------------------------------------ 00175 void CExampleWindow::onMouseMove(int x, int y) 00176 { 00177 m_angleX = 0.0; 00178 m_angleY = 0.0; 00179 m_moveX = 0.0; 00180 m_moveY = 0.0; 00181 m_zoomX = 0.0; 00182 m_zoomY = 0.0; 00183 00184 if (isLeftButtonDown()) 00185 { 00186 m_angleX = (x - m_beginX); 00187 m_angleY = (y - m_beginY); 00188 m_beginX = x; 00189 m_beginY = y; 00190 m_camera->rotatePositionY(m_angleX/100.0); 00191 m_camera->rotatePositionX(m_angleY/100.0); 00192 redraw(); 00193 } 00194 00195 if (isRightButtonDown()) 00196 { 00197 if (getModifierKey() == CIvfWidgetBase::MT_SHIFT) 00198 { 00199 m_zoomX = (x - m_beginX); 00200 m_zoomY = (y - m_beginY); 00201 } 00202 else 00203 { 00204 m_moveX = (x - m_beginX); 00205 m_moveY = (y - m_beginY); 00206 } 00207 m_beginX = x; 00208 m_beginY = y; 00209 00210 m_camera->moveSideways(m_moveX/100.0); 00211 m_camera->moveVertical(m_moveY/100.0); 00212 m_camera->moveDepth(m_zoomY/50.0); 00213 00214 redraw(); 00215 } 00216 } 00217 00218 // ------------------------------------------------------------ 00219 void CExampleWindow::onMouseUp(int x, int y) 00220 { 00221 m_angleX = 0.0; 00222 m_angleY = 0.0; 00223 m_moveX = 0.0; 00224 m_moveY = 0.0; 00225 m_zoomX = 0.0; 00226 m_zoomY = 0.0; 00227 } 00228 00229 // ------------------------------------------------------------ 00230 // Main program 00231 // ------------------------------------------------------------ 00232 00233 int main(int argc, char **argv) 00234 { 00235 // Create Ivf++ application object. 00236 00237 CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB); 00238 00239 // Create a window 00240 00241 CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512); 00242 00243 // Set window title and show window 00244 00245 window->setWindowTitle("Ivf++ Extrusion example"); 00246 window->show(); 00247 00248 // Enter main application loop 00249 00250 app->run(); 00251 00252 return 0; 00253 }