~ubuntu-branches/ubuntu/natty/meshlab/natty

« back to all changes in this revision

Viewing changes to vcglib/apps/sample/trimesh_pos_demo/glwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Teemu Ikonen
  • Date: 2009-05-27 18:07:15 UTC
  • Revision ID: james.westby@ubuntu.com-20090527180715-xv4sqfs9skj5y74h
Tags: upstream-1.2.1
ImportĀ upstreamĀ versionĀ 1.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
* VCGLib                                                            o o     *
 
3
* Visual and Computer Graphics Library                            o     o   *
 
4
*                                                                _   O  _   *
 
5
* Copyright(C) 2004                                                \/)\/    *
 
6
* Visual Computing Lab                                            /\/|      *
 
7
* ISTI - Italian National Research Council                           |      *
 
8
*                                                                    \      *
 
9
* All rights reserved.                                                      *
 
10
*                                                                           *
 
11
* This program is free software; you can redistribute it and/or modify      *   
 
12
* it under the terms of the GNU General Public License as published by      *
 
13
* the Free Software Foundation; either version 2 of the License, or         *
 
14
* (at your option) any later version.                                       *
 
15
*                                                                           *
 
16
* This program is distributed in the hope that it will be useful,           *
 
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 
19
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
 
20
* for more details.                                                         *
 
21
*                                                                           *
 
22
****************************************************************************/
 
23
/****************************************************************************
 
24
  History
 
25
 
 
26
$Log: not supported by cvs2svn $
 
27
Revision 1.4  2007/05/17 09:06:44  ganovelli
 
28
gestione double click
 
29
 
 
30
Revision 1.3  2006/12/10 23:29:57  ganovelli
 
31
added VFIterator (Pos is disabled in this version)
 
32
 
 
33
Revision 1.2  2006/12/10 22:17:18  ganovelli
 
34
cvs problem during frist committ. repeated
 
35
 
 
36
*/
 
37
 
 
38
#include <QtGui>
 
39
#include <GL/glew.h>
 
40
#include <QtOpenGL>
 
41
 
 
42
#include <math.h>
 
43
#include "glwidget.h"
 
44
#include <wrap/io_trimesh/import_PLY.h>
 
45
#include <wrap/gl/picking.h>
 
46
#include <wrap/gl/space.h>
 
47
#include <wrap/gl/pos.h>
 
48
#include <vcg/complex/trimesh/update/bounding.h>
 
49
#include <vcg/complex/trimesh/update/normal.h>
 
50
#include <vcg/complex/trimesh/update/topology.h>
 
51
 
 
52
 
 
53
GLWidget::GLWidget(QWidget *parent)
 
54
    : QGLWidget(parent)
 
55
{
 
56
    object = 0;
 
57
 
 
58
    trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
 
59
    trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
 
60
                track.SetPosition(vcg::Point3f(0.0,0.0,0.0));
 
61
                track.SetIdentity();
 
62
                track.radius = 0.4;
 
63
                pos.f=NULL;
 
64
                vfite.f = NULL;
 
65
                doPickVfIte = false;
 
66
                doPickPos = false;
 
67
}
 
68
 
 
69
GLWidget::~GLWidget()
 
70
{
 
71
    makeCurrent();
 
72
    glDeleteLists(object, 1);
 
73
}
 
74
 
 
75
QSize GLWidget::minimumSizeHint() const
 
76
{
 
77
    return QSize(50, 50);
 
78
}
 
79
 
 
80
QSize GLWidget::sizeHint() const
 
81
{
 
82
    return QSize(400, 400);
 
83
}
 
84
 
 
85
void GLWidget::LoadTriMesh(QString &namefile)
 
86
{
 
87
        vcg::tri::io::ImporterPLY<MyStraightMesh>::Open(mesh,namefile.toAscii());
 
88
        vcg::tri::UpdateBounding<MyStraightMesh>::Box(mesh);
 
89
        vcg::tri::UpdateNormals<MyStraightMesh>::PerFace(mesh);
 
90
        vcg::tri::UpdateNormals<MyStraightMesh>::PerVertex(mesh);
 
91
        vcg::tri::UpdateTopology<MyStraightMesh>::FaceFace(mesh);
 
92
        vcg::tri::UpdateTopology<MyStraightMesh>::VertexFace(mesh);
 
93
        pos.f=0;
 
94
        vfite.f=NULL;
 
95
}
 
