source: git/src/gla.h @ 236a1b1

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

Add --stereo option

Supports arguments "buffers" (OpenGL stereo buffers), "anaglyph"
(for use with coloured glasses) and "2up" (suitable for use with
a "cardboard" headset).

  • 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
164protected:
165    int m_Eye;
166
167    const stereo_mode_type& stereo_mode;
168
169public:
170    GLACanvas(wxWindow* parent, int id);
171    ~GLACanvas();
172
173    static bool check_visual();
174
175    static void SetStereoMode(stereo_mode_type mode);
176
177    void FirstShow();
178
179    void Clear();
180    void StartDrawing();
181    void FinishDrawing();
182
183    void SetVolumeDiameter(glaCoord diameter);
184    void SetDataTransform();
185    void SetIndicatorTransform();
186
187    void DrawList(unsigned int l);
188    void DrawListZPrepass(unsigned int l);
189    void DrawList2D(unsigned int l, glaCoord x, glaCoord y, Double rotation);
190    void InvalidateList(unsigned int l) {
191        if (l < drawing_lists.size()) {
192            // Invalidate any existing cached list.
193            drawing_lists[l].invalidate_if(CACHED);
194        }
195    }
196
197    virtual void GenerateList(unsigned int l) = 0;
198
199    void SetColour(const GLAPen& pen, double rgb_scale);
200    void SetColour(const GLAPen& pen);
201    void SetColour(gla_colour colour, double rgb_scale);
202    void SetColour(gla_colour colour);
203    void SetAlpha(double new_alpha) { alpha = new_alpha; }
204
205    void DrawText(glaCoord x, glaCoord y, glaCoord z, const wxString& str);
206    void DrawIndicatorText(int x, int y, const wxString& str);
207    void GetTextExtent(const wxString& str, int * x_ext, int * y_ext) const;
208
209    void BeginQuadrilaterals();
210    void EndQuadrilaterals();
211    void BeginLines();
212    void EndLines();
213    void BeginTriangleStrip();
214    void EndTriangleStrip();
215    void BeginTriangles();
216    void EndTriangles();
217    void BeginPolyline();
218    void EndPolyline();
219    void BeginPolygon();
220    void EndPolygon();
221    void BeginBlobs();
222    void EndBlobs();
223    void BeginCrosses();
224    void EndCrosses();
225
226    void DrawRectangle(gla_colour fill, gla_colour edge,
227                       glaCoord x0, glaCoord y0, glaCoord w, glaCoord h);
228    void DrawShadedRectangle(const GLAPen & fill_bot, const GLAPen & fill_top,
229                             glaCoord x0, glaCoord y0, glaCoord w, glaCoord h);
230    void DrawCircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius);
231    void DrawSemicircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius, glaCoord start);
232    void DrawTriangle(gla_colour edge, gla_colour fill,
233                      const Vector3 &p0, const Vector3 &p1, const Vector3 &p2);
234
235    void DrawBlob(glaCoord x, glaCoord y, glaCoord z);
236    void DrawBlob(glaCoord x, glaCoord y);
237    void DrawCross(glaCoord x, glaCoord y, glaCoord z);
238    void DrawRing(glaCoord x, glaCoord y);
239
240    void PlaceVertex(const Vector3 & v, glaTexCoord tex_x, glaTexCoord tex_y) {
241        PlaceVertex(v.GetX(), v.GetY(), v.GetZ(), tex_x, tex_y);
242    }
243    void PlaceVertex(const Vector3 & v) {
244        PlaceVertex(v.GetX(), v.GetY(), v.GetZ());
245    }
246    void PlaceVertex(glaCoord x, glaCoord y, glaCoord z);
247    void PlaceVertex(glaCoord x, glaCoord y, glaCoord z,
248                     glaTexCoord tex_x, glaTexCoord tex_y);
249    void PlaceIndicatorVertex(glaCoord x, glaCoord y);
250
251    void PlaceNormal(const Vector3 &v);
252
253    void EnableDashedLines();
254    void DisableDashedLines();
255
256    void EnableSmoothPolygons(bool filled);
257    void DisableSmoothPolygons();
258
259    void SetRotation(double pan, double tilt) {
260        m_Pan = pan;
261        m_Tilt = tilt;
262    }
263    void SetScale(Double);
264    void SetTranslation(const Vector3 &v) {
265        m_Translation = v;
266    }
267    void AddTranslation(const Vector3 &v) {
268        m_Translation += v;
269    }
270    const Vector3 & GetTranslation() const {
271        return m_Translation;
272    }
273    void AddTranslationScreenCoordinates(int dx, int dy);
274
275    bool Transform(const Vector3 & v, double* x_out, double* y_out, double* z_out) const;
276    void ReverseTransform(Double x, Double y, double* x_out, double* y_out, double* z_out) const;
277
278    int GetFontSize() const { return m_Font.get_font_size(); }
279
280    void ToggleSmoothShading();
281    bool GetSmoothShading() const { return m_SmoothShading; }
282
283    Double SurveyUnitsAcrossViewport() const;
284
285    void ToggleTextured();
286    bool GetTextured() const { return m_Textured; }
287
288    void TogglePerspective() { m_Perspective = !m_Perspective; }
289    bool GetPerspective() const { return m_Perspective; }
290
291    void ToggleFog() { m_Fog = !m_Fog; }
292    bool GetFog() const { return m_Fog; }
293
294    void ToggleAntiAlias() { m_AntiAlias = !m_AntiAlias; }
295    bool GetAntiAlias() const { return m_AntiAlias; }
296
297    bool SaveScreenshot(const wxString & fnm, wxBitmapType type) const;
298
299    void ReadPixels(int width, int height, unsigned char * buf) const;
300
301    void PolygonOffset(bool on) const;
302
303    int GetXSize() const {
304        list_flags |= INVALIDATE_ON_X_RESIZE;
305        return (stereo_mode == STEREO_2UP ? x_size / 2 : x_size);
306    }
307
308    int GetYSize() const { list_flags |= INVALIDATE_ON_Y_RESIZE; return y_size; }
309
310    void OnSize(wxSizeEvent & event);
311
312    glaCoord GetVolumeDiameter() const { return m_VolumeDiameter; }
313
314private:
315    DECLARE_EVENT_TABLE()
316};
317
318#endif
Note: See TracBrowser for help on using the repository browser.