| [56da40e] | 1 | // |
|---|
| 2 | // gla.h |
|---|
| 3 | // |
|---|
| 4 | // Header file for the GLA abstraction layer. |
|---|
| 5 | // |
|---|
| 6 | // Copyright (C) 2002 Mark R. Shinwell. |
|---|
| [9284d37] | 7 | // Copyright (C) 2003-2025 Olly Betts |
|---|
| [56da40e] | 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 |
|---|
| [ecbc6c18] | 21 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| [56da40e] | 22 | // |
|---|
| 23 | |
|---|
| [caa5fda] | 24 | #ifndef gla_h |
|---|
| 25 | #define gla_h |
|---|
| 26 | |
|---|
| [5627cbb] | 27 | #include <string> |
|---|
| [bae6a7c] | 28 | #include <vector> |
|---|
| 29 | |
|---|
| 30 | using namespace std; |
|---|
| 31 | |
|---|
| [56da40e] | 32 | #include "wx.h" |
|---|
| [08253d9] | 33 | #include "vector3.h" |
|---|
| [56da40e] | 34 | |
|---|
| [1aa3fb7] | 35 | #include "glbitmapfont.h" |
|---|
| [1eeb55a] | 36 | |
|---|
| [2279fbed] | 37 | #ifdef HAVE_GL_GL_H |
|---|
| 38 | # include <GL/gl.h> |
|---|
| 39 | #elif defined HAVE_OPENGL_GL_H |
|---|
| 40 | # include <OpenGL/gl.h> |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | #ifdef HAVE_GL_GLU_H |
|---|
| 44 | # include <GL/glu.h> |
|---|
| 45 | #elif defined HAVE_OPENGL_GLU_H |
|---|
| 46 | # include <OpenGL/glu.h> |
|---|
| 47 | #endif |
|---|
| 48 | |
|---|
| [56da40e] | 49 | class GfxCore; |
|---|
| 50 | |
|---|
| [5627cbb] | 51 | string GetGLSystemDescription(); |
|---|
| [cc1a1d9] | 52 | |
|---|
| [d67450e] | 53 | // #define GLA_DEBUG |
|---|
| [1897247] | 54 | |
|---|
| [147847c] | 55 | typedef GLdouble glaCoord; |
|---|
| [56da40e] | 56 | |
|---|
| [ba828d4] | 57 | typedef GLfloat glaTexCoord; |
|---|
| [f336ab9] | 58 | |
|---|
| [aa048c3] | 59 | // Colours for drawing. Don't reorder these! |
|---|
| 60 | enum gla_colour { |
|---|
| 61 | col_BLACK = 0, |
|---|
| 62 | col_GREY, |
|---|
| 63 | col_LIGHT_GREY, |
|---|
| 64 | col_LIGHT_GREY_2, |
|---|
| 65 | col_DARK_GREY, |
|---|
| 66 | col_WHITE, |
|---|
| 67 | col_TURQUOISE, |
|---|
| 68 | col_GREEN, |
|---|
| 69 | col_INDICATOR_1, |
|---|
| 70 | col_INDICATOR_2, |
|---|
| 71 | col_YELLOW, |
|---|
| 72 | col_RED, |
|---|
| [f4c5932] | 73 | col_BLUE, |
|---|
| [39b8117] | 74 | col_MAGENTA, |
|---|
| [aa048c3] | 75 | col_LAST // must be the last entry here |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| [56da40e] | 78 | class GLAPen { |
|---|
| [aa048c3] | 79 | friend class GLACanvas; // allow direct access to components |
|---|
| 80 | |
|---|
| [a3f83057] | 81 | double components[3] = { 0.0, 0.0, 0.0 }; // red, green, blue |
|---|
| [9cf3688] | 82 | |
|---|
| [56da40e] | 83 | public: |
|---|
| [a3f83057] | 84 | GLAPen() {} |
|---|
| [56da40e] | 85 | |
|---|
| 86 | void SetColour(double red, double green, double blue); // arguments in range 0 to 1.0 |
|---|
| [f383708] | 87 | void Interpolate(const GLAPen&, double how_far); |
|---|
| [56da40e] | 88 | |
|---|
| [f7ea0e1] | 89 | double GetRed() const; |
|---|
| 90 | double GetGreen() const; |
|---|
| 91 | double GetBlue() const; |
|---|
| [56da40e] | 92 | }; |
|---|
| 93 | |
|---|
| [db59b02] | 94 | class GLAList { |
|---|
| [a3f83057] | 95 | GLuint gl_list = 0; |
|---|
| 96 | unsigned int flags = 0; |
|---|
| [db59b02] | 97 | public: |
|---|
| [a3f83057] | 98 | GLAList() { } |
|---|
| [db59b02] | 99 | GLAList(GLuint gl_list_, unsigned int flags_) |
|---|
| 100 | : gl_list(gl_list_), flags(flags_) { } |
|---|
| 101 | operator bool() { return gl_list != 0; } |
|---|
| [620c0c9] | 102 | bool need_to_generate(); |
|---|
| 103 | void finalise(unsigned int list_flags); |
|---|
| 104 | bool DrawList() const; |
|---|
| [b3f1bbe] | 105 | void invalidate_if(unsigned int mask) { |
|---|
| 106 | // If flags == NEVER_CACHE, the list won't be invalidated (unless |
|---|
| 107 | // mask is 0, which isn't a normal thing to pass). |
|---|
| 108 | if (flags & mask) |
|---|
| 109 | flags = 0; |
|---|
| 110 | } |
|---|
| [db59b02] | 111 | }; |
|---|
| 112 | |
|---|
| [56da40e] | 113 | class GLACanvas : public wxGLCanvas { |
|---|
| [b3f1bbe] | 114 | friend class GLAList; // For flag values. |
|---|
| [620c0c9] | 115 | |
|---|
| [8c048fa] | 116 | wxGLContext ctx; |
|---|
| 117 | |
|---|
| [203d2a7] | 118 | #ifdef GLA_DEBUG |
|---|
| 119 | int m_Vertices; |
|---|
| 120 | #endif |
|---|
| 121 | |
|---|
| [dde4fe7] | 122 | GLdouble modelview_matrix[16]; |
|---|
| 123 | GLdouble projection_matrix[16]; |
|---|
| 124 | GLint viewport[4]; |
|---|
| 125 | |
|---|
| [c5fc8eb] | 126 | // Viewing volume diameter: |
|---|
| [a3f83057] | 127 | glaCoord m_VolumeDiameter = 1.0; |
|---|
| [56da40e] | 128 | |
|---|
| 129 | // Parameters for plotting data: |
|---|
| [a3f83057] | 130 | double m_Pan = 0.0, m_Tilt = 0.0; |
|---|
| 131 | double m_Scale = 0.0; |
|---|
| [d67450e] | 132 | Vector3 m_Translation; |
|---|
| [56da40e] | 133 | |
|---|
| [9284d37] | 134 | double z_stretch = 1.0; |
|---|
| 135 | |
|---|
| [1aa3fb7] | 136 | BitmapFont m_Font; |
|---|
| [d9b3270] | 137 | |
|---|
| [a3f83057] | 138 | GLUquadric* m_Quadric = nullptr; |
|---|
| [a517825] | 139 | |
|---|
| [a3f83057] | 140 | GLuint m_Texture = 0; |
|---|
| [cab6f11] | 141 | GLuint m_BlobTexture; |
|---|
| [95ce35f] | 142 | GLuint m_CrossTexture; |
|---|
| [a517825] | 143 | |
|---|
| [a3f83057] | 144 | double alpha = 1.0; |
|---|
| [4a1cede] | 145 | |
|---|
| [a3f83057] | 146 | bool m_SmoothShading = false; |
|---|
| 147 | bool m_Textured = false; |
|---|
| 148 | bool m_Perspective = false; |
|---|
| 149 | bool m_Fog = false; |
|---|
| 150 | bool m_AntiAlias = false; |
|---|
| [807f9dd] | 151 | bool save_hints; |
|---|
| [fe075d7] | 152 | enum { UNKNOWN = 0, POINT = 'P', LINES = 'L', SPRITE = 'S' }; |
|---|
| [a3f83057] | 153 | int blob_method = UNKNOWN; |
|---|
| 154 | int cross_method = UNKNOWN; |
|---|
| [1eeb55a] | 155 | |
|---|
| [a3f83057] | 156 | int x_size = 0; |
|---|
| 157 | int y_size = 0; |
|---|
| [90430f2] | 158 | |
|---|
| [eadf5fd] | 159 | double dpi_scale_factor = 1.0; |
|---|
| [705adee9] | 160 | |
|---|
| [db59b02] | 161 | vector<GLAList> drawing_lists; |
|---|
| [90430f2] | 162 | |
|---|
| 163 | enum { |
|---|
| 164 | INVALIDATE_ON_SCALE = 1, |
|---|
| 165 | INVALIDATE_ON_X_RESIZE = 2, |
|---|
| 166 | INVALIDATE_ON_Y_RESIZE = 4, |
|---|
| [2dacc8bd] | 167 | INVALIDATE_ON_HIDPI = 8, |
|---|
| 168 | NEVER_CACHE = 16, |
|---|
| 169 | CACHED = 32 |
|---|
| [90430f2] | 170 | }; |
|---|
| [a3f83057] | 171 | mutable unsigned int list_flags = 0; |
|---|
| [bae6a7c] | 172 | |
|---|
| [807f9dd] | 173 | wxString vendor, renderer; |
|---|
| 174 | |
|---|
| 175 | bool CheckVisualFidelity(const unsigned char * target) const; |
|---|
| 176 | |
|---|
| [5f8464d] | 177 | void UpdateSize(); |
|---|
| 178 | |
|---|
| [56da40e] | 179 | public: |
|---|
| [84f1ed1] | 180 | GLACanvas(wxWindow* parent, int id); |
|---|
| [56da40e] | 181 | ~GLACanvas(); |
|---|
| [2c8b64f] | 182 | |
|---|
| [caa5fda] | 183 | static bool check_visual(); |
|---|
| 184 | |
|---|
| [1b12b82] | 185 | void FirstShow(); |
|---|
| 186 | |
|---|
| [56da40e] | 187 | void Clear(); |
|---|
| [8a7f1c5] | 188 | void ClearNative(); |
|---|
| [56da40e] | 189 | void StartDrawing(); |
|---|
| 190 | void FinishDrawing(); |
|---|
| 191 | |
|---|
| [c5fc8eb] | 192 | void SetVolumeDiameter(glaCoord diameter); |
|---|
| [56da40e] | 193 | void SetDataTransform(); |
|---|
| 194 | void SetIndicatorTransform(); |
|---|
| [9cf3688] | 195 | |
|---|
| [d2fcc9b] | 196 | void DrawList(unsigned int l); |
|---|
| [11fe902] | 197 | void DrawListZPrepass(unsigned int l); |
|---|
| [147847c] | 198 | void DrawList2D(unsigned int l, glaCoord x, glaCoord y, double rotation); |
|---|
| [b3f1bbe] | 199 | void InvalidateList(unsigned int l) { |
|---|
| 200 | if (l < drawing_lists.size()) { |
|---|
| 201 | // Invalidate any existing cached list. |
|---|
| 202 | drawing_lists[l].invalidate_if(CACHED); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| [d2fcc9b] | 206 | virtual void GenerateList(unsigned int l) = 0; |
|---|
| [9cf3688] | 207 | |
|---|
| [aa048c3] | 208 | void SetColour(const GLAPen& pen, double rgb_scale); |
|---|
| 209 | void SetColour(const GLAPen& pen); |
|---|
| [d1ce9bd] | 210 | void SetColour(gla_colour colour, double rgb_scale); |
|---|
| [aa048c3] | 211 | void SetColour(gla_colour colour); |
|---|
| [4a1cede] | 212 | void SetAlpha(double new_alpha) { alpha = new_alpha; } |
|---|
| [aa048c3] | 213 | |
|---|
| [56da40e] | 214 | void DrawText(glaCoord x, glaCoord y, glaCoord z, const wxString& str); |
|---|
| [1eeb55a] | 215 | void DrawIndicatorText(int x, int y, const wxString& str); |
|---|
| [dbd50e2] | 216 | void GetTextExtent(const wxString& str, int * x_ext, int * y_ext) const; |
|---|
| [9cf3688] | 217 | |
|---|
| [56da40e] | 218 | void BeginQuadrilaterals(); |
|---|
| 219 | void EndQuadrilaterals(); |
|---|
| 220 | void BeginLines(); |
|---|
| 221 | void EndLines(); |
|---|
| [dde4fe7] | 222 | void BeginTriangleStrip(); |
|---|
| 223 | void EndTriangleStrip(); |
|---|
| [56da40e] | 224 | void BeginTriangles(); |
|---|
| 225 | void EndTriangles(); |
|---|
| 226 | void BeginPolyline(); |
|---|
| 227 | void EndPolyline(); |
|---|
| [0642381] | 228 | void BeginPolyloop(); |
|---|
| 229 | void EndPolyloop(); |
|---|
| [45aa1d6] | 230 | void BeginPolygon(); |
|---|
| 231 | void EndPolygon(); |
|---|
| [51eab3b] | 232 | void BeginPoints(); |
|---|
| 233 | void EndPoints(); |
|---|
| [e633bb1] | 234 | void BeginBlobs(); |
|---|
| 235 | void EndBlobs(); |
|---|
| [86fe6e4] | 236 | void BeginCrosses(); |
|---|
| 237 | void EndCrosses(); |
|---|
| [9cf3688] | 238 | |
|---|
| [522e0bd] | 239 | void DrawRectangle(gla_colour fill, gla_colour edge, |
|---|
| [d67450e] | 240 | glaCoord x0, glaCoord y0, glaCoord w, glaCoord h); |
|---|
| [aa048c3] | 241 | void DrawShadedRectangle(const GLAPen & fill_bot, const GLAPen & fill_top, |
|---|
| 242 | glaCoord x0, glaCoord y0, glaCoord w, glaCoord h); |
|---|
| 243 | void DrawCircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius); |
|---|
| 244 | void DrawSemicircle(gla_colour edge, gla_colour fill, glaCoord cx, glaCoord cy, glaCoord radius, glaCoord start); |
|---|
| [9cf3688] | 245 | |
|---|
| [e633bb1] | 246 | void DrawBlob(glaCoord x, glaCoord y, glaCoord z); |
|---|
| [81aea4e] | 247 | void DrawBlob(glaCoord x, glaCoord y); |
|---|
| [86fe6e4] | 248 | void DrawCross(glaCoord x, glaCoord y, glaCoord z); |
|---|
| [e633bb1] | 249 | void DrawRing(glaCoord x, glaCoord y); |
|---|
| [9cf3688] | 250 | |
|---|
| [f336ab9] | 251 | void PlaceVertex(const Vector3 & v, glaTexCoord tex_x, glaTexCoord tex_y) { |
|---|
| [b839829] | 252 | PlaceVertex(v.GetX(), v.GetY(), v.GetZ(), tex_x, tex_y); |
|---|
| 253 | } |
|---|
| [d67450e] | 254 | void PlaceVertex(const Vector3 & v) { |
|---|
| 255 | PlaceVertex(v.GetX(), v.GetY(), v.GetZ()); |
|---|
| 256 | } |
|---|
| [56da40e] | 257 | void PlaceVertex(glaCoord x, glaCoord y, glaCoord z); |
|---|
| [f336ab9] | 258 | void PlaceVertex(glaCoord x, glaCoord y, glaCoord z, |
|---|
| 259 | glaTexCoord tex_x, glaTexCoord tex_y); |
|---|
| [56da40e] | 260 | void PlaceIndicatorVertex(glaCoord x, glaCoord y); |
|---|
| [203d2a7] | 261 | |
|---|
| [d67450e] | 262 | void PlaceNormal(const Vector3 &v); |
|---|
| [9cf3688] | 263 | |
|---|
| [56da40e] | 264 | void EnableDashedLines(); |
|---|
| 265 | void DisableDashedLines(); |
|---|
| 266 | |
|---|
| [d67450e] | 267 | void EnableSmoothPolygons(bool filled); |
|---|
| [1b12b82] | 268 | void DisableSmoothPolygons(); |
|---|
| 269 | |
|---|
| [08253d9] | 270 | void SetRotation(double pan, double tilt) { |
|---|
| 271 | m_Pan = pan; |
|---|
| 272 | m_Tilt = tilt; |
|---|
| 273 | } |
|---|
| [147847c] | 274 | void SetScale(double); |
|---|
| [d67450e] | 275 | void SetTranslation(const Vector3 &v) { |
|---|
| 276 | m_Translation = v; |
|---|
| 277 | } |
|---|
| 278 | void AddTranslation(const Vector3 &v) { |
|---|
| 279 | m_Translation += v; |
|---|
| 280 | } |
|---|
| 281 | const Vector3 & GetTranslation() const { |
|---|
| 282 | return m_Translation; |
|---|
| 283 | } |
|---|
| [56da40e] | 284 | void AddTranslationScreenCoordinates(int dx, int dy); |
|---|
| 285 | |
|---|
| [147847c] | 286 | bool Transform(const Vector3 & v, glaCoord* x_out, glaCoord* y_out, glaCoord* z_out) const; |
|---|
| 287 | void ReverseTransform(double x, double y, glaCoord* x_out, glaCoord* y_out, glaCoord* z_out) const; |
|---|
| [56da40e] | 288 | |
|---|
| [9284d37] | 289 | void SetZStretch(double z_stretch_) { z_stretch = z_stretch_; } |
|---|
| 290 | |
|---|
| [1aa3fb7] | 291 | int GetFontSize() const { return m_Font.get_font_size(); } |
|---|
| [087bc72] | 292 | |
|---|
| [d67450e] | 293 | void ToggleSmoothShading(); |
|---|
| 294 | bool GetSmoothShading() const { return m_SmoothShading; } |
|---|
| 295 | |
|---|
| [147847c] | 296 | double SurveyUnitsAcrossViewport() const; |
|---|
| [6abab84] | 297 | |
|---|
| [a517825] | 298 | void ToggleTextured(); |
|---|
| 299 | bool GetTextured() const { return m_Textured; } |
|---|
| 300 | |
|---|
| [6abab84] | 301 | void TogglePerspective() { m_Perspective = !m_Perspective; } |
|---|
| 302 | bool GetPerspective() const { return m_Perspective; } |
|---|
| [045e2af] | 303 | |
|---|
| [c60062d] | 304 | void ToggleFog() { m_Fog = !m_Fog; } |
|---|
| 305 | bool GetFog() const { return m_Fog; } |
|---|
| 306 | |
|---|
| [db452ae] | 307 | void ToggleAntiAlias() { m_AntiAlias = !m_AntiAlias; } |
|---|
| 308 | bool GetAntiAlias() const { return m_AntiAlias; } |
|---|
| 309 | |
|---|
| [1ada489] | 310 | bool SaveScreenshot(const wxString & fnm, wxBitmapType type) const; |
|---|
| [aea4f8b] | 311 | |
|---|
| 312 | void ReadPixels(int width, int height, unsigned char * buf) const; |
|---|
| [f9ca87c] | 313 | |
|---|
| 314 | void PolygonOffset(bool on) const; |
|---|
| [90430f2] | 315 | |
|---|
| [2dacc8bd] | 316 | int GetXSize() const { |
|---|
| 317 | list_flags |= INVALIDATE_ON_X_RESIZE; |
|---|
| 318 | return x_size; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | int GetYSize() const { |
|---|
| 322 | list_flags |= INVALIDATE_ON_Y_RESIZE; |
|---|
| 323 | return y_size; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| [eadf5fd] | 326 | double GetDPIScaleFactor() const { |
|---|
| [2dacc8bd] | 327 | list_flags |= INVALIDATE_ON_HIDPI; |
|---|
| [eadf5fd] | 328 | return dpi_scale_factor; |
|---|
| [2dacc8bd] | 329 | } |
|---|
| 330 | |
|---|
| 331 | void OnMove(wxMoveEvent & event); |
|---|
| [90430f2] | 332 | |
|---|
| 333 | void OnSize(wxSizeEvent & event); |
|---|
| [70acad9] | 334 | |
|---|
| 335 | glaCoord GetVolumeDiameter() const { return m_VolumeDiameter; } |
|---|
| [9071cf5] | 336 | |
|---|
| [705adee9] | 337 | void ScaleMouseEvent(wxMouseEvent& e) const { |
|---|
| [2ef89f9] | 338 | #ifdef wxHAS_DPI_INDEPENDENT_PIXELS |
|---|
| 339 | auto content_scale_factor = GetContentScaleFactor(); |
|---|
| 340 | |
|---|
| 341 | e.SetX(e.GetX() * content_scale_factor); |
|---|
| 342 | e.SetY(e.GetY() * content_scale_factor); |
|---|
| 343 | #else |
|---|
| 344 | (void)e; |
|---|
| 345 | #endif |
|---|
| [705adee9] | 346 | } |
|---|
| 347 | |
|---|
| [9071cf5] | 348 | private: |
|---|
| 349 | DECLARE_EVENT_TABLE() |
|---|
| [56da40e] | 350 | }; |
|---|
| [caa5fda] | 351 | |
|---|
| 352 | #endif |
|---|