#include "Viewer.h" #include #include #include #include using namespace X3DTK; using namespace std; Viewer::Viewer() { } Viewer::~Viewer() { scene.release(); } void Viewer::keyPressEvent(QKeyEvent *e) { switch (e->key()) { case Qt::Key_L : loadFile(); updateGL(); break; case Qt::Key_V : toggleDrawPoints(); updateGL(); break; case Qt::Key_E : toggleDrawEdges(); updateGL(); break; case Qt::Key_F : toggleDrawFaces(); updateGL(); break; case Qt::Key_C : toggleOneColorPerMesh(); updateGL(); break; default : QGLViewer::keyPressEvent(e); } } 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( qglviewer::Vec(scene.getBBoxMin()[0],scene.getBBoxMin()[1],scene.getBBoxMin()[2]), qglviewer::Vec(scene.getBBoxMax()[0],scene.getBBoxMax()[1],scene.getBBoxMax()[2]) ); setSceneRadius(2.0f*sceneRadius()); showEntireScene(); } void Viewer::init() { glPolygonOffset(-2.0, -2.0); #ifdef GL_RESCALE_NORMAL glEnable(GL_RESCALE_NORMAL); #endif loadFile(); } void Viewer::draw() { scene.drawMesh(); scene.drawSelected(); } void Viewer::about() { QMessageBox::about(this, "about the simpleAnimationViewer", "this is an example showing how to animate a simple X3D scene.Type 'h' to display help"); } 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(); } void Viewer::select(const QMouseEvent* e) { // Make openGL context current makeCurrent(); const int SENSITIVITY = 6; const int NB_HITS_MAX = 32768; // Prepare the selection mode static GLuint hits[NB_HITS_MAX]; glSelectBuffer(NB_HITS_MAX, hits); glRenderMode(GL_SELECT); glInitNames(); // Loads the matrices glMatrixMode(GL_PROJECTION); glLoadIdentity(); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT,viewport); camera()->getViewport(viewport); gluPickMatrix(static_cast(e->x()), static_cast(e->y()), SENSITIVITY, SENSITIVITY, viewport); // Don't use loadProjectionMatrix() directly as it clears the GL_PROJECTION matrix with a glLoadIdentity. // The false flag indicates that no glLoadIdentity should be called, in order to combine the matrices. camera()->loadProjectionMatrix(false); camera()->loadModelViewMatrix(); // Render scene with objects ids scene.drawMesh(true); glFlush(); // Get the results GLint nb_hits = glRenderMode(GL_RENDER); // Too many elements in selection buffer if (nb_hits < 0) return; // No selection if (nb_hits == 0) { scene.setSelectedType(NOTHING); cout << "No selection" << endl; return; } // Interpret results unsigned int zMin = hits[1]; scene.setSelectedMesh(hits[3]); scene.setSelectedType(static_cast(hits[4])); scene.setSelectedId(hits[5]); for (int i=1; i(hits[i*6+4])); scene.setSelectedId(hits[i*6+5]); } switch (scene.getSelectedType()) { case NOTHING : break; case VERTICES :cout << "Vertex "; break; case EDGES : cout << "Edge "; break; case FACES : cout << "Face "; break; } cout << scene.getSelectedId() << " selected on mesh " << scene.getSelectedMesh() << endl; } QString Viewer::helpString() const { QString message("L loads a new file
"); message += "F toggles face display
"; message += "E toggles edge display
"; message += "V toggles vertex display
"; message += "C toggles one color per mesh
"; message += "Shift+click to select an element
"; message += "
"; message += QGLViewer::helpString(); return message; }