00001
00002
00003
00004
00005
00006
00007 #include <QResizeEvent>
00008 #include <QPaintEvent>
00009 #include <QCloseEvent>
00010
00011 #include "CGLSurface.h"
00012
00013 #include "gl/gl.h"
00014 #include "gl/glu.h"
00015 #pragma comment(lib,"OpenGL32.lib")
00016 #pragma comment(lib,"glu32.lib")
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 CGLSurface::CGLSurface(QGLFormat & requestFormat,QWidget *parent)
00028 : QGLWidget(requestFormat,parent),renderer(this)
00029 {
00030 ui.setupUi(this);
00031 setAttribute(Qt::WA_NoSystemBackground);
00032
00033
00034 useRenderThread = true;
00035 pInputHandler = NULL;
00036 initialized = false;
00037 bRenderWithOverlay = false;
00038 pPainter = new QPainter();
00039 }
00040
00041
00042 CGLSurface::~CGLSurface(){}
00043
00044
00045
00046 void CGLSurface::registerInputHandler(TSmartPointer<IInputHandler> handler){
00047 pInputHandler = handler;
00048 }
00049
00050 void CGLSurface::unregisterInputHandler(TSmartPointer<IInputHandler> handler){
00051 pInputHandler = NULL;
00052 }
00053
00054
00055 void CGLSurface::StartAnimate(int ms){timerID = this->startTimer(ms);}
00056 void CGLSurface::EndAnimate(){this->killTimer(timerID);}
00057
00058
00059 void CGLSurface::EnableThread(bool tf){
00060 if(tf){
00061 useRenderThread = true;
00062 if(initialized){
00063 Log("QGLSurface switching Threaded Rendering ON");
00064 finishRendering();
00065 initRendering();
00066 }
00067 }else{
00068 useRenderThread = false;
00069 if(initialized){
00070 Log("QGLSurface switching Threaded Rendering OFF");
00071 finishRendering();
00072 }
00073 }
00074 }
00075
00076 void CGLSurface::initRendering( )
00077 {
00078 renderer.render_flag = true;
00079 renderer.start();
00080 render_condition.wakeAll();
00081 }
00082
00083 void CGLSurface::finishRendering( )
00084 {
00085 renderer.render_flag = false;
00086 render_condition.wakeAll();
00087 renderer.wait();
00088 }
00089
00090
00091 void CGLSurface::lockGLContext( )
00092 {
00093
00094 render_mutex.lock();
00095
00096 this->makeCurrent();
00097 }
00098
00099 void CGLSurface::unlockGLContext( )
00100 {
00101
00102
00103 this->doneCurrent();
00104
00105 render_mutex.unlock();
00106 }
00107
00108
00109
00110 void CGLSurface::showEvent(QShowEvent * _e)
00111 {
00112 QWidget::showEvent(_e);
00113 }
00114
00115 void CGLSurface::resizeEvent(QResizeEvent * _e)
00116 {
00117 viewportWidth = _e->size().width();
00118 viewportHeight = _e->size().height();
00119
00120 if(initialized){
00121 if(!useRenderThread){
00122 makeCurrent();
00123 onResize();
00124 }else{
00125 renderer.resize_flag = true;
00126 }
00127 }
00128 }
00129
00130 void CGLSurface::paintEvent(QPaintEvent *)
00131 {
00132 if(!initialized)
00133 {
00134 if(useRenderThread){
00135 renderer.resize_flag = true;
00136 initRendering();
00137 Log("QGLSurface initializing (Threaded)");
00138 }else{
00139 makeCurrent();
00140 onResize();
00141 onInitialize();
00142 Log("QGLSurface initializing (NonThreaded)");
00143 initialized = true;
00144 }
00145 }
00146 Render();
00147 }
00148
00149 void CGLSurface::closeEvent(QCloseEvent * _e)
00150 {
00151 if(!renderer.isRunning()){
00152 makeCurrent();
00153 onFinishRendering();
00154 }else{
00155 finishRendering();
00156 }
00157 _e->accept();
00158 }
00159
00160
00161 void CGLSurface::Render()
00162 {
00163 if(renderer.isRunning()){
00164
00165 render_condition.wakeAll();
00166 }else{
00167 if(bRenderWithOverlay){
00168 onOverlayDraw(pPainter);
00169 }else{
00170 makeCurrent();
00171 onDraw();
00172 }
00173 }
00174 }
00175
00176 void CGLSurface::EnableOverlay(bool tf)
00177 {
00178 bRenderWithOverlay = tf;
00179
00180 if(useRenderThread){
00181 lockGLContext();
00182 onInitialize();
00183 unlockGLContext();
00184 }else{
00185 makeCurrent();
00186 onInitialize();
00187 }
00188 Render();
00189 }
00190
00191
00192 void CGLSurface::onInitialize()
00193 {
00194
00195 }
00196
00197
00198 void CGLSurface::onResize()
00199 {
00200 glViewport(0, 0, viewportWidth, viewportHeight);
00201 }
00202
00203
00204 void CGLSurface::onDraw(){
00205
00206 static float rot = 0;
00207
00208
00209 QPainter painter(this);
00210 painter.setBackgroundMode(Qt::OpaqueMode);
00211
00212
00213 glPushAttrib(GL_ALL_ATTRIB_BITS);
00214
00215
00216
00217 glClearColor(0,0,0,0);
00218 glClear(GL_COLOR_BUFFER_BIT);
00219
00220 glMatrixMode(GL_PROJECTION);
00221 glPushMatrix();
00222 glLoadIdentity();
00223 float aspect = (float)viewportHeight/viewportWidth;
00224 gluOrtho2D(0,0,viewportWidth*aspect,viewportHeight);
00225
00226 rot+=5;if(rot > 360)rot = 0;
00227
00228 glMatrixMode(GL_MODELVIEW);
00229 glPushMatrix();
00230 glLoadIdentity();
00231 glRotatef(rot,0,0,1);
00232 glScalef(0.1,0.1,1);
00233
00234 glColor3f(0.7,0.7,1.0);
00235 glBegin(GL_POLYGON);
00236 glVertex2f(-4,-4);
00237 glVertex2f(4,-4);
00238 glVertex2f(4,4);
00239 glVertex2f(-4,4);
00240 glEnd();
00241
00242
00243 glPopAttrib();
00244 glMatrixMode(GL_PROJECTION);
00245 glPopMatrix();
00246 glMatrixMode(GL_MODELVIEW);
00247 glPopMatrix();
00248
00249
00250 painter.setRenderHint(QPainter::TextAntialiasing);
00251 painter.setPen(Qt::white);
00252 painter.setFont(QFont("Arial",20));
00253 int width = painter.fontMetrics().width("Reimplement onDraw()!");
00254 int height = painter.fontMetrics().height();
00255 painter.drawText((viewportWidth-width)/2,(viewportHeight-height)/2,width,height,Qt::AlignCenter,"Reimplement onDraw()!");
00256
00257 painter.end();
00258 }
00259
00260
00261 void CGLSurface::onFinishRendering(){
00262
00263 }
00264
00265
00266
00267
00268
00269 void CGLSurface::enterEvent(QEvent *event){
00270 grabKeyboard();
00271 this->setFocus(Qt::NoFocusReason);
00272 event->accept();
00273 }
00274
00275 void CGLSurface::leaveEvent(QEvent *event){
00276 releaseKeyboard();
00277 event->accept();
00278 }
00279
00280 void CGLSurface::keyPressEvent(QKeyEvent *event){
00281 if(pInputHandler){
00282 pInputHandler->OnKeyDown(event->key());
00283 }
00284 event->accept();
00285 }
00286
00287 void CGLSurface::keyReleaseEvent(QKeyEvent *event){
00288 if(pInputHandler){
00289 pInputHandler->OnKeyUp(event->key());
00290 }
00291 event->accept();
00292 }
00293
00294 void CGLSurface::contextMenuEvent(QContextMenuEvent *event){
00295 event->ignore();
00296 }
00297
00298 void CGLSurface::mouseMoveEvent(QMouseEvent *event){
00299
00300 dx = event->pos().x() - oldPosX;
00301 dy = event->pos().y() - oldPosY;
00302
00303 if(pInputHandler){
00304 pInputHandler->OnMouseMove(dx,dy,0);
00305 }
00306
00307 oldPosX = event->pos().x();
00308 oldPosY = event->pos().y();
00309
00310 event->accept();
00311 Render();
00312 }
00313
00314 void CGLSurface::mousePressEvent(QMouseEvent *event){
00315
00316 oldPosX = event->pos().x();
00317 oldPosY = event->pos().y();
00318
00319 if(pInputHandler){
00320 if(event->button()!= Qt::NoButton){
00321 pInputHandler->OnMouseKeyDown(event->button());
00322 }
00323 }
00324 event->accept();
00325 }
00326
00327 void CGLSurface::mouseReleaseEvent(QMouseEvent *event){
00328 if(pInputHandler){
00329 if(event->button()!= Qt::NoButton){
00330 pInputHandler->OnMouseKeyUp(event->button());
00331 }
00332 }
00333 event->accept();
00334 }