From e82e573a623b108e405284b94ed2f7ca1808ce49 Mon Sep 17 00:00:00 2001 From: Paolo Cignoni cignoni Date: Thu, 12 Jan 2006 22:14:39 +0000 Subject: [PATCH] added vertigo effect and moveable near clip plane --- src/meshlab/glarea.cpp | 153 ++++++++++++++++++++++++++--------------- src/meshlab/glarea.h | 20 ++++-- 2 files changed, 110 insertions(+), 63 deletions(-) diff --git a/src/meshlab/glarea.cpp b/src/meshlab/glarea.cpp index 92a2d3622..2ead297c4 100644 --- a/src/meshlab/glarea.cpp +++ b/src/meshlab/glarea.cpp @@ -24,11 +24,8 @@ History $Log$ -Revision 1.67 2006/01/12 11:00:07 cignoni -Better Management of deallocation of memory - -Revision 1.66 2006/01/11 20:47:51 mariolatronico -ButtonPressed::KEY_SHIFT -> KEY_SHIFT in wheelEvent() +Revision 1.68 2006/01/12 22:14:39 alemochi +added vertigo effect and moveable near clip plane Revision 1.65 2006/01/10 16:29:29 alemochi now background and panel info not move with fov @@ -272,7 +269,6 @@ using namespace vcg; GLArea::GLArea(QWidget *parent) : QGLWidget(parent) { - setAttribute(Qt::WA_DeleteOnClose,true); iRenderer=0; //Shader support iDecoratorsList=0; currentTime=0; @@ -289,9 +285,11 @@ GLArea::GLArea(QWidget *parent) currentSharder = NULL; lastFilterRef = NULL; time.start(); - currentButton=0; + objDist=3.0; currLogLevel = -1; - mm=0; + currentButton=GLArea::BUTTON_NONE; + clipRatioFar=1; + clipRatioNear=1; } @@ -306,7 +304,8 @@ void GLArea::displayModelInfo() renderText(currentWidth-currentWidth*0.15,currentHeight-45,strVertex); renderText(currentWidth-currentWidth*0.15,currentHeight-30,strTriangle); renderText(currentWidth-currentWidth*0.15,currentHeight-70,QString("Fov ")+QString::number((int)fov,10)); - + QString strNear=QString("Near: %1").arg(clipRatioNear,7,'f',1); + renderText(currentWidth-currentWidth*0.15,currentHeight-80,strNear); } QSize GLArea::minimumSizeHint() const {return QSize(400,300);} @@ -418,6 +417,8 @@ void GLArea::myGluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GL top = fTop - yOff; glFrustum(left, right, bottom, top, zNear, zFar); + + } @@ -428,19 +429,18 @@ void GLArea::paintGL() initTexture(); glClearColor(1.0,1.0,1.0,0.0); //vannini: alpha was 1.0 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glLoadIdentity(); - - gluLookAt(0,0,3, 0,0,0, 0,1,0); - - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - gluPerspective(60,(GLdouble) vpWidth / (GLdouble) vpHeight, 0.2, 5); glMatrixMode(GL_MODELVIEW); - + glLoadIdentity(); + gluLookAt(0,0,3, 0,0,0 ,0,1,0); + glPushMatrix(); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(60,(float)vpWidth/vpHeight,0.2,5); + glPushMatrix(); + glMatrixMode(GL_MODELVIEW); + if (!takeSnapTile) { - // == Background glPushMatrix(); glLoadIdentity(); glPushAttrib(GL_ENABLE_BIT); @@ -451,27 +451,38 @@ void GLArea::paintGL() glColor(cs.bColorBottom); glVertex3f(-4.f,-4.f,-1.f); glColor(cs.bColorTop); glVertex3f( 4.f, 4.f,-1.f); glColor(cs.bColorBottom); glVertex3f( 4.f,-4.f,-1.f); - glEnd(); + glEnd(); glPopAttrib(); glPopMatrix(); - // == } - + double m[16]; + Matrix44d modelview1; // Take glLookAt(.....) + glGetDoublev(GL_MODELVIEW_MATRIX, m); + modelview1.Import(Matrix44d(m)); + Transpose(modelview1); glColor3f(1.f,1.f,1.f); + trackball.center=Point3f(0, 0, 0); trackball.radius= 1; trackball.GetView(); trackball.Apply(trackBallVisible && !takeSnapTile); - + + Matrix44d modelview2; // Take gluLookAt*Apply Matrix + glGetDoublev(GL_MODELVIEW_MATRIX, m); + modelview2.Import(Matrix44d(m)); + Transpose(modelview2); + Matrix44d modelview1Inv; + modelview1Inv.SetIdentity(); + modelview1Inv=Inverse(modelview1); + Matrix44d m_apply=modelview1Inv*modelview2; // Get Apply matrix + + // Setting camera e projection + setVertigoCamera(); + glMultMatrix(m_apply); float d=2.0f/mm->cm.bbox.Diag(); glScale(d); glTranslate(-mm->cm.bbox.Center()); - - setLightModel(); - - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); + setLightModel(); // Modify frustum... if (takeSnapTile) @@ -497,20 +508,8 @@ void GLArea::paintGL() } - if (!takeSnapTile) - { - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - gluPerspective(fov,(GLdouble) vpWidth / (GLdouble) vpHeight, 0.2, 5); - mm->Render(rm.drawMode,rm.colorMode,rm.textureMode); - glPopMatrix(); - } - else mm->Render(rm.drawMode,rm.colorMode,rm.textureMode); - - glMatrixMode(GL_MODELVIEW); - - + mm->Render(rm.drawMode,rm.colorMode,rm.textureMode); + if(iRenderer) { glUseProgramObjectARB(0); } @@ -521,6 +520,12 @@ void GLArea::paintGL() } // ...and take a snapshot + + glMatrixMode(GL_PROJECTION); // restore fix projection matrix + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); // restore fix viewpoint + glPopMatrix(); + if (takeSnapTile) { glPushAttrib(GL_ENABLE_BIT); @@ -620,18 +625,17 @@ Trackball::Button QT2VCG(Qt::MouseButton qtbt, Qt::KeyboardModifiers modifiers) void GLArea::keyPressEvent ( QKeyEvent * e ) { //currentButton=GLArea::BUTTON_NONE; - if (e->key ()==Qt::Key_Shift) { currentButton|=GLArea::KEY_SHIFT; qDebug("Pressed Qt::Key_Shift %0x",currentButton);} - if (e->key ()==Qt::Key_Control) { currentButton|=GLArea::KEY_CTRL; qDebug("Pressed Qt::Key_Control %0x",currentButton);} - if (e->key ()==Qt::Key_Alt) { currentButton|=GLArea::KEY_ALT; qDebug("Pressed Qt::Key_Alt %0x",currentButton);} + if (e->key ()==Qt::Key_Shift) currentButton|=GLArea::KEY_SHIFT; + if (e->key ()==Qt::Key_Control) currentButton|=GLArea::KEY_CTRL; + if (e->key ()==Qt::Key_Alt) currentButton|=GLArea::KEY_ALT; } void GLArea::keyReleaseEvent ( QKeyEvent * e ) { - if (e->key()==Qt::Key_Shift) {currentButton &= (~GLArea::KEY_SHIFT); qDebug("Released Qt::Key_Shift %0x",currentButton);} - if (e->key()==Qt::Key_Control){currentButton &= (~GLArea::KEY_CTRL); qDebug("Released Qt::Key_Control %0x",currentButton);} - if (e->key()==Qt::Key_Alt) {currentButton &= (~GLArea::KEY_ALT); qDebug("Released Qt::Key_Alt %0x",currentButton);} - + if (e->key()==Qt::Key_Shift) currentButton-=GLArea::KEY_SHIFT; + if (e->key()==Qt::Key_Control) currentButton-=GLArea::KEY_CTRL; + if (e->key()==Qt::Key_Alt) currentButton-=GLArea::KEY_ALT; } void GLArea::mousePressEvent(QMouseEvent*e) { @@ -660,18 +664,24 @@ void GLArea::mouseReleaseEvent(QMouseEvent*e) void GLArea::wheelEvent(QWheelEvent*e) { const int WHEEL_DELTA =120; - if (currentButton & KEY_SHIFT) + + if (currentButton & ButtonPressed::KEY_SHIFT) { + if (currentButton & ButtonPressed::KEY_CTRL) + { + clipRatioNear-=(e->delta()/ float(WHEEL_DELTA))/10; + updateGL(); + } + else + { fov+= e->delta()/ float(WHEEL_DELTA); updateGL(); + } } - else - { - trackball.MouseWheel( e->delta()/ float(WHEEL_DELTA) ); - } + else trackball.MouseWheel( e->delta()/ float(WHEEL_DELTA) ); update(); -} - + +} void GLArea::setDrawMode(vcg::GLW::DrawMode mode) { @@ -795,6 +805,35 @@ void GLArea::setSnapshotSetting(const SnapshotSetting & s) ss=s; } +void GLArea::setVertigoCamera() +{ + GLfloat ClipRatio=1; + GLfloat fAspect = (GLfloat)vpWidth/ vpHeight; + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + // Si deve mettere la camera ad una distanza che inquadri la sfera unitaria bene. + float y=sin(vcg::math::ToRad(fov/2.0)); + float x=cos(vcg::math::ToRad(fov/2.0)); + objDist= 1.5*(x*1.0/y); + float nearv = objDist - 2.0*clipRatioNear; + float farv = objDist + 2.0*clipRatioFar; + if(nearv<=objDist/10.0) nearv=objDist/10.0; + if(fov==5) + { + glOrtho(-1.5*fAspect,1.5*fAspect,-1.5,1.5,- 2.0*clipRatioNear, 2.0*clipRatioFar); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + } + else + { + + gluPerspective(fov, fAspect, nearv, farv); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(0, 0, objDist,0, 0, 0, 0, 1, 0); + } +} + void GLArea::updateFps() { static int j=0; diff --git a/src/meshlab/glarea.h b/src/meshlab/glarea.h index 0dc44e024..93fd48524 100644 --- a/src/meshlab/glarea.h +++ b/src/meshlab/glarea.h @@ -24,8 +24,8 @@ History $Log$ -Revision 1.44 2006/01/12 11:00:07 cignoni -Better Management of deallocation of memory +Revision 1.45 2006/01/12 22:14:39 alemochi +added vertigo effect and moveable near clip plane Revision 1.43 2006/01/09 18:31:18 alemochi Fov, work in progress..... @@ -190,6 +190,13 @@ First rough version. It simply load a mesh. #include #include #include +#include +#include +#include +#include +#include +#include +//#include <../wrap/gl/math.h> #include "GLLogStream.h" @@ -269,10 +276,7 @@ class GLArea : public QGLWidget public: GLArea(QWidget *parent = 0); - ~GLArea(){ - qDebug("Destructing glarea"); - if(mm) delete mm; - } + ~GLArea(){} enum ButtonPressed { BUTTON_NONE = 0x0000, WHEEL = 0x0008, @@ -324,6 +328,7 @@ public: void setLightMode(bool state,LightingModel lmode); void saveSnapshot(); void setLightModel(); + void setVertigoCamera(); void resetTrackBall(); list > *iDecoratorsList; @@ -366,6 +371,9 @@ private: int currentButton; float cfps; float fov; + float objDist; + float clipRatioFar; + float clipRatioNear; QTime time; int deltaTime; int lastTime;