PartRenderer::modelToScreenCoordinates FINALLY WORKS

Wed, 26 Feb 2020 22:26:05 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 26 Feb 2020 22:26:05 +0200
changeset 56
fad4a5dd8dee
parent 55
cb81ecb5fb23
child 57
5c0005f63319

PartRenderer::modelToScreenCoordinates FINALLY WORKS

src/gl/partrenderer.cpp file | annotate | diff | comparison | revisions
src/gl/partrenderer.h file | annotate | diff | comparison | revisions
src/ui/canvas.cpp file | annotate | diff | comparison | revisions
--- 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
+	};
+}
--- a/src/gl/partrenderer.h	Wed Feb 26 02:21:07 2020 +0200
+++ b/src/gl/partrenderer.h	Wed Feb 26 22:26:05 2020 +0200
@@ -23,6 +23,7 @@
 		QWidget* parent = nullptr);
 	~PartRenderer() override;
 	void setRenderPreferences(const gl::RenderPreferences& newPreferences);
+	static glm::vec3 viewport(const glm::vec3& point, float width, float height);
 protected:
 	ldraw::Id pick(const QPoint& where);
 	void initializeGL() override;
@@ -30,13 +31,14 @@
 	void paintGL() override;
 	void mouseMoveEvent(QMouseEvent* event) override;
 	void wheelEvent(QWheelEvent* event) override;
+	glm::vec3 viewport(const glm::vec3& point);
 	Model* const model;
 	DocumentManager* const documents;
 	const ldraw::ColorTable& colorTable;
 	gl::Compiler* const compiler;
 	ldraw::Id highlighted = ldraw::NULL_ID;
-	std::optional<glm::vec3> cameraToGrid(const QPoint& point);
-	QPointF worldToCamera(const glm::vec3& point);
+	std::optional<glm::vec3> screenToModelCoordinates(const QPoint& point);
+	QPointF modelToScreenCoordinates(const glm::vec3& point);
 private:
 	void setFragmentStyle(gl::FragmentStyle fragStyle);
 	void renderAllArrays();
@@ -48,7 +50,6 @@
 	gl::RenderPreferences renderPreferences;
 	glm::mat4 projectionMatrix;
 	glm::mat4 viewMatrix;
-	glm::mat3 viewportMatrix;
 	glm::quat modelQuaternion;
 	std::optional<GridProgram> gridProgram;
 	static constexpr double MIN_ZOOM = 0.0;
--- a/src/ui/canvas.cpp	Wed Feb 26 02:21:07 2020 +0200
+++ b/src/ui/canvas.cpp	Wed Feb 26 22:26:05 2020 +0200
@@ -33,8 +33,8 @@
 		this->newStatusText("Position: <none>"_q);
 	}
 #else
-	const QPointF originAtCamera = this->worldToCamera({1, 1, 1});
-	this->newStatusText("Origin at (%1, %2), cursor at (%3, %4)"_q.arg(originAtCamera.x()).arg(originAtCamera.y()).arg(event->pos().x()).arg(event->pos().y()));
+	const QPointF originAtCamera = this->modelToScreenCoordinates({1, 1, 1});
+	this->newStatusText("{1,1,1} is at (%1, %2), cursor at (%3, %4)"_q.arg(originAtCamera.x()).arg(originAtCamera.y()).arg(event->pos().x()).arg(event->pos().y()));
 #endif
 	const ldraw::Id id = this->pick(event->pos());
 	this->highlighted = id;

mercurial