src/gl/partrenderer.cpp

changeset 56
fad4a5dd8dee
parent 55
cb81ecb5fb23
child 57
5c0005f63319
--- a/src/gl/partrenderer.cpp	Wed Feb 26 02:21:07 2020 +0200
+++ b/src/gl/partrenderer.cpp	Wed Feb 26 22:26:05 2020 +0200
@@ -72,11 +72,6 @@
 void PartRenderer::resizeGL(int width, int height)
 {
 	glViewport(0, 0, width, height);
-	this->viewportMatrix = {
-		{1, 0, 0},
-		{0, 1, 0},
-		{width * 0.5f, height * 0.5f, 1}
-	};
 	this->projectionMatrix = glm::perspective(
 		glm::radians(45.0f),
 		static_cast<float>(width) / static_cast<float>(height),
@@ -101,11 +96,25 @@
 	throw std::runtime_error{"Bad vbo class passed to getGlTypeForVboClass"};
 }
 
+#include <QPainter>
 void PartRenderer::paintGL()
 {
 	glEnable(GL_DEPTH_TEST);
 	glShadeModel(GL_SMOOTH);
 	this->renderScene();
+	QPainter painter{this};
+	painter.setPen(Qt::white);
+	painter.setBrush(Qt::red);
+	for (float x : {1, -1})
+	{
+		for (float y : {1, -1})
+		{
+			for (float z : {1, -1})
+			{
+				painter.drawEllipse(this->modelToScreenCoordinates({x, y, z}), 10, 10);
+			}
+		}
+	}
 }
 
 void PartRenderer::renderScene()
@@ -285,10 +294,14 @@
 	this->update();
 }
 
-std::optional<glm::vec3> PartRenderer::cameraToGrid(const QPoint& point)
+glm::vec3 PartRenderer::viewport(const glm::vec3& point)
+{
+	return viewport(point, this->width(), this->height());
+}
+
+std::optional<glm::vec3> PartRenderer::screenToModelCoordinates(const QPoint& point)
 {
 	glm::vec3 pp = {point.x(), this->height() - point.y(), 1};
-	pp = glm::inverse(this->viewportMatrix) * pp;
 	glm::mat4 matrix;
 	matrix = this->projectionMatrix * this->viewMatrix * glm::mat4_cast(this->modelQuaternion);
 	matrix = glm::transpose(glm::inverse(matrix));
@@ -298,18 +311,14 @@
 	return geom::linePlaneIntersection(line, geom::XY);
 }
 
-QPointF PartRenderer::worldToCamera(const glm::vec3& point)
+QPointF PartRenderer::modelToScreenCoordinates(const glm::vec3& point)
 {
 	glm::vec4 p = {point, 1};
 	p = glm::mat4_cast(this->modelQuaternion) * p;
 	p = this->viewMatrix * p;
 	p = this->projectionMatrix * p;
-	glm::vec2 pp = this->viewportMatrix * glm::vec3{p.x, p.y, 1};
+	glm::vec2 pp = this->viewport({p.x / p.w, p.y / p.w, 1});
 	return toQPointF(pp);
-	/*
-	const glm::mat4 matrix = this->projectionMatrix * this->viewMatrix * glm::mat4_cast(this->modelQuaternion);
-	return toQPointF(matrix * glm::vec4{point, 1});
-	*/
 }
 
 ldraw::Id PartRenderer::pick(const QPoint& where)
@@ -349,7 +358,15 @@
 	{
 		this->compiler->build(this->model, this->documents, this->renderPreferences);
 		this->setupBackgroundColor();
-
 	}
 	this->update();
 }
+
+glm::vec3 PartRenderer::viewport(const glm::vec3& point, float width, float height)
+{
+	return {
+		width * 0.5 * (point.x + 1),
+		height * 0.5 * (-point.y + 1),
+		0
+	};
+}

mercurial