src/glRenderer.h

changeset 986
525921eae58c
parent 984
a7b6f987d269
child 1000
c064cc048f14
equal deleted inserted replaced
985:ed7b31b9f904 986:525921eae58c
33 class QSpinBox; 33 class QSpinBox;
34 class QLineEdit; 34 class QLineEdit;
35 class QTimer; 35 class QTimer;
36 class MagicWandMode; 36 class MagicWandMode;
37 37
38 struct LDFixedCamera
39 {
40 int glrotate[3];
41 Axis localX;
42 Axis localY;
43 bool negatedX;
44 bool negatedY;
45 bool negatedDepth; // is greater depth value closer to camera?
46 };
47
38 // 48 //
39 // Meta for overlays 49 // Meta for overlays
40 // 50 //
41 struct LDGLOverlay 51 struct LDGLOverlay
42 { 52 {
49 QString fname; 59 QString fname;
50 QImage* img; 60 QImage* img;
51 bool invalid; 61 bool invalid;
52 }; 62 };
53 63
54 struct LDFixedCamera
55 {
56 const char glrotate[3];
57 const Axis axisX,
58 axisY;
59 const bool negX,
60 negY,
61 negatedDepth; // is greater depth value closer to camera?
62 };
63
64 // 64 //
65 // Document-specific data 65 // Document-specific data
66 // 66 //
67 struct LDGLData 67 struct LDGLData
68 { 68 {
69 double rotX, 69 double rotationX;
70 rotY, 70 double rotationY;
71 rotZ, 71 double rotationZ;
72 panX[7], 72 double panX[7];
73 panY[7], 73 double panY[7];
74 zoom[7]; 74 double zoom[7];
75 double depthValues[6]; 75 double depthValues[6];
76 LDGLOverlay overlays[6]; 76 LDGLOverlay overlays[6];
77 bool init; 77 bool init;
78 bool needZoomToFit; 78 bool needZoomToFit;
79 79
80 LDGLData() : 80 LDGLData() :
81 rotX (0.0), 81 rotationX (0.0),
82 rotY (0.0), 82 rotationY (0.0),
83 rotZ (0.0), 83 rotationZ (0.0),
84 init (false), 84 init (false),
85 needZoomToFit (true) 85 needZoomToFit (true)
86 { 86 {
87 for (int i = 0; i < 7; ++i) 87 for (int i = 0; i < 7; ++i)
88 { 88 {
115 }; 115 };
116 116
117 MAKE_ITERABLE_ENUM (ECamera) 117 MAKE_ITERABLE_ENUM (ECamera)
118 118
119 // 119 //
120 // CameraIcon::img is a heap-allocated QPixmap because otherwise it gets 120 // CameraIcon::image is a heap-allocated QPixmap because otherwise it gets
121 // initialized before program gets to main() and constructs a QApplication 121 // initialized before program gets to main() and constructs a QApplication
122 // and Qt doesn't like that. 122 // and Qt doesn't like that.
123 // 123 //
124 struct CameraIcon 124 struct CameraIcon
125 { 125 {
126 QPixmap* img; 126 QPixmap* image;
127 QRect srcRect, 127 QRect sourceRect;
128 destRect, 128 QRect targetRect;
129 selRect; 129 QRect selRect;
130 ECamera cam; 130 ECamera cam;
131 }; 131 };
132 132
133 // 133 // The main renderer object, draws the brick on the screen, manages the camera and selection picking.
134 // The main renderer object, draws the brick on the screen, manages the camera
135 // and selection picking. The instance of GLRenderer is accessible as
136 // g_win->R()
137 //
138 class GLRenderer : public QGLWidget, protected QOpenGLFunctions, public HierarchyElement 134 class GLRenderer : public QGLWidget, protected QOpenGLFunctions, public HierarchyElement
139 { 135 {
140 public:
141 Q_OBJECT 136 Q_OBJECT
142 PROPERTY (public, bool, isDrawOnly, setDrawOnly, STOCK_WRITE)
143 PROPERTY (public, MessageManager*, messageLog, setMessageLog, STOCK_WRITE)
144 PROPERTY (private, bool, isPicking, setPicking, CUSTOM_WRITE)
145 PROPERTY (public, LDDocument*, document, setDocument, CUSTOM_WRITE)
146 PROPERTY (public, GLCompiler*, compiler, setCompiler, STOCK_WRITE)
147 PROPERTY (public, LDObject*, objectAtCursor, setObjectAtCursor, STOCK_WRITE)
148 PROPERTY (private, bool, isCameraMoving, setCameraMoving, STOCK_WRITE)
149 137
150 public: 138 public:
151 GLRenderer (QWidget* parent = nullptr); 139 GLRenderer (QWidget* parent = nullptr);
152 ~GLRenderer(); 140 ~GLRenderer();
153 141
154 inline ECamera camera() const; 142 ECamera camera() const;
155 void clearOverlay(); 143 QString cameraName (ECamera camera) const;
156 void compileObject (LDObject* obj); 144 QByteArray capturePixels();
157 Vertex coordconv2_3 (const QPoint& pos2d, bool snap) const; 145 void clearOverlay();
158 QPoint coordconv3_2 (const Vertex& pos3d); 146 void compileObject (LDObject* obj);
159 EditModeType currentEditModeType() const; 147 GLCompiler* compiler() const;
160 int depthNegateFactor() const; 148 Vertex convert2dTo3d (const QPoint& pos2d, bool snap) const;
161 void drawBlip (QPainter& paint, QPointF pos) const; 149 QPoint convert3dTo2d (const Vertex& pos3d);
162 void drawGLScene(); 150 QString currentCameraName() const;
163 void forgetObject (LDObject* obj); 151 EditModeType currentEditModeType() const;
164 Axis getCameraAxis (bool y, ECamera camid = (ECamera) -1); 152 int depthNegateFactor() const;
165 const char* getCameraName() const; 153 LDDocument* document() const;
166 double getDepthValue() const; 154 void drawBlip (QPainter& paint, QPointF pos) const;
167 LDFixedCamera const& getFixedCamera (ECamera cam) const; 155 void drawGLScene();
168 void getRelativeAxes (Axis& relX, Axis& relY) const; 156 void forgetObject (LDObject* obj);
169 Axis getRelativeZ() const; 157 Axis getCameraAxis (bool y, ECamera camid = (ECamera) -1);
170 LDGLOverlay& getOverlay (int newcam); 158 double getDepthValue() const;
171 uchar* getScreencap (int& w, int& h); 159 const LDFixedCamera& getFixedCamera (ECamera cam) const;
172 Qt::KeyboardModifiers keyboardModifiers() const; 160 LDGLOverlay& getOverlay (int newcam);
173 void hardRefresh(); 161 void getRelativeAxes (Axis& relX, Axis& relY) const;
174 void highlightCursorObject(); 162 Axis getRelativeZ() const;
175 void initGLData(); 163 void hardRefresh();
176 void initOverlaysFromObjects(); 164 void highlightCursorObject();
177 QPen linePen() const; 165 void initGLData();
178 bool mouseHasMoved() const; 166 void initOverlaysFromObjects();
179 QPoint const& mousePosition() const; 167 bool isDrawOnly() const;
180 QPointF const& mousePositionF() const; 168 bool isPicking() const;
181 void needZoomToFit(); 169 Qt::KeyboardModifiers keyboardModifiers() const;
182 void pick (int mouseX, int mouseY, bool additive); 170 QPen linePen() const;
183 void pick (QRect const& range, bool additive); 171 MessageManager* messageLog() const;
184 LDObject* pickOneObject (int mouseX, int mouseY); 172 bool mouseHasMoved() const;
185 Vertex const& position3D() const; 173 QPoint const& mousePosition() const;
186 void refresh(); 174 QPointF const& mousePositionF() const;
187 void resetAngles(); 175 void needZoomToFit();
188 void resetAllAngles(); 176 LDObject* objectAtCursor() const;
189 void setBackground(); 177 void pick (int mouseX, int mouseY, bool additive);
190 void setCamera (const ECamera cam); 178 void pick (QRect const& range, bool additive);
191 void setDepthValue (double depth); 179 LDObject* pickOneObject (int mouseX, int mouseY);
192 void setEditMode (EditModeType type); 180 Vertex const& position3D() const;
193 bool setupOverlay (ECamera cam, QString file, int x, int y, int w, int h); 181 void refresh();
194 QPen textPen() const; 182 void resetAllAngles();
195 void updateOverlayObjects(); 183 void resetAngles();
196 void zoomNotch (bool inward); 184 void setBackground();
197 185 void setCamera (const ECamera cam);
198 QColor getMainColor(); 186 void setDepthValue (double depth);
187 void setDocument (LDDocument* document);
188 void setDrawOnly (bool value);
189 void setEditMode (EditModeType type);
190 void setPicking (bool a);
191 bool setupOverlay (ECamera cam, QString file, int x, int y, int w, int h);
192 QPen textPen() const;
193 void updateOverlayObjects();
194 void zoomNotch (bool inward);
199 195
200 protected: 196 protected:
201 void contextMenuEvent (QContextMenuEvent* ev); 197 void contextMenuEvent (QContextMenuEvent* ev);
202 void dragEnterEvent (QDragEnterEvent* ev); 198 void dragEnterEvent (QDragEnterEvent* ev);
203 void dropEvent (QDropEvent* ev); 199 void dropEvent (QDropEvent* ev);
204 void initializeGL(); 200 void initializeGL();
205 void keyPressEvent (QKeyEvent* ev); 201 void keyPressEvent (QKeyEvent* ev);
206 void keyReleaseEvent (QKeyEvent* ev); 202 void keyReleaseEvent (QKeyEvent* ev);
207 void leaveEvent (QEvent* ev); 203 void leaveEvent (QEvent* ev);
208 void mouseDoubleClickEvent (QMouseEvent* ev); 204 void mouseDoubleClickEvent (QMouseEvent* ev);
209 void mousePressEvent (QMouseEvent* ev); 205 void mousePressEvent (QMouseEvent* ev);
210 void mouseMoveEvent (QMouseEvent* ev); 206 void mouseMoveEvent (QMouseEvent* ev);
211 void mouseReleaseEvent (QMouseEvent* ev); 207 void mouseReleaseEvent (QMouseEvent* ev);
212 void paintEvent (QPaintEvent* ev); 208 void paintEvent (QPaintEvent* ev);
213 void resizeGL (int w, int h); 209 void resizeGL (int w, int h);
214 void wheelEvent (QWheelEvent* ev); 210 void wheelEvent (QWheelEvent* ev);
215 211
216 private: 212 private:
217 CameraIcon m_cameraIcons[7]; 213 MessageManager* m_messageLog;
218 QTimer* m_toolTipTimer; 214 LDDocument* m_document;
219 Qt::MouseButtons m_lastButtons; 215 GLCompiler* m_compiler;
220 Qt::KeyboardModifiers m_keymods; 216 LDObject* m_objectAtCursor;
221 Vertex m_position3D; 217
222 double m_virtWidth, 218 CameraIcon m_cameraIcons[7];
223 m_virtHeight; 219 QTimer* m_toolTipTimer;
224 bool m_darkbg, 220 Qt::MouseButtons m_lastButtons;
225 m_drawToolTip, 221 Qt::KeyboardModifiers m_currentKeyboardModifiers;
226 m_screencap, 222 Vertex m_position3D;
227 m_panning; 223 double m_virtualWidth;
228 QPoint m_mousePosition, 224 double m_virtualHeight;
229 m_globalpos; 225 bool m_useDarkBackground;
230 QPointF m_mousePositionF; 226 bool m_drawToolTip;
231 QPen m_thinBorderPen; 227 bool m_takingScreenCapture;
232 ECamera m_camera, 228 bool m_panning;
233 m_toolTipCamera; 229 bool m_initialized;
234 GLuint m_axeslist; 230 bool m_isDrawOnly;
235 int m_width, 231 bool m_isPicking;
236 m_height, 232 bool m_isCameraMoving;
237 m_totalmove; 233 QPoint m_mousePosition;
238 QColor m_bgcolor; 234 QPoint m_globalpos;
239 AbstractEditMode* m_editmode; 235 QPointF m_mousePositionF;
240 GLuint m_axesVBO; 236 QPen m_thinBorderPen;
241 GLuint m_axesColorVBO; 237 ECamera m_camera;
242 238 ECamera m_toolTipCamera;
243 void calcCameraIcons(); 239 GLuint m_axeslist;
244 void clampAngle (double& angle) const; 240 int m_width;
245 inline LDGLData& currentDocumentData() const; 241 int m_height;
246 void drawVBOs (EVBOSurface surface, EVBOComplement colors, GLenum type); 242 int m_totalMouseMove;
247 void doMakeCurrent(); 243 QColor m_backgroundColor;
248 LDOverlay* findOverlayObject (ECamera cam); 244 AbstractEditMode* m_currentEditMode;
249 inline double& pan (Axis ax); 245 GLuint m_axesVbo;
250 inline const double& pan (Axis ax) const; 246 GLuint m_axesColorVbo;
251 inline double& rot (Axis ax); 247
252 inline double& zoom(); 248 void calcCameraIcons();
253 void zoomToFit(); 249 void clampAngle (double& angle) const;
254 void zoomAllToFit(); 250 LDGLData& currentDocumentData() const;
251 void drawVbos (SurfaceVboType surface, ComplementVboType colors, GLenum type);
252 void doMakeCurrent();
253 LDOverlay* findOverlayObject (ECamera cam);
254 double& panning (Axis ax);
255 double panning (Axis ax) const;
256 double& rotation (Axis ax);
257 double& zoom();
258 void zoomToFit();
259 void zoomAllToFit();
255 260
256 template<typename... Args> 261 template<typename... Args>
257 inline QString format (QString fmtstr, Args... args) 262 QString format (QString fmtstr, Args... args)
258 { 263 {
259 return ::format (fmtstr, args...); 264 return ::format (fmtstr, args...);
260 } 265 }
261 266
262 private slots: 267 private slots:
263 void slot_toolTipTimer(); 268 void slot_toolTipTimer();
264 void initializeAxes(); 269 void initializeAxes();
265 }; 270 };
266
267 inline ECamera GLRenderer::camera() const
268 {
269 return m_camera;
270 }
271
272 inline LDGLData& GLRenderer::currentDocumentData() const
273 {
274 return *document()->glData();
275 }
276
277 inline double& GLRenderer::rot (Axis ax)
278 {
279 return
280 (ax == X) ? currentDocumentData().rotX :
281 (ax == Y) ? currentDocumentData().rotY :
282 currentDocumentData().rotZ;
283 }
284
285 inline double& GLRenderer::pan (Axis ax)
286 {
287 return (ax == X) ? currentDocumentData().panX[camera()] :
288 currentDocumentData().panY[camera()];
289 }
290
291 inline double const& GLRenderer::pan (Axis ax) const
292 {
293 return (ax == X) ? currentDocumentData().panX[camera()] :
294 currentDocumentData().panY[camera()];
295 }
296
297 inline double& GLRenderer::zoom()
298 {
299 return currentDocumentData().zoom[camera()];
300 }
301
302 LDFixedCamera const& GetFixedCamera (ECamera cam);
303
304 extern const char* g_CameraNames[7];

mercurial