Templatized OpenGLWidget so users can specify the version of OpenGL to support.

This commit is contained in:
David Williams 2015-02-24 22:17:46 +01:00
parent d3b71a92b9
commit e985dce075
8 changed files with 33 additions and 18 deletions

View File

@ -24,13 +24,13 @@ PROJECT(BasicExample)
#Projects source files #Projects source files
SET(SRC_FILES SET(SRC_FILES
main.cpp main.cpp
../common/OpenGLWidget.cpp
../common/PolyVoxExample.cpp ../common/PolyVoxExample.cpp
) )
#Projects headers files #Projects headers files
SET(INC_FILES SET(INC_FILES
../common/OpenGLWidget.h ../common/OpenGLWidget.h
../common/OpenGLWidget.inl
../common/PolyVoxExample.h ../common/PolyVoxExample.h
) )

View File

@ -24,13 +24,13 @@ PROJECT(DecodeOnGPUExample)
#Projects source files #Projects source files
SET(SRC_FILES SET(SRC_FILES
main.cpp main.cpp
../common/OpenGLWidget.cpp
../common/PolyVoxExample.cpp ../common/PolyVoxExample.cpp
) )
#Projects headers files #Projects headers files
SET(INC_FILES SET(INC_FILES
../common/OpenGLWidget.h ../common/OpenGLWidget.h
../common/OpenGLWidget.inl
../common/PolyVoxExample.h ../common/PolyVoxExample.h
) )

View File

@ -29,7 +29,6 @@ SET(SRC_FILES
#OpenGLImmediateModeSupport.cpp #OpenGLImmediateModeSupport.cpp
#OpenGLSupport.cpp #OpenGLSupport.cpp
#OpenGLVertexBufferObjectSupport.cpp #OpenGLVertexBufferObjectSupport.cpp
../common/OpenGLWidget.cpp
../common/PolyVoxExample.cpp ../common/PolyVoxExample.cpp
Shapes.cpp Shapes.cpp
) )
@ -40,6 +39,7 @@ SET(INC_FILES
#OpenGLSupport.h #OpenGLSupport.h
#OpenGLVertexBufferObjectSupport.h #OpenGLVertexBufferObjectSupport.h
../common/OpenGLWidget.h ../common/OpenGLWidget.h
../common/OpenGLWidget.inl
../common/PolyVoxExample.h ../common/PolyVoxExample.h
Shapes.h Shapes.h
) )

View File

