Tidying up code.
This commit is contained in:
parent
9547824f14
commit
d3b71a92b9
@ -4,19 +4,28 @@
|
|||||||
#include <QMatrix4x4>
|
#include <QMatrix4x4>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
//#include <QtMath>
|
|
||||||
|
|
||||||
using namespace PolyVox;
|
using namespace PolyVox;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Public functions
|
// Protected functions
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
OpenGLWidget::OpenGLWidget(QWidget *parent)
|
OpenGLWidget::OpenGLWidget(QWidget *parent)
|
||||||
:QGLWidget(parent)
|
:QGLWidget(parent)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const QMatrix4x4& OpenGLWidget::viewMatrix()
|
||||||
|
{
|
||||||
|
return mViewMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QMatrix4x4& OpenGLWidget::projectionMatrix()
|
||||||
|
{
|
||||||
|
return mProjectionMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw)
|
void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw)
|
||||||
{
|
{
|
||||||
mCameraPosition = position;
|
mCameraPosition = position;
|
||||||
@ -24,40 +33,8 @@ void OpenGLWidget::setCameraTransform(QVector3D position, float pitch, float yaw
|
|||||||
mCameraPitch = pitch;
|
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()
|
void OpenGLWidget::initializeGL()
|
||||||
{
|
{
|
||||||
@ -106,9 +83,8 @@ void OpenGLWidget::resizeGL(int w, int h)
|
|||||||
float zNear = 1.0;
|
float zNear = 1.0;
|
||||||
float zFar = 1000.0;
|
float zFar = 1000.0;
|
||||||
|
|
||||||
projectionMatrix.setToIdentity();
|
mProjectionMatrix.setToIdentity();
|
||||||
//projectionMatrix.frustum(-aspectRatio, aspectRatio, -1, 1, zNear, zFar);
|
mProjectionMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar);
|
||||||
projectionMatrix.perspective(mCameraFOV, aspectRatio, zNear, zFar);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLWidget::paintGL()
|
void OpenGLWidget::paintGL()
|
||||||
@ -154,8 +130,8 @@ void OpenGLWidget::paintGL()
|
|||||||
mCameraPosition -= cameraRight * deltaTime * mCameraMoveSpeed;
|
mCameraPosition -= cameraRight * deltaTime * mCameraMoveSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
viewMatrix.setToIdentity();
|
mViewMatrix.setToIdentity();
|
||||||
viewMatrix.lookAt(
|
mViewMatrix.lookAt(
|
||||||
mCameraPosition, // Camera is here
|
mCameraPosition, // Camera is here
|
||||||
mCameraPosition + cameraForward, // and looks here : at the same position, plus "direction"
|
mCameraPosition + cameraForward, // and looks here : at the same position, plus "direction"
|
||||||
cameraUp // Head is up (set to 0,-1,0 to look upside-down)
|
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;
|
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());
|
||||||
|
}
|
@ -34,17 +34,33 @@ distribution.
|
|||||||
#include <QOpenGLVertexArrayObject>
|
#include <QOpenGLVertexArrayObject>
|
||||||
#include <QOpenGLBuffer>
|
#include <QOpenGLBuffer>
|
||||||
|
|
||||||
// Our OpenGLWidget is used by all the examples to render the extracted meshes. It is
|
// This is a very basic class for getting an OpenGL example up and running with Qt5. It simply displays
|
||||||
// fairly specific to our needs (you probably won't want to use it in your own project)
|
// an OpenGL widget and implements an FPS-style camera as well as other very basic functionality. User
|
||||||
// but should provide a useful illustration of how PolyVox meshes can be rendered.
|
// code can derive from this and override the provided virtual functions to implement functionality.
|
||||||
class OpenGLWidget : public QGLWidget, protected QOpenGLFunctions_3_1
|
class OpenGLWidget : public QGLWidget, protected QOpenGLFunctions_3_1
|
||||||
{
|
{
|
||||||
public:
|
protected:
|
||||||
// Constructor
|
// Protected constructor because this widget should not be created directly - it should only be subclassed.
|
||||||
OpenGLWidget(QWidget *parent);
|
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);
|
void setCameraTransform(QVector3D position, float pitch, float yaw);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
// Qt OpenGL functions
|
||||||
|
void initializeGL();
|
||||||
|
void resizeGL(int w, int h);
|
||||||
|
void paintGL();
|
||||||
|
|
||||||
// Mouse handling
|
// Mouse handling
|
||||||
void mouseMoveEvent(QMouseEvent* event);
|
void mouseMoveEvent(QMouseEvent* event);
|
||||||
void mousePressEvent(QMouseEvent* event);
|
void mousePressEvent(QMouseEvent* event);
|
||||||
@ -53,21 +69,9 @@ public:
|
|||||||
void keyPressEvent(QKeyEvent* event);
|
void keyPressEvent(QKeyEvent* event);
|
||||||
void keyReleaseEvent(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
|
// Matrices
|
||||||
QMatrix4x4 viewMatrix;
|
QMatrix4x4 mViewMatrix;
|
||||||
QMatrix4x4 projectionMatrix;
|
QMatrix4x4 mProjectionMatrix;
|
||||||
|
|
||||||
// Mouse data
|
// Mouse data
|
||||||
QPoint m_LastFrameMousePos;
|
QPoint m_LastFrameMousePos;
|
||||||
|
@ -163,8 +163,8 @@ protected:
|
|||||||
mShader->bind();
|
mShader->bind();
|
||||||
|
|
||||||
// These two matrices are constant for all meshes.
|
// These two matrices are constant for all meshes.
|
||||||
mShader->setUniformValue("viewMatrix", viewMatrix);
|
mShader->setUniformValue("viewMatrix", viewMatrix());
|
||||||
mShader->setUniformValue("projectionMatrix", projectionMatrix);
|
mShader->setUniformValue("projectionMatrix", projectionMatrix());
|
||||||
|
|
||||||
// Iterate over each mesh which the user added to our list, and render it.
|
// Iterate over each mesh which the user added to our list, and render it.
|
||||||
for (OpenGLMeshData meshData : mMeshData)
|
for (OpenGLMeshData meshData : mMeshData)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user