src/gldraw.h

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

mercurial