diff --git a/examples/common/OpenGLWidget.cpp b/examples/common/OpenGLWidget.cpp index 3eb24fb6..2867d574 100644 --- a/examples/common/OpenGLWidget.cpp +++ b/examples/common/OpenGLWidget.cpp @@ -4,19 +4,28 @@ #include #include #include -//#include using namespace PolyVox; using namespace std; //////////////////////////////////////////////////////////////////////////////// -// Public functions +// Protected functions //////////////////////////////////////////////////////////////////////////////// OpenGLWidget::OpenGLWidget(QWidget *parent) :QGLWidget(parent) { } +const QMatrix4x4& OpenGLWidget::viewMatrix() +{ + return mViewMatrix; +} + +const QMatrix4x4& OpenGLWidget::projectionMatrix() +{ + return mProjectionMatrix; +} + void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw) { mCameraPosition = position; @@ -24,40 +33,8 @@ void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw mCameraPitch = pitch; } -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) -{ - // Update the x and y rotations based on the mouse movement. - m_CurrentMousePos = event->pos(); - QPoint diff = m_CurrentMousePos - m_LastFrameMousePos; - mCameraYaw -= diff.x() * mCameraRotateSpeed; - mCameraPitch -= diff.y() * mCameraRotateSpeed; - m_LastFrameMousePos = m_CurrentMousePos; -} - -void OpenGLWidget::keyPressEvent(QKeyEvent* event) -{ - if (event->key() == Qt::Key_Escape) - { - close(); - } - - mPressedKeys.append(event->key()); -} - -void OpenGLWidget::keyReleaseEvent(QKeyEvent* event) -{ - mPressedKeys.removeAll(event->key()); -} - //////////////////////////////////////////////////////////////////////////////// -// Protected functions +// Private functions //////////////////////////////////////////////////////////////////////////////// void OpenGLWidget::initializeGL() { @@ -106,9 +83,8 @@ void OpenGLWidget::resizeGL(int w, int h) float zNear = 1.0; float zFar = 1000.0; - projectionMatrix.setToIdentity(); - //projectionMatrix.frustum(-aspectRatio, aspectRatio, -1, 1, zNear, zFar); - projectionMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar); + mProjectionMatrix.setToIdentity(); + mProjectionMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar); } void OpenGLWidget::paintGL() @@ -154,8 +130,8 @@ void OpenGLWidget::paintGL() mCameraPosition -= cameraRight * deltaTime * mCameraMoveSpeed; } - viewMatrix.setToIdentity(); - viewMatrix.lookAt( + mViewMatrix.setToIdentity(); + mViewMatrix.lookAt( mCameraPosition, // Camera is here mCameraPosition + cameraForward, // and looks here : at the same position, plus "direction" cameraUp // Head is up (set to 0,-1,0 to look upside-down) @@ -173,3 +149,35 @@ void OpenGLWidget::paintGL() std::cerr << "OpenGL Error: " << errCode << std::endl; } } + +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) +{ + // Update the x and y rotations based on the mouse movement. + m_CurrentMousePos = event->pos(); + QPoint diff = m_CurrentMousePos - m_LastFrameMousePos; + mCameraYaw -= diff.x() * mCameraRotateSpeed; + mCameraPitch -= diff.y() * mCameraRotateSpeed; + m_LastFrameMousePos = m_CurrentMousePos; +} + +void OpenGLWidget::keyPressEvent(QKeyEvent* event) +{ + if (event->key() == Qt::Key_Escape) + { + close(); + } + + mPressedKeys.append(event->key()); +} + +void OpenGLWidget::keyReleaseEvent(QKeyEvent* event) +{ + mPressedKeys.removeAll(event->key()); +} \ No newline at end of file diff --git a/examples/common/OpenGLWidget.h b/examples/common/OpenGLWidget.h index 8bbb3e1e..31d73244 100644 --- a/examples/common/OpenGLWidget.h +++ b/examples/common/OpenGLWidget.h @@ -34,17 +34,33 @@ distribution. #include #include -// Our OpenGLWidget is used by all the examples to render the extracted meshes. It is -// fairly specific to our needs (you probably won't want to use it in your own project) -// but should provide a useful illustration of how PolyVox meshes can be rendered. +// 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 { -public: - // Constructor +protected: + // Protected constructor because this widget should not be created directly - it should only be subclassed. OpenGLWidget(QWidget *parent); + // Derived classes should override these to provide functionality. + virtual void initialize() {} + virtual void renderOneFrame() {} + + // Getters for properties defined by this widget. + const QMatrix4x4& viewMatrix(); + const QMatrix4x4& projectionMatrix(); + + // Setters for properties defined by this widget. void setCameraTransform(QVector3D position, float pitch, float yaw); +private: + + // Qt OpenGL functions + void initializeGL(); + void resizeGL(int w, int h); + void paintGL(); + // Mouse handling void mouseMoveEvent(QMouseEvent* event); void mousePressEvent(QMouseEvent* event); @@ -53,21 +69,9 @@ public: void keyPressEvent(QKeyEvent* event); void keyReleaseEvent(QKeyEvent* event); -protected: - - // Qt OpenGL functions - void initializeGL(); - void resizeGL(int w, int h); - void paintGL(); - - virtual void initialize() {} - virtual void renderOneFrame() {} - -protected: - // Matrices - QMatrix4x4 viewMatrix; - QMatrix4x4 projectionMatrix; + QMatrix4x4 mViewMatrix; + QMatrix4x4 mProjectionMatrix; // Mouse data QPoint m_LastFrameMousePos; diff --git a/examples/common/PolyVoxExample.h b/examples/common/PolyVoxExample.h index b80fe56b..2fcbadcb 100644 --- a/examples/common/PolyVoxExample.h +++ b/examples/common/PolyVoxExample.h @@ -163,8 +163,8 @@ protected: mShader->bind(); // These two matrices are constant for all meshes. - mShader->setUniformValue("viewMatrix", viewMatrix); - mShader->setUniformValue("projectionMatrix", projectionMatrix); + mShader->setUniformValue("viewMatrix", viewMatrix()); + mShader->setUniformValue("projectionMatrix", projectionMatrix()); // Iterate over each mesh which the user added to our list, and render it. for (OpenGLMeshData meshData : mMeshData)