96
 
 
97
void GLWidget::OpenFile(){
 
98
        QStringList filters;
 
99
        
 
100
 
 
101
        QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"),".", filters.join("\n"));
 
102
        
 
103
        if (fileName.isEmpty()) return;
 
104
        else
 
105
         LoadTriMesh( fileName );
 
106
 
 
107
}
 
108
 
 
109
void GLWidget::flipV( ){
 
110
        if(pos.f) pos.FlipV();
 
111
        repaint();
 
112
}
 
113
void GLWidget::flipE( ){
 
114
        if(pos.f) pos.FlipE();
 
115
        repaint();
 
116
}
 
117
void GLWidget::flipF( ){
 
118
        if(pos.f) pos.FlipF();
 
119
        repaint();
 
120
}
 
121
void GLWidget::nextE( ){
 
122
        if(pos.f) pos.NextE();
 
123
        repaint();
 
124
}
 
125
void GLWidget::nextB( ){
 
126
        if(pos.f) pos.NextB();
 
127
        repaint();
 
128
}
 
129
 
 
130
void GLWidget::nextVfite( ){
 
131
        if(vfite.F()) ++vfite;
 
132
        repaint();
 
133
}
 
134
 
 
135
void GLWidget::initializeGL()
 
136
{
 
137
    qglClearColor(trolltechPurple.dark());
 
138
    glShadeModel(GL_FLAT);
 
139
    glEnable(GL_DEPTH_TEST);
 
140
    glEnable(GL_CULL_FACE);
 
141
}
 
142
 
 
143
template <class VertexType>
 
144
void drawVertex(VertexType & v){
 
145
        glPushAttrib(0xffffffff);
 
146
        glPointSize(2.0);
 
147
        glBegin(GL_POINTS);
 
148
        glVertex(v.P());
 
149
        glEnd();
 
150
        glPopAttrib();
 
151
}
 
152
 
 
153
void GLWidget::paintGL()
 
154
{
 
155
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
156
    glMatrixMode(GL_MODELVIEW);
 
157
    glLoadIdentity();
 
158
    gluLookAt(0,0,2,   0,0,0,   0,1,0);    
 
159
    
 
160
                track.GetView();
 
161
    track.Apply();
 
162
 
 
163
                
 
164
                glScalef(1/glWrap.m->bbox.Diag(),1/glWrap.m->bbox.Diag(),1/glWrap.m->bbox.Diag());
 
165
                glTranslate(-glWrap.m->bbox.Center());  
 
166
 
 
167
                // to do some picking
 
168
                 MyStraightMesh::FaceType* fp=NULL;
 
169
                 MyStraightMesh::VertexType* vp=NULL;
 
170
                        if(doPickPos)
 
171
                        {
 
172
                                std::vector<MyStraightMesh::FaceType*> res;
 
173
                                int yes = vcg::Pick<MyStraightMesh::FaceContainer>(pic_x,ScreenH-pic_y+1,mesh.face,res,vcg::glTriangle3<MyStraightMesh::FaceType>,1,1);
 
174
                                if(yes) 
 
175
                                        {fp = res[0];
 
176
                                                pos.Set(fp,0,fp->V(0));
 
177
                                        }
 
178
                                doPickPos=false;
 
179
                  }else
 
180
                        if(doPickVfIte)
 
181
                        {
 
182
                                std::vector<MyStraightMesh::VertexType*> res;
 
183
                                int yes = vcg::Pick<MyStraightMesh::VertContainer>(pic_x,ScreenH-pic_y+1,mesh.vert,res,drawVertex<MyStraightMesh::VertexType>,3,3);
 
184
                                if(yes) 
 
185
                                        {vp = res[0];
 
186
MyStraightMesh::FaceType* g  = vp->VFp();
 
187
                                                vfite=vcg::face::VFIterator<MyStraightMesh::FaceType>(vp);
 
188
                                        }
 
189
 
 
190
                                doPickVfIte = false;
 
191
                        }
 
192
                
 
193
                glWrap.Draw<vcg::GLW::DMFlatWire,vcg::GLW::CMNone,vcg::GLW::TMNone> ();
 
194
 
 
195
                if(pos.f!=NULL) {
 
196
                        glPushAttrib(0xffffffff);
 
197
                        glDisable(GL_LIGHTING);
 
198
                        glColor3f(0.0,1.0,0.0);
 
199
                        glDepthRange(0.0,0.999);
 
200
                        vcg::GlPos<vcg::face::Pos<MyStraightMesh::FaceType> >::Draw(pos);
 
201
                        glPopAttrib();
 
202
                }
 
203
                if(vfite.F()!=NULL) {
 
204
                        glPushAttrib(0xffffffff);
 
205
                        glDisable(GL_LIGHTING);
 
206
                        glColor3f(0.0,1.0,0.0);
 
207
                        glDepthRange(0.0,0.99);
 
208
                        vcg::GlVfIterator<vcg::face::VFIterator<MyStraightMesh::FaceType> >::Draw(vfite);
 
209
                        glPopAttrib();
 
210
                }
 
211
 
 
212
}
 
