#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);
const Mesh::MFVertex &vertices = M->getVertices();
glBegin(GL_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);
const Mesh::MFEdge &edges = M->getEdges();
glBegin(GL_LINES);
for (Mesh::MFEdge::const_iterator e = edges.begin(), end = edges.end() ; e != end; ++e)
{
if ((*e)->isBoundary())
glColor3f(0.3, 0.3, 0.9);
else
glColor3f(0.7, 0.7, 0.7);
glVertex3fv((*e)->getFromVertex()->data().getPoint());
glVertex3fv((*e)->getToVertex()->data().getPoint());
}
glEnd();
}
void BasicDrawer::drawFaces(Mesh *M)
{
glEnable(GL_LIGHTING);
bool hasColor = M->data().hasColor();
const Mesh::MFFace &faces = M->getFaces();
for (Mesh::MFFace::const_iterator f = faces.begin(), end = faces.end() ; f != end; ++f)
{
glBegin(GL_POLYGON);
const SFFace::MFEdge &edges = (*f)->getEdges();
for (SFFace::MFEdge::const_iterator e = edges.begin(), end = edges.end() ; e != end; ++e)
{
SFVertex *v = (*e)->getFromVertex();
glNormal3fv(v->data().getNormalOfFace(*f));
if (hasColor)
glColor4fv(v->data().getColorOfFace(*f));
else
glColor4f(0.8f, 0.8f, 0.8f, 1.0f);
glVertex3fv(v->data().getPoint());
}
glEnd();
}
}
}
}
#include "Viewer.h"
#include <math.h>
#include <iostream>
#include <qfiledialog.h>
#include <qmessagebox.h>
using namespace std;
Viewer::Viewer()
: entity(FACE)
{
}
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);
}
updateGL();
}
void Viewer::loadFile()
{
QString name = QFileDialog::getOpenFileName("", "X3D files (*.x3d *.X3D);;All files (*)", this);
if (name.isEmpty())
return;
scene.load(name);
setSceneBoundingBox(scene.getBBoxMin(), scene.getBBoxMax());
showEntireScene();
}
void Viewer::init()
{
loadFile();
}
void Viewer::draw()
{
if (scene.getMesh() == 0) return;
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();
}