simplifiedMeshViewer

Introduction

This example shows how to use the easy-to-use X3DTK::SimplifiedMeshScene class. We read the simplified MESH scene graph and draw the vertices, edges and faces using default Mesh datas. X3DTK::SimplifiedMeshScene 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: X3DTK::Mesh and X3DTK::Vertex.

Some important functions and classes:

BasicDrawer

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

Code

BasicDrawer.h

#ifndef MESH_BASICDRAWER_H
#define MESH_BASICDRAWER_H

#include <X3DTK/kernel.h>
#include <X3DTK/MESH/scenegraph.h>

namespace X3DTK {
namespace MESH {

// Processor drawing the mesh from the Mesh scene graph.

class BasicDrawer
{
public:
  void drawVertices(Mesh *M);
  void drawEdges(Mesh *M);
  void drawFaces(Mesh *M);
};

}
}

#endif

BasicDrawer.cpp

#include "BasicDrawer.h"

#include <iostream>

using namespace std;

namespace X3DTK {
namespace MESH {

void BasicDrawer::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)
    glVertex3fv((*v)->data().getPoint());

  glEnd();
}
  
void BasicDrawer::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.
    glVertex3fv((*e)->getFromVertex()->data().getPoint());
    glVertex3fv((*e)->getToVertex()->data().getPoint());
  }
  glEnd();  
}
  
void BasicDrawer::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.
      glNormal3fv(v->data().getNormalOfFace(*f));

      // setting the color of the vertex.
      if (hasColor)
        glColor4fv(v->data().getColorOfFace(*f));
      else
        glColor4f(0.8f, 0.8f, 0.8f, 1.0f);
      
      // Getting the point data.  
      glVertex3fv(v->data().getPoint());
    }
    glEnd();
  }
}

}
}

Viewer.h

#ifndef VIEWER_H
#define VIEWER_H

#include <QGLViewer/qglviewer.h>
#include <X3DTK/simplifiedmeshscene.h>

#include "BasicDrawer.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();
  QString helpString() const;
  void help() const;
  
private:
  X3DTK::SimplifiedMeshScene scene;
  X3DTK::MESH::BasicDrawer drawer;
  Entity entity;
};

#endif

Viewer.cpp

#include "Viewer.h"
#include <math.h>
#include <iostream>
#include <qfiledialog.h>
#include <qmessagebox.h> 

using namespace std;

// 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()
{
  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;      
  }
}

QString Viewer::helpString() const
{
  QString message("");
  
  message += "<b>L</b>" + QString(" loads a new file<br>");
  message += "<b>V</b>" + QString(" draws the vertices<br>");
  message += "<b>E</b>" + QString(" draws the edges<br>");
  message += "<b>F</b>" + QString(" draws the faces<br>");
  
  message += QGLViewer::helpString();
  
  return message;
}

void Viewer::help() const
{
  QMessageBox *mb = new QMessageBox("help", helpString(), QMessageBox::NoIcon,QMessageBox::Ok | QMessageBox::Default, QMessageBox::NoButton,QMessageBox::NoButton, NULL, "Help", false,Qt::WStyle_DialogBorder | Qt::WType_Dialog | Qt::WDestructiveClose);
  mb->show();
}

Generated on Fri Aug 27 13:16:29 2004 for X3DToolKit by doxygen 1.3.6