From e985dce075380228e9a519b140216708f79a7ed6 Mon Sep 17 00:00:00 2001 From: David Williams Date: Tue, 24 Feb 2015 22:17:46 +0100 Subject: [PATCH] Templatized OpenGLWidget so users can specify the version of OpenGL to support. --- examples/Basic/CMakeLists.txt | 2 +- examples/DecodeOnGPU/CMakeLists.txt | 2 +- examples/OpenGL/CMakeLists.txt | 2 +- examples/Paging/CMakeLists.txt | 2 +- examples/SmoothLOD/CMakeLists.txt | 2 +- examples/common/OpenGLWidget.h | 6 +++- .../{OpenGLWidget.cpp => OpenGLWidget.inl} | 33 ++++++++++++------- examples/common/PolyVoxExample.h | 2 +- 8 files changed, 33 insertions(+), 18 deletions(-) rename examples/common/{OpenGLWidget.cpp => OpenGLWidget.inl} (78%) diff --git a/examples/Basic/CMakeLists.txt b/examples/Basic/CMakeLists.txt index 51156dc9..a7d74483 100644 --- a/examples/Basic/CMakeLists.txt +++ b/examples/Basic/CMakeLists.txt @@ -24,13 +24,13 @@ PROJECT(BasicExample) #Projects source files SET(SRC_FILES main.cpp - ../common/OpenGLWidget.cpp ../common/PolyVoxExample.cpp ) #Projects headers files SET(INC_FILES ../common/OpenGLWidget.h + ../common/OpenGLWidget.inl ../common/PolyVoxExample.h ) diff --git a/examples/DecodeOnGPU/CMakeLists.txt b/examples/DecodeOnGPU/CMakeLists.txt index 063c2ffd..53fd6f94 100644 --- a/examples/DecodeOnGPU/CMakeLists.txt +++ b/examples/DecodeOnGPU/CMakeLists.txt @@ -24,13 +24,13 @@ PROJECT(DecodeOnGPUExample) #Projects source files SET(SRC_FILES main.cpp - ../common/OpenGLWidget.cpp ../common/PolyVoxExample.cpp ) #Projects headers files SET(INC_FILES ../common/OpenGLWidget.h + ../common/OpenGLWidget.inl ../common/PolyVoxExample.h ) diff --git a/examples/OpenGL/CMakeLists.txt b/examples/OpenGL/CMakeLists.txt index c63ee818..994c2556 100644 --- a/examples/OpenGL/CMakeLists.txt +++ b/examples/OpenGL/CMakeLists.txt @@ -29,7 +29,6 @@ SET(SRC_FILES #OpenGLImmediateModeSupport.cpp #OpenGLSupport.cpp #OpenGLVertexBufferObjectSupport.cpp - ../common/OpenGLWidget.cpp ../common/PolyVoxExample.cpp Shapes.cpp ) @@ -40,6 +39,7 @@ SET(INC_FILES #OpenGLSupport.h #OpenGLVertexBufferObjectSupport.h ../common/OpenGLWidget.h + ../common/OpenGLWidget.inl ../common/PolyVoxExample.h Shapes.h ) diff --git a/examples/Paging/CMakeLists.txt b/examples/Paging/CMakeLists.txt index f5fffe89..060a4271 100644 --- a/examples/Paging/CMakeLists.txt +++ b/examples/Paging/CMakeLists.txt @@ -24,7 +24,6 @@ PROJECT(PagingExample) #Projects source files SET(SRC_FILES main.cpp - ../common/OpenGLWidget.cpp Perlin.cpp ../common/PolyVoxExample.cpp ) @@ -32,6 +31,7 @@ SET(SRC_FILES #Projects headers files SET(INC_FILES ../common/OpenGLWidget.h + ../common/OpenGLWidget.inl Perlin.h ../common/PolyVoxExample.h ) diff --git a/examples/SmoothLOD/CMakeLists.txt b/examples/SmoothLOD/CMakeLists.txt index 492662bb..dbdad08a 100644 --- a/examples/SmoothLOD/CMakeLists.txt +++ b/examples/SmoothLOD/CMakeLists.txt @@ -24,13 +24,13 @@ PROJECT(SmoothLODExample) #Projects source files SET(SRC_FILES main.cpp - ../common/OpenGLWidget.cpp ../common/PolyVoxExample.cpp ) #Projects headers files SET(INC_FILES ../common/OpenGLWidget.h + ../common/OpenGLWidget.inl ../common/PolyVoxExample.h ) diff --git a/examples/common/OpenGLWidget.h b/examples/common/OpenGLWidget.h index 31d73244..c2caadb1 100644 --- a/examples/common/OpenGLWidget.h +++ b/examples/common/OpenGLWidget.h @@ -37,7 +37,9 @@ distribution. // This is a very basic class for getting an OpenGL example up and running with Qt5. It simply displays // an OpenGL widget and implements an FPS-style camera as well as other very basic functionality. User // code can derive from this and override the provided virtual functions to implement functionality. -class OpenGLWidget : public QGLWidget, protected QOpenGLFunctions_3_1 +// The class is templatized so users can specify the OpenGL version via the appropriate QOpenGLFunctions. +template +class OpenGLWidget : public QGLWidget, protected QOpenGLFunctionsType { protected: // Protected constructor because this widget should not be created directly - it should only be subclassed. @@ -93,4 +95,6 @@ private: QElapsedTimer mElapsedTimer; }; +#include "OpenGLWidget.inl" + #endif //__BasicExample_OpenGLWidget_H__ diff --git a/examples/common/OpenGLWidget.cpp b/examples/common/OpenGLWidget.inl similarity index 78% rename from examples/common/OpenGLWidget.cpp rename to examples/common/OpenGLWidget.inl index 2867d574..2c7f1c78 100644 --- a/examples/common/OpenGLWidget.cpp +++ b/examples/common/OpenGLWidget.inl @@ -11,22 +11,26 @@ using namespace std; //////////////////////////////////////////////////////////////////////////////// // Protected functions //////////////////////////////////////////////////////////////////////////////// -OpenGLWidget::OpenGLWidget(QWidget *parent) +template +OpenGLWidget::OpenGLWidget(QWidget *parent) :QGLWidget(parent) { } -const QMatrix4x4& OpenGLWidget::viewMatrix() +template +const QMatrix4x4& OpenGLWidget::viewMatrix() { return mViewMatrix; } -const QMatrix4x4& OpenGLWidget::projectionMatrix() +template +const QMatrix4x4& OpenGLWidget::projectionMatrix() { return mProjectionMatrix; } -void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw) +template +void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw) { mCameraPosition = position; mCameraYaw = yaw; @@ -36,7 +40,8 @@ void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw //////////////////////////////////////////////////////////////////////////////// // Private functions //////////////////////////////////////////////////////////////////////////////// -void OpenGLWidget::initializeGL() +template +void OpenGLWidget::initializeGL() { if (!initializeOpenGLFunctions()) { @@ -74,7 +79,8 @@ void OpenGLWidget::initializeGL() mElapsedTimer.start(); } -void OpenGLWidget::resizeGL(int w, int h) +template +void OpenGLWidget::resizeGL(int w, int h) { //Setup the viewport glViewport(0, 0, w, h); @@ -87,7 +93,8 @@ void OpenGLWidget::resizeGL(int w, int h) mProjectionMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar); } -void OpenGLWidget::paintGL() +template +void OpenGLWidget::paintGL() { // Direction : Spherical coordinates to Cartesian coordinates conversion QVector3D cameraForward( @@ -150,14 +157,16 @@ void OpenGLWidget::paintGL() } } -void OpenGLWidget::mousePressEvent(QMouseEvent* event) +template +void OpenGLWidget::mousePressEvent(QMouseEvent* event) { // Initialise these variables which will be used when the mouse actually moves. m_CurrentMousePos = event->pos(); m_LastFrameMousePos = m_CurrentMousePos; } -void OpenGLWidget::mouseMoveEvent(QMouseEvent* event) +template +void OpenGLWidget::mouseMoveEvent(QMouseEvent* event) { // Update the x and y rotations based on the mouse movement. m_CurrentMousePos = event->pos(); @@ -167,7 +176,8 @@ void OpenGLWidget::mouseMoveEvent(QMouseEvent* event) m_LastFrameMousePos = m_CurrentMousePos; } -void OpenGLWidget::keyPressEvent(QKeyEvent* event) +template +void OpenGLWidget::keyPressEvent(QKeyEvent* event) { if (event->key() == Qt::Key_Escape) { @@ -177,7 +187,8 @@ void OpenGLWidget::keyPressEvent(QKeyEvent* event) mPressedKeys.append(event->key()); } -void OpenGLWidget::keyReleaseEvent(QKeyEvent* event) +template +void OpenGLWidget::keyReleaseEvent(QKeyEvent* event) { mPressedKeys.removeAll(event->key()); } \ No newline at end of file diff --git a/examples/common/PolyVoxExample.h b/examples/common/PolyVoxExample.h index 2fcbadcb..65a77351 100644 --- a/examples/common/PolyVoxExample.h +++ b/examples/common/PolyVoxExample.h @@ -39,7 +39,7 @@ struct OpenGLMeshData float scale; }; -class PolyVoxExample : public OpenGLWidget +class PolyVoxExample : public OpenGLWidget { public: PolyVoxExample(QWidget *parent)