src/gl/partrenderer.cpp

changeset 28
c92c1daf735f
parent 27
c57fb7a5ffa3
child 30
1536f23cfab7
--- a/src/gl/partrenderer.cpp	Wed Jan 22 01:17:11 2020 +0200
+++ b/src/gl/partrenderer.cpp	Wed Jan 22 22:41:17 2020 +0200
@@ -17,6 +17,8 @@
  */
 
 #include <GL/glut.h>
+#include <glm/ext/matrix_transform.hpp>
+#include <glm/ext/matrix_clip_space.hpp>
 #include <QMouseEvent>
 #include <QMessageBox>
 #include "partrenderer.h"
@@ -85,13 +87,12 @@
 
 void PartRenderer::resizeGL(int width, int height)
 {
-	constexpr GLdouble near = 1.0;
-	constexpr GLdouble far = 1e+05;
-	glViewport (0, 0, width, height);
-	glMatrixMode(GL_PROJECTION);
-	glLoadIdentity();
-	gluPerspective(45.0, static_cast<double>(width) / static_cast<double>(height), near, far);
-	glMatrixMode(GL_MODELVIEW);
+	glViewport(0, 0, width, height);
+	this->projectionMatrix = glm::perspective(
+		glm::radians(90.0f),
+		static_cast<float>(width) / static_cast<float>(height),
+		0.1f,
+		100.f);
 }
 
 static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
@@ -149,7 +150,7 @@
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 		break;
 	}
-	this->compiler->setUniform("CameraTransformation", rotationMatrix);
+	this->compiler->setUniform("CameraTransformation", gl::toQMatrix(this->projectionMatrix * this->viewMatrix));
 	// Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
 	renderVao(gl::ArrayClass::Triangles);
 	renderVao(gl::ArrayClass::Quads);
@@ -195,6 +196,9 @@
 			0.6f * static_cast<float>(std::hypot(move.x(), move.y()))
 		);
 		this->rotation = versor * this->rotation;
+		QVector3D cameraPosition = this->rotation.rotatedVector({0, 0, 4.5});
+		glm::vec3 cameraPosition_glm = {cameraPosition.x(), cameraPosition.y(), cameraPosition.z()};
+		this->viewMatrix = glm::lookAt(cameraPosition_glm, {0, 0, 0}, {0, -1, 0});
 		this->update();
 	}
 	this->lastMousePosition = pointToPointF(event->pos());

mercurial