advgeom.cpp

This is an example of how to use the CIvfGLPrimitive derived classes.

00001 // ------------------------------------------------------------
00002 //
00003 // Ivf++ Advanced geometry 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/IvfMaterial.h>
00025 #include <ivf/IvfTexture.h>
00026 
00027 // CIvfGLPrimitive derived classes
00028 
00029 #include <ivf/IvfPointSet.h>
00030 #include <ivf/IvfLineSet.h>
00031 #include <ivf/IvfLineStripSet.h>
00032 #include <ivf/IvfTriSet.h>
00033 #include <ivf/IvfTriStripSet.h>
00034 #include <ivf/IvfQuadSet.h>
00035 
00036 // From the image library
00037 
00038 #include <ivfimage/IvfPngImage.h>
00039 
00040 // From the file library
00041 
00042 #include <ivffile/IvfDxfWriter.h>
00043 
00044 // ------------------------------------------------------------
00045 // Window class definition
00046 // ------------------------------------------------------------
00047 
00048 IvfSmartPointer(CExampleWindow);
00049 
00050 class CExampleWindow: public CIvfWindow {
00051 private:
00052         CIvfCameraPtr           m_camera;
00053         CIvfCompositePtr        m_scene;
00054         CIvfLightPtr            m_light;
00055 
00056         double m_angleX;
00057         double m_angleY;
00058         double m_moveX;
00059         double m_moveY;
00060         double m_zoomX;
00061         double m_zoomY;
00062 
00063         int m_beginX;
00064         int m_beginY;
00065 public:
00066         CExampleWindow(int X, int Y, int W, int H)
00067                 :CIvfWindow(X, Y, W, H) {};
00068 
00069         virtual void onInit(int width, int height);
00070         virtual void onResize(int width, int height);
00071         virtual void onRender();
00072         virtual void onMouseDown(int x, int y);
00073         virtual void onMouseMove(int x, int y);
00074         virtual void onMouseUp(int x, int y);
00075 };
00076 
00077 // ------------------------------------------------------------
00078 // Window class implementation
00079 // ------------------------------------------------------------
00080 
00081 void CExampleWindow::onInit(int width, int height)
00082 {
00083         // Initialize variables
00084 
00085         m_angleX = 0.0;
00086         m_angleY = 0.0;
00087         m_moveX = 0.0;
00088         m_moveY = 0.0;
00089         m_zoomX = 0.0;
00090         m_zoomY = 0.0;
00091 
00092         // Initialize Ivf++ camera
00093 
00094         m_camera = new CIvfCamera();
00095         m_camera->setPosition(-6.6, 9.2, 10.5);
00096         m_camera->setTarget(1.1, 0.1, 0.6);
00097 
00098         // Create a materials
00099 
00100         CIvfMaterialPtr greenMaterial = new CIvfMaterial();
00101         greenMaterial->setDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f);
00102         greenMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
00103         greenMaterial->setAmbientColor(0.0f, 0.2f, 0.0f, 1.0f);
00104         
00105         CIvfMaterialPtr redMaterial = new CIvfMaterial();
00106         redMaterial->setDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f);
00107         redMaterial->setSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
00108         redMaterial->setAmbientColor(0.2f, 0.0f, 0.0f, 1.0f);
00109 
00110         // Create textures
00111 
00112         CIvfPngImagePtr logoImage = new CIvfPngImage();
00113         logoImage->setFileName("images/ivf.png");
00114         logoImage->read();
00115 
00116         CIvfTexturePtr logoTexture = new CIvfTexture();
00117         logoTexture->setImage(logoImage);
00118 
00119         // Create scene composite
00120 
00121         m_scene = new CIvfComposite();
00122 
00123         // Create a PointSet
00124 
00125         CIvfPointSetPtr pointSet = new CIvfPointSet();
00126 
00127         pointSet->addCoord(0.0, 0.0, 0.0);
00128         pointSet->addCoord(0.0, 0.0, 1.0);
00129         pointSet->addCoord(1.0, 0.5, 0.0);
00130         pointSet->addCoord(1.0, 0.5, 1.0);
00131         pointSet->addCoord(2.0, 0.5, 0.0);
00132         pointSet->addCoord(2.0, 0.5, 1.0);
00133         pointSet->addCoord(3.0, 0.0, 0.0);
00134         pointSet->addCoord(3.0, 0.0, 1.0);
00135         
00136         CIvfIndexPtr coordIdx = new CIvfIndex();
00137         coordIdx->createLinear(8);
00138 
00139         pointSet->addCoordIndex(coordIdx);
00140 
00141         pointSet->addColor(0.0, 0.0, 1.0);
00142         pointSet->addColor(0.0, 1.0, 0.0);
00143         pointSet->addColor(0.0, 1.0, 1.0);
00144         pointSet->addColor(1.0, 0.0, 0.0);
00145         pointSet->addColor(1.0, 0.0, 1.0);
00146         pointSet->addColor(1.0, 1.0, 0.0);
00147         pointSet->addColor(1.0, 1.0, 1.0);
00148         pointSet->addColor(0.0, 0.0, 1.0);
00149 
00150         CIvfIndexPtr colorIdx = new CIvfIndex();
00151         colorIdx->createLinear(8);
00152         pointSet->addColorIndex(colorIdx);
00153         pointSet->setUseColor(true);
00154 
00155         pointSet->setPosition(-3.0, 0.0, 3.0);
00156         pointSet->setPointSize(3);
00157         
00158         m_scene->addChild(pointSet);
00159 
00160         // Create a LineSet
00161 
00162         CIvfLineSetPtr lineSet = new CIvfLineSet();     
00163         
00164         lineSet->addCoord(0.0, 0.0, 0.0);
00165         lineSet->addCoord(0.0, 0.0, 1.0);
00166         lineSet->addCoord(1.0, 0.5, 0.0);
00167         lineSet->addCoord(1.0, 0.5, 1.0);
00168         lineSet->addCoord(2.0, 0.5, 0.0);
00169         lineSet->addCoord(2.0, 0.5, 1.0);
00170         lineSet->addCoord(3.0, 0.0, 0.0);
00171         lineSet->addCoord(3.0, 0.0, 1.0);
00172         
00173         coordIdx = new CIvfIndex();
00174         coordIdx->createLinear(8);
00175 
00176         lineSet->addCoordIndex(coordIdx);
00177 
00178         lineSet->addColor(0.0, 0.0, 1.0);
00179         lineSet->addColor(0.0, 1.0, 0.0);
00180         lineSet->addColor(0.0, 1.0, 1.0);
00181         lineSet->addColor(1.0, 0.0, 0.0);
00182         lineSet->addColor(1.0, 0.0, 1.0);
00183         lineSet->addColor(1.0, 1.0, 0.0);
00184         lineSet->addColor(1.0, 1.0, 1.0);
00185         lineSet->addColor(0.0, 0.0, 1.0);
00186 
00187         colorIdx = new CIvfIndex();
00188         colorIdx->createLinear(8);
00189 
00190         lineSet->addColorIndex(colorIdx);
00191 
00192         lineSet->setPosition(1.5, 0.0, 3.0);
00193         lineSet->setMaterial(redMaterial);
00194         lineSet->setUseColor(true);
00195         lineSet->setLineWidth(2);
00196 
00197         m_scene->addChild(lineSet);
00198 
00199         // Create a LineStripSet
00200         
00201         CIvfLineStripSetPtr lineStripSet = new CIvfLineStripSet();
00202 
00203         lineStripSet->addCoord(0.0, 0.0, 0.0);
00204         lineStripSet->addCoord(0.0, 0.0, 1.0);
00205         lineStripSet->addCoord(1.0, 0.5, 0.0);
00206         lineStripSet->addCoord(1.0, 0.5, 1.0);
00207         lineStripSet->addCoord(2.0, 0.5, 0.0);
00208         lineStripSet->addCoord(2.0, 0.5, 1.0);
00209         lineStripSet->addCoord(3.0, 0.0, 0.0);
00210         lineStripSet->addCoord(3.0, 0.0, 1.0);
00211         
00212         coordIdx = new CIvfIndex();
00213         coordIdx->createLinear(8);
00214 
00215         lineStripSet->addCoordIndex(coordIdx);
00216 
00217         lineStripSet->addColor(0.0, 0.0, 1.0);
00218         lineStripSet->addColor(0.0, 1.0, 0.0);
00219         lineStripSet->addColor(0.0, 1.0, 1.0);
00220         lineStripSet->addColor(1.0, 0.0, 0.0);
00221         lineStripSet->addColor(1.0, 0.0, 1.0);
00222         lineStripSet->addColor(1.0, 1.0, 0.0);
00223         lineStripSet->addColor(1.0, 1.0, 1.0);
00224         lineStripSet->addColor(0.0, 0.0, 1.0);
00225 
00226         colorIdx = new CIvfIndex();
00227         colorIdx->createLinear(8);
00228 
00229         lineStripSet->addColorIndex(colorIdx);
00230 
00231         lineStripSet->setPosition(1.5, 3.0, 3.0);
00232         lineStripSet->setUseColor(true);
00233         lineStripSet->setLineWidth(2);
00234 
00235         m_scene->addChild(lineStripSet);
00236 
00237         // Create a TriSet
00238 
00239         CIvfTriSetPtr triSet = new CIvfTriSet();
00240         
00241         triSet->addCoord(0.0,0.0,2.0);
00242         triSet->addCoord(1.0,0.3,2.0);
00243         triSet->addCoord(2.0,0.0,2.0);
00244         triSet->addCoord(0.0,0.3,1.0);
00245         triSet->addCoord(1.0,0.5,1.0);
00246         triSet->addCoord(2.0,0.3,1.0);
00247         triSet->addCoord(0.0,0.0,0.0);
00248         triSet->addCoord(1.0,0.3,0.0);
00249         triSet->addCoord(2.0,0.0,0.0);
00250 
00251         coordIdx = new CIvfIndex();
00252         coordIdx->add(0,1,4);
00253         coordIdx->add(0,4,3);
00254         coordIdx->add(1,2,5);
00255         coordIdx->add(1,5,4);
00256         coordIdx->add(3,4,7);
00257         coordIdx->add(3,7,6);
00258         coordIdx->add(4,5,8);
00259         coordIdx->add(4,8,7);
00260 
00261         triSet->addCoordIndex(coordIdx);
00262 
00263         triSet->addTextureCoord(0.0,0.0);
00264         triSet->addTextureCoord(0.5,0.0);
00265         triSet->addTextureCoord(1.0,0.0);
00266         triSet->addTextureCoord(0.0,0.5);
00267         triSet->addTextureCoord(0.5,0.5);
00268         triSet->addTextureCoord(1.0,0.5);
00269         triSet->addTextureCoord(0.0,1.0);
00270         triSet->addTextureCoord(0.5,1.0);
00271         triSet->addTextureCoord(1.0,1.0);
00272 
00273         CIvfIndexPtr textureIdx = new CIvfIndex();
00274         textureIdx->assignFrom(coordIdx);
00275 
00276         triSet->addTextureIndex(textureIdx);
00277 
00278         triSet->setMaterial(greenMaterial);
00279         triSet->setTexture(logoTexture);
00280         //triSet->setUseVertexNormals(true);
00281         triSet->setPosition(-3.0, 0.0, -3.0);
00282 
00283         m_scene->addChild(triSet);
00284 
00285         // Create a TriStripSet
00286 
00287         CIvfTriStripSetPtr triStripSet = new CIvfTriStripSet();
00288 
00289         triStripSet->addCoord(0.0, 0.0, 0.0);
00290         triStripSet->addCoord(0.0, 0.0, 1.0);
00291         triStripSet->addCoord(1.0, 0.5, 0.0);
00292         triStripSet->addCoord(1.0, 0.5, 1.0);
00293         triStripSet->addCoord(2.0, 0.5, 0.0);
00294         triStripSet->addCoord(2.0, 0.5, 1.0);
00295         triStripSet->addCoord(3.0, 0.0, 0.0);
00296         triStripSet->addCoord(3.0, 0.0, 1.0);
00297 
00298         triStripSet->addCoord(0.0, 1.0, 0.0);
00299         triStripSet->addCoord(0.0, 1.0, 1.0);
00300         triStripSet->addCoord(1.0, 1.5, 0.0);
00301         triStripSet->addCoord(1.0, 1.5, 1.0);
00302         triStripSet->addCoord(2.0, 1.5, 0.0);
00303         triStripSet->addCoord(2.0, 1.5, 1.0);
00304         triStripSet->addCoord(3.0, 1.0, 0.0);
00305         triStripSet->addCoord(3.0, 1.0, 1.0);
00306 
00307         coordIdx = new CIvfIndex();
00308         coordIdx->createLinear(8);
00309 
00310         triStripSet->addCoordIndex(coordIdx);
00311 
00312         coordIdx = new CIvfIndex();
00313         coordIdx->createLinear(8,8);
00314 
00315         triStripSet->addCoordIndex(coordIdx);
00316 
00317         triStripSet->addColor(0.0, 0.0, 1.0);
00318         triStripSet->addColor(0.0, 1.0, 0.0);
00319         triStripSet->addColor(0.0, 1.0, 1.0);
00320         triStripSet->addColor(1.0, 0.0, 0.0);
00321         triStripSet->addColor(1.0, 0.0, 1.0);
00322         triStripSet->addColor(1.0, 1.0, 0.0);
00323         triStripSet->addColor(1.0, 1.0, 1.0);
00324         triStripSet->addColor(0.0, 0.0, 1.0);
00325 
00326         colorIdx = new CIvfIndex();
00327         colorIdx->createLinear(8);
00328 
00329         triStripSet->addColorIndex(colorIdx);
00330 
00331         triStripSet->setMaterial(redMaterial);
00332         triStripSet->setUseColor(true);
00333         triStripSet->setUseVertexNormals(true);
00334         triStripSet->setPosition(1.5, 0.0, -3.0);
00335 
00336         m_scene->addChild(triStripSet);
00337 
00338         // Create a QuadSet
00339 
00340         CIvfQuadSetPtr quadSet = new CIvfQuadSet();
00341         
00342         quadSet->addCoord(0.0,0.0,1.0);
00343         quadSet->addCoord(1.0,0.0,1.0);
00344         quadSet->addCoord(1.0,0.0,0.0);
00345         quadSet->addCoord(0.0,0.0,0.0);
00346         quadSet->addCoord(0.0,1.0,1.0);
00347         quadSet->addCoord(1.0,1.0,1.0);
00348         quadSet->addCoord(1.0,1.0,0.0);
00349         quadSet->addCoord(0.0,1.0,0.0);
00350 
00351         coordIdx = new CIvfIndex();
00352         coordIdx->add(0,1,5,4);
00353         coordIdx->add(1,2,6,5);
00354         coordIdx->add(2,3,7,6);
00355         coordIdx->add(3,0,4,7);
00356         coordIdx->add(4,5,6,7);
00357         coordIdx->add(0,3,2,1);
00358 
00359         quadSet->addCoordIndex(coordIdx);
00360 
00361         quadSet->addColor(0.0, 0.0, 1.0);
00362         quadSet->addColor(0.0, 1.0, 0.0);
00363         quadSet->addColor(0.0, 1.0, 1.0);
00364         quadSet->addColor(1.0, 0.0, 0.0);
00365         quadSet->addColor(1.0, 0.0, 1.0);
00366         quadSet->addColor(1.0, 1.0, 0.0);
00367         quadSet->addColor(1.0, 1.0, 1.0);
00368         quadSet->addColor(0.0, 0.0, 1.0);
00369         
00370         colorIdx = new CIvfIndex();
00371         colorIdx->assignFrom(coordIdx);
00372         
00373         quadSet->addColorIndex(colorIdx);
00374 
00375         quadSet->setUseColor(true);
00376         quadSet->setPosition(-3.0, 3.0, -3.0);
00377 
00378         m_scene->addChild(quadSet);
00379 
00380         CIvfAxisPtr axis = new CIvfAxis();
00381         axis->setSize(1.5);
00382         m_scene->addChild(axis);
00383         
00384         // Create a light
00385 
00386         CIvfLightingPtr lighting = CIvfLighting::getInstance();
00387         lighting->enable();
00388 
00389         m_light = lighting->getLight(0);
00390         m_light->setLightPosition(1.0, 1.0, 1.0, 0.0);
00391         m_light->setAmbientColor(0.2f, 0.2f, 0.2f, 1.0f); 
00392         m_light->enable();
00393 
00394         // Export all to a DXF file
00395 
00396         /*
00397         CIvfDxfWriterPtr dxfWriter = new CIvfDxfWriter();
00398         dxfWriter->setFileName("advgeom.dxf");
00399         dxfWriter->setShape(m_scene);
00400         dxfWriter->write();
00401         */
00402 }
00403 
00404 // ------------------------------------------------------------
00405 void CExampleWindow::onResize(int width, int height)
00406 {
00407         m_camera->setPerspective(45.0, 0.1, 100.0);
00408         m_camera->setViewPort(width, height);
00409         m_camera->initialize();
00410 }
00411 
00412 // ------------------------------------------------------------
00413 void CExampleWindow::onRender()
00414 {
00415         m_light->render();
00416         m_camera->render();
00417         m_scene->render();
00418 }
00419 
00420 // ------------------------------------------------------------
00421 void CExampleWindow::onMouseDown(int x, int y)
00422 {
00423         m_beginX = x;
00424         m_beginY = y;
00425 }
00426 
00427 // ------------------------------------------------------------
00428 void CExampleWindow::onMouseMove(int x, int y)
00429 {
00430         m_angleX = 0.0;
00431         m_angleY = 0.0;
00432         m_moveX = 0.0;
00433         m_moveY = 0.0;
00434         m_zoomX = 0.0;
00435         m_zoomY = 0.0;
00436 
00437         if (isLeftButtonDown())
00438         {
00439                 m_angleX = (x - m_beginX);
00440                 m_angleY = (y - m_beginY);
00441                 m_beginX = x;
00442                 m_beginY = y;
00443                 m_camera->rotatePositionY(m_angleX/100.0);
00444                 m_camera->rotatePositionX(m_angleY/100.0);
00445                 redraw();
00446         }
00447 
00448         if (isRightButtonDown())
00449         {
00450                 if (getModifierKey() == CIvfWidgetBase::MT_SHIFT)
00451                 {
00452                         m_zoomX = (x - m_beginX);
00453                         m_zoomY = (y - m_beginY);
00454                 }
00455                 else
00456                 {
00457                         m_moveX = (x - m_beginX);
00458                         m_moveY = (y - m_beginY);
00459                 }
00460                 m_beginX = x;
00461                 m_beginY = y;
00462 
00463                 m_camera->moveSideways(m_moveX/100.0);
00464                 m_camera->moveVertical(m_moveY/100.0);
00465                 m_camera->moveDepth(m_zoomY/50.0);
00466 
00467                 redraw();
00468         }
00469 }
00470 
00471 // ------------------------------------------------------------
00472 void CExampleWindow::onMouseUp(int x, int y)
00473 {
00474         m_angleX = 0.0;
00475         m_angleY = 0.0;
00476         m_moveX = 0.0;
00477         m_moveY = 0.0;
00478         m_zoomX = 0.0;
00479         m_zoomY = 0.0;
00480 }
00481 
00482 // ------------------------------------------------------------
00483 // Main program
00484 // ------------------------------------------------------------
00485 
00486 int main(int argc, char **argv) 
00487 {
00488         // Create Ivf++ application object.
00489 
00490         CIvfApplicationPtr app = new CIvfApplication(IVF_DOUBLE|IVF_RGB);
00491 
00492         // Create a window
00493 
00494         CExampleWindowPtr window = new CExampleWindow(0, 0, 512, 512);
00495 
00496         // Set window title and show window
00497 
00498         window->setWindowTitle("Ivf++ Advanced geometry");
00499         window->show();
00500 
00501         // Enter main application loop
00502 
00503     app->run();
00504 
00505         return 0;
00506 }

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