source: git/src/gla.h @ 09ad6d7

RELEASE/1.2debug-cidebug-ci-sanitiserswalls-datawalls-data-hanging-as-warning
Last change on this file since 09ad6d7 was caa5fda, checked in by Olly Betts <olly@…>, 6 years ago

Refactor start-up check for OpenGL

Rather than using the apparently totally undocumented wxGLApp class,
use the documented wxGLCanvas::IsDisplaySupported?() static method
which allows moving this OpenGL-specific code into the same source
file as the other OpenGL-specific code, means we can merge the two
divergent versions of the list of attributes we ask for, and also
likely fixes handling of the case when double buffering isn't available.

  • Property mode set to 100644
File size: 8.1 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,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
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 Double 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_LAST // must be the last entry here
64};
65
66class GLAPen {
67    friend class GLACanvas; // allow direct access to components
68
69    double components[3]; // red, green, blue
70
71public:
72    GLAPen();
73
74    void SetColour(double red, double green, double blue); // arguments in range 0 to 1.0
75    void Interpolate(const GLAPen&, double how_far);
76
77    double GetRed() const;
78    double GetGreen() const;
79    double GetBlue() const;
80};
81
82class GLAList {
83    GLuint gl_list;
84    unsigned int flags;
85  public:
86    GLAList() : gl_list(0), flags(0) { }
87    GLAList(GLuint gl_list_, unsigned int flags_)
88        : gl_list(gl_list_), flags(flags_) { }
89    operator bool() { return gl_list != 0; }
90    bool need_to_generate();
91    void finalise(unsigned int list_flags);
92    bool DrawList() const;
93    void invalidate_if(unsigned int mask) {
94        // If flags == NEVER_CACHE, the list won't be invalidated (unless
95        // mask is 0, which isn't a normal thing to pass).
96        if (flags & mask)
97            flags = 0;
98    }
99};
100
101class GLACanvas : public wxGLCanvas {
102    friend class GLAList; // For flag values.
103
104    wxGLContext ctx;
105
106#ifdef GLA_DEBUG
107    int m_Vertices;
108#endif
109
110    GLdouble modelview_matrix[16];
111    GLdouble projection_matrix[16];
112    GLint viewport[4];
113
114    // Viewing volume diameter:
115    glaCoord m_VolumeDiameter;
116
117    // Parameters for plotting data:
118    Double m_Pan, m_Tilt;
119    Double m_Scale;
120    Vector3 m_Translation;
121
122    BitmapFont m_Font;
123
124    GLUquadric* m_Quadric;
125
126    GLuint m_Texture;
127    GLuint m_BlobTexture;
128    GLuint m_CrossTexture;
129
130    Double alpha;
131
132    bool m_SmoothShading;
133    bool m_Textured;
134    bool m_Perspective;
135    bool m_Fog;
136    bool m_AntiAlias;
137    bool save_hints;
138    enum { UNKNOWN = 0, POINT = 'P', LINES = 'L', SPRITE = 'S' };
139    int blob_method;
140    int cross_method;
141
142    int x_size;
143    int y_size;
144
145    vector<GLAList> drawing_lists;
146
147    enum {
148        INVALIDATE_ON_SCALE = 1,
149        INVALIDATE_ON_X_RESIZE = 2,
150        INVALIDATE_ON_Y_RESIZE = 4,
151        NEVER_CACHE = 8,
152        CACHED = 16
153    };
154    mutable unsigned int list_flags;
155
156    wxString vendor, renderer;
157
158    bool CheckVisualFidelity(const unsigned char * target) const;
159
160public:
161    GLACanvas(wxWindow* parent, int id);
162    ~GLACanvas();
163
164    static bool check_visual();
165
166    void FirstShow();
167
168    void Clear();
169    void StartDrawing();
170    void FinishDrawing();
171
172    void SetVolumeDiameter(glaCoord diameter);
173    void SetDataTransform();
174    void SetIndicatorTransform();
175
176    void DrawList(unsigned int l);
177    void DrawListZPrepass(unsigned int l);
178    void DrawList2D(unsigned int l, glaCoord x, glaCoord y, Double rotation);
179    void InvalidateList(unsigned int l) {
180        if (l < drawing_lists.size()) {
181            // Invalidate any existing cached list.
182            drawing_lists[l].invalidate_if(CACHED);
183        }
184    }
185
186    virtual void GenerateList(unsigned int l) = 0;
187
188    void SetColour(const GLAPen& pen, double rgb_scale);
189    void SetColour(const GLAPen& pen);
190    void SetColour(gla_colour colour, double rgb_scale);
191    void SetColour(gla_colour colour);
192    void SetAlpha(double new_alpha) { alpha = new_alpha; }
193
194    void DrawText(glaCoord x, glaCoord y, glaCoord z, const wxString& str);
195    void DrawIndicatorText(int x, int y, const wxString& str);
196    void GetTextExtent(const wxString& str, int * x_ext, int * y_ext) const;
197
198    void BeginQuadrilaterals();
199    void EndQuadrilaterals();
200    void BeginLines();
201    void EndLines();
202    void BeginTriangleStrip();
203    void EndTriangleStrip();
204    void BeginTriangles();
205    void EndTriangles();
206    void BeginPolyline();
207    void EndPolyline();
208    void BeginPolygon();
209    void EndPolygon();
210    void BeginBlobs();
211    void EndBlobs();
212    void BeginCrosses();
213    void EndCrosses();
214
215    void DrawRectangle(gla_colour fill, gla_colour edge,
216                       glaCoord x0, glaCoord y0, glaCoord w, glaCoord h);
217    void DrawShadedRectangle(const GLAPen & fill_bot, const GLAPen & fill_top,
218                             glaCoord x0, glaCoord y0, glaCoord w, glaCoord h);
219    void DrawCircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius);
220    void DrawSemicircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius, glaCoord start);
221    void DrawTriangle(gla_colour edge, gla_colour fill,
222                      const Vector3 &p0, const Vector3 &p1, const Vector3 &p2);
223
224    void DrawBlob(glaCoord x, glaCoord y, glaCoord z);
225    void DrawBlob(glaCoord x, glaCoord y);
226    void DrawCross(glaCoord x, glaCoord y, glaCoord z);
227    void DrawRing(glaCoord x, glaCoord y);
228
229    void PlaceVertex(const Vector3 & v, glaTexCoord tex_x, glaTexCoord tex_y) {
230        PlaceVertex(v.GetX(), v.GetY(), v.GetZ(), tex_x, tex_y);
231    }
232    void PlaceVertex(const Vector3 & v) {
233        PlaceVertex(v.GetX(), v.GetY(), v.GetZ());
234    }
235    void PlaceVertex(glaCoord x, glaCoord y, glaCoord z);
236    void PlaceVertex(glaCoord x, glaCoord y, glaCoord z,
237                     glaTexCoord tex_x, glaTexCoord tex_y);
238    void PlaceIndicatorVertex(glaCoord x, glaCoord y);
239
240    void PlaceNormal(const Vector3 &v);
241
242    void EnableDashedLines();
243    void DisableDashedLines();
244
245    void EnableSmoothPolygons(bool filled);
246    void DisableSmoothPolygons();
247
248    void SetRotation(double pan, double tilt) {
249        m_Pan = pan;
250        m_Tilt = tilt;
251    }
252    void SetScale(Double);
253    void SetTranslation(const Vector3 &v) {
254        m_Translation = v;
255    }
256    void AddTranslation(const Vector3 &v) {
257        m_Translation += v;
258    }
259    const Vector3 & GetTranslation() const {
260        return m_Translation;
261    }
262    void AddTranslationScreenCoordinates(int dx, int dy);
263
264    bool Transform(const Vector3 & v, double* x_out, double* y_out, double* z_out) const;
265    void ReverseTransform(Double x, Double y, double* x_out, double* y_out, double* z_out) const;
266
267    int GetFontSize() const { return m_Font.get_font_size(); }
268
269    void ToggleSmoothShading();
270    bool GetSmoothShading() const { return m_SmoothShading; }
271
272    Double SurveyUnitsAcrossViewport() const;
273
274    void ToggleTextured();
275    bool GetTextured() const { return m_Textured; }
276
277    void TogglePerspective() { m_Perspective = !m_Perspective; }
278    bool GetPerspective() const { return m_Perspective; }
279
280    void ToggleFog() { m_Fog = !m_Fog; }
281    bool GetFog() const { return m_Fog; }
282
283    void ToggleAntiAlias() { m_AntiAlias = !m_AntiAlias; }
284    bool GetAntiAlias() const { return m_AntiAlias; }
285
286    bool SaveScreenshot(const wxString & fnm, wxBitmapType type) const;
287
288    void ReadPixels(int width, int height, unsigned char * buf) const;
289
290    void PolygonOffset(bool on) const;
291
292    int GetXSize() const { list_flags |= INVALIDATE_ON_X_RESIZE; return x_size; }
293    int GetYSize() const { list_flags |= INVALIDATE_ON_Y_RESIZE; return y_size; }
294
295    void OnSize(wxSizeEvent & event);
296
297    glaCoord GetVolumeDiameter() const { return m_VolumeDiameter; }
298
299private:
300    DECLARE_EVENT_TABLE()
301};
302
303#endif
Note: See TracBrowser for help on using the repository browser.