00001 00004 /***************************************************************************** 00005 ** Includes 00006 *****************************************************************************/ 00007 00008 #include <qglv/opengl.hpp> 00009 #include <QDateTime> 00010 #include "../../include/qglv/widgets/qgl_viewer.hpp" 00011 00012 /***************************************************************************** 00013 ** Namespaces 00014 *****************************************************************************/ 00015 00016 namespace qglv { 00017 00018 /***************************************************************************** 00019 ** Implementation 00020 *****************************************************************************/ 00021 00022 QGLViewer::QGLViewer(QWidget *parent) 00023 : ::QGLViewer(parent) 00024 , last_redraw_(0) 00025 , redraw_interval_ms_(20) 00026 { 00027 // MouseBindings need to be here so the mouseString gets modified for use early. 00028 setMouseTracking(true); 00029 setMouseBinding(Qt::ControlModifier, Qt::LeftButton, CAMERA, SCREEN_ROTATE); 00030 setMouseBinding(Qt::NoModifier, Qt::LeftButton | Qt::RightButton, CAMERA, SCREEN_ROTATE); 00031 } 00032 00033 QGLViewer::~QGLViewer() {} 00034 00035 void QGLViewer::init() { 00036 00037 GLenum err = glewInit(); 00038 if (err != GLEW_OK) 00039 { 00040 std::cout << "ERROR : failed to initialise glew" << std::endl; 00041 // TODO abort or exceptions here 00042 } 00043 /**************************************** 00044 ** QGL Defaults 00045 ****************************************/ 00046 setForegroundColor(QColor(80, 80, 80)); // defaults are 180 00047 setBackgroundColor(QColor(0, 0, 0)); // defaults are 51 00048 00049 // setAxisIsDrawn(); // we draw our own axis 00050 // setGridIsDrawn(); // we draw our own grid 00051 setSceneRadius(6.0); 00052 showEntireScene(); 00053 setSceneRadius(20.0); // cheap hack in getting the camera to first zoom in, and then set a large scene for defining clipping planes 00054 00055 // libqglviewer by default sets the following (see the docs) 00056 // glEnable(GL_LIGHT0); 00057 // glEnable(GL_LIGHTING); 00058 // glEnable(GL_DEPTH_TEST); 00059 // glEnable(GL_COLOR_MATERIAL); 00060 // // libQGL enables lighting, but default lighting makes frames hard to read, so set some ambient light 00061 // GLfloat light_ambient[] = {0.8, 0.8, 0.8, 1.0}; 00062 // glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); 00063 glDisable(GL_LIGHTING); 00064 00065 last_redraw_ = QDateTime::currentMSecsSinceEpoch(); 00066 00067 // restoreStateFromFile(); 00068 00069 // Enable direct frame manipulation when the mouse hovers. 00070 // setMouseTracking(true); 00071 //setMouseBinding(Qt::AltModifier, Qt::RightButton, QGLViewer::FRAME, QGLViewer::ROTATE); 00072 // setKeyDescription(Qt::Key_Plus, "Increases interpolation speed"); 00073 // setKeyDescription(Qt::Key_Minus, "Decreases interpolation speed"); 00074 // setKeyDescription(Qt::Key_Left, "Selects previous key frame"); 00075 // setKeyDescription(Qt::Key_Right, "Selects next key frame"); 00076 // setKeyDescription(Qt::Key_Return, "Starts/stops interpolation"); 00077 // help(); // don't call qglviewer's help object, we send our help, keyboard, mouse strings to a higher level window. 00078 00079 } 00080 00081 /***************************************************************************** 00082 ** Events 00083 *****************************************************************************/ 00084 00085 //void QGLViewer::mouseMoveEvent(QMouseEvent *e) 00086 //{ 00091 // if (( ecl::TimeStamp() - last_show_image_ ) > show_image_interval_) { 00092 // signalImagesToShow_(e->pos()); 00093 // last_show_image_ = ecl::TimeStamp(); 00094 // } 00095 // QGLViewer::mouseMoveEvent(e); 00096 //} 00097 00098 //void QGLViewer::keyPressEvent(QKeyEvent *e) 00099 //{ 00100 // switch (e->key()) 00101 // { 00102 // default: 00103 // QGLViewer::keyPressEvent(e); 00104 // } 00105 //} 00106 00107 /***************************************************************************** 00108 ** Help 00109 *****************************************************************************/ 00110 00111 QString QGLViewer::helpString() const 00112 { 00113 QString text("<h2>DSlam QGL Viewer </h2>"); 00114 text += "Common qgl viewer class for dslam applications.<br/>"; 00115 text += "<br/>"; 00116 text += "<b>Specifications</b><br/>"; 00117 text += "<br/>"; 00118 text += "<i>Grid Cells</i> : 1mx1m.<br/>"; 00119 text += "<br/>"; 00120 return text; 00121 } 00122 00123 /***************************************************************************** 00124 ** Drawing 00125 *****************************************************************************/ 00126 00127 void QGLViewer::redraw() { 00128 if (( QDateTime::currentMSecsSinceEpoch() - last_redraw_ ) > redraw_interval_ms_) { 00129 updateGL(); 00130 last_redraw_ = QDateTime::currentMSecsSinceEpoch(); 00131 } 00132 } 00133 00134 void QGLViewer::draw() 00135 { 00136 /******************** 00137 ** World Setup 00138 ********************/ 00139 qglColor(foregroundColor()); 00140 00141 // TODO : figure out a good 'scale' mapping metres to OpenGL units for everything 00142 // this will effect our grid dimensions below and the way we draw objects. 00143 glLineWidth(0.5); drawGrid(40.0f, 80); // currently, size is centre->edge (in metres), subdivisions 00144 00145 // /**************************************** 00146 // ** State 00147 // ****************************************/ 00148 // float cached_colour[4], cached_line_width; 00149 // glGetFloatv(GL_CURRENT_COLOR, cached_colour); 00150 // glGetFloatv(GL_LINE_WIDTH, &cached_line_width); 00151 // 00152 // /**************************************** 00153 // ** Restoration 00154 // ****************************************/ 00155 // glLineWidth(cached_line_width); 00156 // glColor4f(cached_colour[0], cached_colour[1], cached_colour[2], cached_colour[3]); 00157 } 00158 00159 /***************************************************************************** 00160 ** Trailers 00161 *****************************************************************************/ 00162 00163 } // namespace qglv