source: git/src/gla.h @ 3e36d459

stereo-2025
Last change on this file since 3e36d459 was 9284d37, checked in by Olly Betts <olly@…>, 4 months ago

Allow exaggerating Z in aven

Add a spin control to the toolbar which allows setting a Z scale
factor from 0.1 and 10.0.

Fixes #49

  • Property mode set to 100644
File size: 9.6 KB
Line 
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-2025 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
30using namespace std;
31
32#include "wx.h"
33#include "aventypes.h"
34#include "vector3.h"
35
36#include "glbitmapfont.h"
37
38class GfxCore;
39
40string GetGLSystemDescription();
41
42// #define GLA_DEBUG
43
44typedef GLdouble glaCoord;
45
46typedef GLfloat glaTexCoord;
47
48// Colours for drawing.  Don't reorder these!
49enum 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
67class GLAPen {
68    friend class GLACanvas; // allow direct access to components
69
70    double components[3] = { 0.0, 0.0, 0.0 }; // red, green, blue
71
72public:
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
83class GLAList {
84    GLuint gl_list = 0;
85    unsigned int flags = 0;
86  public:
87    GLAList() { }
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
102class 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 = 1.0;
117
118    // Parameters for plotting data:
119    double m_Pan = 0.0, m_Tilt = 0.0;
120    double m_Scale = 0.0;
121    Vector3 m_Translation;
122
123    double z_stretch = 1.0;
124
125    BitmapFont m_Font;
126
127    GLUquadric* m_Quadric = nullptr;
128
129    GLuint m_Texture = 0;
130    GLuint m_BlobTexture;
131    GLuint m_CrossTexture;
132
133    double alpha = 1.0;
134
135    bool m_SmoothShading = false;
136    bool m_Textured = false;
137    bool m_Perspective = false;
138    bool m_Fog = false;
139    bool m_AntiAlias = false;
140    bool save_hints;
141    enum { UNKNOWN = 0, POINT = 'P', LINES = 'L', SPRITE = 'S' };
142    int blob_method = UNKNOWN;
143    int cross_method = UNKNOWN;
144
145    int x_size = 0;
146    int y_size = 0;
147
148    // wxHAS_DPI_INDEPENDENT_PIXELS is new in 3.1.6.  In older versions we just
149    // always do the scaling which is slightly less efficient for platforms
150    // where pixel coordinates don't scale with DPI.
151#if defined wxHAS_DPI_INDEPENDENT_PIXELS || \
152    !wxCHECK_VERSION(3,1,6)
153# define HAS_DPI_INDEPENDENT_PIXELS
154#endif
155
156#ifdef HAS_DPI_INDEPENDENT_PIXELS
157    double content_scale_factor = 1.0;
158#else
159    static constexpr unsigned content_scale_factor = 1;
160#endif
161
162    vector<GLAList> drawing_lists;
163
164    enum {
165        INVALIDATE_ON_SCALE = 1,
166        INVALIDATE_ON_X_RESIZE = 2,
167        INVALIDATE_ON_Y_RESIZE = 4,
168        INVALIDATE_ON_HIDPI = 8,
169        NEVER_CACHE = 16,
170        CACHED = 32
171    };
172    mutable unsigned int list_flags = 0;
173
174    wxString vendor, renderer;
175
176    bool CheckVisualFidelity(const unsigned char * target) const;
177
178public:
179    GLACanvas(wxWindow* parent, int id);
180    ~GLACanvas();
181
182    static bool check_visual();
183
184    void FirstShow();
185
186    void Clear();
187    void ClearNative();
188    void StartDrawing();
189    void FinishDrawing();
190
191    void SetVolumeDiameter(glaCoord diameter);
192    void SetDataTransform();
193    void SetIndicatorTransform();
194
195    void DrawList(unsigned int l);
196    void DrawListZPrepass(unsigned int l);
197    void DrawList2D(unsigned int l, glaCoord x, glaCoord y, double rotation);
198    void InvalidateList(unsigned int l) {
199        if (l < drawing_lists.size()) {
200            // Invalidate any existing cached list.
201            drawing_lists[l].invalidate_if(CACHED);
202        }
203    }
204
205    virtual void GenerateList(unsigned int l) = 0;
206
207    void SetColour(const GLAPen& pen, double rgb_scale);
208    void SetColour(const GLAPen& pen);
209    void SetColour(gla_colour colour, double rgb_scale);
210    void SetColour(gla_colour colour);
211    void SetAlpha(double new_alpha) { alpha = new_alpha; }
212
213    void DrawText(glaCoord x, glaCoord y, glaCoord z, const wxString& str);
214    void DrawIndicatorText(int x, int y, const wxString& str);
215    void GetTextExtent(const wxString& str, int * x_ext, int * y_ext) const;
216
217    void BeginQuadrilaterals();
218    void EndQuadrilaterals();
219    void BeginLines();
220    void EndLines();
221    void BeginTriangleStrip();
222    void EndTriangleStrip();
223    void BeginTriangles();
224    void EndTriangles();
225    void BeginPolyline();
226    void EndPolyline();
227    void BeginPolyloop();
228    void EndPolyloop();
229    void BeginPolygon();
230    void EndPolygon();
231    void BeginPoints();
232    void EndPoints();
233    void BeginBlobs();
234    void EndBlobs();
235    void BeginCrosses();
236    void EndCrosses();
237
238    void DrawRectangle(gla_colour fill, gla_colour edge,
239                       glaCoord x0, glaCoord y0, glaCoord w, glaCoord h);
240    void DrawShadedRectangle(const GLAPen & fill_bot, const GLAPen & fill_top,
241                             glaCoord x0, glaCoord y0, glaCoord w, glaCoord h);
242    void DrawCircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius);
243    void DrawSemicircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius, glaCoord start);
244
245    void DrawBlob(glaCoord x, glaCoord y, glaCoord z);
246    void DrawBlob(glaCoord x, glaCoord y);
247    void DrawCross(glaCoord x, glaCoord y, glaCoord z);
248    void DrawRing(glaCoord x, glaCoord y);
249
250    void PlaceVertex(const Vector3 & v, glaTexCoord tex_x, glaTexCoord tex_y) {
251        PlaceVertex(v.GetX(), v.GetY(), v.GetZ(), tex_x, tex_y);
252    }
253    void PlaceVertex(const Vector3 & v) {
254        PlaceVertex(v.GetX(), v.GetY(), v.GetZ());
255    }
256    void PlaceVertex(glaCoord x, glaCoord y, glaCoord z);
257    void PlaceVertex(glaCoord x, glaCoord y, glaCoord z,
258                     glaTexCoord tex_x, glaTexCoord tex_y);
259    void PlaceIndicatorVertex(glaCoord x, glaCoord y);
260
261    void PlaceNormal(const Vector3 &v);
262
263    void EnableDashedLines();
264    void DisableDashedLines();
265
266    void EnableSmoothPolygons(bool filled);
267    void DisableSmoothPolygons();
268
269    void SetRotation(double pan, double tilt) {
270        m_Pan = pan;
271        m_Tilt = tilt;
272    }
273    void SetScale(double);
274    void SetTranslation(const Vector3 &v) {
275        m_Translation = v;
276    }
277    void AddTranslation(const Vector3 &v) {
278        m_Translation += v;
279    }
280    const Vector3 & GetTranslation() const {
281        return m_Translation;
282    }
283    void AddTranslationScreenCoordinates(int dx, int dy);
284
285    bool Transform(const Vector3 & v, glaCoord* x_out, glaCoord* y_out, glaCoord* z_out) const;
286    void ReverseTransform(double x, double y, glaCoord* x_out, glaCoord* y_out, glaCoord* z_out) const;
287
288    void SetZStretch(double z_stretch_) { z_stretch = z_stretch_; }
289
290    int GetFontSize() const { return m_Font.get_font_size(); }
291
292    void ToggleSmoothShading();
293    bool GetSmoothShading() const { return m_SmoothShading; }
294
295    double SurveyUnitsAcrossViewport() const;
296
297    void ToggleTextured();
298    bool GetTextured() const { return m_Textured; }
299
300    void TogglePerspective() { m_Perspective = !m_Perspective; }
301    bool GetPerspective() const { return m_Perspective; }
302
303    void ToggleFog() { m_Fog = !m_Fog; }
304    bool GetFog() const { return m_Fog; }
305
306    void ToggleAntiAlias() { m_AntiAlias = !m_AntiAlias; }
307    bool GetAntiAlias() const { return m_AntiAlias; }
308
309    bool SaveScreenshot(const wxString & fnm, wxBitmapType type) const;
310
311    void ReadPixels(int width, int height, unsigned char * buf) const;
312
313    void PolygonOffset(bool on) const;
314
315    int GetXSize() const {
316        list_flags |= INVALIDATE_ON_X_RESIZE;
317        return x_size;
318    }
319
320    int GetYSize() const {
321        list_flags |= INVALIDATE_ON_Y_RESIZE;
322        return y_size;
323    }
324
325#ifdef HAS_DPI_INDEPENDENT_PIXELS
326    double GetContentScaleFactor() const {
327        list_flags |= INVALIDATE_ON_HIDPI;
328        return content_scale_factor;
329    }
330
331    void UpdateContentScaleFactor();
332    void OnMove(wxMoveEvent & event);
333#else
334    // wxWindow::GetContentScaleFactor() will always return 1.0, so arrange
335    // things so it's a compile-time constant the compiler can optimise away.
336    // Use a macro so we can return unsigned instead of double without a lot
337    // of pain from trying to override a virtual method while changing the
338    // return type.
339# define GetContentScaleFactor() 1u
340    void UpdateContentScaleFactor() { }
341#endif
342
343    void OnSize(wxSizeEvent & event);
344
345    glaCoord GetVolumeDiameter() const { return m_VolumeDiameter; }
346
347    void ScaleMouseEvent(wxMouseEvent& e) const {
348        e.SetX(e.GetX() * content_scale_factor);
349        e.SetY(e.GetY() * content_scale_factor);
350    }
351
352private:
353    DECLARE_EVENT_TABLE()
354};
355
356#endif
Note: See TracBrowser for help on using the repository browser.