Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

simpleMeshViewer

Introduction

This example shows how to use the easy-to-use SimpleMeshScene class. We read the simplified MESH scene graph and draw the vertices, edges and faces. SimpleMeshScene encapsulates basic operations: loading an X3D file into an X3D scene graph, computing the bounding box of the scene, computing a simplified MESH scene graph that only has two nodes: Mesh and Vertex.

Drawer

We implement a small drawer which draws the scene loaded by the SimpleMeshScene. Drawer draws the vertices, edges and faces. It provides an example of requests that can be done to the Mesh node.

Code

Drawer.h

#ifndef MESH_DRAWER_H #define MESH_DRAWER_H #include <X3DTK/kernel.h> #include <X3DTK/MESH/scenegraph.h> namespace X3DTK { namespace MESH { // Processor drawing the mesh from the Mesh scene graph. class Drawer { public: void drawVertices(Mesh *M); void drawEdges(Mesh *M); void drawFaces(Mesh *M); }; } } #endif
00001 #ifndef MESH_DRAWER_H 00002 #define MESH_DRAWER_H 00003 00004 #include <X3DTK/kernel.h> 00005 #include <X3DTK/MESH/scenegraph.h> 00006 00007 namespace X3DTK { 00008 namespace MESH { 00009 00010 // Processor drawing the mesh from the Mesh scene graph. 00011 00012 class Drawer 00013 { 00014 public: 00015 void drawVertices(Mesh *M); 00016 void drawEdges(Mesh *M); 00017 void drawFaces(Mesh *M); 00018 }; 00019 00020 } 00021 } 00022 00023 #endif 00024

Drawer.cpp

