Initial release.

This commit is contained in:
Paolo Cignoni 2007-10-18 08:52:06 +00:00
parent a45b2e8fa0
commit 905795302a
7 changed files with 721 additions and 0 deletions

View File

@ -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 <QtGui>
#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<CMesh>::Open(mesh,(fileName.toStdString()).c_str());
if(err!=0){
const char* errmsg=vcg::tri::io::ImporterPLY<CMesh>::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<CMesh>::Box(mesh);
// update Normals
vcg::tri::UpdateNormals<CMesh>::PerVertexNormalizedPerFace(mesh);
vcg::tri::UpdateNormals<CMesh>::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<vcg::GLW::DMSmooth, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
break;
case POINTS:
glWrap.Draw<vcg::GLW::DMPoints, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
break;
case WIRE:
glWrap.Draw<vcg::GLW::DMWire, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
break;
case FLATWIRE:
glWrap.Draw<vcg::GLW::DMFlatWire, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
break;
case HIDDEN:
glWrap.Draw<vcg::GLW::DMHidden, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
break;
case FLAT:
glWrap.Draw<vcg::GLW::DMFlat, vcg::GLW::CMNone,vcg::GLW::TMNone> ();
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 ();
}

View File

@ -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 <GL/glew.h>
#include <QtOpenGL/QGLWidget>
/// vcg imports
#include <vcg/simplex/vertexplus/base.h>
#include <vcg/simplex/faceplus/base.h>
#include <vcg/complex/trimesh/base.h>
#include <vcg/complex/trimesh/update/bounding.h>
#include <vcg/complex/trimesh/update/normal.h>
#include <vcg/complex/trimesh/create/platonic.h>
/// wrapper imports
#include <wrap/io_trimesh/import.h>
#include <wrap/gl/trimesh.h>
#include <wrap/gui/trackball.h>
/// 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<CVertex>, std::vector<CFace> > {};
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<CMesh> 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_ */

View File

@ -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 <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow *mw = new MainWindow;
mw->show();
return app.exec();
}

View File

@ -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 <QtGui>
#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);
}

View File

@ -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_ */

View File

@ -0,0 +1,186 @@
<ui version="4.0" >
<class>mainWindow</class>
<widget class="QMainWindow" name="mainWindow" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>715</width>
<height>579</height>
</rect>
</property>
<property name="windowTitle" >
<string>Trimesh QT</string>
</property>
<widget class="QWidget" name="centralwidget" >
<layout class="QVBoxLayout" >
<item>
<widget class="QFrame" name="toolsFrame" >
<property name="sizePolicy" >
<sizepolicy vsizetype="Fixed" hsizetype="Preferred" >
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape" >
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow" >
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" >
<item>
<widget class="QLabel" name="drawModeLabel" >
<property name="text" >
<string>Draw &amp;Mode :</string>
</property>
<property name="buddy" >
<cstring>drawModeComboBox</cstring>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="drawModeComboBox" >
<item>
<property name="text" >
<string>Smooth</string>
</property>
</item>
<item>
<property name="text" >
<string>Points</string>
</property>
</item>
<item>
<property name="text" >
<string>Wire</string>
</property>
</item>
<item>
<property name="text" >
<string>Flat Wire</string>
</property>
</item>
<item>
<property name="text" >
<string>Hidden</string>
</property>
</item>
<item>
<property name="text" >
<string>Flat</string>
</property>
</item>
</widget>
</item>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" >
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="loadMeshPushButton" >
<property name="text" >
<string>Load &amp;Mesh</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="loadTetrahedronPushButton" >
<property name="text" >
<string>Load &amp;Tetrahedron</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="loadDodecahedronPushButton" >
<property name="text" >
<string>Load &amp;Dodecahedron</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QFrame" name="glFrame" >
<property name="frameShape" >
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow" >
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" >
<property name="spacing" >
<number>6</number>
</property>
<property name="leftMargin" >
<number>9</number>
</property>
<property name="topMargin" >
<number>9</number>
</property>
<property name="rightMargin" >
<number>9</number>
</property>
<property name="bottomMargin" >
<number>9</number>
</property>
<item>
<widget class="GLArea" native="1" name="glArea" >
<property name="minimumSize" >
<size>
<width>320</width>
<height>240</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>715</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar" />
</widget>
<customwidgets>
<customwidget>
<class>GLArea</class>
<extends>QWidget</extends>
<header>glarea.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>drawModeComboBox</tabstop>
<tabstop>loadMeshPushButton</tabstop>
<tabstop>loadTetrahedronPushButton</tabstop>
<tabstop>loadDodecahedronPushButton</tabstop>
</tabstops>
<resources/>
<connections/>
</ui>

View File

@ -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