diff --git a/examples/Basic/OpenGLWidget.cpp b/examples/Basic/OpenGLWidget.cpp index 5ca5880f..17355b2f 100644 --- a/examples/Basic/OpenGLWidget.cpp +++ b/examples/Basic/OpenGLWidget.cpp @@ -9,6 +9,8 @@ using namespace std; OpenGLWidget::OpenGLWidget(QWidget *parent) :QGLWidget(parent) + ,mCenterPoint(32,32,32) + ,mDistFromCenter(50) ,m_xRotation(0) ,m_yRotation(0) { @@ -129,6 +131,8 @@ void OpenGLWidget::initializeGL() std::cerr << shader.log().toStdString() << std::endl; exit(EXIT_FAILURE); } + + setupWorldToCameraMatrix(); } void OpenGLWidget::resizeGL(int w, int h) @@ -196,17 +200,22 @@ void OpenGLWidget::mouseMoveEvent(QMouseEvent* event) m_yRotation += diff.y(); m_LastFrameMousePos = m_CurrentMousePos; + setupWorldToCameraMatrix(); + + update(); +} + +void OpenGLWidget::setupWorldToCameraMatrix() +{ shader.bind(); QMatrix4x4 worldToCameraMatrix{}; - worldToCameraMatrix.translate(0, 0, -50); //Move the camera back by 50 units + worldToCameraMatrix.translate(0, 0, -mDistFromCenter); //Move the camera back by the required amount worldToCameraMatrix.rotate(m_xRotation, 0, 1, 0); //rotate around y-axis worldToCameraMatrix.rotate(m_yRotation, 1, 0, 0); //rotate around x-axis - worldToCameraMatrix.translate(-32, -32, -32); //centre the model on the origin + worldToCameraMatrix.translate(-mCenterPoint); //centre the model on the origin shader.setUniformValue("worldToCameraMatrix", worldToCameraMatrix); shader.release(); - - update(); -} +} \ No newline at end of file diff --git a/examples/Basic/OpenGLWidget.h b/examples/Basic/OpenGLWidget.h index b797b5b3..5e7e46d9 100644 --- a/examples/Basic/OpenGLWidget.h +++ b/examples/Basic/OpenGLWidget.h @@ -59,6 +59,9 @@ protected: void paintGL(); private: + + void setupWorldToCameraMatrix(); + //Index/vertex buffer data std::vector mMeshData; @@ -67,6 +70,10 @@ private: //Mouse data QPoint m_LastFrameMousePos; QPoint m_CurrentMousePos; + + //Camera setup + QVector3D mCenterPoint; + float mDistFromCenter; int m_xRotation; int m_yRotation; };