Fri, 03 Mar 2017 00:35:43 +0200
Added support for ideal co-ordinates, which should make writing editing tools a lot easier. For some reason they don't yet work with the back and right cameras.
src/basics.cpp | file | annotate | diff | comparison | revisions | |
src/basics.h | file | annotate | diff | comparison | revisions | |
src/canvas.cpp | file | annotate | diff | comparison | revisions | |
src/glShared.h | file | annotate | diff | comparison | revisions | |
src/glcamera.cpp | file | annotate | diff | comparison | revisions | |
src/glcamera.h | file | annotate | diff | comparison | revisions | |
src/glrenderer.cpp | file | annotate | diff | comparison | revisions |
--- a/src/basics.cpp Thu Mar 02 14:45:34 2017 +0200 +++ b/src/basics.cpp Fri Mar 03 00:35:43 2017 +0200 @@ -122,6 +122,18 @@ return false; } +/* + * Transforms this vertex with a tranformation matrix and returns the result. + */ +Vertex Vertex::transformed(const GLRotationMatrix& matrix) const +{ + return { + matrix(0, 0) * x() + matrix(0, 1) * y() + matrix(0, 2) * z(), + matrix(1, 0) * x() + matrix(1, 1) * y() + matrix(1, 2) * z(), + matrix(2, 0) * x() + matrix(2, 1) * y() + matrix(2, 2) * z(), + }; +} + // ============================================================================= // BoundingBox::BoundingBox()
--- a/src/basics.h Thu Mar 02 14:45:34 2017 +0200 +++ b/src/basics.h Fri Mar 03 00:35:43 2017 +0200 @@ -25,12 +25,14 @@ #include <QVector3D> #include <QVector> #include <QFile> +#include <QMatrix4x4> #include <functional> #include <math.h> #include "macros.h" #include "transform.h" class Matrix; +using GLRotationMatrix = QMatrix4x4; template<typename T, typename R> using Pair = std::pair<T, R>; @@ -59,6 +61,7 @@ void apply (ApplyConstFunction func) const; QString toString (bool mangled = false) const; void transform (const Matrix& matr, const Vertex& pos); + Vertex transformed(const GLRotationMatrix& matrix) const; void setCoordinate (Axis ax, qreal value); Vertex& operator+= (const Vertex& other);
--- a/src/canvas.cpp Thu Mar 02 14:45:34 2017 +0200 +++ b/src/canvas.cpp Fri Mar 03 00:35:43 2017 +0200 @@ -55,7 +55,9 @@ if (camera() != Camera::Free) { // Paint the coordinates onto the screen. - QString text = format(tr("X: %1, Y: %2, Z: %3"), m_position3D[X], m_position3D[Y], m_position3D[Z]); + Vertex idealized = currentCamera().idealize(m_position3D); + QString text = format(tr("X: %1, Y: %2, Z: %3, %4"), m_position3D[X], m_position3D[Y], m_position3D[Z], + idealized.toString(true)); QFontMetrics metrics {font()}; QRect textSize = metrics.boundingRect (0, 0, width(), height(), Qt::AlignCenter, text); painter.setPen(textPen());
--- a/src/glShared.h Thu Mar 02 14:45:34 2017 +0200 +++ b/src/glShared.h Fri Mar 03 00:35:43 2017 +0200 @@ -22,25 +22,6 @@ #include <QGenericMatrix> #include "basics.h" -class GLRotationMatrix : public QGenericMatrix<4, 4, GLfloat> -{ -public: - GLRotationMatrix() {} - GLRotationMatrix(GLfloat values[16]) : - QGenericMatrix<4, 4, GLfloat> {values} {} - GLRotationMatrix(std::initializer_list<GLfloat>&& values) - { - auto iterator = values.begin(); - - for (int i = 0; i < 4; ++i) - for (int j = 0; j < 4; ++j) - { - (*this)(i, j) = *iterator; - ++iterator; - } - } -}; - inline void glMultMatrixf(const GLRotationMatrix& matrix) { glMultMatrixf(matrix.constData());
--- a/src/glcamera.cpp Thu Mar 02 14:45:34 2017 +0200 +++ b/src/glcamera.cpp Fri Mar 03 00:35:43 2017 +0200 @@ -17,6 +17,7 @@ */ #include "glcamera.h" +#include "glrenderer.h" #include "grid.h" #include "miscallenous.h" @@ -253,3 +254,23 @@ return matrix; } + +static const GLRotationMatrix ldrawToIdealAdapterMatrix = {1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1}; + +/* + * Converts from rea co-ordinates to ideal co-ordinates. + * In ideal co-ordinates, X and Y axes correspond to the 2D X and Y as seen in the camera, and +Z is "outwards" from the screen. + */ +Vertex GLCamera::idealize(const Vertex& realCoordinates) const +{ + return realCoordinates.transformed(m_rotationMatrix).transformed(ldrawToIdealAdapterMatrix); +} + +/* + * Converts from ideal co-ordinates to real co-ordinates. + */ +Vertex GLCamera::realize(const Vertex& idealCoordinates) const +{ + // The adapter matrix would be inverted here, but it is its own inverse so let's not bother. + return idealCoordinates.transformed(ldrawToIdealAdapterMatrix).transformed(m_rotationMatrix.inverted()); +}
--- a/src/glcamera.h Thu Mar 02 14:45:34 2017 +0200 +++ b/src/glcamera.h Fri Mar 03 00:35:43 2017 +0200 @@ -50,6 +50,8 @@ Axis axisZ() const; Vertex convert2dTo3d(const QPoint& pos2d, class Grid* grid = nullptr) const; QPoint convert3dTo2d(const Vertex& pos3d) const; + Vertex realize(const Vertex& idealCoordinates) const; + Vertex idealize(const Vertex& realCoordinates) const; double depth() const; bool isAxisNegated(Axis axis) const; const QString& name() const;
--- a/src/glrenderer.cpp Thu Mar 02 14:45:34 2017 +0200 +++ b/src/glrenderer.cpp Fri Mar 03 00:35:43 2017 +0200 @@ -39,14 +39,14 @@ // Transformation matrices for the fixed cameras. const GLRotationMatrix GLRenderer::topCameraMatrix = GLRotationMatrix {}; -const GLRotationMatrix GLRenderer::frontCameraMatrix = {{1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1}}; -const GLRotationMatrix GLRenderer::leftCameraMatrix = {{0, -1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 1}}; -const GLRotationMatrix GLRenderer::bottomCameraMatrix = {{1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1}}; -const GLRotationMatrix GLRenderer::backCameraMatrix = {{-1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1}}; -const GLRotationMatrix GLRenderer::rightCameraMatrix = {{0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1}}; +const GLRotationMatrix GLRenderer::frontCameraMatrix = {1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1}; +const GLRotationMatrix GLRenderer::leftCameraMatrix = {0, -1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 1}; +const GLRotationMatrix GLRenderer::bottomCameraMatrix = {1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1}; +const GLRotationMatrix GLRenderer::backCameraMatrix = {-1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1}; +const GLRotationMatrix GLRenderer::rightCameraMatrix = {0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1}; // Conversion matrix from LDraw to OpenGL coordinates. -const GLRotationMatrix GLRenderer::ldrawToGLAdapterMatrix = {{1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1}}; +const GLRotationMatrix GLRenderer::ldrawToGLAdapterMatrix = {1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1}; /* * Constructs a GL renderer.