source: git/src/gla-gl.cc @ 8436378

main
Last change on this file since 8436378 was a871ac3, checked in by Olly Betts <olly@…>, 4 months ago

Clean up inclusion of wx headers

  • Property mode set to 100644
File size: 49.5 KB
RevLine 
[56da40e]1//
2//  gla-gl.cc
3//
4//  OpenGL implementation for the GLA abstraction layer.
5//
[f4c5932]6//  Copyright (C) 2002-2003,2005 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
[cbfa50d]24#include <config.h>
25
[bd551c0]26#include <wx/confbase.h>
27
[87cab10]28#include <algorithm>
29
[78924eb]30#include "aven.h"
[56da40e]31#include "gla.h"
[0273042]32#include "gllogerror.h"
[bd551c0]33#include "message.h"
[e577f89]34#include "useful.h"
[1eeb55a]35
[ee63994]36#ifdef HAVE_GL_GL_H
37# include <GL/gl.h>
38#elif defined HAVE_OPENGL_GL_H
39# include <OpenGL/gl.h>
40#endif
41
[2279fbed]42#ifdef HAVE_GL_GLU_H
43# include <GL/glu.h>
44#elif defined HAVE_OPENGL_GLU_H
45# include <OpenGL/glu.h>
46#endif
47
[de4b099]48#ifdef HAVE_GL_GLEXT_H
49# include <GL/glext.h>
50#elif defined HAVE_OPENGL_GLEXT_H
[ee63994]51# include <OpenGL/glext.h>
[b72f4b5]52#endif
[f960f23]53
[b05f516]54#ifndef GL_POINT_SIZE_MAX
55#define GL_POINT_SIZE_MAX 0x8127
56#endif
[9baa53a]57#ifndef GL_POINT_SPRITE
58#define GL_POINT_SPRITE 0x8861
[f960f23]59#endif
[9baa53a]60#ifndef GL_COORD_REPLACE
61#define GL_COORD_REPLACE 0x8862
[f960f23]62#endif
[04bf822]63// GL_POINT_SIZE_RANGE is deprecated in OpenGL 1.2 and later, and replaced by
64// GL_SMOOTH_POINT_SIZE_RANGE.
65#ifndef GL_SMOOTH_POINT_SIZE_RANGE
66#define GL_SMOOTH_POINT_SIZE_RANGE GL_POINT_SIZE_RANGE
67#endif
68// GL_POINT_SIZE_GRANULARITY is deprecated in OpenGL 1.2 and later, and
69// replaced by GL_SMOOTH_POINT_SIZE_GRANULARITY.
70#ifndef GL_SMOOTH_POINT_SIZE_GRANULARITY
71#define GL_SMOOTH_POINT_SIZE_GRANULARITY GL_POINT_SIZE_GRANULARITY
72#endif
73// GL_ALIASED_POINT_SIZE_RANGE was added in OpenGL 1.2.
74#ifndef GL_ALIASED_POINT_SIZE_RANGE
75#define GL_ALIASED_POINT_SIZE_RANGE 0x846D
76#endif
[f960f23]77
[87cab10]78using namespace std;
79
[bde8c9a]80const int BLOB_DIAMETER = 5;
[4ba80e0]81
[edfaf35]82#define BLOB_TEXTURE \
83            o, o, o, o, o, o, o, o,\
84            o, o, o, o, o, o, o, o,\
85            o, o, I, I, I, o, o, o,\
86            o, I, I, I, I, I, o, o,\
87            o, I, I, I, I, I, o, o,\
88            o, I, I, I, I, I, o, o,\
89            o, o, I, I, I, o, o, o,\
90            o, o, o, o, o, o, o, o
91
92#define CROSS_TEXTURE \
93            o, o, o, o, o, o, o, o,\
94            I, o, o, o, o, o, I, o,\
95            o, I, o, o, o, I, o, o,\
96            o, o, I, o, I, o, o, o,\
97            o, o, o, I, o, o, o, o,\
98            o, o, I, o, I, o, o, o,\
99            o, I, o, o, o, I, o, o,\
100            I, o, o, o, o, o, I, o
101
[0273042]102// Declared in gllogerror.h.
103bool opengl_initialised = false;
[4f3e5d1]104
[caa5fda]105static bool double_buffered = false;
106
107static const int* wx_gl_attribs = NULL;
108
109bool
110GLACanvas::check_visual()
111{
112    static const int wx_gl_attribs_full[] = {
113        WX_GL_DOUBLEBUFFER,
114        WX_GL_RGBA,
115        WX_GL_DEPTH_SIZE, 16,
116        0
117    };
118
119    // Use a double-buffered visual if available, as it will give much smoother
120    // animation.
121    double_buffered = true;
122    wx_gl_attribs = wx_gl_attribs_full;
123    if (!IsDisplaySupported(wx_gl_attribs)) {
124        ++wx_gl_attribs;
125        if (!IsDisplaySupported(wx_gl_attribs)) {
126            return false;
127        }
128        double_buffered = false;
129    }
130    return true;
131}
132
[5627cbb]133string GetGLSystemDescription()
[cc1a1d9]134{
[4f3e5d1]135    // If OpenGL isn't initialised we may get a SEGV from glGetString.
136    if (!opengl_initialised)
137        return "No OpenGL information available yet - try opening a file.";
[cc1a1d9]138    const char *p = (const char*)glGetString(GL_VERSION);
[4f3e5d1]139    if (!p)
140        return "Couldn't read OpenGL version!";
[cc1a1d9]141
[5627cbb]142    string info;
[cc1a1d9]143    info += "OpenGL ";
144    info += p;
145    info += '\n';
146    info += (const char*)glGetString(GL_VENDOR);
147    info += '\n';
148    info += (const char*)glGetString(GL_RENDERER);
[36b2cc3]149#if !(wxUSE_GLCANVAS_EGL-0) && \
150    (defined __WXGTK__ || defined __WXX11__ || defined __WXMOTIF__)
[c293aa9]151    info += string_format("\nGLX %0.1f\n", wxGLCanvas::GetGLXVersion() * 0.1);
[a6c5ffb]152#else
153    info += '\n';
[90688f5]154#endif
[cc1a1d9]155
156    GLint red, green, blue;
157    glGetIntegerv(GL_RED_BITS, &red);
158    glGetIntegerv(GL_GREEN_BITS, &green);
159    glGetIntegerv(GL_BLUE_BITS, &blue);
160    GLint max_texture_size;
161    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
162    GLint max_viewport[2];
163    glGetIntegerv(GL_MAX_VIEWPORT_DIMS, max_viewport);
164    GLdouble point_size_range[2];
[04bf822]165    glGetDoublev(GL_SMOOTH_POINT_SIZE_RANGE, point_size_range);
[cc1a1d9]166    GLdouble point_size_granularity;
[04bf822]167    glGetDoublev(GL_SMOOTH_POINT_SIZE_GRANULARITY, &point_size_granularity);
[5627cbb]168    info += string_format("R%dG%dB%d\n"
[cc1a1d9]169             "Max Texture size: %dx%d\n"
170             "Max Viewport size: %dx%d\n"
[04bf822]171             "Smooth Point Size %.3f-%.3f (granularity %.3f)",
[cc1a1d9]172             (int)red, (int)green, (int)blue,
173             (int)max_texture_size, (int)max_texture_size,
174             (int)max_viewport[0], (int)max_viewport[1],
175             point_size_range[0], point_size_range[1],
176             point_size_granularity);
[04bf822]177    glGetDoublev(GL_ALIASED_POINT_SIZE_RANGE, point_size_range);
178    if (glGetError() != GL_INVALID_ENUM) {
179        info += string_format("\nAliased point size %.3f-%.3f",
180                              point_size_range[0], point_size_range[1]);
181    }
[5627cbb]182
[c293aa9]183    info += "\nDouble buffered: ";
184    if (double_buffered)
185        info += "true";
186    else
187        info += "false";
188
[5627cbb]189    const GLubyte* gl_extensions = glGetString(GL_EXTENSIONS);
190    if (*gl_extensions) {
191        info += '\n';
192        info += (const char*)gl_extensions;
193    }
[cc1a1d9]194    return info;
195}
196
[fc5f476]197static bool
198glpoint_sprite_works()
199{
200    // Point sprites provide an easy, fast way for us to draw crosses by
201    // texture mapping GL points.
202    //
203    // If we have OpenGL >= 2.0 then we definitely have GL_POINT_SPRITE.
204    // Otherwise see if we have the GL_ARB_point_sprite or GL_NV_point_sprite
205    // extensions.
206    //
207    // The symbolic constants GL_POINT_SPRITE, GL_POINT_SPRITE_ARB, and
208    // GL_POINT_SPRITE_NV all give the same number so it doesn't matter
209    // which we use.
210    static bool glpoint_sprite = false;
211    static bool checked = false;
212    if (!checked) {
213        float maxSize = 0.0f;
214        glGetFloatv(GL_POINT_SIZE_MAX, &maxSize);
215        if (maxSize >= 8) {
216            glpoint_sprite = (atoi((const char *)glGetString(GL_VERSION)) >= 2);
217            if (!glpoint_sprite) {
218                const char * p = (const char *)glGetString(GL_EXTENSIONS);
219                while (true) {
220                    size_t l = 0;
221                    if (memcmp(p, "GL_ARB_point_sprite", 19) == 0) {
222                        l = 19;
223                    } else if (memcmp(p, "GL_NV_point_sprite", 18) == 0) {
224                        l = 18;
225                    }
226                    if (l) {
227                        p += l;
228                        if (*p == '\0' || *p == ' ') {
229                            glpoint_sprite = true;
230                            break;
231                        }
232                    }
233                    p = strchr(p + 1, ' ');
234                    if (!p) break;
235                    ++p;
236                }
237            }
238        }
239        checked = true;
240    }
241    return glpoint_sprite;
242}
243
[0273042]244void
[963e611]245log_gl_error(const wxChar * str, GLenum error_code)
246{
[0273042]247    wxString msg;
248    switch (error_code) {
249        case GL_INVALID_ENUM:
250            msg = "Invalid OpenGL enumerated value";
251            break;
252        case GL_INVALID_VALUE:
253            msg = "Invalid OpenGL numeric argument value";
254            break;
255        case GL_INVALID_OPERATION:
256            msg = "Invalid OpenGL operation";
257            break;
258        case GL_INVALID_FRAMEBUFFER_OPERATION:
259            msg = "Invalid OpenGL framebuffer operation";
260            break;
261        case GL_OUT_OF_MEMORY:
262            msg = wmsg(/*Out of memory*/389);
263            break;
264        case GL_STACK_UNDERFLOW:
265            msg = "OpenGL stack underflow";
266            break;
267        case GL_STACK_OVERFLOW:
268            msg = "OpenGL stack overflow";
269            break;
270        default:
271            msg.Format("Unknown OpenGL error code: %d", int(error_code));
272            break;
273    }
274    wxLogError(str, msg);
[963e611]275}
276
[56da40e]277//
278//  GLAPen
[096e56c]279//
[56da40e]280
281void GLAPen::SetColour(double red, double green, double blue)
282{
[aa048c3]283    components[0] = red;
284    components[1] = green;
285    components[2] = blue;
[56da40e]286}
287
[f7ea0e1]288double GLAPen::GetRed() const
[56da40e]289{
[aa048c3]290    return components[0];
[56da40e]291}
292
[f7ea0e1]293double GLAPen::GetGreen() const
[56da40e]294{
[aa048c3]295    return components[1];
[56da40e]296}
297
[f7ea0e1]298double GLAPen::GetBlue() const
[56da40e]299{
[aa048c3]300    return components[2];
[56da40e]301}
302
[f383708]303void GLAPen::Interpolate(const GLAPen& pen, double how_far)
304{
[588ff16]305    components[0] += how_far * (pen.GetRed() - components[0]);
306    components[1] += how_far * (pen.GetGreen() - components[1]);
307    components[2] += how_far * (pen.GetBlue() - components[2]);
[aa048c3]308}
309
310struct ColourTriple {
311    // RGB triple: values are from 0-255 inclusive for each component.
312    unsigned char r, g, b;
313};
314
[5558726]315// Order must match that in enum gla_colour[] in gla.h.
316static const ColourTriple COLOURS[] = {
[aa048c3]317    { 0, 0, 0 },       // black
318    { 100, 100, 100 }, // grey
319    { 180, 180, 180 }, // light grey
320    { 140, 140, 140 }, // light grey 2
321    { 90, 90, 90 },    // dark grey
322    { 255, 255, 255 }, // white
323    { 0, 100, 255},    // turquoise
324    { 0, 255, 40 },    // green
325    { 150, 205, 224 }, // indicator 1
326    { 114, 149, 160 }, // indicator 2
327    { 255, 255, 0 },   // yellow
328    { 255, 0, 0 },     // red
[f4c5932]329    { 40, 40, 255 },   // blue
[39b8117]330    { 255, 0, 255 },   // magenta
[aa048c3]331};
[f383708]332
[620c0c9]333bool GLAList::need_to_generate() {
334    // Bail out if the list is already cached, or can't usefully be cached.
335    if (flags & (GLACanvas::CACHED|GLACanvas::NEVER_CACHE))
336        return false;
337
338    // Create a new OpenGL list to hold this sequence of drawing
339    // operations.
340    if (gl_list == 0) {
341        gl_list = glGenLists(1);
342        CHECK_GL_ERROR("GLAList::need_to_generate", "glGenLists");
343#ifdef GLA_DEBUG
344        printf("glGenLists(1) returned %u\n", (unsigned)gl_list);
345#endif
346        if (gl_list == 0) {
347            // If we can't create a list for any reason, fall back to just
[b3f1bbe]348            // drawing directly, and flag the list as NEVER_CACHE as there's
349            // unlikely to be much point calling glGenLists() again.
350            flags = GLACanvas::NEVER_CACHE;
[620c0c9]351            return false;
352        }
353
354        // We should have 256 lists for font drawing and a dozen or so for 2D
355        // and 3D lists.  So something is amiss if we've generated 1000 lists,
356        // probably a infinite loop in the lazy list mechanism.
357        assert(gl_list < 1000);
358    }
[2c1c52e]359    // https://www.opengl.org/resources/faq/technical/displaylist.htm advises:
[620c0c9]360    //
361    // "Stay away from GL_COMPILE_AND_EXECUTE mode. Instead, create the
362    // list using GL_COMPILE mode, then execute it with glCallList()."
363    glNewList(gl_list, GL_COMPILE);
364    CHECK_GL_ERROR("GLAList::need_to_generate", "glNewList");
365    return true;
366}
367
368void GLAList::finalise(unsigned int list_flags)
369{
370    glEndList();
371    CHECK_GL_ERROR("GLAList::finalise", "glEndList");
372    if (list_flags & GLACanvas::NEVER_CACHE) {
373        glDeleteLists(gl_list, 1);
374        CHECK_GL_ERROR("GLAList::finalise", "glDeleteLists");
375        gl_list = 0;
376        flags = GLACanvas::NEVER_CACHE;
377    } else {
378        flags = list_flags | GLACanvas::CACHED;
379    }
380}
381
382bool GLAList::DrawList() const {
383    if ((flags & GLACanvas::CACHED) == 0)
384        return false;
[db59b02]385    glCallList(gl_list);
386    CHECK_GL_ERROR("GLAList::DrawList", "glCallList");
[620c0c9]387    return true;
[db59b02]388}
389
[56da40e]390//
391//  GLACanvas
[096e56c]392//
[56da40e]393
[9071cf5]394BEGIN_EVENT_TABLE(GLACanvas, wxGLCanvas)
395    EVT_SIZE(GLACanvas::OnSize)
[fd0e32a]396#ifdef wxHAS_DPI_INDEPENDENT_PIXELS
[2dacc8bd]397    EVT_MOVE(GLACanvas::OnMove)
398#endif
[9071cf5]399END_EVENT_TABLE()
400
[84f1ed1]401// Pass wxWANTS_CHARS so that the window gets cursor keys on MS Windows.
402GLACanvas::GLACanvas(wxWindow* parent, int id)
[caa5fda]403    : wxGLCanvas(parent, id, wx_gl_attribs, wxDefaultPosition,
[cbd9829]404                 wxDefaultSize, wxWANTS_CHARS),
[a3f83057]405      ctx(this)
[56da40e]406{
407}
408
409GLACanvas::~GLACanvas()
410{
411    // Destructor.
412
[1b12b82]413    if (m_Quadric) {
[b49ac56]414        gluDeleteQuadric(m_Quadric);
415        CHECK_GL_ERROR("~GLACanvas", "gluDeleteQuadric");
[1b12b82]416    }
417}
418
419void GLACanvas::FirstShow()
420{
[5f8464d]421    UpdateSize();
[90430f2]422
[8c048fa]423    ctx.SetCurrent(*this);
[4f3e5d1]424    opengl_initialised = true;
[19b2f99]425
426    // Set the background colour of the canvas to black.
427    glClearColor(0.0, 0.0, 0.0, 1.0);
428    CHECK_GL_ERROR("FirstShow", "glClearColor");
429
430    // Set viewport.
431    glViewport(0, 0, x_size, y_size);
432    CHECK_GL_ERROR("FirstShow", "glViewport");
433
[807f9dd]434    save_hints = false;
[5846a74]435
[807f9dd]436    vendor = wxString((const char *)glGetString(GL_VENDOR), wxConvUTF8);
437    renderer = wxString((const char *)glGetString(GL_RENDERER), wxConvUTF8);
[fe075d7]438    {
[5846a74]439        wxConfigBase * cfg = wxConfigBase::Get();
[fe075d7]440        wxString s;
[5846a74]441        if (cfg->Read(wxT("opengl_survex"), &s, wxString()) && s == wxT(VERSION) &&
442            cfg->Read(wxT("opengl_vendor"), &s, wxString()) && s == vendor &&
[fe075d7]443            cfg->Read(wxT("opengl_renderer"), &s, wxString()) && s == renderer) {
[5846a74]444            // The survex version, vendor and renderer are the same as those
445            // we cached hints for, so use those hints.
[fe075d7]446            int v;
447            if (cfg->Read(wxT("blob_method"), &v, 0) &&
[1ecdc29]448                (v == SPRITE || v == POINT || v == LINES)) {
[fe075d7]449                // How to draw blobs.
450                blob_method = v;
451            }
452            if (cfg->Read(wxT("cross_method"), &v, 0) &&
453                (v == SPRITE || v == LINES)) {
454                // How to draw crosses.
455                cross_method = v;
456            }
457        }
458    }
[db59b02]459
[4f3e5d1]460    if (m_Quadric) return;
461    // One time initialisation follows.
[657402d]462
[1b12b82]463    m_Quadric = gluNewQuadric();
[bb1f293]464    CHECK_GL_ERROR("FirstShow", "gluNewQuadric");
[1b12b82]465    if (!m_Quadric) {
466        abort(); // FIXME need to cope somehow
467    }
[096e56c]468
[203d2a7]469    glShadeModel(GL_FLAT);
[dde4fe7]470    CHECK_GL_ERROR("FirstShow", "glShadeModel");
[d67450e]471    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // So text works.
[dde4fe7]472    CHECK_GL_ERROR("FirstShow", "glPolygonMode");
[e633bb1]473    //glAlphaFunc(GL_GREATER, 0.5f);
474    //CHECK_GL_ERROR("FirstShow", "glAlphaFunc");
[096e56c]475
[c8f449c3]476    // We want glReadPixels() to read from the front buffer (which is the
477    // default for single-buffered displays).
478    if (double_buffered) {
479        glReadBuffer(GL_FRONT);
480        CHECK_GL_ERROR("FirstShow", "glReadBuffer");
481    }
482
[c60062d]483    // Grey fog effect.
484    GLfloat fogcolour[4] = { 0.5, 0.5, 0.5, 1.0 };
485    glFogfv(GL_FOG_COLOR, fogcolour);
[9baa53a]486    CHECK_GL_ERROR("FirstShow", "glFogfv");
[c60062d]487
488    // Linear fogging.
489    glFogi(GL_FOG_MODE, GL_LINEAR);
[9baa53a]490    CHECK_GL_ERROR("FirstShow", "glFogi");
[c60062d]491
492    // Optimise for speed (compute fog per vertex).
493    glHint(GL_FOG_HINT, GL_FASTEST);
[9baa53a]494    CHECK_GL_ERROR("FirstShow", "glHint");
[1eeb55a]495
[a517825]496    // No padding on pixel packing and unpacking (default is to pad each
497    // line to a multiple of 4 bytes).
498    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // For setting texture maps.
[028829f]499    CHECK_GL_ERROR("FirstShow", "glPixelStorei GL_UNPACK_ALIGNMENT");
[a517825]500    glPixelStorei(GL_PACK_ALIGNMENT, 1); // For screengrabs and movies.
[028829f]501    CHECK_GL_ERROR("FirstShow", "glPixelStorei GL_PACK_ALIGNMENT");
502
[1eeb55a]503    // Load font
[8a05a7a]504    wxString path = wmsg_cfgpth();
[bd551c0]505    path += wxCONFIG_PATH_SEPARATOR;
[1aa3fb7]506    path += wxT("unifont.pixelfont");
[eadf5fd]507    if (!m_Font.load(path, dpi_scale_factor >= 2)) {
[1aa3fb7]508        // FIXME: do something better.
[17c483d]509        // We have this message available: Error in format of font file “%s”
[df3e1a0c]510        fprintf(stderr, "Failed to parse compiled-in font data\n");
[1aa3fb7]511        exit(1);
512    }
[4ba80e0]513
[fe075d7]514    if (blob_method == UNKNOWN) {
515        // Check if we can use GL_POINTS to plot blobs at stations.
516        GLdouble point_size_range[2];
517        glGetDoublev(GL_SMOOTH_POINT_SIZE_RANGE, point_size_range);
518        CHECK_GL_ERROR("FirstShow", "glGetDoublev GL_SMOOTH_POINT_SIZE_RANGE");
519        if (point_size_range[0] <= BLOB_DIAMETER &&
520            point_size_range[1] >= BLOB_DIAMETER) {
521            blob_method = POINT;
522        } else {
[cab6f11]523            blob_method = glpoint_sprite_works() ? SPRITE : LINES;
[fe075d7]524        }
525        save_hints = true;
526    }
527
528    if (blob_method == POINT) {
[4ba80e0]529        glPointSize(BLOB_DIAMETER);
530        CHECK_GL_ERROR("FirstShow", "glPointSize");
531    }
[95ce35f]532
[fe075d7]533    if (cross_method == UNKNOWN) {
[fc5f476]534        cross_method = glpoint_sprite_works() ? SPRITE : LINES;
[fe075d7]535        save_hints = true;
[95ce35f]536    }
[185d793]537
[fe075d7]538    if (cross_method == SPRITE) {
[95ce35f]539        glGenTextures(1, &m_CrossTexture);
[9baa53a]540        CHECK_GL_ERROR("FirstShow", "glGenTextures");
[95ce35f]541        glBindTexture(GL_TEXTURE_2D, m_CrossTexture);
[9baa53a]542        CHECK_GL_ERROR("FirstShow", "glBindTexture");
[fe075d7]543        // Cross image for drawing crosses using texture mapped point sprites.
[95ce35f]544        const unsigned char crossteximage[128] = {
[edfaf35]545#define o 0,0
546#define I 255,255
547            CROSS_TEXTURE
548#undef o
549#undef I
[95ce35f]550        };
551        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
[9baa53a]552        CHECK_GL_ERROR("FirstShow", "glPixelStorei");
[95ce35f]553        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
[9baa53a]554        CHECK_GL_ERROR("FirstShow", "glTexEnvi");
[95ce35f]555        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
[9baa53a]556        CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_S");
[95ce35f]557        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
[9baa53a]558        CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_T");
[95ce35f]559        glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, 8, 8, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, (GLvoid *)crossteximage);
[9baa53a]560        CHECK_GL_ERROR("FirstShow", "glTexImage2D");
[95ce35f]561        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
[9baa53a]562        CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MAG_FILTER");
[95ce35f]563        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
[9baa53a]564        CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MIN_FILTER");
[95ce35f]565    }
[cab6f11]566
567    if (blob_method == SPRITE) {
568        glGenTextures(1, &m_BlobTexture);
569        CHECK_GL_ERROR("FirstShow", "glGenTextures");
570        glBindTexture(GL_TEXTURE_2D, m_BlobTexture);
571        CHECK_GL_ERROR("FirstShow", "glBindTexture");
572        // Image for drawing blobs using texture mapped point sprites.
573        const unsigned char blobteximage[128] = {
574#define o 0,0
575#define I 255,255
576            BLOB_TEXTURE
577#undef o
578#undef I
579        };
580        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
581        CHECK_GL_ERROR("FirstShow", "glPixelStorei");
582        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
583        CHECK_GL_ERROR("FirstShow", "glTexEnvi");
584        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
585        CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_S");
586        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
587        CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_T");
588        glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, 8, 8, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, (GLvoid *)blobteximage);
589        CHECK_GL_ERROR("FirstShow", "glTexImage2D");
590        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
591        CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MAG_FILTER");
592        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
593        CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MIN_FILTER");
594    }
[ca8c864]595}
596
[56da40e]597void GLACanvas::Clear()
598{
599    // Clear the canvas.
[d727368]600
[56da40e]601    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[1897247]602    CHECK_GL_ERROR("Clear", "glClear");
[56da40e]603}
604
[8a7f1c5]605void GLACanvas::ClearNative()
606{
607    // Clear the canvas to the native background colour.
608
609    wxColour background_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWFRAME);
610    glClearColor(background_colour.Red() / 255.,
611                 background_colour.Green() / 255.,
612                 background_colour.Blue() / 255.,
613                 1.0);
614    CHECK_GL_ERROR("ClearNative", "glClearColor");
615    glClear(GL_COLOR_BUFFER_BIT);
616    CHECK_GL_ERROR("ClearNative", "glClear");
617    glClearColor(0.0, 0.0, 0.0, 1.0);
618    CHECK_GL_ERROR("ClearNative", "glClearColor (2)");
619}
620
[147847c]621void GLACanvas::SetScale(double scale)
[56da40e]622{
[db59b02]623    if (scale != m_Scale) {
[229b9cf]624        for (auto & i : drawing_lists) {
625            i.invalidate_if(INVALIDATE_ON_SCALE);
[db59b02]626        }
627
628        m_Scale = scale;
629    }
[56da40e]630}
631
[2dacc8bd]632void GLACanvas::OnMove(wxMoveEvent & event)
633{
[5f8464d]634    UpdateSize();
[2dacc8bd]635    event.Skip();
636}
637
[5f8464d]638void GLACanvas::UpdateSize() {
639    // Update our record of the DPI and client area size, and invalidate any
640    // cached lists which depend on anything that has changed.
641    unsigned int mask = 0;
[705adee9]642
[5f8464d]643    double new_dpi_scale_factor = wxGLCanvas::GetDPIScaleFactor();
644    if (dpi_scale_factor != new_dpi_scale_factor) {
645        dpi_scale_factor = new_dpi_scale_factor;
646        mask |= INVALIDATE_ON_HIDPI;
647    }
[90430f2]648
[5f8464d]649    int new_w, new_h;
650    GetClientSize(&new_w, &new_h);
[2ef89f9]651#ifdef wxHAS_DPI_INDEPENDENT_PIXELS
652    auto content_scale_factor = GetContentScaleFactor();
653    new_w *= content_scale_factor;
654    new_h *= content_scale_factor;
655#endif
[705adee9]656    // The width and height go to zero when the panel is dragged right
657    // across so we clamp them to be at least 1 to avoid problems.
658    if (new_w < 1) new_w = 1;
659    if (new_h < 1) new_h = 1;
[5f8464d]660    if (x_size != new_w) {
[705adee9]661        x_size = new_w;
[5f8464d]662        mask |= INVALIDATE_ON_X_RESIZE;
663    }
664    if (y_size != new_h) {
[705adee9]665        y_size = new_h;
[5f8464d]666        mask |= INVALIDATE_ON_Y_RESIZE;
667    }
668    if (mask) {
[229b9cf]669        for (auto& i : drawing_lists) {
670            i.invalidate_if(mask);
[90430f2]671        }
672    }
[5f8464d]673}
674
675void GLACanvas::OnSize(wxSizeEvent & event)
676{
677    UpdateSize();
[90430f2]678
[9071cf5]679    event.Skip();
[6cf4daa]680
[19b2f99]681    if (!opengl_initialised) return;
682
[6cf4daa]683    // Set viewport.
684    glViewport(0, 0, x_size, y_size);
[92678b5]685    CHECK_GL_ERROR("OnSize", "glViewport");
[90430f2]686}
687
[56da40e]688void GLACanvas::AddTranslationScreenCoordinates(int dx, int dy)
689{
690    // Translate the data by a given amount, specified in screen coordinates.
[096e56c]691
[56da40e]692    // Find out how far the translation takes us in data coordinates.
693    SetDataTransform();
694
[f6d8375]695    double x0, y0, z0;
696    double x, y, z;
[1b12b82]697    gluUnProject(0.0, 0.0, 0.0, modelview_matrix, projection_matrix, viewport,
[b49ac56]698                 &x0, &y0, &z0);
[bb1f293]699    CHECK_GL_ERROR("AddTranslationScreenCoordinates", "gluUnProject");
[1b12b82]700    gluUnProject(dx, -dy, 0.0, modelview_matrix, projection_matrix, viewport,
[b49ac56]701                 &x, &y, &z);
[bb1f293]702    CHECK_GL_ERROR("AddTranslationScreenCoordinates", "gluUnProject (2)");
[56da40e]703
704    // Apply the translation.
[d67450e]705    AddTranslation(Vector3(x - x0, y - y0, z - z0));
[56da40e]706}
707
[c5fc8eb]708void GLACanvas::SetVolumeDiameter(glaCoord diameter)
[56da40e]709{
[c5fc8eb]710    // Set the size of the data drawing volume by giving the diameter of the
711    // smallest sphere containing it.
[56da40e]712
[f6d8375]713    m_VolumeDiameter = max(glaCoord(1.0), diameter);
[56da40e]714}
715
716void GLACanvas::StartDrawing()
717{
718    // Prepare for a redraw operation.
[096e56c]719
[8c048fa]720    ctx.SetCurrent(*this);
[12ec820]721    glDepthMask(GL_TRUE);
[807f9dd]722
723    if (!save_hints) return;
724
725    // We want to check on the second redraw.
726    static int draw_count = 2;
727    if (--draw_count != 0) return;
728
729    if (cross_method != LINES) {
730        SetColour(col_WHITE);
731        Clear();
732        SetDataTransform();
733        BeginCrosses();
734        DrawCross(-m_Translation.GetX(), -m_Translation.GetY(), -m_Translation.GetZ());
735        EndCrosses();
736        static const unsigned char expected_cross[64 * 3] = {
737#define o 0,0,0
738#define I 255,255,255
[edfaf35]739            CROSS_TEXTURE
[dd64a45]740#undef o
741#undef I
[807f9dd]742        };
743        if (!CheckVisualFidelity(expected_cross)) {
744            cross_method = LINES;
745            save_hints = true;
746        }
747    }
748
749    if (blob_method != LINES) {
750        SetColour(col_WHITE);
751        Clear();
752        SetDataTransform();
753        BeginBlobs();
754        DrawBlob(-m_Translation.GetX(), -m_Translation.GetY(), -m_Translation.GetZ());
755        EndBlobs();
756        static const unsigned char expected_blob[64 * 3] = {
757#define o 0,0,0
758#define I 255,255,255
[edfaf35]759            BLOB_TEXTURE
[dd64a45]760#undef o
761#undef I
[807f9dd]762        };
763        if (!CheckVisualFidelity(expected_blob)) {
764            blob_method = LINES;
765            save_hints = true;
766        }
767    }
768
769    wxConfigBase * cfg = wxConfigBase::Get();
[5846a74]770    cfg->Write(wxT("opengl_survex"), wxT(VERSION));
[807f9dd]771    cfg->Write(wxT("opengl_vendor"), vendor);
772    cfg->Write(wxT("opengl_renderer"), renderer);
773    cfg->Write(wxT("blob_method"), blob_method);
774    cfg->Write(wxT("cross_method"), cross_method);
775    cfg->Flush();
776    save_hints = false;
[56da40e]777}
778
[d67450e]779void GLACanvas::EnableSmoothPolygons(bool filled)
[1b12b82]780{
781    // Prepare for drawing smoothly-shaded polygons.
782    // Only use this when required (in particular lines in lists may not be
783    // coloured correctly when this is enabled).
[096e56c]784
[d67450e]785    glPushAttrib(GL_ENABLE_BIT|GL_LIGHTING_BIT|GL_POLYGON_BIT);
786    if (filled) {
787        glShadeModel(GL_SMOOTH);
788        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
789    } else {
790        glDisable(GL_LINE_SMOOTH);
791        glDisable(GL_TEXTURE_2D);
792        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
793    }
794    CHECK_GL_ERROR("EnableSmoothPolygons", "glPolygonMode");
795
796    if (filled && m_SmoothShading) {
797        static const GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 1.0 };
798        static const GLfloat light_position[] = { -1.0, -1.0, -1.0, 0.0 };
799        static const GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
800        static const GLfloat light_diffuse[] = { 0.7, 0.7, 0.7, 1.0 };
801        glEnable(GL_COLOR_MATERIAL);
802        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
803        glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 10.0);
804        glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
805        glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
806        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
807        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
808        glEnable(GL_LIGHTING);
809        glEnable(GL_LIGHT0);
810    }
[1b12b82]811}
812
813void GLACanvas::DisableSmoothPolygons()
814{
[d67450e]815    glPopAttrib();
[203d2a7]816}
817
[d67450e]818void GLACanvas::PlaceNormal(const Vector3 &v)
[203d2a7]819{
820    // Add a normal (for polygons etc.)
821
[d67450e]822    glNormal3d(v.GetX(), v.GetY(), v.GetZ());
[1b12b82]823}
824
[56da40e]825void GLACanvas::SetDataTransform()
826{
[de24f93]827    // Set projection.
828    glMatrixMode(GL_PROJECTION);
829    CHECK_GL_ERROR("SetDataTransform", "glMatrixMode");
830    glLoadIdentity();
831    CHECK_GL_ERROR("SetDataTransform", "glLoadIdentity");
832
[6cf4daa]833    double aspect = double(y_size) / double(x_size);
834
[147847c]835    GLdouble near_plane = 1.0;
[de24f93]836    if (m_Perspective) {
[147847c]837        GLdouble lr = near_plane * tan(rad(25.0));
838        GLdouble far_plane = m_VolumeDiameter * 5 + near_plane; // FIXME: work out properly
839        GLdouble tb = lr * aspect;
[de24f93]840        glFrustum(-lr, lr, -tb, tb, near_plane, far_plane);
841        CHECK_GL_ERROR("SetViewportAndProjection", "glFrustum");
842    } else {
[5092ee7]843        near_plane = 0.0;
[d877aa2]844        assert(m_Scale != 0.0);
[147847c]845        GLdouble lr = m_VolumeDiameter / m_Scale * 0.5;
846        GLdouble far_plane = m_VolumeDiameter + near_plane;
847        GLdouble tb = lr;
[ea35995]848        if (aspect >= 1.0) {
849            tb *= aspect;
850        } else {
851            lr /= aspect;
852        }
[de24f93]853        glOrtho(-lr, lr, -tb, tb, near_plane, far_plane);
854        CHECK_GL_ERROR("SetViewportAndProjection", "glOrtho");
855    }
856
[56da40e]857    // Set the modelview transform for drawing data.
858    glMatrixMode(GL_MODELVIEW);
[1897247]859    CHECK_GL_ERROR("SetDataTransform", "glMatrixMode");
[56da40e]860    glLoadIdentity();
[1897247]861    CHECK_GL_ERROR("SetDataTransform", "glLoadIdentity");
[d877aa2]862    if (m_Perspective) {
863        glTranslated(0.0, 0.0, -near_plane);
864    } else {
865        glTranslated(0.0, 0.0, -0.5 * m_VolumeDiameter);
866    }
[1bbd9a8]867    CHECK_GL_ERROR("SetDataTransform", "glTranslated");
[2491b8c]868    // Get axes the correct way around (z upwards, y into screen)
869    glRotated(-90.0, 1.0, 0.0, 0.0);
870    CHECK_GL_ERROR("SetDataTransform", "glRotated");
[7a57dc7]871    glRotated(-m_Tilt, 1.0, 0.0, 0.0);
[08253d9]872    CHECK_GL_ERROR("SetDataTransform", "glRotated");
873    glRotated(m_Pan, 0.0, 0.0, 1.0);
[9284d37]874    CHECK_GL_ERROR("SetDataTransform", "glRotated");
[5092ee7]875    if (m_Perspective) {
[d67450e]876        glTranslated(m_Translation.GetX(),
877                     m_Translation.GetY(),
878                     m_Translation.GetZ());
[5092ee7]879        CHECK_GL_ERROR("SetDataTransform", "glTranslated");
880    }
[9284d37]881    if (z_stretch != 1.0) {
882        glScaled(1.0, 1.0, z_stretch);
883        CHECK_GL_ERROR("SetDataTransform", "glScaled");
884    }
[dde4fe7]885
[de24f93]886    // Save projection matrix.
887    glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix);
888    CHECK_GL_ERROR("SetDataTransform", "glGetDoublev");
889
890    // Save viewport coordinates.
891    glGetIntegerv(GL_VIEWPORT, viewport);
892    CHECK_GL_ERROR("SetDataTransform", "glGetIntegerv");
893
894    // Save modelview matrix.
[dde4fe7]895    glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix);
896    CHECK_GL_ERROR("SetDataTransform", "glGetDoublev");
[de24f93]897
[5092ee7]898    if (!m_Perspective) {
899        // Adjust the translation so we don't change the Z position of the model
[f6d8375]900        double X, Y, Z;
[d67450e]901        gluProject(m_Translation.GetX(),
902                   m_Translation.GetY(),
903                   m_Translation.GetZ(),
[5092ee7]904                   modelview_matrix, projection_matrix, viewport,
905                   &X, &Y, &Z);
[f6d8375]906        double Tx, Ty, Tz;
[5092ee7]907        gluUnProject(X, Y, 0.5, modelview_matrix, projection_matrix, viewport,
908                     &Tx, &Ty, &Tz);
909        glTranslated(Tx, Ty, Tz);
910        CHECK_GL_ERROR("SetDataTransform", "glTranslated");
911        glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix);
912    }
[096e56c]913
[de24f93]914    glEnable(GL_DEPTH_TEST);
915    CHECK_GL_ERROR("SetDataTransform", "glEnable GL_DEPTH_TEST");
[c60062d]916
[a517825]917    if (m_Textured) {
918        glBindTexture(GL_TEXTURE_2D, m_Texture);
919        glEnable(GL_TEXTURE_2D);
920        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
921        CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_WRAP_S");
922        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
923        CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_WRAP_T");
924        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
925        CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_MAG_FILTER");
[ecbdd96]926        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
927                        GL_LINEAR_MIPMAP_LINEAR);
[a517825]928        CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_MIN_FILTER");
929        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
930    } else {
931        glDisable(GL_TEXTURE_2D);
932    }
[c60062d]933    if (m_Fog) {
934        glFogf(GL_FOG_START, near_plane);
935        glFogf(GL_FOG_END, near_plane + m_VolumeDiameter);
936        glEnable(GL_FOG);
[db452ae]937    } else {
938        glDisable(GL_FOG);
939    }
940
[4a1cede]941    glEnable(GL_BLEND);
942    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
[db452ae]943    if (m_AntiAlias) {
944        glEnable(GL_LINE_SMOOTH);
945    } else {
946        glDisable(GL_LINE_SMOOTH);
[c60062d]947    }
[56da40e]948}
949
950void GLACanvas::SetIndicatorTransform()
951{
[db59b02]952    list_flags |= NEVER_CACHE;
953
[bb1f293]954    // Set the modelview transform and projection for drawing indicators.
[56da40e]955
[bb1f293]956    glDisable(GL_DEPTH_TEST);
957    CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_DEPTH_TEST");
[c60062d]958    glDisable(GL_FOG);
959    CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_FOG");
[bb1f293]960
[78924eb]961    // Just a simple 2D projection.
[bb1f293]962    glMatrixMode(GL_PROJECTION);
963    CHECK_GL_ERROR("SetIndicatorTransform", "glMatrixMode");
964    glLoadIdentity();
965    CHECK_GL_ERROR("SetIndicatorTransform", "glLoadIdentity (2)");
[6cf4daa]966    gluOrtho2D(0, x_size, 0, y_size);
[bb1f293]967    CHECK_GL_ERROR("SetIndicatorTransform", "gluOrtho2D");
[a517825]968
[78924eb]969    // No modelview transform.
970    glMatrixMode(GL_MODELVIEW);
971    CHECK_GL_ERROR("SetIndicatorTransform", "glMatrixMode");
972    glLoadIdentity();
973    CHECK_GL_ERROR("SetIndicatorTransform", "glLoadIdentity");
974
[a517825]975    glDisable(GL_TEXTURE_2D);
[6adffadf]976    CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_TEXTURE_2D");
[e4d40792]977    glDisable(GL_BLEND);
[6adffadf]978    CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_BLEND");
[36c3285]979    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
[9baa53a]980    CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_WRAP_S");
[36c3285]981    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
[9baa53a]982    CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_WRAP_T");
[36c3285]983    glAlphaFunc(GL_GREATER, 0.5f);
[9baa53a]984    CHECK_GL_ERROR("SetIndicatorTransform", "glAlphaFunc");
[096e56c]985    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
[9baa53a]986    CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_MAG_FILTER");
[36c3285]987    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
[9baa53a]988    CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_MIN_FILTER");
[36c3285]989    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
[9baa53a]990    CHECK_GL_ERROR("SetIndicatorTransform", "glHint");
[56da40e]991}
992
993void GLACanvas::FinishDrawing()
994{
995    // Complete a redraw operation.
[096e56c]996
[c293aa9]997    if (double_buffered) {
998        SwapBuffers();
999    } else {
1000        glFlush();
1001        CHECK_GL_ERROR("FinishDrawing", "glFlush");
1002    }
[56da40e]1003}
1004
[d2fcc9b]1005void GLACanvas::DrawList(unsigned int l)
[56da40e]1006{
[4ba80e0]1007    // FIXME: uncomment to disable use of lists for debugging:
1008    // GenerateList(l); return;
[d2fcc9b]1009    if (l >= drawing_lists.size()) drawing_lists.resize(l + 1);
[bae6a7c]1010
[d2fcc9b]1011    // We generate the OpenGL lists lazily to minimise delays on startup.
1012    // So check if we need to generate the OpenGL list now.
[620c0c9]1013    if (drawing_lists[l].need_to_generate()) {
[db59b02]1014        // Clear list_flags so that we can note what conditions to invalidate
1015        // the cached OpenGL list on.
1016        list_flags = 0;
[620c0c9]1017
1018#ifdef GLA_DEBUG
1019        printf("generating list #%u... ", l);
1020        m_Vertices = 0;
1021#endif
[d2fcc9b]1022        GenerateList(l);
[edb6576]1023#ifdef GLA_DEBUG
[bae6a7c]1024        printf("done (%d vertices)\n", m_Vertices);
[edb6576]1025#endif
[620c0c9]1026        drawing_lists[l].finalise(list_flags);
[bae6a7c]1027    }
[ee78822]1028
[620c0c9]1029    if (!drawing_lists[l].DrawList()) {
1030        // That list isn't cached (which means it probably can't usefully be
1031        // cached).
[db59b02]1032        GenerateList(l);
1033    }
[56da40e]1034}
1035
[11fe902]1036void GLACanvas::DrawListZPrepass(unsigned int l)
1037{
1038    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1039    DrawList(l);
1040    glDepthMask(GL_FALSE);
1041    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1042    glDepthFunc(GL_EQUAL);
1043    DrawList(l);
1044    glDepthMask(GL_TRUE);
1045    glDepthFunc(GL_LESS);
1046}
1047
[147847c]1048void GLACanvas::DrawList2D(unsigned int l, glaCoord x, glaCoord y, double rotation)
[76dd228]1049{
1050    glMatrixMode(GL_PROJECTION);
1051    CHECK_GL_ERROR("DrawList2D", "glMatrixMode");
1052    glPushMatrix();
1053    CHECK_GL_ERROR("DrawList2D", "glPushMatrix");
1054    glTranslated(x, y, 0);
1055    CHECK_GL_ERROR("DrawList2D", "glTranslated");
1056    if (rotation != 0.0) {
1057        glRotated(rotation, 0, 0, -1);
1058        CHECK_GL_ERROR("DrawList2D", "glRotated");
1059    }
1060    DrawList(l);
1061    glMatrixMode(GL_PROJECTION);
1062    CHECK_GL_ERROR("DrawList2D", "glMatrixMode 2");
1063    glPopMatrix();
1064    CHECK_GL_ERROR("DrawList2D", "glPopMatrix");
1065}
1066
[aa048c3]1067void GLACanvas::SetColour(const GLAPen& pen, double rgb_scale)
[56da40e]1068{
1069    // Set the colour for subsequent operations.
[4a1cede]1070    glColor4f(pen.GetRed() * rgb_scale, pen.GetGreen() * rgb_scale,
1071              pen.GetBlue() * rgb_scale, alpha);
[56da40e]1072}
1073
[aa048c3]1074void GLACanvas::SetColour(const GLAPen& pen)
[f383708]1075{
[aa048c3]1076    // Set the colour for subsequent operations.
[4a1cede]1077    glColor4d(pen.components[0], pen.components[1], pen.components[2], alpha);
[f383708]1078}
1079
[d1ce9bd]1080void GLACanvas::SetColour(gla_colour colour, double rgb_scale)
1081{
1082    // Set the colour for subsequent operations.
1083    rgb_scale /= 255.0;
1084    glColor4f(COLOURS[colour].r * rgb_scale,
1085              COLOURS[colour].g * rgb_scale,
1086              COLOURS[colour].b * rgb_scale,
1087              alpha);
1088}
1089
[aa048c3]1090void GLACanvas::SetColour(gla_colour colour)
[56da40e]1091{
[aa048c3]1092    // Set the colour for subsequent operations.
[4943ae8]1093    if (alpha == 1.0) {
1094        glColor3ubv(&COLOURS[colour].r);
1095    } else {
1096        glColor4ub(COLOURS[colour].r,
1097                   COLOURS[colour].g,
1098                   COLOURS[colour].b,
1099                   (unsigned char)(255 * alpha));
1100    }
[56da40e]1101}
1102
1103void GLACanvas::DrawText(glaCoord x, glaCoord y, glaCoord z, const wxString& str)
1104{
1105    // Draw a text string on the current buffer in the current font.
1106    glRasterPos3d(x, y, z);
[1897247]1107    CHECK_GL_ERROR("DrawText", "glRasterPos3d");
[1aa3fb7]1108    m_Font.write_string(str.data(), str.size());
[56da40e]1109}
1110
[1eeb55a]1111void GLACanvas::DrawIndicatorText(int x, int y, const wxString& str)
[56da40e]1112{
[1aa3fb7]1113    glRasterPos2d(x, y);
1114    CHECK_GL_ERROR("DrawIndicatorText", "glRasterPos2d");
1115    m_Font.write_string(str.data(), str.size());
[56da40e]1116}
1117
[dbd50e2]1118void GLACanvas::GetTextExtent(const wxString& str, int * x_ext, int * y_ext) const
[56da40e]1119{
[1aa3fb7]1120    m_Font.get_text_extent(str.data(), str.size(), x_ext, y_ext);
[56da40e]1121}
1122
1123void GLACanvas::BeginQuadrilaterals()
1124{
1125    // Commence drawing of quadrilaterals.
[096e56c]1126
[56da40e]1127    glBegin(GL_QUADS);
1128}
1129
1130void GLACanvas::EndQuadrilaterals()
1131{
1132    // Finish drawing of quadrilaterals.
[096e56c]1133
[56da40e]1134    glEnd();
[9baa53a]1135    CHECK_GL_ERROR("EndQuadrilaterals", "glEnd GL_QUADS");
[56da40e]1136}
1137
1138void GLACanvas::BeginLines()
1139{
1140    // Commence drawing of a set of lines.
1141
1142    glBegin(GL_LINES);
1143}
1144
1145void GLACanvas::EndLines()
1146{
1147    // Finish drawing of a set of lines.
1148
1149    glEnd();
[9baa53a]1150    CHECK_GL_ERROR("EndLines", "glEnd GL_LINES");
[56da40e]1151}
1152
1153void GLACanvas::BeginTriangles()
1154{
1155    // Commence drawing of a set of triangles.
1156
1157    glBegin(GL_TRIANGLES);
1158}
1159
1160void GLACanvas::EndTriangles()
1161{
1162    // Finish drawing of a set of triangles.
1163
1164    glEnd();
[9baa53a]1165    CHECK_GL_ERROR("EndTriangles", "glEnd GL_TRIANGLES");
[56da40e]1166}
1167
[dde4fe7]1168void GLACanvas::BeginTriangleStrip()
1169{
1170    // Commence drawing of a triangle strip.
1171
1172    glBegin(GL_TRIANGLE_STRIP);
1173}
1174
1175void GLACanvas::EndTriangleStrip()
1176{
1177    // Finish drawing of a triangle strip.
1178
1179    glEnd();
[9baa53a]1180    CHECK_GL_ERROR("EndTriangleStrip", "glEnd GL_TRIANGLE_STRIP");
[dde4fe7]1181}
1182
[56da40e]1183void GLACanvas::BeginPolyline()
1184{
1185    // Commence drawing of a polyline.
1186
1187    glBegin(GL_LINE_STRIP);
1188}
1189
1190void GLACanvas::EndPolyline()
1191{
1192    // Finish drawing of a polyline.
[096e56c]1193
[56da40e]1194    glEnd();
[9baa53a]1195    CHECK_GL_ERROR("EndPolyline", "glEnd GL_LINE_STRIP");
[56da40e]1196}
1197
[0642381]1198void GLACanvas::BeginPolyloop()
1199{
1200    // Commence drawing of a polyloop.
1201
1202    glBegin(GL_LINE_LOOP);
1203}
1204
1205void GLACanvas::EndPolyloop()
1206{
1207    // Finish drawing of a polyloop.
1208
1209    glEnd();
1210    CHECK_GL_ERROR("EndPolyloop", "glEnd GL_LINE_LOOP");
1211}
1212
[45aa1d6]1213void GLACanvas::BeginPolygon()
1214{
1215    // Commence drawing of a polygon.
1216
1217    glBegin(GL_POLYGON);
1218}
1219
1220void GLACanvas::EndPolygon()
1221{
1222    // Finish drawing of a polygon.
[096e56c]1223
[45aa1d6]1224    glEnd();
[9baa53a]1225    CHECK_GL_ERROR("EndPolygon", "glEnd GL_POLYGON");
[45aa1d6]1226}
1227
[51eab3b]1228void GLACanvas::BeginPoints()
1229{
1230    // Commence drawing points.
1231
1232    glPushAttrib(GL_POINT_BIT);
1233    CHECK_GL_ERROR("BeginPoints", "glPushAttrib");
1234    glPointSize(3);
1235    CHECK_GL_ERROR("BeginPoints", "glPointSize");
1236    glBegin(GL_POINTS);
1237}
1238
1239void GLACanvas::EndPoints()
1240{
1241    // Finish drawing points.
1242
1243    glEnd();
1244    CHECK_GL_ERROR("EndPoints", "glEnd GL_POINTS");
1245    glPopAttrib();
1246    CHECK_GL_ERROR("EndPoints", "glPopAttrib");
1247}
1248
[56da40e]1249void GLACanvas::PlaceVertex(glaCoord x, glaCoord y, glaCoord z)
1250{
1251    // Place a vertex for the current object being drawn.
1252
[edb6576]1253#ifdef GLA_DEBUG
1254    m_Vertices++;
1255#endif
[56da40e]1256    glVertex3d(x, y, z);
1257}
1258
[f336ab9]1259void GLACanvas::PlaceVertex(glaCoord x, glaCoord y, glaCoord z,
1260                            glaTexCoord tex_x, glaTexCoord tex_y)
[b839829]1261{
1262    // Place a vertex for the current object being drawn.
1263
1264#ifdef GLA_DEBUG
1265    m_Vertices++;
1266#endif
[ba828d4]1267    glTexCoord2f(tex_x, tex_y);
[b839829]1268    glVertex3d(x, y, z);
1269}
1270
[56da40e]1271void GLACanvas::PlaceIndicatorVertex(glaCoord x, glaCoord y)
1272{
1273    // Place a vertex for the current indicator object being drawn.
1274
[087bc72]1275    PlaceVertex(x, y, 0.0);
[56da40e]1276}
1277
[e633bb1]1278void GLACanvas::BeginBlobs()
[d9b3270]1279{
[fe075d7]1280    // Commence drawing of a set of blobs.
[cab6f11]1281    if (blob_method == SPRITE) {
1282        glPushAttrib(GL_ENABLE_BIT|GL_POINT_BIT);
1283        CHECK_GL_ERROR("BeginBlobs", "glPushAttrib");
1284        glBindTexture(GL_TEXTURE_2D, m_BlobTexture);
1285        CHECK_GL_ERROR("BeginBlobs", "glBindTexture");
1286        glEnable(GL_ALPHA_TEST);
1287        CHECK_GL_ERROR("BeginBlobs", "glEnable GL_ALPHA_TEST");
1288        glPointSize(8);
1289        CHECK_GL_ERROR("BeginBlobs", "glPointSize");
1290        glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
1291        CHECK_GL_ERROR("BeginBlobs", "glTexEnvi GL_POINT_SPRITE");
1292        glEnable(GL_TEXTURE_2D);
1293        CHECK_GL_ERROR("BeginBlobs", "glEnable GL_TEXTURE_2D");
1294        glEnable(GL_POINT_SPRITE);
1295        CHECK_GL_ERROR("BeginBlobs", "glEnable GL_POINT_SPRITE");
1296        glBegin(GL_POINTS);
1297    } else if (blob_method == POINT) {
[78924eb]1298        glPushAttrib(GL_ENABLE_BIT);
1299        CHECK_GL_ERROR("BeginBlobs", "glPushAttrib");
[4ba80e0]1300        glEnable(GL_ALPHA_TEST);
1301        CHECK_GL_ERROR("BeginBlobs", "glEnable GL_ALPHA_TEST");
1302        glEnable(GL_POINT_SMOOTH);
1303        CHECK_GL_ERROR("BeginBlobs", "glEnable GL_POINT_SMOOTH");
1304        glBegin(GL_POINTS);
1305    } else {
[6662d02]1306        glPushAttrib(GL_TRANSFORM_BIT|GL_VIEWPORT_BIT|GL_ENABLE_BIT);
[4ba80e0]1307        CHECK_GL_ERROR("BeginBlobs", "glPushAttrib");
1308        SetIndicatorTransform();
[6f5f9e5]1309        glEnable(GL_DEPTH_TEST);
1310        CHECK_GL_ERROR("BeginBlobs", "glEnable GL_DEPTH_TEST");
[bde8c9a]1311        glBegin(GL_LINES);
[4ba80e0]1312    }
[e633bb1]1313}
[d9b3270]1314
[e633bb1]1315void GLACanvas::EndBlobs()
1316{
[bde8c9a]1317    // Finish drawing of a set of blobs.
1318    glEnd();
[cab6f11]1319    if (blob_method != LINES) {
[bde8c9a]1320        CHECK_GL_ERROR("EndBlobs", "glEnd GL_POINTS");
1321    } else {
1322        CHECK_GL_ERROR("EndBlobs", "glEnd GL_LINES");
[4ba80e0]1323    }
[78924eb]1324    glPopAttrib();
1325    CHECK_GL_ERROR("EndBlobs", "glPopAttrib");
[e633bb1]1326}
[d9b3270]1327
[e633bb1]1328void GLACanvas::DrawBlob(glaCoord x, glaCoord y, glaCoord z)
1329{
[cab6f11]1330    if (blob_method != LINES) {
[4ba80e0]1331        // Draw a marker.
1332        PlaceVertex(x, y, z);
1333    } else {
[f6d8375]1334        double X, Y, Z;
[d67450e]1335        if (!Transform(Vector3(x, y, z), &X, &Y, &Z)) {
[4ba80e0]1336            printf("bad transform\n");
1337            return;
1338        }
1339        // Stuff behind us (in perspective view) will get clipped,
1340        // but we can save effort with a cheap check here.
1341        if (Z <= 0) return;
1342
[bde8c9a]1343        X -= BLOB_DIAMETER * 0.5;
1344        Y -= BLOB_DIAMETER * 0.5;
1345
1346        PlaceVertex(X, Y + 1, Z);
1347        PlaceVertex(X, Y + (BLOB_DIAMETER - 1), Z);
1348
1349        for (int i = 1; i < (BLOB_DIAMETER - 1); ++i) {
1350            PlaceVertex(X + i, Y, Z);
1351            PlaceVertex(X + i, Y + BLOB_DIAMETER, Z);
1352        }
1353
1354        PlaceVertex(X + (BLOB_DIAMETER - 1), Y + 1, Z);
1355        PlaceVertex(X + (BLOB_DIAMETER - 1), Y + (BLOB_DIAMETER - 1), Z);
[78924eb]1356    }
[95ce35f]1357#ifdef GLA_DEBUG
[78924eb]1358    m_Vertices++;
[95ce35f]1359#endif
[429465a]1360}
1361
[81aea4e]1362void GLACanvas::DrawBlob(glaCoord x, glaCoord y)
1363{
[cab6f11]1364    if (blob_method != LINES) {
[81aea4e]1365        // Draw a marker.
1366        PlaceVertex(x, y, 0);
1367    } else {
[bde8c9a]1368        x -= BLOB_DIAMETER * 0.5;
1369        y -= BLOB_DIAMETER * 0.5;
1370
1371        PlaceVertex(x, y + 1, 0);
1372        PlaceVertex(x, y + (BLOB_DIAMETER - 1), 0);
1373
1374        for (int i = 1; i < (BLOB_DIAMETER - 1); ++i) {
1375            PlaceVertex(x + i, y, 0);
1376            PlaceVertex(x + i, y + BLOB_DIAMETER, 0);
1377        }
1378
1379        PlaceVertex(x + (BLOB_DIAMETER - 1), y + 1, 0);
1380        PlaceVertex(x + (BLOB_DIAMETER - 1), y + (BLOB_DIAMETER - 1), 0);
[81aea4e]1381    }
1382#ifdef GLA_DEBUG
1383    m_Vertices++;
1384#endif
1385}
1386
[86fe6e4]1387void GLACanvas::BeginCrosses()
1388{
[95ce35f]1389    // Plot crosses.
[fe075d7]1390    if (cross_method == SPRITE) {
[e473a81]1391        list_flags |= NEVER_CACHE;
1392        SetDataTransform();
[95ce35f]1393        glPushAttrib(GL_ENABLE_BIT|GL_POINT_BIT);
[9baa53a]1394        CHECK_GL_ERROR("BeginCrosses", "glPushAttrib");
[95ce35f]1395        glBindTexture(GL_TEXTURE_2D, m_CrossTexture);
[9baa53a]1396        CHECK_GL_ERROR("BeginCrosses", "glBindTexture");
[95ce35f]1397        glEnable(GL_ALPHA_TEST);
[9baa53a]1398        CHECK_GL_ERROR("BeginCrosses", "glEnable GL_ALPHA_TEST");
[95ce35f]1399        glPointSize(8);
[9baa53a]1400        CHECK_GL_ERROR("BeginCrosses", "glPointSize");
1401        glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
1402        CHECK_GL_ERROR("BeginCrosses", "glTexEnvi GL_POINT_SPRITE");
[95ce35f]1403        glEnable(GL_TEXTURE_2D);
[9baa53a]1404        CHECK_GL_ERROR("BeginCrosses", "glEnable GL_TEXTURE_2D");
1405        glEnable(GL_POINT_SPRITE);
1406        CHECK_GL_ERROR("BeginCrosses", "glEnable GL_POINT_SPRITE");
[95ce35f]1407        glBegin(GL_POINTS);
1408    } else {
1409        // To get the crosses to appear at a constant size and orientation on
1410        // screen, we plot them in the Indicator transform coordinates (which
1411        // unfortunately means they can't be usefully put in an opengl display
1412        // list).
[6662d02]1413        glPushAttrib(GL_TRANSFORM_BIT|GL_VIEWPORT_BIT|GL_ENABLE_BIT);
[9baa53a]1414        CHECK_GL_ERROR("BeginCrosses", "glPushAttrib 2");
[95ce35f]1415        SetIndicatorTransform();
[200eca7]1416        // Align line drawing to pixel centres to get pixel-perfect rendering
1417        // (graphics card and driver bugs aside).
1418        glTranslated(-0.5, -0.5, 0);
1419        CHECK_GL_ERROR("BeginCrosses", "glTranslated");
[6f5f9e5]1420        glEnable(GL_DEPTH_TEST);
[9baa53a]1421        CHECK_GL_ERROR("BeginCrosses", "glEnable GL_DEPTH_TEST");
[95ce35f]1422        glBegin(GL_LINES);
1423    }
[86fe6e4]1424}
1425
1426void GLACanvas::EndCrosses()
1427{
[95ce35f]1428    glEnd();
[fe075d7]1429    if (cross_method == SPRITE) {
[9baa53a]1430        CHECK_GL_ERROR("EndCrosses", "glEnd GL_POINTS");
1431    } else {
1432        CHECK_GL_ERROR("EndCrosses", "glEnd GL_LINES");
1433    }
[86fe6e4]1434    glPopAttrib();
1435    CHECK_GL_ERROR("EndCrosses", "glPopAttrib");
1436}
1437
1438void GLACanvas::DrawCross(glaCoord x, glaCoord y, glaCoord z)
1439{
[fe075d7]1440    if (cross_method == SPRITE) {
[7e1da12]1441        // Draw a marker.
[95ce35f]1442        PlaceVertex(x, y, z);
1443    } else {
[f6d8375]1444        double X, Y, Z;
[d67450e]1445        if (!Transform(Vector3(x, y, z), &X, &Y, &Z)) {
[95ce35f]1446            printf("bad transform\n");
1447            return;
1448        }
1449        // Stuff behind us (in perspective view) will get clipped,
1450        // but we can save effort with a cheap check here.
[7e1da12]1451        if (Z <= 0) return;
1452
1453        // Round to integers before adding on the offsets for the
1454        // cross arms to avoid uneven crosses.
1455        X = rint(X);
1456        Y = rint(Y);
[200eca7]1457        // Need to extend lines by an extra pixel (which shouldn't get drawn by
1458        // the diamond-exit rule).
[7e1da12]1459        PlaceVertex(X - 3, Y - 3, Z);
[200eca7]1460        PlaceVertex(X + 4, Y + 4, Z);
[7e1da12]1461        PlaceVertex(X - 3, Y + 3, Z);
[200eca7]1462        PlaceVertex(X + 4, Y - 4, Z);
[86fe6e4]1463    }
[78924eb]1464#ifdef GLA_DEBUG
1465    m_Vertices++;
1466#endif
[86fe6e4]1467}
1468
[e633bb1]1469void GLACanvas::DrawRing(glaCoord x, glaCoord y)
[429465a]1470{
[127435f]1471    // Draw an unfilled circle of radius 4
1472
1473    // Round to integers to get an even ring.
1474    x = rint(x);
1475    y = rint(y);
1476
1477    glBegin(GL_LINE_LOOP);
1478    PlaceIndicatorVertex(x + 3.5, y - 1.5);
1479    PlaceIndicatorVertex(x + 1.5, y - 3.5);
1480    PlaceIndicatorVertex(x - 1.5, y - 3.5);
1481    PlaceIndicatorVertex(x - 3.5, y - 1.5);
1482    PlaceIndicatorVertex(x - 3.5, y + 1.5);
1483    PlaceIndicatorVertex(x - 1.5, y + 3.5);
1484    PlaceIndicatorVertex(x + 1.5, y + 3.5);
1485    PlaceIndicatorVertex(x + 3.5, y + 1.5);
1486    glEnd();
1487    CHECK_GL_ERROR("DrawRing", "glEnd GL_LINE_LOOP");
[d9b3270]1488}
1489
[522e0bd]1490void GLACanvas::DrawRectangle(gla_colour fill, gla_colour edge,
[b49ac56]1491                              glaCoord x0, glaCoord y0, glaCoord w, glaCoord h)
[56da40e]1492{
1493    // Draw a filled rectangle with an edge in the indicator plane.
[ca8c864]1494    // (x0, y0) specify the bottom-left corner of the rectangle and (w, h) the
1495    // size.
[56da40e]1496
[c10dd28]1497    SetColour(fill);
[aa048c3]1498    BeginQuadrilaterals();
[56da40e]1499    PlaceIndicatorVertex(x0, y0);
1500    PlaceIndicatorVertex(x0 + w, y0);
1501    PlaceIndicatorVertex(x0 + w, y0 + h);
1502    PlaceIndicatorVertex(x0, y0 + h);
1503    EndQuadrilaterals();
1504
[aa048c3]1505    if (edge != fill) {
[b49ac56]1506        SetColour(edge);
[522e0bd]1507        BeginPolyline();
[b49ac56]1508        PlaceIndicatorVertex(x0, y0);
1509        PlaceIndicatorVertex(x0 + w, y0);
1510        PlaceIndicatorVertex(x0 + w, y0 + h);
1511        PlaceIndicatorVertex(x0, y0 + h);
[522e0bd]1512        PlaceIndicatorVertex(x0, y0);
[6e1a54e3]1513        EndPolyline();
[c10dd28]1514    }
[56da40e]1515}
1516
[aa048c3]1517void
1518GLACanvas::DrawShadedRectangle(const GLAPen & fill_bot, const GLAPen & fill_top,
1519                               glaCoord x0, glaCoord y0,
1520                               glaCoord w, glaCoord h)
1521{
1522    // Draw a graduated filled rectangle in the indicator plane.
1523    // (x0, y0) specify the bottom-left corner of the rectangle and (w, h) the
1524    // size.
1525
[02aa4d4]1526    glShadeModel(GL_SMOOTH);
[9baa53a]1527    CHECK_GL_ERROR("DrawShadedRectangle", "glShadeModel GL_SMOOTH");
[aa048c3]1528    BeginQuadrilaterals();
1529    SetColour(fill_bot);
1530    PlaceIndicatorVertex(x0, y0);
1531    PlaceIndicatorVertex(x0 + w, y0);
1532    SetColour(fill_top);
1533    PlaceIndicatorVertex(x0 + w, y0 + h);
1534    PlaceIndicatorVertex(x0, y0 + h);
1535    EndQuadrilaterals();
[02aa4d4]1536    glShadeModel(GL_FLAT);
[9baa53a]1537    CHECK_GL_ERROR("DrawShadedRectangle", "glShadeModel GL_FLAT");
[aa048c3]1538}
1539
1540void GLACanvas::DrawCircle(gla_colour edge, gla_colour fill,
1541                           glaCoord cx, glaCoord cy, glaCoord radius)
[56da40e]1542{
[087bc72]1543    // Draw a filled circle with an edge.
[56da40e]1544    SetColour(fill);
[78924eb]1545    glMatrixMode(GL_MODELVIEW);
1546    CHECK_GL_ERROR("DrawCircle", "glMatrixMode");
1547    glPushMatrix();
1548    CHECK_GL_ERROR("DrawCircle", "glPushMatrix");
[56da40e]1549    glTranslated(cx, cy, 0.0);
[8326157]1550    CHECK_GL_ERROR("DrawCircle", "glTranslated");
[1b12b82]1551    assert(m_Quadric);
1552    gluDisk(m_Quadric, 0.0, radius, 36, 1);
[8326157]1553    CHECK_GL_ERROR("DrawCircle", "gluDisk");
[56da40e]1554    SetColour(edge);
[1b12b82]1555    gluDisk(m_Quadric, radius - 1.0, radius, 36, 1);
[8326157]1556    CHECK_GL_ERROR("DrawCircle", "gluDisk (2)");
[78924eb]1557    glPopMatrix();
1558    CHECK_GL_ERROR("DrawCircle", "glPopMatrix");
[56da40e]1559}
1560
[aa048c3]1561void GLACanvas::DrawSemicircle(gla_colour edge, gla_colour fill,
[b49ac56]1562                               glaCoord cx, glaCoord cy,
1563                               glaCoord radius, glaCoord start)
[56da40e]1564{
1565    // Draw a filled semicircle with an edge.
[ca8c864]1566    // The semicircle extends from "start" deg to "start"+180 deg (increasing
1567    // clockwise, 0 deg upwards).
[56da40e]1568    SetColour(fill);
[78924eb]1569    glMatrixMode(GL_MODELVIEW);
1570    CHECK_GL_ERROR("DrawSemicircle", "glMatrixMode");
1571    glPushMatrix();
1572    CHECK_GL_ERROR("DrawSemicircle", "glPushMatrix");
[56da40e]1573    glTranslated(cx, cy, 0.0);
[78924eb]1574    CHECK_GL_ERROR("DrawSemicircle", "glTranslated");
[1b12b82]1575    assert(m_Quadric);
1576    gluPartialDisk(m_Quadric, 0.0, radius, 36, 1, start, 180.0);
[78924eb]1577    CHECK_GL_ERROR("DrawSemicircle", "gluPartialDisk");
[56da40e]1578    SetColour(edge);
[1b12b82]1579    gluPartialDisk(m_Quadric, radius - 1.0, radius, 36, 1, start, 180.0);
[78924eb]1580    CHECK_GL_ERROR("DrawSemicircle", "gluPartialDisk (2)");
1581    glPopMatrix();
1582    CHECK_GL_ERROR("DrawSemicircle", "glPopMatrix");
[56da40e]1583}
1584
1585void GLACanvas::EnableDashedLines()
1586{
1587    // Enable dashed lines, and start drawing in them.
1588
[9eb58d0]1589    glLineStipple(1, 0x3333);
[8326157]1590    CHECK_GL_ERROR("EnableDashedLines", "glLineStipple");
[56da40e]1591    glEnable(GL_LINE_STIPPLE);
[bb1f293]1592    CHECK_GL_ERROR("EnableDashedLines", "glEnable GL_LINE_STIPPLE");
[56da40e]1593}
1594
1595void GLACanvas::DisableDashedLines()
1596{
1597    glDisable(GL_LINE_STIPPLE);
[bb1f293]1598    CHECK_GL_ERROR("DisableDashedLines", "glDisable GL_LINE_STIPPLE");
[56da40e]1599}
1600
[d67450e]1601bool GLACanvas::Transform(const Vector3 & v,
[147847c]1602                          glaCoord* x_out, glaCoord* y_out, glaCoord* z_out) const
[56da40e]1603{
1604    // Convert from data coordinates to screen coordinates.
[096e56c]1605
[087bc72]1606    // Perform the projection.
[d67450e]1607    return gluProject(v.GetX(), v.GetY(), v.GetZ(),
1608                      modelview_matrix, projection_matrix, viewport,
[6adffadf]1609                      x_out, y_out, z_out);
[56da40e]1610}
1611
[147847c]1612void GLACanvas::ReverseTransform(double x, double y,
1613                                 glaCoord* x_out, glaCoord* y_out, glaCoord* z_out) const
[a2b3d62]1614{
1615    // Convert from screen coordinates to data coordinates.
[096e56c]1616
[a2b3d62]1617    // Perform the projection.
[1b12b82]1618    gluUnProject(x, y, 0.0, modelview_matrix, projection_matrix, viewport,
[b49ac56]1619                 x_out, y_out, z_out);
[8326157]1620    CHECK_GL_ERROR("ReverseTransform", "gluUnProject");
[a2b3d62]1621}
1622
[147847c]1623double GLACanvas::SurveyUnitsAcrossViewport() const
[087bc72]1624{
[8326157]1625    // Measure the current viewport in survey units, taking into account the
1626    // current display scale.
[087bc72]1627
[9eb58d0]1628    assert(m_Scale != 0.0);
[db59b02]1629    list_flags |= INVALIDATE_ON_SCALE;
[147847c]1630    double result = m_VolumeDiameter / m_Scale;
[ea35995]1631    if (y_size < x_size) {
1632        result = result * x_size / y_size;
1633    }
1634    return result;
[087bc72]1635}
[045e2af]1636
[d67450e]1637void GLACanvas::ToggleSmoothShading()
1638{
1639    m_SmoothShading = !m_SmoothShading;
1640}
1641
[a517825]1642void GLACanvas::ToggleTextured()
1643{
1644    m_Textured = !m_Textured;
1645    if (m_Textured && m_Texture == 0) {
1646        glGenTextures(1, &m_Texture);
1647        CHECK_GL_ERROR("ToggleTextured", "glGenTextures");
1648
1649        glBindTexture(GL_TEXTURE_2D, m_Texture);
1650        CHECK_GL_ERROR("ToggleTextured", "glBindTexture");
1651
1652        ::wxInitAllImageHandlers();
[5627cbb]1653
[a517825]1654        wxImage img;
[8a05a7a]1655        wxString texture(wmsg_cfgpth());
[5627cbb]1656        texture += wxCONFIG_PATH_SEPARATOR;
[09dfd18]1657        texture += wxT("images");
[5627cbb]1658        texture += wxCONFIG_PATH_SEPARATOR;
1659        texture += wxT("texture.png");
1660        if (!img.LoadFile(texture, wxBITMAP_TYPE_PNG)) {
[a517825]1661            // FIXME
1662            fprintf(stderr, "Couldn't load image.\n");
1663            exit(1);
1664        }
[5627cbb]1665
[ecbdd96]1666        // Generate mipmaps.
[7c18431]1667        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, // was GL_LUMINANCE
[ecbdd96]1668                          img.GetWidth(), img.GetHeight(),
1669                          GL_RGB, GL_UNSIGNED_BYTE, img.GetData());
1670        CHECK_GL_ERROR("ToggleTextured", "gluBuild2DMipmaps");
[a517825]1671
1672        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1673        CHECK_GL_ERROR("ToggleTextured", "glTexEnvi");
1674    }
1675}
1676
[1ada489]1677bool GLACanvas::SaveScreenshot(const wxString & fnm, wxBitmapType type) const
[045e2af]1678{
[6cf4daa]1679    const int width = x_size;
1680    const int height = y_size;
[1bd4841]1681    unsigned char *pixels = (unsigned char *)malloc(3 * width * (height + 1));
[045e2af]1682    if (!pixels) return false;
1683    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *)pixels);
[a106530]1684    CHECK_GL_ERROR("SaveScreenshot", "glReadPixels");
[1bd4841]1685    unsigned char * tmp_row = pixels + 3 * width * height;
1686    // We need to flip the image vertically - this approach should be more
1687    // efficient than using wxImage::Mirror(false) as that creates a new
1688    // wxImage object.
1689    for (int y = height / 2 - 1; y >= 0; --y) {
1690        unsigned char * upper = pixels + 3 * width * y;
1691        unsigned char * lower = pixels + 3 * width * (height - y - 1);
1692        memcpy(tmp_row, upper, 3 * width);
1693        memcpy(upper, lower, 3 * width);
1694        memcpy(lower, tmp_row, 3 * width);
1695    }
1696    // NB wxImage constructor calls free(pixels) for us.
[045e2af]1697    wxImage grab(width, height, pixels);
[1bd4841]1698    return grab.SaveFile(fnm, type);
[045e2af]1699}
[807f9dd]1700
1701bool GLACanvas::CheckVisualFidelity(const unsigned char * target) const
1702{
1703    unsigned char pixels[3 * 8 * 8];
[c8f449c3]1704    if (double_buffered) {
1705        glReadBuffer(GL_BACK);
1706        CHECK_GL_ERROR("FirstShow", "glReadBuffer");
1707    }
[edfaf35]1708    glReadPixels(x_size / 2 - 4, y_size / 2 - 5, 8, 8,
[6cf4daa]1709                 GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *)pixels);
[a106530]1710    CHECK_GL_ERROR("CheckVisualFidelity", "glReadPixels");
[c8f449c3]1711    if (double_buffered) {
1712        glReadBuffer(GL_FRONT);
1713        CHECK_GL_ERROR("FirstShow", "glReadBuffer");
1714    }
[4f7f965]1715#if 0
1716    // Show what got drawn and what was expected for debugging.
1717    for (int y = 0; y < 8; ++y) {
1718        for (int x = 0; x < 8; ++x) {
1719            int o = (y * 8 + x) * 3;
1720            printf("%c", pixels[o] ? 'X' : '.');
1721        }
1722        printf(" ");
1723        for (int x = 0; x < 8; ++x) {
1724            int o = (y * 8 + x) * 3;
1725            printf("%c", target[o] ? 'X' : '.');
1726        }
1727        printf("\n");
1728    }
1729#endif
[807f9dd]1730    return (memcmp(pixels, target, sizeof(pixels)) == 0);
1731}
[aea4f8b]1732
1733void GLACanvas::ReadPixels(int width, int height, unsigned char * buf) const
1734{
[a106530]1735    CHECK_GL_ERROR("ReadPixels", "glReadPixels");
[aea4f8b]1736    glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *)buf);
1737}
[f9ca87c]1738
1739void GLACanvas::PolygonOffset(bool on) const
1740{
1741    if (on) {
1742        glPolygonOffset(1.0, 1.0);
1743        glEnable(GL_POLYGON_OFFSET_FILL);
1744    } else {
1745        glDisable(GL_POLYGON_OFFSET_FILL);
1746    }
1747}
Note: See TracBrowser for help on using the repository browser.