src/GLRenderer.h

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

mercurial