213
 
 
214
 
 
215
 
 
216
 
 
217
 
 
218
void GLWidget::mouseMoveEvent(QMouseEvent *e)
 
219
{
 
220
                        track.MouseMove(e->x(),ScreenH-e->y()+1);
 
221
                        repaint();
 
222
 
 
223
    //if (event->buttons() & Qt::LeftButton) {
 
224
    //    setXRotation(xRot + 8 * dy);
 
225
    //} else if (event->buttons() & Qt::RightButton) {
 
226
    //    setXRotation(xRot + 8 * dy);
 
227
    //}
 
228
 //   lastPos = event->pos();
 
229
}
 
230
 void GLWidget::keyPressEvent ( QKeyEvent * e ) {
 
231
                if((keypress == Qt::Key_Control)&&(e->key()==Qt::Key_Control))
 
232
                        keypress = -1;
 
233
                else
 
234
                        keypress = e->key();
 
235
 }
 
236
 void GLWidget::mouseDoubleClickEvent(QMouseEvent *e){
 
237
                                        if(e->button() == Qt::RightButton)
 
238
                                                doPickPos=true;
 
239
 
 
240
 }
 
241
 void GLWidget:: mousePressEvent(QMouseEvent *e)
 
242
{
 
243
                if( (keypress==Qt::Key_Control) && (e->button() == Qt::LeftButton) )
 
244
                                        track.MouseDown(e->x(),ScreenH-e->y()+1,vcg::Trackball::KEY_CTRL|vcg::Trackball::BUTTON_LEFT );
 
245
                        else
 
246
                                if(e->button() == Qt::LeftButton )
 
247
                                        track.MouseDown(e->x(),ScreenH-e->y()+1,vcg::Trackball::BUTTON_LEFT);
 
248
                                else
 
249
                                        if(e->button() == Qt::RightButton)
 
250
                                        {
 
251
                                                doPickVfIte=true;
 
252
                                                pic_x = e->x();
 
253
                                                pic_y = e->y();
 
254
                                        }
 
255
                        repaint();
 
256
                }
 
257
 
 
258
 void GLWidget::mouseReleaseEvent ( QMouseEvent * e ){
 
259
                        
 
260
                        if( (keypress==Qt::Key_Control) && (e->button() == Qt::LeftButton) )
 
261
                                        track.MouseUp(e->x(),ScreenH-e->y()+1,vcg::Trackball::KEY_CTRL );
 
262
                        else
 
263
                                if(e->button() == Qt::LeftButton )
 
264
                                        track.MouseUp(e->x(),ScreenH-e->y()+1,vcg::Trackball::BUTTON_LEFT);
 
265
                        repaint();
 
266
                }
 
267
 
 
268
 
 
269
 void GLWidget::resizeGL(int w,int h){
 
270
                        ScreenW=w; ScreenH=h;
 
271
                        glViewport(0,0,w,h);
 
272
                        glMatrixMode(GL_PROJECTION);
 
273
                        glLoadIdentity();
 
274
                        gluPerspective(45,ScreenW/(float)ScreenH,0.01,5);
 
275
                }
 
276
 void GLWidget::wheelEvent ( QWheelEvent * e ){
 
277
                        int v =  e->delta()/(float) 120;
 
278
                        track.MouseWheel(v);
 
279
                        repaint();
 
280
                }