arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

BasicQGL.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------
00002 //
00003 //   Joe Kniss
00004 //     8-29-02
00005 //                   ________    ____   ___ 
00006 //                  |        \  /    | /  /
00007 //                  +---+     \/     |/  /
00008 //                  +--+|  |\    /|     < 
00009 //                  |  ||  | \  / |  |\  \ 
00010 //                  |      |  \/  |  | \  \ 
00011 //                   \_____|      |__|  \__\
00012 //                       Copyright  2001 
00013 //                      Joe Michael Kniss
00014 //                   <<< jmk@cs.utah.edu >>>
00015 //               "All Your Base are Belong to Us"
00016 //-------------------------------------------------------------------------
00017 // BasicQGL.cpp: implementation of the BasicQGL class.
00018 //
00019 //////////////////////////////////////////////////////////////////////
00020 
00021 
00022 #include <GL/glew.h>
00023 #ifdef WIN32
00024 #include <GL/wglew.h>
00025 #endif
00026 
00027 #include "BasicQGL.h"
00028 
00029 //#include <GL/glut.h>
00030 
00031 #include <GL/glUtil.h>
00032 #include "QtGutzEvent.h"
00033 
00034 #include <qmessagebox.h>
00035 
00036 #include <iostream>
00037 using namespace std;
00038 
00039 //#include <GL/gl.h>
00040 
00041 using namespace gutz;
00042 
00043 #if defined(Q_CC_MSVC)
00044 #pragma warning(disable:4305) // init: truncation from const double to float
00045 #endif
00046 
00047 //////////////////////////////////////////////////////////////////////
00048 // Construction/Destruction
00049 //////////////////////////////////////////////////////////////////////
00050 
00051 BasicQGL::BasicQGL(QWidget *parent, const char *name )
00052 : QGLWidget( parent, name ), 
00053   _cam(0), _manip(0),
00054   _mouseDown(false),
00055   _curMouse(0), _curMove(0),
00056   _timeOn(false), _time("BasicQGL::draw", 0)
00057 {
00058    _cam   = new Camera();
00059    _manip = new Manip();
00060 
00061    initManipulators();
00062 }
00063 
00064 BasicQGL::~BasicQGL()
00065 {
00066 
00067 }
00068 
00069 //=====================================================================
00070 // Initialize GL
00071 //=====================================================================
00072 
00073 void BasicQGL::initializeGL ()
00074 {
00075    glewInit();
00076 
00077    /* Black background and interpolated shading. */
00078    glClearColor(0.0, 0.0, 0.0, 0.0);
00079    glShadeModel(GL_SMOOTH);
00080 
00081    /* Set up lighting. mostly for widgets */
00082    static float ambient[]             = {0.4, 0.4, 0.4, 4.0};
00083    static float diffuse[]             = {0.5, 1.0, 1.0, 1.0};
00084    static float front_mat_shininess[] = {60.0};
00085    static float front_mat_specular[]  = {0.2, 0.2,  0.2,  1.0};
00086    static float front_mat_diffuse[]   = {0.5, 0.28, 0.38, 1.0};
00087    static float lmodel_ambient[]      = {0.2, 0.2,  0.2,  1.0};
00088    static float lmodel_twoside[]      = {GL_FALSE};
00089    glEnable(GL_LIGHT0);
00090    glLightfv(GL_LIGHT0, GL_AMBIENT,  ambient);
00091    glLightfv(GL_LIGHT0, GL_DIFFUSE,  diffuse);
00092    static float ltpos[] = {25,25,25,1};
00093    glLightfv(GL_LIGHT0, GL_POSITION, ltpos);
00094    glLightModelfv(GL_LIGHT_MODEL_AMBIENT,  lmodel_ambient);
00095    glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
00096 
00097    glEnable(GL_LIGHTING);
00098    glEnable(GL_COLOR_MATERIAL);
00099 
00100    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_mat_shininess);
00101    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  front_mat_specular);
00102    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE,   front_mat_diffuse);
00103 
00104    glEnable(GL_DEPTH_TEST);
00105    glDepthFunc(GL_LEQUAL);
00106    glDisable(GL_CULL_FACE);
00107 
00108    //glEnable(GL_LINE_SMOOTH);
00109    //glLineWidth(3);
00110 
00111    //glWinStats gws;
00112    //getGLWinStats(gws, true);
00113 
00114    cerr << "gl initialized" << endl;
00115 
00116 }
00117 
00118 
00119 //=====================================================================
00120 // Resize GL
00121 //=====================================================================
00122 void BasicQGL::resizeGL ( int w, int h )
00123 {
00124    _cam->setScreen(w, h);
00125    /// had to change this to respect the 
00126    ///  users settings for perspective/ortho
00127    //_cam->setPerspective(60);
00128 
00129    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
00130 
00131    glMatrixMode(GL_PROJECTION);
00132    glLoadMatrixf(_cam->getProjectMatrix().v());
00133 
00134    glMatrixMode(GL_MODELVIEW);
00135 
00136    cerr << " current perspective matrix : " << endl;
00137    cerr << _cam->getProjectMatrix() << endl;
00138 }
00139 
00140 //=====================================================================
00141 //=====================================================================
00142 //                          DRAW
00143 //=====================================================================
00144 //=====================================================================
00145 void BasicQGL::draw()
00146 {
00147 
00148    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00149 
00150    glMatrixMode(GL_PROJECTION);
00151    glLoadMatrixf(_cam->getProjectMatrix().m);
00152    glMatrixMode(GL_MODELVIEW);
00153    glLoadMatrixf(_cam->getViewMatrix().m);
00154 
00155    glPushMatrix();
00156    {
00157       glColor4f(1,0,0,0);
00158       //glutSolidSphere(10,100,100);
00159       glBegin(GL_QUADS);
00160       {
00161          glVertex3f(-1,-1,0);
00162          glVertex3f(-1,1,0);
00163          glVertex3f(1,1,0);
00164          glVertex3f(1,-1,0);
00165       }
00166       glEnd();
00167    }
00168    glPopMatrix();       
00169 
00170    /// TODO: remove QT signals for simian events, use gutz::Signal
00171    drawFinished();
00172    drawDone();
00173 }
00174 
00175 //=====================================================================
00176 // Mouse Press
00177 //=====================================================================
00178 void BasicQGL::mousePressEvent( QMouseEvent * e )
00179 {
00180    _mouseDown = true;
00181 
00182    _curMouse = qt2GutzMouse(e,_cam,_manip);
00183    _curMove = new MouseMoveEvent(*_curMouse, _curMouse->getPos());
00184 
00185    _cam->mouse(*_curMouse);
00186    _manip->mouse(*_curMouse);
00187 
00188    //_cam->event(  e->x(), e->y(), qt2GutzButton(e) | qt2GutzState(e));
00189    //_manip->event(e->x(), e->y(), qt2GutzButton(e) | qt2GutzState(e));
00190 
00191    update();
00192 
00193    cerr << "The mouse was pressed at " << e->x() << " , " << e->y() 
00194         << " Global : " << e->globalX() << " , " << e->globalY() << endl;
00195 }
00196 
00197 //=====================================================================
00198 // Mouse Double Click
00199 //=====================================================================
00200 void BasicQGL::mouseDoubleClickEvent ( QMouseEvent * e )
00201 {
00202    _mouseDown = true;
00203 
00204    _curMouse = qt2GutzMouse(e,_cam,_manip);
00205    _curMove = new MouseMoveEvent(*_curMouse, _curMouse->getPos());
00206 
00207    _cam->mouse(*_curMouse);
00208    _manip->mouse(*_curMouse);
00209 
00210    //_cam->event(e->x(), e->y(), qt2GutzDblButton(e) | qt2GutzState(e));
00211    //_manip->event(e->x(), e->y(), qt2GutzDblButton(e) | qt2GutzState(e));
00212 
00213    update();
00214 
00215    //cerr << "The mouse was double clicked" << endl;
00216 }
00217 
00218 //=====================================================================
00219 // Mouse Release
00220 //=====================================================================
00221 void BasicQGL::mouseReleaseEvent ( QMouseEvent * e )
00222 {
00223    _mouseDown = false;
00224 
00225    if(_curMouse)
00226    {            
00227      _curMouse->setButtonDown(false);
00228    }
00229    if(_cam && _curMouse)    _cam->mouse(*_curMouse);
00230    if(_manip && _curMouse)  _manip->mouse(*_curMouse);
00231 
00232    if(_curMouse)
00233      _curMouse->setButton(GUTZ_BUTTON_NONE);
00234 
00235    update();
00236 
00237    cerr << "The mouse was released" << endl;
00238 }
00239 
00240 //=====================================================================
00241 // Mouse Move
00242 //=====================================================================
00243 void BasicQGL::mouseMoveEvent( QMouseEvent * e )
00244 {
00245   if(! _mouseDown ) return;
00246 
00247    if(!_curMove)
00248    {
00249       cerr << "RenderableQGL::mouseMoveEvent(), no move defined " << endl;
00250       if(!_curMouse)
00251       {
00252          cerr << "RenderableQGL::mouseMoveEvent(), no mouse defined " << endl;
00253          _curMouse = qt2GutzMouse(e,_cam,_manip);
00254       }
00255       _curMove = new MouseMoveEvent(*_curMouse, vec3f(e->x(), e->y(),_curMouse->z()));
00256    }
00257 
00258    /// update the current move event's  position
00259    _curMove->setPos(vec3f(e->x(), e->y(), _curMouse->z()));
00260 
00261    //if(_cam->updateEvent(e->x(),e->y()))
00262     if(_cam->move(*_curMove))
00263     {
00264       cameraChanged(_cam);
00265     }
00266     //if(_manip->updateEvent(e->x(),e->y()))
00267     if(_manip->move(*_curMove))
00268     {
00269       manipChanged(_manip);
00270     }
00271 
00272    update();
00273 
00274    cerr << "The mouse is moving " << e->x() << " , " << e->y() 
00275         << " Global : " << e->globalX() << " , " << e->globalY() << endl;
00276 }
00277 
00278 //=====================================================================
00279 // Key press
00280 //=====================================================================
00281 void BasicQGL::keyPressEvent ( QKeyEvent * e )
00282 {
00283    /// Let's be a nice interface and
00284    /// only use the key if we want it
00285    e->ignore();
00286 
00287    QString s = "Key event\n";
00288 
00289    handleKeyCmd(e);
00290    if(e->isAccepted()) return;
00291 
00292    switch(e->key())
00293    {
00294    case 4096: //< escape key
00295       e->accept();
00296       close();
00297       break;
00298 
00299    default:
00300       //QString s = "Key event\n";
00301       //s += QString(" %1 \n").arg(e->key());
00302       //QMessageBox::information( this, "simian", s);
00303       cerr << "Key event : " << e->key() << " = ' " << char(e->ascii()) << " ' " << endl;
00304       break;
00305    }
00306 }
00307 
00308 //=====================================================================
00309 // Key release
00310 //=====================================================================
00311 void BasicQGL::keyReleaseEvent ( QKeyEvent * e )
00312 {
00313    //cerr << "Key up    : " << e->key() << " = " << endl;
00314    //this crashes the program!!!! with anything other than ascii chars (F1, Shift, alt, ctrl...)
00315    //cerr << e->text() << endl;
00316 }
00317 
00318 //=====================================================================
00319 // Key command
00320 //=====================================================================
00321 void BasicQGL::handleKeyCmd( QKeyEvent *e )
00322 {
00323    KeyCmdSPType cmd = _keyMap[e->ascii()];
00324    if(cmd)
00325    {
00326       cmd->setKeyEvent(e);
00327       cmd->exec();
00328       return;
00329    }
00330    else
00331    {
00332       e->ignore();
00333    }
00334 }
00335 
00336 //=====================================================================
00337 // Init Camera and manip
00338 //=====================================================================
00339 //////////////////////////////////
00340 /// init Camera and Manip
00341 void BasicQGL::initManipulators()
00342 {       
00343 
00344    _cam->setEyePos(vec3f(0,0,2));
00345 
00346    _cam->mapEvent(CAM_ROT,    GUTZ_LEFT_MOUSE | GUTZ_SHIFT, 1);
00347    _cam->mapEvent(CAM_SWIVEL, GUTZ_LEFT_MOUSE | GUTZ_CTRL, .2);
00348    //_cam->mapEvent(CAM_DOLLY,  GUTZ_RIGHT_MOUSE, .01);
00349    _cam->mapEvent(CAM_ZOOM,  GUTZ_RIGHT_MOUSE, .01);
00350 
00351    _cam->mapEvent(CAM_TRANS_XY, GUTZ_MIDDLE_MOUSE, .02);
00352    _cam->mapEvent(CAM_TRANS_XZ, GUTZ_MIDDLE_MOUSE | GUTZ_SHIFT, .02);
00353    _cam->mapEvent(CAM_TRANS_ZY, GUTZ_MIDDLE_MOUSE | GUTZ_CTRL, .02);
00354 
00355    _cam->mapEvent(CAM_DOLLY,     GUTZ_UP_ARROW, .2);
00356    _cam->mapEvent(CAM_DOLLY,     GUTZ_DOWN_ARROW, .2);
00357 
00358    _cam->setTumble(true);
00359 
00360    _manip->mapEvent(GUTZ_LEFT_MOUSE, new RotateManipEvent(1.0f));
00361 
00362    _manip->setCamera(_cam);
00363    _manip->setTumble(true);
00364 
00365 }
00366 
00367 //=====================================================================
00368 // Read Frame buffer
00369 //=====================================================================
00370 bool BasicQGL::readFrameBuffer(void *dataPtr, int size, 
00371                                GLenum format, 
00372                                GLenum type)
00373 {
00374   switch(format)
00375     {
00376     case GL_RGBA:
00377     case GL_BGRA_EXT:
00378       if(size < int(_cam->getScreen().x) * int(_cam->getScreen().y) * 4)
00379         return true;
00380       break;
00381     case GL_RGB:
00382     case GL_BGR_EXT:
00383       if(size < int(_cam->getScreen().x) * int(_cam->getScreen().y) * 3)
00384         return true;
00385       break;
00386     default:
00387       cerr << "BasicQGL::readFrameBuffer(), format not supported " << endl 
00388            << " - possibly not implemented " << endl;
00389       return true;
00390     }
00391 
00392   glReadPixels(0,0,_cam->getScreen().x,_cam->getScreen().y,
00393                format, type, dataPtr);
00394 
00395   return glErr(cerr, "BasicQGL::readFrameBuffer()");
00396 }
00397 
00398 //=====================================================================
00399 // Write Frame buffer
00400 //=====================================================================
00401 bool BasicQGL::writeFrameBuffer(void *dataPtr, int screenx, int screeny,       
00402                                GLenum format, 
00403                                GLenum type)
00404 {
00405   glDrawPixels(screenx, screeny, format, type, dataPtr);
00406 
00407   return glErr(cerr, "BasicQGL::readFrameBuffer()");
00408 }

Send questions, comments, and bug reports to:
jmk