Go to the documentation of this file.
5 #ifndef GLVERTEX_QT_GLWINDOW_H
6 #define GLVERTEX_QT_GLWINDOW_H
10 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
32 #include <QElapsedTimer>
35 #include <QOpenGLContext>
36 #include <QOpenGLFunctions>
37 #include <QCoreApplication>
51 red_(0), green_(0), blue_(0), alpha_(1),
62 m_update_pending(false)
64 setSurfaceType(QWindow::OpenGLSurface);
66 QSurfaceFormat format;
67 format.setDepthBufferSize(24);
68 format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
69 #if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
70 format.setSwapInterval(1);
74 #if (LGL_OPENGL_VERSION>=30) || defined(LGL_GLES)
75 #if (LGL_OPENGL_VERSION >= 30)
76 #if (LGL_OPENGL_VERSION >= 32) && !defined(__APPLE__)
77 format.setVersion(3, 2);
79 format.setVersion(3, 0);
82 format.setVersion(2, 0);
84 #ifndef FORCE_COMPATIBILITY_PROFILE
85 format.setProfile(QSurfaceFormat::CoreProfile);
87 format.setProfile(QSurfaceFormat::CompatibilityProfile);
95 #if (LGL_OPENGL_VERSION >= 30)
96 std::cerr <<
"Creating QWindow with"
99 << (format.profile()==QSurfaceFormat::CoreProfile?
"core" :
"compatibility")
103 << format.majorVersion() <<
"." << format.minorVersion()
131 timerid_ = startTimer((
int)(1000/fps_));
134 if (timerid_ >= 0) killTimer(timerid_);
149 void background(
double red,
double green,
double blue,
double alpha=1)
162 if (red_+green_+blue_ > 1.5)
172 if (animation_) timer_.start();
253 if (on != wireframe_)
268 void override(
bool on)
271 if (!override_) background_ =
false;
278 if (on != fullscreen_)
281 if (fullscreen_) showFullScreen();
307 bool needsInitialize =
false;
311 m_context =
new QOpenGLContext(
this);
312 m_context->setFormat(requestedFormat());
315 needsInitialize =
true;
318 m_context->makeCurrent(
this);
323 initializeOpenGLFunctions();
336 float scale = devicePixelRatio();
345 double dt = 0.001*timer_.elapsed();
346 if (!animation_) dt = 0;
354 finishOpenGL(time_+=dt);
356 m_context->swapBuffers(
this);
365 if (!m_update_pending)
367 m_update_pending =
true;
368 QCoreApplication::postEvent(
this,
new QEvent(QEvent::UpdateRequest));
382 double red_, green_, blue_, alpha_;
393 QElapsedTimer timer_;
396 bool event(QEvent *event)
398 switch (event->type())
400 case QEvent::UpdateRequest:
401 m_update_pending =
false;
405 return(QWindow::event(event));
409 void exposeEvent(QExposeEvent *)
415 void keyPressEvent(QKeyEvent *event)
418 Qt::KeyboardModifier ctrl = Qt::ControlModifier;
420 Qt::KeyboardModifier ctrl = Qt::MetaModifier;
423 if (event->modifiers() & ctrl)
424 if (event->key() == Qt::Key_Space)
428 else if (event->key() == Qt::Key_Z)
432 else if (event->key() == Qt::Key_C)
436 else if (event->key() == Qt::Key_B)
440 else if (event->key() == Qt::Key_A)
444 else if (event->key() == Qt::Key_W)
448 else if (event->key() == Qt::Key_0)
452 else if (event->key() == Qt::Key_1)
456 else if (event->key() == Qt::Key_2)
460 else if (event->key() == Qt::Key_O)
462 override(!override_);
464 else if (event->key() == Qt::Key_F)
474 void timerEvent(QTimerEvent *)
481 virtual void initializeOpenGL() = 0;
482 virtual void prepareOpenGL(
double dt) {}
483 virtual void renderOpenGL(
double dt) = 0;
484 virtual void updateOpenGL(
double dt) {}
485 virtual void finishOpenGL(
double t) {}
486 virtual void finish() {}
490 QOpenGLContext *m_context;
491 bool m_update_pending;
void blending(bool on)
enable alpha blending
Definition: glvertex_qt_glwindow.h:217
void lglBlendMode(lgl_blendmode_enum mode)
change OpenGL state (blending)
Definition: glvertex_api.h:404
void lglAlphaTest(bool on=false, float value=0.0f, bool greater=true, bool equal=false)
change OpenGL state (alpha test)
Definition: glvertex_api.h:416
void lglInitializeOpenGL(float r=0, float g=0, float b=0, float a=1, bool ztest=true, bool culling=false)
initialize OpenGL state (clear color, depth test, back-face culling)
Definition: glvertex_api.h:367
virtual ~lgl_Qt_GLWindow()
dtor
Definition: glvertex_qt_glwindow.h:114
void lglPolygonMode(lgl_polygonmode_enum mode)
specify polygon mode (as defined by OpenGL 1.2)
Definition: glvertex_api.h:168
void animation(bool on)
enable animation
Definition: glvertex_qt_glwindow.h:169
void restart()
restart time
Definition: glvertex_qt_glwindow.h:295
void setFPS(double rate)
set frame rate
Definition: glvertex_qt_glwindow.h:126
void lglViewport(int ax, int ay, int bx, int by)
specify viewport (as defined by OpenGL 1.2)
Definition: glvertex_api.h:132
void toggle_ztest()
toggle z-buffer test
Definition: glvertex_qt_glwindow.h:194
void toggle_culling()
toggle back-face culling
Definition: glvertex_qt_glwindow.h:211
void toggle_blending()
toggle alpha blending
Definition: glvertex_qt_glwindow.h:228
void lglClear(GLuint bits=GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
clear color and depth buffer (as defined by OpenGL 1.2)
Definition: glvertex_api.h:144
lgl_Qt_GLWindow(bool core=true)
default ctor
Definition: glvertex_qt_glwindow.h:45
void lglClearColor(float w=0, float a=1)
specify clear color (as defined by OpenGL 1.2)
Definition: glvertex_api.h:136
void lglDepthTest(bool on=false)
change OpenGL state (depth test)
Definition: glvertex_api.h:388
void lglBackFaceCulling(bool on=true)
change OpenGL state (back-face culling)
Definition: glvertex_api.h:396
void toggle_atest()
toggle alpha testing
Definition: glvertex_qt_glwindow.h:245
void culling(bool on)
enable back-face culling
Definition: glvertex_qt_glwindow.h:200
void fullscreen(bool on)
enable fullscreen mode
Definition: glvertex_qt_glwindow.h:276
void atest(bool on)
enable alpha testing
Definition: glvertex_qt_glwindow.h:234
void setTimeLapse(double speedup)
set time lapse speedup
Definition: glvertex_qt_glwindow.h:143
void toggle_animation()
toggle animation
Definition: glvertex_qt_glwindow.h:177
void update()
update
Definition: glvertex_qt_glwindow.h:120
void toggle_wireframe()
toggle wireframe mode
Definition: glvertex_qt_glwindow.h:262
void background(double red, double green, double blue, double alpha=1)
set background color
Definition: glvertex_qt_glwindow.h:149
mat4 scale(const mat4 &m, const vec4 &c)
inline scale
Definition: glslmath.h:2591
void toggle_fullscreen()
toggle fullscreen mode
Definition: glvertex_qt_glwindow.h:289
void ztest(bool on)
enable z-buffer test
Definition: glvertex_qt_glwindow.h:183
void toggle_background()
toggle background color
Definition: glvertex_qt_glwindow.h:160
Qt OpenGL window base class.
Definition: glvertex_qt_glwindow.h:40
void wireframe(bool on)
enable wireframe mode
Definition: glvertex_qt_glwindow.h:251