src/gldraw.h

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

mercurial