1 | // |
---|
2 | // gla.h |
---|
3 | // |
---|
4 | // Header file for the GLA abstraction layer. |
---|
5 | // |
---|
6 | // Copyright (C) 2002 Mark R. Shinwell. |
---|
7 | // Copyright (C) 2003,2004,2005,2006,2007,2011,2012,2014,2017,2018 Olly Betts |
---|
8 | // |
---|
9 | // This program is free software; you can redistribute it and/or modify |
---|
10 | // it under the terms of the GNU General Public License as published by |
---|
11 | // the Free Software Foundation; either version 2 of the License, or |
---|
12 | // (at your option) any later version. |
---|
13 | // |
---|
14 | // This program is distributed in the hope that it will be useful, |
---|
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | // GNU General Public License for more details. |
---|
18 | // |
---|
19 | // You should have received a copy of the GNU General Public License |
---|
20 | // along with this program; if not, write to the Free Software |
---|
21 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | // |
---|
23 | |
---|
24 | #ifndef gla_h |
---|
25 | #define gla_h |
---|
26 | |
---|
27 | #include <string> |
---|
28 | #include <vector> |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | #include "wx.h" |
---|
33 | #include "aventypes.h" |
---|
34 | #include "vector3.h" |
---|
35 | |
---|
36 | #include "glbitmapfont.h" |
---|
37 | |
---|
38 | class GfxCore; |
---|
39 | |
---|
40 | string GetGLSystemDescription(); |
---|
41 | |
---|
42 | // #define GLA_DEBUG |
---|
43 | |
---|
44 | typedef Double glaCoord; |
---|
45 | |
---|
46 | typedef GLfloat glaTexCoord; |
---|
47 | |
---|
48 | // Colours for drawing. Don't reorder these! |
---|
49 | enum gla_colour { |
---|
50 | col_BLACK = 0, |
---|
51 | col_GREY, |
---|
52 | col_LIGHT_GREY, |
---|
53 | col_LIGHT_GREY_2, |
---|
54 | col_DARK_GREY, |
---|
55 | col_WHITE, |
---|
56 | col_TURQUOISE, |
---|
57 | col_GREEN, |
---|
58 | col_INDICATOR_1, |
---|
59 | col_INDICATOR_2, |
---|
60 | col_YELLOW, |
---|
61 | col_RED, |
---|
62 | col_BLUE, |
---|
63 | col_MAGENTA, |
---|
64 | col_LAST // must be the last entry here |
---|
65 | }; |
---|
66 | |
---|
67 | class GLAPen { |
---|
68 | friend class GLACanvas; // allow direct access to components |
---|
69 | |
---|
70 | double components[3]; // red, green, blue |
---|
71 | |
---|
72 | public: |
---|
73 | GLAPen(); |
---|
74 | |
---|
75 | void SetColour(double red, double green, double blue); // arguments in range 0 to 1.0 |
---|
76 | void Interpolate(const GLAPen&, double how_far); |
---|
77 | |
---|
78 | double GetRed() const; |
---|
79 | double GetGreen() const; |
---|
80 | double GetBlue() const; |
---|
81 | }; |
---|
82 | |
---|
83 | class GLAList { |
---|
84 | GLuint gl_list; |
---|
85 | unsigned int flags; |
---|
86 | public: |
---|
87 | GLAList() : gl_list(0), flags(0) { } |
---|
88 | GLAList(GLuint gl_list_, unsigned int flags_) |
---|
89 | : gl_list(gl_list_), flags(flags_) { } |
---|
90 | operator bool() { return gl_list != 0; } |
---|
91 | bool need_to_generate(); |
---|
92 | void finalise(unsigned int list_flags); |
---|
93 | bool DrawList() const; |
---|
94 | void invalidate_if(unsigned int mask) { |
---|
95 | // If flags == NEVER_CACHE, the list won't be invalidated (unless |
---|
96 | // mask is 0, which isn't a normal thing to pass). |
---|
97 | if (flags & mask) |
---|
98 | flags = 0; |
---|
99 | } |
---|
100 | }; |
---|
101 | |
---|
102 | class GLACanvas : public wxGLCanvas { |
---|
103 | friend class GLAList; // For flag values. |
---|
104 | |
---|
105 | wxGLContext ctx; |
---|
106 | |
---|
107 | #ifdef GLA_DEBUG |
---|
108 | int m_Vertices; |
---|
109 | #endif |
---|
110 | |
---|
111 | GLdouble modelview_matrix[16]; |
---|
112 | GLdouble projection_matrix[16]; |
---|
113 | GLint viewport[4]; |
---|
114 | |
---|
115 | // Viewing volume diameter: |
---|
116 | glaCoord m_VolumeDiameter; |
---|
117 | |
---|
118 | // Parameters for plotting data: |
---|
119 | Double m_Pan, m_Tilt; |
---|
120 | Double m_Scale; |
---|
121 | Vector3 m_Translation; |
---|
122 | |
---|
123 | BitmapFont m_Font; |
---|
124 | |
---|
125 | GLUquadric* m_Quadric; |
---|
126 | |
---|
127 | GLuint m_Texture; |
---|
128 | GLuint m_BlobTexture; |
---|
129 | GLuint m_CrossTexture; |
---|
130 | |
---|
131 | Double alpha; |
---|
132 | |
---|
133 | bool m_SmoothShading; |
---|
134 | bool m_Textured; |
---|
135 | bool m_Perspective; |
---|
136 | bool m_Fog; |
---|
137 | bool m_AntiAlias; |
---|
138 | bool save_hints; |
---|
139 | enum { UNKNOWN = 0, POINT = 'P', LINES = 'L', SPRITE = 'S' }; |
---|
140 | int blob_method; |
---|
141 | int cross_method; |
---|
142 | |
---|
143 | int x_size; |
---|
144 | int y_size; |
---|
145 | |
---|
146 | // wxHAS_DPI_INDEPENDENT_PIXELS is new in 3.1.6. In older versions we just |
---|
147 | // always do the scaling which is slightly less efficient for platforms |
---|
148 | // where pixel coordinates don't scale with DPI. |
---|
149 | #if defined wxHAS_DPI_INDEPENDENT_PIXELS || \ |
---|
150 | !wxCHECK_VERSION(3,1,6) |
---|
151 | # define HAS_DPI_INDEPENDENT_PIXELS |
---|
152 | #endif |
---|
153 | |
---|
154 | #ifdef HAS_DPI_INDEPENDENT_PIXELS |
---|
155 | double content_scale_factor = 1.0; |
---|
156 | #else |
---|
157 | static constexpr unsigned content_scale_factor = 1; |
---|
158 | #endif |
---|
159 | |
---|
160 | vector<GLAList> drawing_lists; |
---|
161 | |
---|
162 | enum { |
---|
163 | INVALIDATE_ON_SCALE = 1, |
---|
164 | INVALIDATE_ON_X_RESIZE = 2, |
---|
165 | INVALIDATE_ON_Y_RESIZE = 4, |
---|
166 | INVALIDATE_ON_HIDPI = 8, |
---|
167 | NEVER_CACHE = 16, |
---|
168 | CACHED = 32 |
---|
169 | }; |
---|
170 | mutable unsigned int list_flags; |
---|
171 | |
---|
172 | wxString vendor, renderer; |
---|
173 | |
---|
174 | bool CheckVisualFidelity(const unsigned char * target) const; |
---|
175 | |
---|
176 | public: |
---|
177 | GLACanvas(wxWindow* parent, int id); |
---|
178 | ~GLACanvas(); |
---|
179 | |
---|
180 | static bool check_visual(); |
---|
181 | |
---|
182 | void FirstShow(); |
---|
183 | |
---|
184 | void Clear(); |
---|
185 | void ClearNative(); |
---|
186 | void StartDrawing(); |
---|
187 | void FinishDrawing(); |
---|
188 | |
---|
189 | void SetVolumeDiameter(glaCoord diameter); |
---|
190 | void SetDataTransform(); |
---|
191 | void SetIndicatorTransform(); |
---|
192 | |
---|
193 | void DrawList(unsigned int l); |
---|
194 | void DrawListZPrepass(unsigned int l); |
---|
195 | void DrawList2D(unsigned int l, glaCoord x, glaCoord y, Double rotation); |
---|
196 | void InvalidateList(unsigned int l) { |
---|
197 | if (l < drawing_lists.size()) { |
---|
198 | // Invalidate any existing cached list. |
---|
199 | drawing_lists[l].invalidate_if(CACHED); |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | virtual void GenerateList(unsigned int l) = 0; |
---|
204 | |
---|
205 | void SetColour(const GLAPen& pen, double rgb_scale); |
---|
206 | void SetColour(const GLAPen& pen); |
---|
207 | void SetColour(gla_colour colour, double rgb_scale); |
---|
208 | void SetColour(gla_colour colour); |
---|
209 | void SetAlpha(double new_alpha) { alpha = new_alpha; } |
---|
210 | |
---|
211 | void DrawText(glaCoord x, glaCoord y, glaCoord z, const wxString& str); |
---|
212 | void DrawIndicatorText(int x, int y, const wxString& str); |
---|
213 | void GetTextExtent(const wxString& str, int * x_ext, int * y_ext) const; |
---|
214 | |
---|
215 | void BeginQuadrilaterals(); |
---|
216 | void EndQuadrilaterals(); |
---|
217 | void BeginLines(); |
---|
218 | void EndLines(); |
---|
219 | void BeginTriangleStrip(); |
---|
220 | void EndTriangleStrip(); |
---|
221 | void BeginTriangles(); |
---|
222 | void EndTriangles(); |
---|
223 | void BeginPolyline(); |
---|
224 | void EndPolyline(); |
---|
225 | void BeginPolyloop(); |
---|
226 | void EndPolyloop(); |
---|
227 | void BeginPolygon(); |
---|
228 | void EndPolygon(); |
---|
229 | void BeginBlobs(); |
---|
230 | void EndBlobs(); |
---|
231 | void BeginCrosses(); |
---|
232 | void EndCrosses(); |
---|
233 | |
---|
234 | void DrawRectangle(gla_colour fill, gla_colour edge, |
---|
235 | glaCoord x0, glaCoord y0, glaCoord w, glaCoord h); |
---|
236 | void DrawShadedRectangle(const GLAPen & fill_bot, const GLAPen & fill_top, |
---|
237 | glaCoord x0, glaCoord y0, glaCoord w, glaCoord h); |
---|
238 | void DrawCircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius); |
---|
239 | void DrawSemicircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius, glaCoord start); |
---|
240 | |
---|
241 | void DrawBlob(glaCoord x, glaCoord y, glaCoord z); |
---|
242 | void DrawBlob(glaCoord x, glaCoord y); |
---|
243 | void DrawCross(glaCoord x, glaCoord y, glaCoord z); |
---|
244 | void DrawRing(glaCoord x, glaCoord y); |
---|
245 | |
---|
246 | void PlaceVertex(const Vector3 & v, glaTexCoord tex_x, glaTexCoord tex_y) { |
---|
247 | PlaceVertex(v.GetX(), v.GetY(), v.GetZ(), tex_x, tex_y); |
---|
248 | } |
---|
249 | void PlaceVertex(const Vector3 & v) { |
---|
250 | PlaceVertex(v.GetX(), v.GetY(), v.GetZ()); |
---|
251 | } |
---|
252 | void PlaceVertex(glaCoord x, glaCoord y, glaCoord z); |
---|
253 | void PlaceVertex(glaCoord x, glaCoord y, glaCoord z, |
---|
254 | glaTexCoord tex_x, glaTexCoord tex_y); |
---|
255 | void PlaceIndicatorVertex(glaCoord x, glaCoord y); |
---|
256 | |
---|
257 | void PlaceNormal(const Vector3 &v); |
---|
258 | |
---|
259 | void EnableDashedLines(); |
---|
260 | void DisableDashedLines(); |
---|
261 | |
---|
262 | void EnableSmoothPolygons(bool filled); |
---|
263 | void DisableSmoothPolygons(); |
---|
264 | |
---|
265 | void SetRotation(double pan, double tilt) { |
---|
266 | m_Pan = pan; |
---|
267 | m_Tilt = tilt; |
---|
268 | } |
---|
269 | void SetScale(Double); |
---|
270 | void SetTranslation(const Vector3 &v) { |
---|
271 | m_Translation = v; |
---|
272 | } |
---|
273 | void AddTranslation(const Vector3 &v) { |
---|
274 | m_Translation += v; |
---|
275 | } |
---|
276 | const Vector3 & GetTranslation() const { |
---|
277 | return m_Translation; |
---|
278 | } |
---|
279 | void AddTranslationScreenCoordinates(int dx, int dy); |
---|
280 | |
---|
281 | bool Transform(const Vector3 & v, double* x_out, double* y_out, double* z_out) const; |
---|
282 | void ReverseTransform(Double x, Double y, double* x_out, double* y_out, double* z_out) const; |
---|
283 | |
---|
284 | int GetFontSize() const { return m_Font.get_font_size(); } |
---|
285 | |
---|
286 | void ToggleSmoothShading(); |
---|
287 | bool GetSmoothShading() const { return m_SmoothShading; } |
---|
288 | |
---|
289 | Double SurveyUnitsAcrossViewport() const; |
---|
290 | |
---|
291 | void ToggleTextured(); |
---|
292 | bool GetTextured() const { return m_Textured; } |
---|
293 | |
---|
294 | void TogglePerspective() { m_Perspective = !m_Perspective; } |
---|
295 | bool GetPerspective() const { return m_Perspective; } |
---|
296 | |
---|
297 | void ToggleFog() { m_Fog = !m_Fog; } |
---|
298 | bool GetFog() const { return m_Fog; } |
---|
299 | |
---|
300 | void ToggleAntiAlias() { m_AntiAlias = !m_AntiAlias; } |
---|
301 | bool GetAntiAlias() const { return m_AntiAlias; } |
---|
302 | |
---|
303 | bool SaveScreenshot(const wxString & fnm, wxBitmapType type) const; |
---|
304 | |
---|
305 | void ReadPixels(int width, int height, unsigned char * buf) const; |
---|
306 | |
---|
307 | void PolygonOffset(bool on) const; |
---|
308 | |
---|
309 | int GetXSize() const { |
---|
310 | list_flags |= INVALIDATE_ON_X_RESIZE; |
---|
311 | return x_size; |
---|
312 | } |
---|
313 | |
---|
314 | int GetYSize() const { |
---|
315 | list_flags |= INVALIDATE_ON_Y_RESIZE; |
---|
316 | return y_size; |
---|
317 | } |
---|
318 | |
---|
319 | #ifdef HAS_DPI_INDEPENDENT_PIXELS |
---|
320 | double GetContentScaleFactor() const { |
---|
321 | list_flags |= INVALIDATE_ON_HIDPI; |
---|
322 | return content_scale_factor; |
---|
323 | } |
---|
324 | |
---|
325 | void UpdateContentScaleFactor(); |
---|
326 | void OnMove(wxMoveEvent & event); |
---|
327 | #else |
---|
328 | // wxWindow::GetContentScaleFactor() will always return 1.0, so arrange |
---|
329 | // things so it's a compile-time constant the compiler can optimise away. |
---|
330 | // Use a macro so we can return unsigned instead of double without a lot |
---|
331 | // of pain from trying to override a virtual method while changing the |
---|
332 | // return type. |
---|
333 | # define GetContentScaleFactor() 1u |
---|
334 | void UpdateContentScaleFactor() { } |
---|
335 | #endif |
---|
336 | |
---|
337 | void OnSize(wxSizeEvent & event); |
---|
338 | |
---|
339 | glaCoord GetVolumeDiameter() const { return m_VolumeDiameter; } |
---|
340 | |
---|
341 | void ScaleMouseEvent(wxMouseEvent& e) const { |
---|
342 | e.SetX(e.GetX() * content_scale_factor); |
---|
343 | e.SetY(e.GetY() * content_scale_factor); |
---|
344 | } |
---|
345 | |
---|
346 | private: |
---|
347 | DECLARE_EVENT_TABLE() |
---|
348 | }; |
---|
349 | |
---|
350 | #endif |
---|