@ -24,7 +24,6 @@ PROJECT(PagingExample)
#Projects source files #Projects source files
SET(SRC_FILES SET(SRC_FILES
main.cpp main.cpp
../common/OpenGLWidget.cpp
Perlin.cpp Perlin.cpp
../common/PolyVoxExample.cpp ../common/PolyVoxExample.cpp
) )
@ -32,6 +31,7 @@ SET(SRC_FILES
#Projects headers files #Projects headers files
SET(INC_FILES SET(INC_FILES
../common/OpenGLWidget.h ../common/OpenGLWidget.h
../common/OpenGLWidget.inl
Perlin.h Perlin.h
../common/PolyVoxExample.h ../common/PolyVoxExample.h
) )

View File

@ -24,13 +24,13 @@ PROJECT(SmoothLODExample)
#Projects source files #Projects source files
SET(SRC_FILES SET(SRC_FILES
main.cpp main.cpp
../common/OpenGLWidget.cpp
../common/PolyVoxExample.cpp ../common/PolyVoxExample.cpp
) )
#Projects headers files #Projects headers files
SET(INC_FILES SET(INC_FILES
../common/OpenGLWidget.h ../common/OpenGLWidget.h
../common/OpenGLWidget.inl
../common/PolyVoxExample.h ../common/PolyVoxExample.h
) )

View File

@ -37,7 +37,9 @@ distribution.
// This is a very basic class for getting an OpenGL example up and running with Qt5. It simply displays // 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 // 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. // 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 <typename QOpenGLFunctionsType>
class OpenGLWidget : public QGLWidget, protected QOpenGLFunctionsType
{ {
protected: protected:
// Protected constructor because this widget should not be created directly - it should only be subclassed. // Protected constructor because this widget should not be created directly - it should only be subclassed.
@ -93,4 +95,6 @@ private:
QElapsedTimer mElapsedTimer; QElapsedTimer mElapsedTimer;
}; };
#include "OpenGLWidget.inl"
#endif //__BasicExample_OpenGLWidget_H__ #endif //__BasicExample_OpenGLWidget_H__

View File

@ -11,22 +11,26 @@ using namespace std;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Protected functions // Protected functions
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
OpenGLWidget::OpenGLWidget(QWidget *parent) template <typename QOpenGLFunctionsType>
OpenGLWidget<QOpenGLFunctionsType>::OpenGLWidget(QWidget *parent)
:QGLWidget(parent) :QGLWidget(parent)
{ {
} }
const QMatrix4x4& OpenGLWidget::viewMatrix() template <typename QOpenGLFunctionsType>
const QMatrix4x4& OpenGLWidget<QOpenGLFunctionsType>::viewMatrix()
{ {
return mViewMatrix; return mViewMatrix;
} }
const QMatrix4x4& OpenGLWidget::projectionMatrix() template <typename QOpenGLFunctionsType>
const QMatrix4x4& OpenGLWidget<QOpenGLFunctionsType>::projectionMatrix()
{ {
return mProjectionMatrix; return mProjectionMatrix;
} }
void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw) template <typename QOpenGLFunctionsType>
void OpenGLWidget<QOpenGLFunctionsType>::setCameraTransform(QVector3D position, float pitch, float yaw)
{ {
mCameraPosition = position; mCameraPosition = position;
mCameraYaw = yaw; mCameraYaw = yaw;
@ -36,7 +40,8 @@ void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Private functions // Private functions
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void OpenGLWidget::initializeGL() template <typename QOpenGLFunctionsType>
void OpenGLWidget<QOpenGLFunctionsType>::initializeGL()
{ {
if (!initializeOpenGLFunctions()) if (!initializeOpenGLFunctions())
{ {
@ -74,7 +79,8 @@ void OpenGLWidget::initializeGL()
mElapsedTimer.start(); mElapsedTimer.start();
} }
void OpenGLWidget::resizeGL(int w, int h) template <typename QOpenGLFunctionsType>
void OpenGLWidget<QOpenGLFunctionsType>::resizeGL(int w, int h)
{ {
//Setup the viewport //Setup the viewport
glViewport(0, 0, w, h); glViewport(0, 0, w, h);
@ -87,7 +93,8 @@ void OpenGLWidget::resizeGL(int w, int h)
mProjectionMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar); mProjectionMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar);
} }
void OpenGLWidget::paintGL() template <typename QOpenGLFunctionsType>
void OpenGLWidget<QOpenGLFunctionsType>::paintGL()
{ {
// Direction : Spherical coordinates to Cartesian coordinates conversion // Direction : Spherical coordinates to Cartesian coordinates conversion
QVector3D cameraForward( QVector3D cameraForward(
@ -150,14 +157,16 @@ void OpenGLWidget::paintGL()
} }
} }
void OpenGLWidget::mousePressEvent(QMouseEvent* event) template <typename QOpenGLFunctionsType>
void OpenGLWidget<QOpenGLFunctionsType>::mousePressEvent(QMouseEvent* event)
{ {
// Initialise these variables which will be used when the mouse actually moves. // Initialise these variables which will be used when the mouse actually moves.
m_CurrentMousePos = event->pos(); m_CurrentMousePos = event->pos();
m_LastFrameMousePos = m_CurrentMousePos; m_LastFrameMousePos = m_CurrentMousePos;
} }
void OpenGLWidget::mouseMoveEvent(QMouseEvent* event) template <typename QOpenGLFunctionsType>
void OpenGLWidget<QOpenGLFunctionsType>::mouseMoveEvent(QMouseEvent* event)
{ {
// Update the x and y rotations based on the mouse movement. // Update the x and y rotations based on the mouse movement.
m_CurrentMousePos = event->pos(); m_CurrentMousePos = event->pos();
@ -167,7 +176,8 @@ void OpenGLWidget::mouseMoveEvent(QMouseEvent* event)
m_LastFrameMousePos = m_CurrentMousePos; m_LastFrameMousePos = m_CurrentMousePos;
} }
void OpenGLWidget::keyPressEvent(QKeyEvent* event) template <typename QOpenGLFunctionsType>
void OpenGLWidget<QOpenGLFunctionsType>::keyPressEvent(QKeyEvent* event)
{ {
if (event->key() == Qt::Key_Escape) if (event->key() == Qt::Key_Escape)
{ {
@ -177,7 +187,8 @@ void OpenGLWidget::keyPressEvent(QKeyEvent* event)
mPressedKeys.append(event->key()); mPressedKeys.append(event->key());
} }
void OpenGLWidget::keyReleaseEvent(QKeyEvent* event) template <typename QOpenGLFunctionsType>
void OpenGLWidget<QOpenGLFunctionsType>::keyReleaseEvent(QKeyEvent* event)
{ {
mPressedKeys.removeAll(event->key()); mPressedKeys.removeAll(event->key());
} }

View File

@ -39,7 +39,7 @@ struct OpenGLMeshData
float scale; float scale;
}; };
class PolyVoxExample : public OpenGLWidget class PolyVoxExample : public OpenGLWidget<QOpenGLFunctions_3_1>
{ {
public: public:
PolyVoxExample(QWidget *parent) PolyVoxExample(QWidget *parent)