From 905795302afbbe55e79319b58ca355fc28d94902 Mon Sep 17 00:00:00 2001 From: cnr-isti-vclab Date: Thu, 18 Oct 2007 08:52:06 +0000 Subject: [PATCH] Initial release. --- apps/sample/trimesh_QT/glarea.cpp | 224 ++++++++++++++++++++++++++ apps/sample/trimesh_QT/glarea.h | 104 ++++++++++++ apps/sample/trimesh_QT/main.cpp | 48 ++++++ apps/sample/trimesh_QT/mainwindow.cpp | 67 ++++++++ apps/sample/trimesh_QT/mainwindow.h | 47 ++++++ apps/sample/trimesh_QT/mainwindow.ui | 186 +++++++++++++++++++++ apps/sample/trimesh_QT/trimesh_qt.pro | 45 ++++++ 7 files changed, 721 insertions(+) create mode 100644 apps/sample/trimesh_QT/glarea.cpp create mode 100644 apps/sample/trimesh_QT/glarea.h create mode 100644 apps/sample/trimesh_QT/main.cpp create mode 100644 apps/sample/trimesh_QT/mainwindow.cpp create mode 100644 apps/sample/trimesh_QT/mainwindow.h create mode 100644 apps/sample/trimesh_QT/mainwindow.ui create mode 100644 apps/sample/trimesh_QT/trimesh_qt.pro diff --git a/apps/sample/trimesh_QT/glarea.cpp b/apps/sample/trimesh_QT/glarea.cpp new file mode 100644 index 00000000..d709750d --- /dev/null +++ b/apps/sample/trimesh_QT/glarea.cpp @@ -0,0 +1,224 @@ +/**************************************************************************** + * VCGLib o o * + * Visual and Computer Graphics Library o o * + * _ O _ * + * Copyright(C) 2007 \/)\/ * + * Visual Computing Lab /\/| * + * ISTI - Italian National Research Council | * + * \ * + * All rights reserved. * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * + * for more details. * + * * + ****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + +****************************************************************************/ + +#include +#include "glarea.h" + +GLArea::GLArea (QWidget * parent) + :QGLWidget (parent) +{ + drawmode= SMOOTH; + GLArea::loadTetrahedron(); +} + +void GLArea::selectDrawMode(int mode){ + drawmode=DrawMode(mode); + updateGL(); +} + +void GLArea::loadMesh(QString fileName) +{ + int err=vcg::tri::io::ImporterPLY::Open(mesh,(fileName.toStdString()).c_str()); + if(err!=0){ + const char* errmsg=vcg::tri::io::ImporterPLY::ErrorMsg(err); + QMessageBox::warning(this,tr("Error Loading Mesh"),QString(errmsg)); + } + initMesh("Loaded \""+fileName+"\"."); +} + +void GLArea::loadTetrahedron(){ + vcg::tri::Tetrahedron(mesh); + initMesh(tr("Tethraedron [builtin]")); +} + +void GLArea::loadDodecahedron(){ + vcg::tri::Dodecahedron(mesh); + initMesh(tr("Dodecahedron [builtin]")); +} + +void GLArea::initMesh(QString message) +{ + // update bounding box + vcg::tri::UpdateBounding::Box(mesh); + // update Normals + vcg::tri::UpdateNormals::PerVertexNormalizedPerFace(mesh); + vcg::tri::UpdateNormals::PerFaceNormalized(mesh); + // Initialize the opengl wrapper + glWrap.m = &mesh; + glWrap.Update(); + updateGL(); + emit setStatusBar(message); +} + +void GLArea::initializeGL () +{ + glClearColor(0, 0, 0, 0); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_NORMALIZE); + glEnable(GL_COLOR_MATERIAL); + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); +} + +void GLArea::resizeGL (int w, int h) +{ + glViewport (0, 0, (GLsizei) w, (GLsizei) h); + initializeGL(); + } + +void GLArea::paintGL () +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(40, GLArea::width()/(float)GLArea::height(), 0.1, 100); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(0,0,5, 0,0,0, 0,1,0); + track.center=vcg::Point3f(0, 0, 0); + track.radius= 1; + track.GetView(); + track.Apply(false); + glPushMatrix(); + float d=1.0f/mesh.bbox.Diag(); + vcg::glScale(d); + glTranslate(-glWrap.m->bbox.Center()); + // the trimesh drawing calls + switch(drawmode) + { + case SMOOTH: + glWrap.Draw (); + break; + case POINTS: + glWrap.Draw (); + break; + case WIRE: + glWrap.Draw (); + break; + case FLATWIRE: + glWrap.Draw (); + break; + case HIDDEN: + glWrap.Draw (); + break; + case FLAT: + glWrap.Draw (); + break; + default: + break; + } + + glPopMatrix(); + track.DrawPostApply(); +} + +vcg::Trackball::Button GLArea::QT2VCG (Qt::MouseButton qtbt, Qt::KeyboardModifiers modifiers) +{ + int vcgbt = vcg::Trackball::BUTTON_NONE; + if (qtbt & Qt::LeftButton) + vcgbt |= vcg::Trackball::BUTTON_LEFT; + if (qtbt & Qt::RightButton) + vcgbt |= vcg::Trackball::BUTTON_RIGHT; + if (qtbt & Qt::MidButton) + vcgbt |= vcg::Trackball::BUTTON_MIDDLE; + if (modifiers & Qt::ShiftModifier) + vcgbt |= vcg::Trackball::KEY_SHIFT; + if (modifiers & Qt::ControlModifier) + vcgbt |= vcg::Trackball::KEY_CTRL; + if (modifiers & Qt::AltModifier) + vcgbt |= vcg::Trackball::KEY_ALT; + return vcg::Trackball::Button (vcgbt); +} + +void GLArea::keyReleaseEvent (QKeyEvent * e) +{ + e->ignore (); + if (e->key () == Qt::Key_Control) + track.ButtonUp (QT2VCG (Qt::NoButton, Qt::ControlModifier)); + if (e->key () == Qt::Key_Shift) + track.ButtonUp (QT2VCG (Qt::NoButton, Qt::ShiftModifier)); + if (e->key () == Qt::Key_Alt) + track.ButtonUp (QT2VCG (Qt::NoButton, Qt::AltModifier)); + updateGL (); +} + +void GLArea::keyPressEvent (QKeyEvent * e) +{ + e->ignore (); + if (e->key () == Qt::Key_Control) + track.ButtonDown (QT2VCG (Qt::NoButton, Qt::ControlModifier)); + if (e->key () == Qt::Key_Shift) + track.ButtonDown (QT2VCG (Qt::NoButton, Qt::ShiftModifier)); + if (e->key () == Qt::Key_Alt) + track.ButtonDown (QT2VCG (Qt::NoButton, Qt::AltModifier)); + updateGL (); +} + +void GLArea::mousePressEvent (QMouseEvent * e) +{ + e->accept (); + setFocus (); + track.MouseDown (e->x (), height () - e->y (), QT2VCG (e->button (), e->modifiers ())); + updateGL (); +} + +void GLArea::mouseMoveEvent (QMouseEvent * e) +{ + if (e->buttons ()) { + track.MouseMove (e->x (), height () - e->y ()); + updateGL (); + } +} + +void GLArea::mouseReleaseEvent (QMouseEvent * e) +{ + track.MouseUp (e->x (), height () - e->y (), QT2VCG (e->button (), e->modifiers ())); + updateGL (); +} + +vcg::Trackball::Button GLArea::QTWheel2VCG (Qt::KeyboardModifiers modifiers) +{ + int vcgbt = vcg::Trackball::WHEEL; + if (modifiers & Qt::ShiftModifier) + vcgbt |= vcg::Trackball::KEY_SHIFT; + if (modifiers & Qt::ControlModifier) + vcgbt |= vcg::Trackball::KEY_CTRL; + if (modifiers & Qt::AltModifier) + vcgbt |= vcg::Trackball::KEY_ALT; + return vcg::Trackball::Button (vcgbt); +} + + +void GLArea::wheelEvent (QWheelEvent * e) +{ + const int WHEEL_STEP = 120; + track.MouseWheel (e->delta () / float (WHEEL_STEP), QTWheel2VCG (e->modifiers ())); + updateGL (); +} diff --git a/apps/sample/trimesh_QT/glarea.h b/apps/sample/trimesh_QT/glarea.h new file mode 100644 index 00000000..0418fed1 --- /dev/null +++ b/apps/sample/trimesh_QT/glarea.h @@ -0,0 +1,104 @@ +/**************************************************************************** + * VCGLib o o * + * Visual and Computer Graphics Library o o * + * _ O _ * + * Copyright(C) 2007 \/)\/ * + * Visual Computing Lab /\/| * + * ISTI - Italian National Research Council | * + * \ * + * All rights reserved. * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * + * for more details. * + * * + ****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + +****************************************************************************/ + + +#ifndef GLAREA_H_ +#define GLAREA_H_ + +/// Opengl related imports +#include +#include + +/// vcg imports +#include +#include +#include +#include +#include +#include + +/// wrapper imports +#include +#include +#include + +/// declaring edge and face type +class CEdge; +class CFace; + +/// compositing wanted proprieties +class CVertex : public vcg::VertexSimp2< CVertex, CEdge, CFace, vcg::vert::Coord3f, vcg::vert::Normal3f, vcg::vert::BitFlags>{}; +class CFace : public vcg::FaceSimp2< CVertex, CEdge, CFace, vcg::face::VertexRef, vcg::face::Normal3f, vcg::face::BitFlags > {}; +class CMesh : public vcg::tri::TriMesh< std::vector, std::vector > {}; + +class GLArea:public QGLWidget +{ +Q_OBJECT +public: + GLArea (QWidget * parent = 0); + /// we choosed a subset of the avaible drawing modes + enum DrawMode{SMOOTH=0,POINTS,WIRE,FLATWIRE,HIDDEN,FLAT}; +public slots: + /// widget-based user interaction slots + void selectDrawMode(int mode); + void loadMesh(QString filename); + void loadTetrahedron(); + void loadDodecahedron(); +signals: + /// signal for setting the statusbar message + void setStatusBar(QString message); +protected: + /// opengl initialization and drawing calls + void initializeGL (); + void resizeGL (int w, int h); + void paintGL (); + /// keyboard and mouse event callbacks + void keyReleaseEvent(QKeyEvent * e); + void keyPressEvent(QKeyEvent * e); + void mousePressEvent(QMouseEvent*e); + void mouseMoveEvent(QMouseEvent*e); + void mouseReleaseEvent(QMouseEvent*e); + void wheelEvent(QWheelEvent*e); +private: + /// the active mesh instance + CMesh mesh; + /// the active mesh opengl wrapper + vcg::GlTrimesh glWrap; + /// the active manipulator + vcg::Trackball track; + /// the current drawmode + DrawMode drawmode; + /// mesh data structure initializer + void initMesh(QString message); + /// translation between QT and VCG keyboard and mouse modifiers + vcg::Trackball::Button QT2VCG (Qt::MouseButton qtbt, Qt::KeyboardModifiers modifiers); + vcg::Trackball::Button QTWheel2VCG (Qt::KeyboardModifiers modifiers); + }; + +#endif /*GLAREA_H_ */ diff --git a/apps/sample/trimesh_QT/main.cpp b/apps/sample/trimesh_QT/main.cpp new file mode 100644 index 00000000..c817955d --- /dev/null +++ b/apps/sample/trimesh_QT/main.cpp @@ -0,0 +1,48 @@ +/**************************************************************************** + * VCGLib o o * + * Visual and Computer Graphics Library o o * + * _ O _ * + * Copyright(C) 2007 \/)\/ * + * Visual Computing Lab /\/| * + * ISTI - Italian National Research Council | * + * \ * + * All rights reserved. * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * + * for more details. * + * * + ****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + +****************************************************************************/ + +/** + * Minimal QT trimesh viewer + * + * This sample shows how to use togheter: + * - the Opengl module in QT using the designer + * - the trimesh loading and initialization + * - basic usage of the default manipulators (the "Trackball") + */ + +#include +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + MainWindow *mw = new MainWindow; + mw->show(); + return app.exec(); +} diff --git a/apps/sample/trimesh_QT/mainwindow.cpp b/apps/sample/trimesh_QT/mainwindow.cpp new file mode 100644 index 00000000..bd093323 --- /dev/null +++ b/apps/sample/trimesh_QT/mainwindow.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** + * VCGLib o o * + * Visual and Computer Graphics Library o o * + * _ O _ * + * Copyright(C) 2007 \/)\/ * + * Visual Computing Lab /\/| * + * ISTI - Italian National Research Council | * + * \ * + * All rights reserved. * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * + * for more details. * + * * + ****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + +****************************************************************************/ + +#include +#include "mainwindow.h" + +MainWindow::MainWindow (QWidget * parent):QMainWindow (parent) +{ + ui.setupUi (this); + //connections + + //from toolFrame to glArea + connect (ui.drawModeComboBox, SIGNAL (currentIndexChanged(int)), + ui.glArea, SLOT (selectDrawMode(int))); + + connect (ui.loadTetrahedronPushButton, SIGNAL (clicked()), + ui.glArea, SLOT (loadTetrahedron())); + + connect (ui.loadDodecahedronPushButton, SIGNAL (clicked()), + ui.glArea, SLOT (loadDodecahedron())); + + //from toolFrame to glArea through mainwindow + connect (ui.loadMeshPushButton, SIGNAL (clicked()), + this, SLOT (chooseMesh())); + connect (this, SIGNAL (loadMesh(QString)), + ui.glArea, SLOT(loadMesh(QString))); + + //from glArea to statusbar + connect (ui.glArea, SIGNAL (setStatusBar(QString)), + ui.statusbar, SLOT (showMessage(QString))); +} + +// mesh chooser file dialog +void MainWindow::chooseMesh() +{ + QString fileName = QFileDialog::getOpenFileName(this, + tr("Open Mesh"), QDir::currentPath(), + tr("Poly Model (*.ply)")); + if(!fileName.isEmpty()) + emit loadMesh(fileName); +} diff --git a/apps/sample/trimesh_QT/mainwindow.h b/apps/sample/trimesh_QT/mainwindow.h new file mode 100644 index 00000000..c686de6c --- /dev/null +++ b/apps/sample/trimesh_QT/mainwindow.h @@ -0,0 +1,47 @@ +/**************************************************************************** + * VCGLib o o * + * Visual and Computer Graphics Library o o * + * _ O _ * + * Copyright(C) 2007 \/)\/ * + * Visual Computing Lab /\/| * + * ISTI - Italian National Research Council | * + * \ * + * All rights reserved. * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * + * for more details. * + * * + ****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + +****************************************************************************/ +#ifndef MAINWINDOW_H_ +#define MAINWINDOW_H_ + +#include "ui_mainwindow.h" + +class MainWindow:public QMainWindow +{ +Q_OBJECT +public: + MainWindow(QWidget * parent = 0); +public slots: + void chooseMesh(); +signals: + void loadMesh(QString newMesh); +private: + Ui::mainWindow ui; +}; + +#endif /*MAINWINDOW_H_ */ diff --git a/apps/sample/trimesh_QT/mainwindow.ui b/apps/sample/trimesh_QT/mainwindow.ui new file mode 100644 index 00000000..7305a2c3 --- /dev/null +++ b/apps/sample/trimesh_QT/mainwindow.ui @@ -0,0 +1,186 @@ + + mainWindow + + + + 0 + 0 + 715 + 579 + + + + Trimesh QT + + + + + + + + 0 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + Draw &Mode : + + + drawModeComboBox + + + + + + + + Smooth + + + + + Points + + + + + Wire + + + + + Flat Wire + + + + + Hidden + + + + + Flat + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Load &Mesh + + + + + + + Load &Tetrahedron + + + + + + + Load &Dodecahedron + + + + + + + Qt::Horizontal + + + + + + + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + 6 + + + 9 + + + 9 + + + 9 + + + 9 + + + + + + 320 + 240 + + + + + + + + + + + + + 0 + 0 + 715 + 31 + + + + + + + + GLArea + QWidget +
glarea.h
+
+
+ + drawModeComboBox + loadMeshPushButton + loadTetrahedronPushButton + loadDodecahedronPushButton + + + +
diff --git a/apps/sample/trimesh_QT/trimesh_qt.pro b/apps/sample/trimesh_QT/trimesh_qt.pro new file mode 100644 index 00000000..8a3c7fc6 --- /dev/null +++ b/apps/sample/trimesh_QT/trimesh_qt.pro @@ -0,0 +1,45 @@ +# Base options +TEMPLATE = app +LANGUAGE = C++ + +# QT modules +QT += opengl + +# Executable name +TARGET = trimesh_qt + +# Directories +DESTDIR = . +UI_DIR = build/ui +MOC_DIR = build/moc +OBJECTS_DIR = build/obj + +# Lib headers +INCLUDEPATH += . +INCLUDEPATH += ../../.. + +# Lib sources +SOURCES += ../../../wrap/ply/plylib.cpp +SOURCES += ../../../wrap/gui/trackball.cpp +SOURCES += ../../../wrap/gui/trackmode.cpp + + +# Compile glew +DEFINES += GLEW_STATIC +INCLUDEPATH += ../../../../code/lib/glew/include +SOURCES += ../../../../code/lib/glew/src/glew.c + +# Awful problem with windows.. +win32{ + DEFINES += NOMINMAX +} + +# Input +HEADERS += mainwindow.h +HEADERS += glarea.h + +SOURCES += main.cpp +SOURCES += mainwindow.cpp +SOURCES += glarea.cpp + +FORMS += mainwindow.ui