# HG changeset patch # User Teemu Piippo # Date 1488575284 -7200 # Node ID 3a88e7a60b637230727a7395b2ed0893ce4577c6 # Parent 8661b9237ed5b8233456f4c97b22c9266842f259 At long last, the grid is finally now rendered onto the viewport. Woop! diff -r 8661b9237ed5 -r 3a88e7a60b63 src/basics.h --- a/src/basics.h Fri Mar 03 00:35:43 2017 +0200 +++ b/src/basics.h Fri Mar 03 23:08:04 2017 +0200 @@ -315,3 +315,15 @@ { return ring.size(); } + +/* + * Extracts the sign of x. + */ +template +T sign(T x) +{ + if (isZero(x)) + return {}; + else + return x / qAbs(x); +} diff -r 8661b9237ed5 -r 3a88e7a60b63 src/canvas.cpp --- a/src/canvas.cpp Fri Mar 03 00:35:43 2017 +0200 +++ b/src/canvas.cpp Fri Mar 03 23:08:04 2017 +0200 @@ -90,6 +90,59 @@ } } +/* + * Assuming we're currently viewing from a fixed camera, draw a backdrop into it. Currently this means drawing the grid. + */ +void Canvas::drawFixedCameraBackdrop() +{ + // Find the top left corner of the grid + Vertex topLeft = currentCamera().idealize(currentCamera().convert2dTo3d({0, 0})); + Vertex bottomRight = currentCamera().idealize(currentCamera().convert2dTo3d({width(), height()})); + qreal gridSize = grid()->coordinateSnap(); + qreal x0 = sign(topLeft.x()) * (fabs(topLeft.x()) - fmod(fabs(topLeft.x()), gridSize)); + qreal y0 = sign(topLeft.y()) * (fabs(topLeft.y()) - fmod(fabs(topLeft.y()), gridSize)); + glEnable(GL_LINE_STIPPLE); + glBegin(GL_LINES); + + static const auto prepareGridLine = [](qreal value) -> bool + { + if (not isZero(value)) + { + if (isZero(fmod(value, 10.0))) + glColor4f(0, 0, 0, 0.6); + else + glColor4f(0, 0, 0, 0.25); + + return true; + } + else + { + return false; + } + }; + + for (qreal x = x0; x < bottomRight.x(); x += gridSize) + { + if (prepareGridLine(x)) + { + glVertex(currentCamera().realize({x, -10000, 999})); + glVertex(currentCamera().realize({x, 10000, 999})); + } + } + + for (qreal y = y0; y < bottomRight.y(); y += gridSize) + { + if (prepareGridLine(y)) + { + glVertex(currentCamera().realize({-10000, y, 999})); + glVertex(currentCamera().realize({10000, y, 999})); + } + } + + glEnd(); + glDisable(GL_LINE_STIPPLE); +} + bool Canvas::freeCameraAllowed() const { return m_currentEditMode->allowFreeCamera(); diff -r 8661b9237ed5 -r 3a88e7a60b63 src/canvas.h --- a/src/canvas.h Fri Mar 03 00:35:43 2017 +0200 +++ b/src/canvas.h Fri Mar 03 23:08:04 2017 +0200 @@ -43,6 +43,7 @@ protected: void contextMenuEvent(QContextMenuEvent* event) override; void dragEnterEvent(QDragEnterEvent* event) override; + void drawFixedCameraBackdrop() override; void dropEvent(QDropEvent* event) override; bool freeCameraAllowed() const override; void keyReleaseEvent(QKeyEvent* event) override; diff -r 8661b9237ed5 -r 3a88e7a60b63 src/glShared.h --- a/src/glShared.h Fri Mar 03 00:35:43 2017 +0200 +++ b/src/glShared.h Fri Mar 03 23:08:04 2017 +0200 @@ -27,6 +27,11 @@ glMultMatrixf(matrix.constData()); } +inline void glVertex(const Vertex& vertex) +{ + glVertex3f(vertex.x(), vertex.y(), vertex.z()); +} + class LDObject; struct LDPolygon diff -r 8661b9237ed5 -r 3a88e7a60b63 src/glcamera.cpp --- a/src/glcamera.cpp Fri Mar 03 00:35:43 2017 +0200 +++ b/src/glcamera.cpp Fri Mar 03 23:08:04 2017 +0200 @@ -255,7 +255,7 @@ return matrix; } -static const GLRotationMatrix ldrawToIdealAdapterMatrix = {1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1}; +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. diff -r 8661b9237ed5 -r 3a88e7a60b63 src/glrenderer.cpp --- a/src/glrenderer.cpp Fri Mar 03 00:35:43 2017 +0200 +++ b/src/glrenderer.cpp Fri Mar 03 23:08:04 2017 +0200 @@ -381,10 +381,11 @@ glLoadIdentity(); const QSizeF& virtualSize = currentCamera().virtualSize(); - glOrtho(-virtualSize.width(), virtualSize.width(), -virtualSize.height(), virtualSize.height(), -100.0f, 100.0f); + glOrtho(-virtualSize.width(), virtualSize.width(), -virtualSize.height(), virtualSize.height(), -1000.0f, 1000.0f); glTranslatef(panning (X), panning (Y), 0.0f); glMultMatrixf(currentCamera().transformationMatrix()); glMultMatrixf(ldrawToGLAdapterMatrix); + drawFixedCameraBackdrop(); } else { @@ -1028,3 +1029,9 @@ { return m_model; } + +/* + * This virtual function lets derivative classes render something to the fixed camera + * before the main brick is rendered. + */ +void GLRenderer::drawFixedCameraBackdrop() {} diff -r 8661b9237ed5 -r 3a88e7a60b63 src/glrenderer.h --- a/src/glrenderer.h Fri Mar 03 00:35:43 2017 +0200 +++ b/src/glrenderer.h Fri Mar 03 23:08:04 2017 +0200 @@ -87,6 +87,7 @@ protected: void initializeGL(); + virtual void drawFixedCameraBackdrop(); void keyPressEvent(QKeyEvent* event); void keyReleaseEvent(QKeyEvent* event); void leaveEvent(QEvent* event);