src/ui/canvas.h

Wed, 25 May 2022 20:36:34 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 20:36:34 +0300
changeset 199
6988973515d2
parent 198
eb9d900dc79a
child 200
ca23936b455b
permissions
-rw-r--r--

Fix pick() picking from weird places on the screen with high DPI scaling

glReadPixels reads data from the frame buffer, which contains data after
high DPI scaling, so any reads to that need to take this scaling into account

47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #pragma once
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
2 #include <QPainter>
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
3 #include <QBrush>
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
4 #include <QPen>
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 #include "gl/partrenderer.h"
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
6 #include "gl/gridprogram.h"
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 67
diff changeset
7 #include "gl/axesprogram.h"
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
8 #include "gl/vertexprogram.h"
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
10 enum EditingMode
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
11 {
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
12 SelectMode,
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
13 DrawMode
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
14 };
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
15
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
16 struct DrawState
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
17 {
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
18 std::vector<glm::vec3> polygon;
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
19 std::vector<glm::vec3> previewPolygon;
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
20 glm::vec3 previewPoint;
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
21 bool isconcave = false;
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
22 EditingMode mode = SelectMode;
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
23 };
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 class Canvas : public PartRenderer
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 {
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 Q_OBJECT
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 public:
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
28 using OverpaintCallback = std::function<void(Canvas*, QPainter*)>;
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 Canvas(
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 Model* model,
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
31 Document* document,
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 DocumentManager* documents,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 const ldraw::ColorTable& colorTable,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 QWidget* parent = nullptr);
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
35 void setOverpaintCallback(OverpaintCallback fn);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
36 void drawWorldPoint(QPainter* painter, const glm::vec3& worldPoint) const;
164
8305e2f968fb Render draw tool preview as a line when we only have 2 points
Teemu Piippo <teemu@hecknology.net>
parents: 130
diff changeset
37 void drawWorldPolyline(QPainter* painter, const std::vector<glm::vec3>& points);
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
38 void drawWorldPolygon(QPainter* painter, const std::vector<glm::vec3>& points);
168
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 166
diff changeset
39 Winding worldPolygonWinding(const std::vector<glm::vec3>& points) const;
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
40 const std::optional<glm::vec3>& getWorldPosition() const;
169
6da096930534 Added the delete action
Teemu Piippo <teemu@hecknology.net>
parents: 168
diff changeset
41 const QSet<ldraw::id_t> selectedObjects() const;
187
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 170
diff changeset
42 const glm::mat4& getGridMatrix() const;
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
43 void closeShape();
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
44 glm::vec3 cameraVector() const;
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
45 std::optional<glm::vec3> worldPosition;
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
46 DrawState* drawState = nullptr;
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
47 public Q_SLOTS:
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
48 void handleSelectionChange(const QSet<ldraw::id_t>& selectedIds, const QSet<ldraw::id_t>& deselectedIds);
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
49 void rebuildVertices(Document *document);
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
50 void setGridMatrix(const glm::mat4 &newMatrix);
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 protected:
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 void mouseMoveEvent(QMouseEvent* event) override;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
53 void mousePressEvent(QMouseEvent* event) override;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
54 void mouseReleaseEvent(QMouseEvent* event) override;
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
55 void initializeGL() override;
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
56 void paintGL() override;
112
5760cbb32bc0 use QT_NO_KEYWORDS
Teemu Piippo <teemu@hecknology.net>
parents: 111
diff changeset
57 Q_SIGNALS:
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 void newStatusText(const QString& newStatusText);
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
59 void mouseClick(QMouseEvent* event);
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
60 void mouseMove(QMouseEvent* event);
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
61 private:
66
77c819262b7a added a method to find out if the view is perpendicular to grid
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
62 bool isGridPerpendicularToScreen(float threshold) const;
168
24590af32ad6 Draw tool now renders the winding of the new polygon
Teemu Piippo <teemu@hecknology.net>
parents: 166
diff changeset
63 QVector<QPointF> convertWorldPointsToScreenPoints(const std::vector<glm::vec3>& worldPoints) const;
165
f6eab2bd46c2 fixed the grid not being black on startup if settings has bright background color
Teemu Piippo <teemu@hecknology.net>
parents: 164
diff changeset
64 Q_SLOT void updateCanvasRenderPreferences();
170
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 169
diff changeset
65 void renderAxesLabels(QPainter& painter);
61
4585d8d7a7ec moved GridProgram to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 57
diff changeset
66 std::optional<GridProgram> gridProgram;
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 67
diff changeset
67 std::optional<AxesProgram> axesProgram;
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 112
diff changeset
68 std::optional<VertexProgram> vertexProgram;
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
69 glm::mat4 gridMatrix;
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 61
diff changeset
70 geom::Plane gridPlane;
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 47
diff changeset
71 int totalMouseMove = 0;
166
8857351912d0 Fix rendering of cursor coordinates on bright background
Teemu Piippo <teemu@hecknology.net>
parents: 165
diff changeset
72 bool isDark = true;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
73 QSet<ldraw::id_t> selection;
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
74 OverpaintCallback overpaintCallback = nullptr;
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
75 Document* document;
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 };
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
77
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
78 void adjustGridToView(Canvas* canvas);

mercurial