00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "glarea.h"
00035 #include <QKeyEvent>
00036 #include <QKeyEvent>
00037 #include <QWheelEvent>
00038 #include <wrap/qt/trackball.h>
00039 #include <cassert>
00040
00041 #ifdef Q_OS_MAC
00042 #define glGenVertexArrays glGenVertexArraysAPPLE
00043 #define glBindVertexArray glBindVertexArrayAPPLE
00044 #define glDeleteVertexArrays glDeleteVertexArraysAPPLE
00045 #endif
00046
00047 GLArea::GLArea (CMeshO& m, MLThreadSafeGLMeshAttributesFeeder& feed,QWidget* parent,QGLWidget* sharedcont)
00048 :QGLWidget (parent,sharedcont),mesh(m),feeder(feed),rq(),drawmode(MDM_SMOOTH)
00049 {
00050 }
00051
00052 GLArea::~GLArea()
00053 {
00054 }
00055
00056 void GLArea::initializeGL()
00057 {
00058 makeCurrent();
00059 glewExperimental=GL_TRUE;
00060 glewInit();
00061 glClearColor(0, 0, 0, 0);
00062 glEnable(GL_LIGHTING);
00063 glEnable(GL_LIGHT0);
00064 glEnable(GL_NORMALIZE);
00065 glEnable(GL_COLOR_MATERIAL);
00066 glEnable(GL_CULL_FACE);
00067 glEnable(GL_DEPTH_TEST);
00068 }
00069
00070 void GLArea::resizeGL (int w, int h)
00071 {
00072 makeCurrent();
00073 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
00074
00075 }
00076
00077 void GLArea::paintGL ()
00078 {
00079 makeCurrent();
00080 glPushAttrib(GL_ALL_ATTRIB_BITS);
00081
00082
00083 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00084 glMatrixMode(GL_PROJECTION);
00085 glPushMatrix();
00086 glLoadIdentity();
00087 gluPerspective(25, GLArea::width()/(float)GLArea::height(), 0.1, 100);
00088 glMatrixMode(GL_MODELVIEW);
00089 glPushMatrix();
00090 glLoadIdentity();
00091 gluLookAt(0,0,5, 0,0,0, 0,1,0);
00092 track.center=vcg::Point3f(0, 0, 0);
00093 track.radius= 1;
00094 track.GetView();
00095 track.Apply();
00096 glPushMatrix();
00097 float d=2.0f/mesh.bbox.Diag();
00098 vcg::glScale(d);
00099 glTranslate(-mesh.bbox.Center());
00100
00101 if (mesh.VN() > 0)
00102 {
00103 switch(drawmode)
00104 {
00105 case MDM_SMOOTH:
00106 case MDM_FLAT:
00107 feeder.drawTriangles(rq);
00108 case MDM_WIRE:
00109 feeder.drawWire(rq);
00110 break;
00111 case MDM_POINTS:
00112 feeder.drawPoints(rq);
00113 break;
00114 case MDM_FLATWIRE:
00115 feeder.drawFlatWire(rq);
00116 break;
00117 default:
00118 break;
00119 }
00120 }
00121 glPopMatrix();
00122
00123 track.DrawPostApply();
00124 glPopMatrix();
00125 glMatrixMode(GL_PROJECTION);
00126 glPopMatrix();
00127 glPopAttrib();
00128
00129 GLenum err = glGetError();
00130 assert(err == GL_NO_ERROR);
00131 }
00132
00133 void GLArea::keyReleaseEvent (QKeyEvent * e)
00134 {
00135 e->ignore ();
00136 if (e->key () == Qt::Key_Control)
00137 track.ButtonUp (QT2VCG (Qt::NoButton, Qt::ControlModifier));
00138 if (e->key () == Qt::Key_Shift)
00139 track.ButtonUp (QT2VCG (Qt::NoButton, Qt::ShiftModifier));
00140 if (e->key () == Qt::Key_Alt)
00141 track.ButtonUp (QT2VCG (Qt::NoButton, Qt::AltModifier));
00142 updateGL ();
00143 }
00144
00145 void GLArea::keyPressEvent (QKeyEvent * e)
00146 {
00147 e->ignore ();
00148 if (e->key () == Qt::Key_Control)
00149 track.ButtonDown (QT2VCG (Qt::NoButton, Qt::ControlModifier));
00150 if (e->key () == Qt::Key_Shift)
00151 track.ButtonDown (QT2VCG (Qt::NoButton, Qt::ShiftModifier));
00152 if (e->key () == Qt::Key_Alt)
00153 track.ButtonDown (QT2VCG (Qt::NoButton, Qt::AltModifier));
00154 updateGL ();
00155 }
00156
00157 void GLArea::mousePressEvent (QMouseEvent * e)
00158 {
00159 e->accept ();
00160 setFocus ();
00161 track.MouseDown (QT2VCG_X(this,e), QT2VCG_Y(this,e), QT2VCG (e->button (), e->modifiers ()));
00162 updateGL ();
00163 }
00164
00165 void GLArea::mouseMoveEvent (QMouseEvent * e)
00166 {
00167 if (e->buttons ()) {
00168 track.MouseMove (QT2VCG_X(this,e), QT2VCG_Y(this,e));
00169 updateGL ();
00170 }
00171 }
00172
00173 void GLArea::mouseReleaseEvent (QMouseEvent * e)
00174 {
00175 track.MouseUp (QT2VCG_X(this,e), QT2VCG_Y(this,e), QT2VCG (e->button (), e->modifiers ()));
00176 updateGL ();
00177 }
00178
00179 void GLArea::wheelEvent (QWheelEvent * e)
00180 {
00181 const int WHEEL_STEP = 120;
00182 track.MouseWheel (e->delta () / float (WHEEL_STEP), QTWheel2VCG (e->modifiers ()));
00183 updateGL ();
00184 }
00185
00186 void GLArea::updateRequested(MyDrawMode md,vcg::GLFeederInfo::ReqAtts& reqatts)
00187 {
00188 drawmode = md;
00189 rq = reqatts;
00190 updateGL();
00191 }
00192
00193 void GLArea::resetTrackBall()
00194 {
00195 makeCurrent();
00196 track.Reset();
00197 updateGL();
00198 doneCurrent();
00199 }
00200
00201
00202
00203 SharedDataOpenGLContext::SharedDataOpenGLContext( CMeshO& mesh,MLThreadSafeMemoryInfo& mi,QWidget* parent )
00204 :QGLWidget(parent),feeder(mesh,mi,100000)
00205 {
00206 }
00207
00208 SharedDataOpenGLContext::~SharedDataOpenGLContext()
00209 {
00210 deAllocateBO();
00211 }
00212
00213 void SharedDataOpenGLContext::myInitGL()
00214 {
00215 makeCurrent();
00216 glewInit();
00217 doneCurrent();
00218 }
00219
00220 void SharedDataOpenGLContext::passInfoToOpenGL(int mode)
00221 {
00222 makeCurrent();
00223 MyDrawMode drawmode = static_cast<MyDrawMode>(mode);
00224
00225 vcg::GLFeederInfo::ReqAtts req;
00226 bool allocated = false;
00227 switch(drawmode)
00228 {
00229 case MDM_SMOOTH:
00230 case MDM_WIRE:
00231 req[vcg::GLFeederInfo::ATT_VERTPOSITION] = true;
00232 req[vcg::GLFeederInfo::ATT_VERTNORMAL] = true;
00233 req[vcg::GLFeederInfo::ATT_VERTINDEX] = true;
00234 req.primitiveModality() = vcg::GLFeederInfo::PR_TRIANGLES;
00235 break;
00236 case MDM_POINTS:
00237 req[vcg::GLFeederInfo::ATT_VERTPOSITION] = true;
00238 req[vcg::GLFeederInfo::ATT_VERTNORMAL] = true;
00239 req.primitiveModality() = vcg::GLFeederInfo::PR_POINTS;
00240 break;
00241 case MDM_FLAT:
00242 case MDM_FLATWIRE:
00243 req[vcg::GLFeederInfo::ATT_VERTPOSITION] = true;
00244 req[vcg::GLFeederInfo::ATT_FACENORMAL] = true;
00245 req.primitiveModality() = vcg::GLFeederInfo::PR_TRIANGLES;
00246 break;
00247 default:
00248 break;
00249 }
00250 vcg::GLFeederInfo::ReqAtts rq = feeder.setupRequestedAttributes(req,allocated);
00251 doneCurrent();
00252 emit dataReadyToBeRead(drawmode,rq);
00253
00254 }
00255
00256 void SharedDataOpenGLContext::deAllocateBO()
00257 {
00258 makeCurrent();
00259 feeder.deAllocateBO();
00260 doneCurrent();
00261 }