--- a/src/gl/partrenderer.cpp Thu Feb 13 15:25:01 2020 +0200 +++ b/src/gl/partrenderer.cpp Wed Feb 26 02:21:07 2020 +0200 @@ -22,6 +22,7 @@ #include <QMouseEvent> #include <QMessageBox> #include <QAbstractButton> +#include "geometry.h" #include "partrenderer.h" PartRenderer::PartRenderer( @@ -62,6 +63,8 @@ this->initialized = true; this->modelQuaternion = glm::angleAxis(glm::radians(30.0f), glm::vec3{-1, 0, 0}); this->modelQuaternion *= glm::angleAxis(glm::radians(225.0f), glm::vec3{-0, 1, 0}); + this->setupBackgroundColor(); + this->updateModelMatrix(); this->updateViewMatrix(); this->update(); } @@ -69,6 +72,11 @@ 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), @@ -194,6 +202,23 @@ this->gridProgram->setViewMatrix(this->viewMatrix); } +void PartRenderer::updateModelMatrix() +{ + const glm::mat4 modelMatrix = glm::mat4_cast(this->modelQuaternion); + this->compiler->setUniformMatrix("modelMatrix", modelMatrix); + this->gridProgram->setModelMatrix(modelMatrix); + this->update(); +} + +void PartRenderer::setupBackgroundColor() +{ + if (this->gridProgram.has_value()) + { + const bool isDark = luma(this->renderPreferences.backgroundColor) < 0.25; + this->gridProgram->setGridColor(isDark ? Qt::white : Qt::black); + } +} + void PartRenderer::renderVao(const gl::ArrayClass arrayClass) { this->compiler->bindVertexArray(arrayClass); @@ -246,10 +271,7 @@ const glm::quat q_x = glm::angleAxis(scalar * move_x, glm::vec3{0, -1, 0}); const glm::quat q_y = glm::angleAxis(scalar * move_y, glm::vec3{-1, 0, 0}); this->modelQuaternion = q_x * q_y * this->modelQuaternion; - const glm::mat4 modelMatrix = glm::mat4_cast(this->modelQuaternion); - this->compiler->setUniformMatrix("modelMatrix", modelMatrix); - this->gridProgram->setModelMatrix(modelMatrix); - this->update(); + this->updateModelMatrix(); } this->lastMousePosition = pointToPointF(event->pos()); } @@ -263,6 +285,33 @@ this->update(); } +std::optional<glm::vec3> PartRenderer::cameraToGrid(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)); + auto p1 = matrix * glm::vec4{pp.x, pp.y, 0, 1}; + auto p2 = matrix * glm::vec4{pp.x, pp.y, 1, 1}; + geom::Line line = geom::lineFromPoints(p1, p2); + return geom::linePlaneIntersection(line, geom::XY); +} + +QPointF PartRenderer::worldToCamera(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}; + 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) { const gl::RenderStyle oldRenderStyle = this->renderPreferences.style; @@ -299,6 +348,8 @@ if (mainColorChanged or backgroundColorChanged) { this->compiler->build(this->model, this->documents, this->renderPreferences); + this->setupBackgroundColor(); + } this->update(); }