source: git/src/gla.h @ 6616cdbf

stereo
Last change on this file since 6616cdbf was 6616cdbf, checked in by Olly Betts <olly@…>, 6 years ago

Push stereo OpenGL code down to gla layer

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