#include "Drawer.h" #include <iostream> using namespace std; namespace X3DTK { namespace MESH { void Drawer::drawVertices(Mesh *M) { glDisable(GL_LIGHTING); glColor3f(1.0f, 0.9f, 0.8f); glPointSize(1.0f); // Getting the vertices. const Mesh::MFVertex &vertices = M->getVertices(); glBegin(GL_POINTS); // iterating the points. for (Mesh::MFVertex::const_iterator v = vertices.begin(), end = vertices.end() ; v != end; ++v) { // Getting the point data. const SFPoint3f &P = (*v)->data().getPoint(); glVertex3fv(P); } glEnd(); } void Drawer::drawEdges(Mesh *M) { glDisable(GL_LIGHTING); // Getting the edges. const Mesh::MFEdge &edges = M->getEdges(); glBegin(GL_LINES); // iterating the edges. for (Mesh::MFEdge::const_iterator e = edges.begin(), end = edges.end() ; e != end; ++e) { // drawing in blue color if the edge is a boundary. if ((*e)->isBoundary()) glColor3f(0.3, 0.3, 0.9); else glColor3f(0.7, 0.7, 0.7); // Getting the extremities of the edge. const SFPoint3f &A = (*e)->getFromVertex()->data().getPoint(); glVertex3fv(A); const SFPoint3f &B = (*e)->getToVertex()->data().getPoint(); glVertex3fv(B); } glEnd(); } void Drawer::drawFaces(Mesh *M) { glEnable(GL_LIGHTING); // Getting the properties of the Mesh. bool hasColor = M->data().hasColor(); // Getting the faces. const Mesh::MFFace &faces = M->getFaces(); // iterating the faces. for (Mesh::MFFace::const_iterator f = faces.begin(), end = faces.end() ; f != end; ++f) { glBegin(GL_POLYGON); // getting the edges of the face. const SFFace::MFEdge &edges = (*f)->getEdges(); // iterating the edges to iterate the vertices of the face. for (SFFace::MFEdge::const_iterator e = edges.begin(), end = edges.end() ; e != end; ++e) { // Getting the vertex. SFVertex *v = (*e)->getFromVertex(); // setting the normal of the vertex. const SFVec3f &N = v->data().getNormalOfFace(*f); glNormal3fv(N); // setting the color of the vertex. if (hasColor) { const SFColorRGBA &C = v->data().getColorOfFace(*f); glColor4fv(C); } else glColor4f(0.8f, 0.8f, 0.8f, 1.0f); // Getting the point data. const SFPoint3f &P = v->data().getPoint(); glVertex3fv(P); } glEnd(); } } } }
00001 #include "Drawer.h" 00002 00003 #include <iostream> 00004 00005 using namespace std; 00006 00007 namespace X3DTK { 00008 namespace MESH { 00009 00010 void Drawer::drawVertices(Mesh *M) 00011 { 00012 glDisable(GL_LIGHTING); 00013 glColor3f(1.0f, 0.9f, 0.8f); 00014 glPointSize(1.0f); 00015 00016 // Getting the vertices. 00017 const Mesh::MFVertex &vertices = M->getVertices(); 00018 00019 glBegin(GL_POINTS); 00020 // iterating the points. 00021 for (Mesh::MFVertex::const_iterator v = vertices.begin(), end = vertices.end() ; v != end; ++v) 00022 { 00023 // Getting the point data. 00024 const SFPoint3f &P = (*v)->data().getPoint(); 00025 glVertex3fv(P); 00026 } 00027 glEnd(); 00028 } 00029 00030 void Drawer::drawEdges(Mesh *M) 00031 { 00032 glDisable(GL_LIGHTING); 00033 00034 // Getting the edges. 00035 const Mesh::MFEdge &edges = M->getEdges(); 00036 00037 glBegin(GL_LINES); 00038 // iterating the edges. 00039 for (Mesh::MFEdge::const_iterator e = edges.begin(), end = edges.end() ; e != end; ++e) 00040 { 00041 // drawing in blue color if the edge is a boundary. 00042 if ((*e)->isBoundary()) 00043 glColor3f(0.3, 0.3, 0.9); 00044 else 00045 glColor3f(0.7, 0.7, 0.7); 00046 00047 // Getting the extremities of the edge. 00048 const SFPoint3f &A = (*e)->getFromVertex()->data().getPoint(); 00049 glVertex3fv(A); 00050 const SFPoint3f &B = (*e)->getToVertex()->data().getPoint(); 00051 glVertex3fv(B); 00052 } 00053 glEnd(); 00054 } 00055 00056 void Drawer::drawFaces(Mesh *M) 00057 { 00058 glEnable(GL_LIGHTING); 00059 00060 // Getting the properties of the Mesh. 00061 bool hasColor = M->data().hasColor(); 00062 00063 // Getting the faces. 00064 const Mesh::MFFace &faces = M->getFaces(); 00065 00066 // iterating the faces. 00067 for (Mesh::MFFace::const_iterator f = faces.begin(), end = faces.end() ; f != end; ++f) 00068 { 00069 glBegin(GL_POLYGON); 00070 // getting the edges of the face. 00071 const SFFace::MFEdge &edges = (*f)->getEdges(); 00072 // iterating the edges to iterate the vertices of the face. 00073 for (SFFace::MFEdge::const_iterator e = edges.begin(), end = edges.end() ; e != end; ++e) 00074 { 00075 // Getting the vertex. 00076 SFVertex *v = (*e)->getFromVertex(); 00077 // setting the normal of the vertex. 00078 const SFVec3f &N = v->data().getNormalOfFace(*f); 00079 glNormal3fv(N); 00080 00081 // setting the color of the vertex. 00082 if (hasColor) 00083 { 00084 const SFColorRGBA &C = v->data().getColorOfFace(*f); 00085 glColor4fv(C); 00086 } 00087 else 00088 glColor4f(0.8f, 0.8f, 0.8f, 1.0f); 00089 00090 // Getting the point data. 00091 const SFPoint3f &P = v->data().getPoint(); 00092 glVertex3fv(P); 00093 } 00094 glEnd(); 00095 } 00096 } 00097 00098 } 00099 } 00100

Viewer.h

#ifndef VIEWER_H #define VIEWER_H #include <QGLViewer/qglviewer.h> #include <X3DTK/simplemeshscene.h> #include "Drawer.h" typedef enum {VERTEX, EDGE, FACE} Entity; // Viewer for the meshViewer. class Viewer : public QGLViewer { public: Viewer(); protected : void loadFile(); void keyPressEvent(QKeyEvent *e); void init(); void draw(); private: X3DTK::SimpleMeshScene scene; X3DTK::MESH::Drawer drawer; Entity entity; }; #endif
00001 #ifndef VIEWER_H 00002 #define VIEWER_H 00003 00004 #include <QGLViewer/qglviewer.h> 00005 #include <X3DTK/simplemeshscene.h> 00006 00007 #include "Drawer.h" 00008 00009 typedef enum {VERTEX, EDGE, FACE} Entity; 00010 00011 // Viewer for the meshViewer. 00012 00013 class Viewer : public QGLViewer 00014 { 00015 public: 00016 Viewer(); 00017 00018 protected : 00019 void loadFile(); 00020 void keyPressEvent(QKeyEvent *e); 00021 void init(); 00022 void draw(); 00023 00024 private: 00025 X3DTK::SimpleMeshScene scene; 00026 X3DTK::MESH::Drawer drawer; 00027 Entity entity; 00028 }; 00029 00030 #endif

