src/glRenderer.h

changeset 706
d79083b9f74d
parent 655
b376645315ab
child 718
a2cbef633673
equal deleted inserted replaced
705:09150d027e8c 706:d79083b9f74d
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #pragma once
20 #include <QGLWidget>
21 #include "main.h"
22 #include "macros.h"
23 #include "ldObject.h"
24 #include "ldDocument.h"
25 #include "glShared.h"
26
27 class GLCompiler;
28 class MessageManager;
29 class QDialogButtonBox;
30 class RadioGroup;
31 class QDoubleSpinBox;
32 class QSpinBox;
33 class QLineEdit;
34 class QTimer;
35
36 enum EditMode
37 {
38 ESelectMode,
39 EDrawMode,
40 ECircleMode,
41 };
42
43 // Meta for overlays
44 struct LDGLOverlay
45 {
46 Vertex v0,
47 v1;
48 int ox,
49 oy;
50 double lw,
51 lh;
52 QString fname;
53 QImage* img;
54 bool invalid;
55 };
56
57 struct LDFixedCameraInfo
58 {
59 const char glrotate[3];
60 const Axis axisX,
61 axisY;
62 const bool negX,
63 negY;
64 };
65
66 // =============================================================================
67 // Document-specific data
68 //
69 struct LDGLData
70 {
71 double rotX,
72 rotY,
73 rotZ,
74 panX[7],
75 panY[7],
76 zoom[7];
77 double depthValues[6];
78 LDGLOverlay overlays[6];
79 bool init;
80
81 LDGLData()
82 {
83 for (int i = 0; i < 7; ++i)
84 {
85 if (i < 6)
86 {
87 overlays[i].img = null;
88 overlays[i].invalid = false;
89 depthValues[i] = 0.0f;
90 }
91
92 zoom[i] = 30.0;
93 panX[i] = 0.0;
94 panY[i] = 0.0;
95 }
96
97 rotX = 0;
98 rotY = 0;
99 rotZ = 0;
100 init = false;
101 }
102 };
103
104 // =============================================================================
105 // The main renderer object, draws the brick on the screen, manages the camera
106 // and selection picking. The instance of GLRenderer is accessible as
107 // g_win->R()
108 //
109 class GLRenderer : public QGLWidget
110 {
111 public:
112 enum EFixedCamera
113 {
114 ETopCamera,
115 EFrontCamera,
116 ELeftCamera,
117 EBottomCamera,
118 EBackCamera,
119 ERightCamera,
120 EFreeCamera
121 };
122
123 enum ListType
124 {
125 NormalList,
126 PickList,
127 BFCFrontList,
128 BFCBackList
129 };
130
131 // CameraIcon::img is a heap-allocated QPixmap because otherwise it gets
132 // initialized before program gets to main() and constructs a QApplication
133 // and Qt doesn't like that.
134 struct CameraIcon
135 {
136 QPixmap* img;
137 QRect srcRect,
138 destRect,
139 selRect;
140 EFixedCamera cam;
141 };
142
143 Q_OBJECT
144 PROPERTY (public, bool, isDrawOnly, setDrawOnly, STOCK_WRITE)
145 PROPERTY (public, MessageManager*, messageLog, setMessageLog, STOCK_WRITE)
146 PROPERTY (private, bool, isPicking, setPicking, STOCK_WRITE)
147 PROPERTY (public, LDDocument*, document, setDocument, CUSTOM_WRITE)
148 PROPERTY (public, EditMode, editMode, setEditMode, CUSTOM_WRITE)
149 PROPERTY (private, GLCompiler*, compiler, setCompiler, STOCK_WRITE)
150
151 public:
152 GLRenderer (QWidget* parent = null);
153 ~GLRenderer();
154
155 inline EFixedCamera camera() const
156 {
157 return m_camera;
158 }
159
160 void clearOverlay();
161 void compileObject (LDObject* obj);
162 void drawGLScene();
163 void endDraw (bool accept);
164 void forgetObject (LDObject* obj);
165 Axis getCameraAxis (bool y, EFixedCamera camid = (EFixedCamera) - 1);
166 const char* getCameraName() const;
167 double getDepthValue() const;
168 LDGLOverlay& getOverlay (int newcam);
169 uchar* getScreencap (int& w, int& h);
170 void hardRefresh();
171 void initGLData();
172 void initOverlaysFromObjects();
173 void refresh();
174 void resetAngles();
175 void resetAllAngles();
176 void setBackground();
177 void setCamera (const EFixedCamera cam);
178 void setDepthValue (double depth);
179 bool setupOverlay (EFixedCamera cam, QString file, int x, int y, int w, int h);
180 void updateOverlayObjects();
181 void zoomNotch (bool inward);
182 void zoomToFit();
183 void zoomAllToFit();
184
185 static void deleteLists (LDObject* obj);
186 static QColor getMainColor();
187
188 protected:
189 void contextMenuEvent (QContextMenuEvent* ev);
190 void initializeGL();
191 void keyPressEvent (QKeyEvent* ev);
192 void keyReleaseEvent (QKeyEvent* ev);
193 void leaveEvent (QEvent* ev);
194 void mouseDoubleClickEvent (QMouseEvent* ev);
195 void mousePressEvent (QMouseEvent* ev);
196 void mouseMoveEvent (QMouseEvent* ev);
197 void mouseReleaseEvent (QMouseEvent* ev);
198 void paintEvent (QPaintEvent* ev);
199 void resizeGL (int w, int h);
200 void wheelEvent (QWheelEvent* ev);
201
202 private:
203 CameraIcon m_cameraIcons[7];
204 QTimer* m_toolTipTimer;
205 Qt::MouseButtons m_lastButtons;
206 Qt::KeyboardModifiers m_keymods;
207 Vertex m_hoverpos;
208 double m_virtWidth,
209 m_virtHeight;
210 bool m_darkbg,
211 m_rangepick,
212 m_addpick,
213 m_drawToolTip,
214 m_screencap,
215 m_panning;
216 QPoint m_pos,
217 m_globalpos,
218 m_rangeStart;
219 QPen m_thickBorderPen,
220 m_thinBorderPen;
221 EFixedCamera m_camera,
222 m_toolTipCamera;
223 GLuint m_axeslist;
224 int m_width,
225 m_height,
226 m_totalmove;
227 QList<Vertex> m_drawedVerts;
228 bool m_rectdraw;
229 Vertex m_rectverts[4];
230 QColor m_bgcolor;
231 QList<Vertex> m_knownVerts;
232
233 void addDrawnVertex (Vertex m_hoverpos);
234 LDOverlay* findOverlayObject (EFixedCamera cam);
235 void updateRectVerts();
236 void getRelativeAxes (Axis& relX, Axis& relY) const;
237 Matrix getCircleDrawMatrix (double scale);
238 void drawBlip (QPainter& paint, QPoint pos) const;
239
240 // Compute geometry for camera icons
241 void calcCameraIcons();
242
243 // How large is the circle we're drawing right now?
244 double getCircleDrawDist (int pos) const;
245
246 // Clamps an angle to [0, 360]
247 void clampAngle (double& angle) const;
248
249 // Compile one of the lists of an object
250 void compileList (LDObject* obj, const ListType list);
251
252 // Sub-routine for object compiling
253 void compileSubObject (LDObject* obj, const GLenum gltype);
254
255 // Compile a single vertex to a list
256 void compileVertex (const Vertex& vrt);
257
258 // Convert a 2D point to a 3D point
259 Vertex coordconv2_3 (const QPoint& pos2d, bool snap) const;
260
261 // Draw a VBO array
262 void drawVBOs (EVBOSurface surface, EVBOComplement colors, GLenum type);
263
264 // Determine which color to draw text with
265 QColor getTextPen() const;
266
267 // Convert a 3D point to a 2D point
268 QPoint coordconv3_2 (const Vertex& pos3d) const;
269
270 // Perform object selection
271 void pick (int mouseX, int mouseY);
272
273 // Set the color to an object list
274 void setObjectColor (LDObject* obj, const ListType list);
275
276 LDGLData& currentDocumentData() const
277 {
278 return *document()->getGLData();
279 }
280
281 // Get a rotation value
282 inline double& rot (Axis ax)
283 {
284 return
285 (ax == X) ? currentDocumentData().rotX :
286 (ax == Y) ? currentDocumentData().rotY :
287 currentDocumentData().rotZ;
288 }
289
290 // Get a panning value
291 inline double& pan (Axis ax)
292 {
293 return (ax == X) ? currentDocumentData().panX[camera()] :
294 currentDocumentData().panY[camera()];
295 }
296
297 // Same except const (can be used in const methods)
298 inline const double& pan (Axis ax) const
299 {
300 return (ax == X) ? currentDocumentData().panX[camera()] :
301 currentDocumentData().panY[camera()];
302 }
303
304 // Get the zoom value
305 inline double& zoom()
306 {
307 return currentDocumentData().zoom[camera()];
308 }
309
310 template<typename... Args>
311 inline QString format (QString fmtstr, Args... args)
312 {
313 return ::format (fmtstr, args...);
314 }
315
316 private slots:
317 void slot_toolTipTimer();
318 void initializeAxes();
319 };
320
321 // Alias for short namespaces
322 typedef GLRenderer GL;
323
324 static const GLRenderer::ListType g_glListTypes[] =
325 {
326 GL::NormalList,
327 GL::PickList,
328 GL::BFCFrontList,
329 GL::BFCBackList,
330 };
331
332 extern const GL::EFixedCamera g_Cameras[7];
333 extern const char* g_CameraNames[7];

mercurial