Viewer.cpp

#include "Viewer.h" #include <math.h> #include <iostream> #include <qfiledialog.h> #include <qmessagebox.h> using namespace std; namespace X3DTK { // Constructor initialized with FACE drawing. Viewer::Viewer() : entity(FACE) { } // Changing entity flag. void Viewer::keyPressEvent(QKeyEvent *e) { switch (e->key()) { case Qt::Key_L : loadFile(); break; case Qt::Key_V : entity = VERTEX; break; case Qt::Key_E : entity = EDGE; break; case Qt::Key_F : entity = FACE; break; default : QGLViewer::keyPressEvent(e); } // Refreshing drawing to take in account appearance changes. updateGL(); } void Viewer::loadFile() { QString name = QFileDialog::getOpenFileName("", "X3D files (*.x3d *.X3D);;All files (*)", this); // In case of Cancel if (name.isEmpty()) return; // Loads the file name. scene.load(name); // QGLViewer settings. setSceneBoundingBox(scene.getBBoxMin(), scene.getBBoxMax()); showEntireScene(); } void Viewer::init() { glPolygonOffset(-2.0, -2.0); #ifdef GL_RESCALE_NORMAL glEnable(GL_RESCALE_NORMAL); #endif loadFile(); } void Viewer::draw() { if (scene.getMesh() == 0) return; // Switching entity. switch (entity) { case VERTEX: drawer.drawVertices(scene.getMesh()); break; case EDGE: drawer.drawEdges(scene.getMesh()); break; case FACE: drawer.drawFaces(scene.getMesh()); break; default: break; } } }
00001 #include "Viewer.h" 00002 #include <math.h> 00003 #include <iostream> 00004 #include <qfiledialog.h> 00005 #include <qmessagebox.h> 00006 00007 using namespace std; 00008 00009 namespace X3DTK { 00010 00011 // Constructor initialized with FACE drawing. 00012 00013 Viewer::Viewer() 00014 : entity(FACE) 00015 { 00016 } 00017 00018 // Changing entity flag. 00019 00020 void Viewer::keyPressEvent(QKeyEvent *e) 00021 { 00022 switch (e->key()) 00023 { 00024 case Qt::Key_L : loadFile(); break; 00025 case Qt::Key_V : entity = VERTEX; break; 00026 case Qt::Key_E : entity = EDGE; break; 00027 case Qt::Key_F : entity = FACE; break; 00028 default : QGLViewer::keyPressEvent(e); 00029 } 00030 // Refreshing drawing to take in account appearance changes. 00031 updateGL(); 00032 } 00033 00034 void Viewer::loadFile() 00035 { 00036 QString name = QFileDialog::getOpenFileName("", "X3D files (*.x3d *.X3D);;All files (*)", this); 00037 00038 // In case of Cancel 00039 if (name.isEmpty()) 00040 return; 00041 00042 // Loads the file name. 00043 scene.load(name); 00044 00045 // QGLViewer settings. 00046 setSceneBoundingBox(scene.getBBoxMin(), scene.getBBoxMax()); 00047 showEntireScene(); 00048 } 00049 00050 void Viewer::init() 00051 { 00052 glPolygonOffset(-2.0, -2.0); 00053 #ifdef GL_RESCALE_NORMAL 00054 glEnable(GL_RESCALE_NORMAL); 00055 #endif 00056 00057 loadFile(); 00058 } 00059 00060 void Viewer::draw() 00061 { 00062 if (scene.getMesh() == 0) return; 00063 00064 // Switching entity. 00065 00066 switch (entity) 00067 { 00068 case VERTEX: 00069 drawer.drawVertices(scene.getMesh()); 00070 break; 00071 case EDGE: 00072 drawer.drawEdges(scene.getMesh()); 00073 break; 00074 case FACE: 00075 drawer.drawFaces(scene.getMesh()); 00076 break; 00077 default: 00078 break; 00079 } 00080 } 00081 00082 }

Generated on Wed May 19 12:28:22 2004 for X3DToolKit by doxygen 1.3.7