4 // OpenGL implementation for the GLA abstraction layer.
6 // Copyright (C) 2002-2003,2005 Mark R. Shinwell
7 // Copyright (C) 2003-2022 Olly Betts
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.
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.
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
26 #include <wx/confbase.h>
33 #include "gllogerror.h"
39 #elif defined HAVE_OPENGL_GL_H
40 # include <OpenGL/gl.h>
43 #ifdef HAVE_GL_GLEXT_H
44 # include <GL/glext.h>
45 #elif defined HAVE_OPENGL_GLEXT_H
46 # include <OpenGL/glext.h>
49 #ifndef GL_POINT_SIZE_MAX
50 #define GL_POINT_SIZE_MAX 0x8127
52 #ifndef GL_POINT_SPRITE
53 #define GL_POINT_SPRITE 0x8861
55 #ifndef GL_COORD_REPLACE
56 #define GL_COORD_REPLACE 0x8862
58 // GL_POINT_SIZE_RANGE is deprecated in OpenGL 1.2 and later, and replaced by
59 // GL_SMOOTH_POINT_SIZE_RANGE.
60 #ifndef GL_SMOOTH_POINT_SIZE_RANGE
61 #define GL_SMOOTH_POINT_SIZE_RANGE GL_POINT_SIZE_RANGE
63 // GL_POINT_SIZE_GRANULARITY is deprecated in OpenGL 1.2 and later, and
64 // replaced by GL_SMOOTH_POINT_SIZE_GRANULARITY.
65 #ifndef GL_SMOOTH_POINT_SIZE_GRANULARITY
66 #define GL_SMOOTH_POINT_SIZE_GRANULARITY GL_POINT_SIZE_GRANULARITY
68 // GL_ALIASED_POINT_SIZE_RANGE was added in OpenGL 1.2.
69 #ifndef GL_ALIASED_POINT_SIZE_RANGE
70 #define GL_ALIASED_POINT_SIZE_RANGE 0x846D
75 const int BLOB_DIAMETER
= 5;
77 #define BLOB_TEXTURE \
78 o, o, o, o, o, o, o, o,\
79 o, o, o, o, o, o, o, o,\
80 o, o, I, I, I, o, o, o,\
81 o, I, I, I, I, I, o, o,\
82 o, I, I, I, I, I, o, o,\
83 o, I, I, I, I, I, o, o,\
84 o, o, I, I, I, o, o, o,\
85 o, o, o, o, o, o, o, o
87 #define CROSS_TEXTURE \
88 o, o, o, o, o, o, o, o,\
89 I, o, o, o, o, o, I, o,\
90 o, I, o, o, o, I, o, o,\
91 o, o, I, o, I, o, o, o,\
92 o, o, o, I, o, o, o, o,\
93 o, o, I, o, I, o, o, o,\
94 o, I, o, o, o, I, o, o,\
95 I, o, o, o, o, o, I, o
97 // Declared in gllogerror.h.
98 bool opengl_initialised
= false;
100 static bool double_buffered
= false;
102 static const int* wx_gl_attribs
= NULL
;
105 GLACanvas::check_visual()
107 static const int wx_gl_attribs_full
[] = {
110 WX_GL_DEPTH_SIZE
, 16,
114 // Use a double-buffered visual if available, as it will give much smoother
116 double_buffered
= true;
117 wx_gl_attribs
= wx_gl_attribs_full
;
118 if (!IsDisplaySupported(wx_gl_attribs
)) {
120 if (!IsDisplaySupported(wx_gl_attribs
)) {
123 double_buffered
= false;
128 string
GetGLSystemDescription()
130 // If OpenGL isn't initialised we may get a SEGV from glGetString.
131 if (!opengl_initialised
)
132 return "No OpenGL information available yet - try opening a file.";
133 const char *p
= (const char*)glGetString(GL_VERSION
);
135 return "Couldn't read OpenGL version!";
141 info
+= (const char*)glGetString(GL_VENDOR
);
143 info
+= (const char*)glGetString(GL_RENDERER
);
144 #if !(wxUSE_GLCANVAS_EGL-0) && \
145 (defined __WXGTK__ || defined __WXX11__ || defined __WXMOTIF__)
146 info
+= string_format("\nGLX %0.1f\n", wxGLCanvas::GetGLXVersion() * 0.1);
151 GLint red
, green
, blue
;
152 glGetIntegerv(GL_RED_BITS
, &red
);
153 glGetIntegerv(GL_GREEN_BITS
, &green
);
154 glGetIntegerv(GL_BLUE_BITS
, &blue
);
155 GLint max_texture_size
;
156 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &max_texture_size
);
157 GLint max_viewport
[2];
158 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, max_viewport
);
159 GLdouble point_size_range
[2];
160 glGetDoublev(GL_SMOOTH_POINT_SIZE_RANGE
, point_size_range
);
161 GLdouble point_size_granularity
;
162 glGetDoublev(GL_SMOOTH_POINT_SIZE_GRANULARITY
, &point_size_granularity
);
163 info
+= string_format("R%dG%dB%d\n"
164 "Max Texture size: %dx%d\n"
165 "Max Viewport size: %dx%d\n"
166 "Smooth Point Size %.3f-%.3f (granularity %.3f)",
167 (int)red
, (int)green
, (int)blue
,
168 (int)max_texture_size
, (int)max_texture_size
,
169 (int)max_viewport
[0], (int)max_viewport
[1],
170 point_size_range
[0], point_size_range
[1],
171 point_size_granularity
);
172 glGetDoublev(GL_ALIASED_POINT_SIZE_RANGE
, point_size_range
);
173 if (glGetError() != GL_INVALID_ENUM
) {
174 info
+= string_format("\nAliased point size %.3f-%.3f",
175 point_size_range
[0], point_size_range
[1]);
178 info
+= "\nDouble buffered: ";
184 const GLubyte
* gl_extensions
= glGetString(GL_EXTENSIONS
);
185 if (*gl_extensions
) {
187 info
+= (const char*)gl_extensions
;
193 glpoint_sprite_works()
195 // Point sprites provide an easy, fast way for us to draw crosses by
196 // texture mapping GL points.
198 // If we have OpenGL >= 2.0 then we definitely have GL_POINT_SPRITE.
199 // Otherwise see if we have the GL_ARB_point_sprite or GL_NV_point_sprite
202 // The symbolic constants GL_POINT_SPRITE, GL_POINT_SPRITE_ARB, and
203 // GL_POINT_SPRITE_NV all give the same number so it doesn't matter
205 static bool glpoint_sprite
= false;
206 static bool checked
= false;
208 float maxSize
= 0.0f
;
209 glGetFloatv(GL_POINT_SIZE_MAX
, &maxSize
);
211 glpoint_sprite
= (atoi((const char *)glGetString(GL_VERSION
)) >= 2);
212 if (!glpoint_sprite
) {
213 const char * p
= (const char *)glGetString(GL_EXTENSIONS
);
216 if (memcmp(p
, "GL_ARB_point_sprite", 19) == 0) {
218 } else if (memcmp(p
, "GL_NV_point_sprite", 18) == 0) {
223 if (*p
== '\0' || *p
== ' ') {
224 glpoint_sprite
= true;
228 p
= strchr(p
+ 1, ' ');
236 return glpoint_sprite
;
240 log_gl_error(const wxChar
* str
, GLenum error_code
)
243 switch (error_code
) {
244 case GL_INVALID_ENUM
:
245 msg
= "Invalid OpenGL enumerated value";
247 case GL_INVALID_VALUE
:
248 msg
= "Invalid OpenGL numeric argument value";
250 case GL_INVALID_OPERATION
:
251 msg
= "Invalid OpenGL operation";
253 case GL_INVALID_FRAMEBUFFER_OPERATION
:
254 msg
= "Invalid OpenGL framebuffer operation";
256 case GL_OUT_OF_MEMORY
:
257 msg
= wmsg(/*Out of memory*/389);
259 case GL_STACK_UNDERFLOW
:
260 msg
= "OpenGL stack underflow";
262 case GL_STACK_OVERFLOW
:
263 msg
= "OpenGL stack overflow";
266 msg
.Format("Unknown OpenGL error code: %d", int(error_code
));
269 wxLogError(str
, msg
);
276 void GLAPen::SetColour(double red
, double green
, double blue
)
279 components
[1] = green
;
280 components
[2] = blue
;
283 double GLAPen::GetRed() const
285 return components
[0];
288 double GLAPen::GetGreen() const
290 return components
[1];
293 double GLAPen::GetBlue() const
295 return components
[2];
298 void GLAPen::Interpolate(const GLAPen
& pen
, double how_far
)
300 components
[0] += how_far
* (pen
.GetRed() - components
[0]);
301 components
[1] += how_far
* (pen
.GetGreen() - components
[1]);
302 components
[2] += how_far
* (pen
.GetBlue() - components
[2]);
305 struct ColourTriple
{
306 // RGB triple: values are from 0-255 inclusive for each component.
307 unsigned char r
, g
, b
;
310 // Order must match that in enum gla_colour[] in gla.h.
311 static const ColourTriple COLOURS
[] = {
312 { 0, 0, 0 }, // black
313 { 100, 100, 100 }, // grey
314 { 180, 180, 180 }, // light grey
315 { 140, 140, 140 }, // light grey 2
316 { 90, 90, 90 }, // dark grey
317 { 255, 255, 255 }, // white
318 { 0, 100, 255}, // turquoise
319 { 0, 255, 40 }, // green
320 { 150, 205, 224 }, // indicator 1
321 { 114, 149, 160 }, // indicator 2
322 { 255, 255, 0 }, // yellow
323 { 255, 0, 0 }, // red
324 { 40, 40, 255 }, // blue
325 { 255, 0, 255 }, // magenta
328 bool GLAList::need_to_generate() {
329 // Bail out if the list is already cached, or can't usefully be cached.
330 if (flags
& (GLACanvas::CACHED
|GLACanvas::NEVER_CACHE
))
333 // Create a new OpenGL list to hold this sequence of drawing
336 gl_list
= glGenLists(1);
337 CHECK_GL_ERROR("GLAList::need_to_generate", "glGenLists");
339 printf("glGenLists(1) returned %u\n", (unsigned)gl_list
);
342 // If we can't create a list for any reason, fall back to just
343 // drawing directly, and flag the list as NEVER_CACHE as there's
344 // unlikely to be much point calling glGenLists() again.
345 flags
= GLACanvas::NEVER_CACHE
;
349 // We should have 256 lists for font drawing and a dozen or so for 2D
350 // and 3D lists. So something is amiss if we've generated 1000 lists,
351 // probably a infinite loop in the lazy list mechanism.
352 assert(gl_list
< 1000);
354 // https://www.opengl.org/resources/faq/technical/displaylist.htm advises:
356 // "Stay away from GL_COMPILE_AND_EXECUTE mode. Instead, create the
357 // list using GL_COMPILE mode, then execute it with glCallList()."
358 glNewList(gl_list
, GL_COMPILE
);
359 CHECK_GL_ERROR("GLAList::need_to_generate", "glNewList");
363 void GLAList::finalise(unsigned int list_flags
)
366 CHECK_GL_ERROR("GLAList::finalise", "glEndList");
367 if (list_flags
& GLACanvas::NEVER_CACHE
) {
368 glDeleteLists(gl_list
, 1);
369 CHECK_GL_ERROR("GLAList::finalise", "glDeleteLists");
371 flags
= GLACanvas::NEVER_CACHE
;
373 flags
= list_flags
| GLACanvas::CACHED
;
377 bool GLAList::DrawList() const {
378 if ((flags
& GLACanvas::CACHED
) == 0)
381 CHECK_GL_ERROR("GLAList::DrawList", "glCallList");
389 BEGIN_EVENT_TABLE(GLACanvas
, wxGLCanvas
)
390 EVT_SIZE(GLACanvas::OnSize
)
391 #ifdef HAS_DPI_INDEPENDENT_PIXELS
392 EVT_MOVE(GLACanvas::OnMove
)
396 // Pass wxWANTS_CHARS so that the window gets cursor keys on MS Windows.
397 GLACanvas::GLACanvas(wxWindow
* parent
, int id
)
398 : wxGLCanvas(parent
, id
, wx_gl_attribs
, wxDefaultPosition
,
399 wxDefaultSize
, wxWANTS_CHARS
),
404 GLACanvas::~GLACanvas()
409 gluDeleteQuadric(m_Quadric
);
410 CHECK_GL_ERROR("~GLACanvas", "gluDeleteQuadric");
414 void GLACanvas::FirstShow()
416 #ifdef HAS_DPI_INDEPENDENT_PIXELS
417 content_scale_factor
= wxGLCanvas::GetContentScaleFactor();
420 // Update our record of the client area size and centre.
421 GetClientSize(&x_size
, &y_size
);
422 x_size
*= content_scale_factor
;
423 y_size
*= content_scale_factor
;
424 if (x_size
< 1) x_size
= 1;
425 if (y_size
< 1) y_size
= 1;
427 ctx
.SetCurrent(*this);
428 opengl_initialised
= true;
430 // Set the background colour of the canvas to black.
431 glClearColor(0.0, 0.0, 0.0, 1.0);
432 CHECK_GL_ERROR("FirstShow", "glClearColor");
435 glViewport(0, 0, x_size
, y_size
);
436 CHECK_GL_ERROR("FirstShow", "glViewport");
440 vendor
= wxString((const char *)glGetString(GL_VENDOR
), wxConvUTF8
);
441 renderer
= wxString((const char *)glGetString(GL_RENDERER
), wxConvUTF8
);
443 wxConfigBase
* cfg
= wxConfigBase::Get();
445 if (cfg
->Read(wxT("opengl_survex"), &s
, wxString()) && s
== wxT(VERSION
) &&
446 cfg
->Read(wxT("opengl_vendor"), &s
, wxString()) && s
== vendor
&&
447 cfg
->Read(wxT("opengl_renderer"), &s
, wxString()) && s
== renderer
) {
448 // The survex version, vendor and renderer are the same as those
449 // we cached hints for, so use those hints.
451 if (cfg
->Read(wxT("blob_method"), &v
, 0) &&
452 (v
== SPRITE
|| v
== POINT
|| v
== LINES
)) {
453 // How to draw blobs.
456 if (cfg
->Read(wxT("cross_method"), &v
, 0) &&
457 (v
== SPRITE
|| v
== LINES
)) {
458 // How to draw crosses.
464 if (m_Quadric
) return;
465 // One time initialisation follows.
467 m_Quadric
= gluNewQuadric();
468 CHECK_GL_ERROR("FirstShow", "gluNewQuadric");
470 abort(); // FIXME need to cope somehow
473 glShadeModel(GL_FLAT
);
474 CHECK_GL_ERROR("FirstShow", "glShadeModel");
475 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
); // So text works.
476 CHECK_GL_ERROR("FirstShow", "glPolygonMode");
477 //glAlphaFunc(GL_GREATER, 0.5f);
478 //CHECK_GL_ERROR("FirstShow", "glAlphaFunc");
480 // We want glReadPixels() to read from the front buffer (which is the
481 // default for single-buffered displays).
482 if (double_buffered
) {
483 glReadBuffer(GL_FRONT
);
484 CHECK_GL_ERROR("FirstShow", "glReadBuffer");
488 GLfloat fogcolour
[4] = { 0.5, 0.5, 0.5, 1.0 };
489 glFogfv(GL_FOG_COLOR
, fogcolour
);
490 CHECK_GL_ERROR("FirstShow", "glFogfv");
493 glFogi(GL_FOG_MODE
, GL_LINEAR
);
494 CHECK_GL_ERROR("FirstShow", "glFogi");
496 // Optimise for speed (compute fog per vertex).
497 glHint(GL_FOG_HINT
, GL_FASTEST
);
498 CHECK_GL_ERROR("FirstShow", "glHint");
500 // No padding on pixel packing and unpacking (default is to pad each
501 // line to a multiple of 4 bytes).
502 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1); // For setting texture maps.
503 CHECK_GL_ERROR("FirstShow", "glPixelStorei GL_UNPACK_ALIGNMENT");
504 glPixelStorei(GL_PACK_ALIGNMENT
, 1); // For screengrabs and movies.
505 CHECK_GL_ERROR("FirstShow", "glPixelStorei GL_PACK_ALIGNMENT");
508 wxString path
= wmsg_cfgpth();
509 path
+= wxCONFIG_PATH_SEPARATOR
;
510 path
+= wxT("unifont.pixelfont");
511 if (!m_Font
.load(path
, content_scale_factor
>= 2)) {
512 // FIXME: do something better.
513 // We have this message available: Error in format of font file “%s”
514 fprintf(stderr
, "Failed to parse compiled-in font data\n");
518 if (blob_method
== UNKNOWN
) {
519 // Check if we can use GL_POINTS to plot blobs at stations.
520 GLdouble point_size_range
[2];
521 glGetDoublev(GL_SMOOTH_POINT_SIZE_RANGE
, point_size_range
);
522 CHECK_GL_ERROR("FirstShow", "glGetDoublev GL_SMOOTH_POINT_SIZE_RANGE");
523 if (point_size_range
[0] <= BLOB_DIAMETER
&&
524 point_size_range
[1] >= BLOB_DIAMETER
) {
527 blob_method
= glpoint_sprite_works() ? SPRITE
: LINES
;
532 if (blob_method
== POINT
) {
533 glPointSize(BLOB_DIAMETER
);
534 CHECK_GL_ERROR("FirstShow", "glPointSize");
537 if (cross_method
== UNKNOWN
) {
538 cross_method
= glpoint_sprite_works() ? SPRITE
: LINES
;
542 if (cross_method
== SPRITE
) {
543 glGenTextures(1, &m_CrossTexture
);
544 CHECK_GL_ERROR("FirstShow", "glGenTextures");
545 glBindTexture(GL_TEXTURE_2D
, m_CrossTexture
);
546 CHECK_GL_ERROR("FirstShow", "glBindTexture");
547 // Cross image for drawing crosses using texture mapped point sprites.
548 const unsigned char crossteximage
[128] = {
555 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
556 CHECK_GL_ERROR("FirstShow", "glPixelStorei");
557 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
558 CHECK_GL_ERROR("FirstShow", "glTexEnvi");
559 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
560 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_S");
561 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
562 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_T");
563 glTexImage2D(GL_TEXTURE_2D
, 0, GL_LUMINANCE_ALPHA
, 8, 8, 0, GL_LUMINANCE_ALPHA
, GL_UNSIGNED_BYTE
, (GLvoid
*)crossteximage
);
564 CHECK_GL_ERROR("FirstShow", "glTexImage2D");
565 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
566 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MAG_FILTER");
567 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
568 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MIN_FILTER");
571 if (blob_method
== SPRITE
) {
572 glGenTextures(1, &m_BlobTexture
);
573 CHECK_GL_ERROR("FirstShow", "glGenTextures");
574 glBindTexture(GL_TEXTURE_2D
, m_BlobTexture
);
575 CHECK_GL_ERROR("FirstShow", "glBindTexture");
576 // Image for drawing blobs using texture mapped point sprites.
577 const unsigned char blobteximage
[128] = {
584 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
585 CHECK_GL_ERROR("FirstShow", "glPixelStorei");
586 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
587 CHECK_GL_ERROR("FirstShow", "glTexEnvi");
588 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
589 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_S");
590 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
591 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_T");
592 glTexImage2D(GL_TEXTURE_2D
, 0, GL_LUMINANCE_ALPHA
, 8, 8, 0, GL_LUMINANCE_ALPHA
, GL_UNSIGNED_BYTE
, (GLvoid
*)blobteximage
);
593 CHECK_GL_ERROR("FirstShow", "glTexImage2D");
594 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
595 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MAG_FILTER");
596 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
597 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MIN_FILTER");
601 void GLACanvas::Clear()
605 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
606 CHECK_GL_ERROR("Clear", "glClear");
609 void GLACanvas::ClearNative()
611 // Clear the canvas to the native background colour.
613 wxColour background_colour
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWFRAME
);
614 glClearColor(background_colour
.Red() / 255.,
615 background_colour
.Green() / 255.,
616 background_colour
.Blue() / 255.,
618 CHECK_GL_ERROR("ClearNative", "glClearColor");
619 glClear(GL_COLOR_BUFFER_BIT
);
620 CHECK_GL_ERROR("ClearNative", "glClear");
621 glClearColor(0.0, 0.0, 0.0, 1.0);
622 CHECK_GL_ERROR("ClearNative", "glClearColor (2)");
625 void GLACanvas::SetScale(double scale
)
627 if (scale
!= m_Scale
) {
628 for (auto & i
: drawing_lists
) {
629 i
.invalidate_if(INVALIDATE_ON_SCALE
);
636 #ifdef HAS_DPI_INDEPENDENT_PIXELS
637 void GLACanvas::UpdateContentScaleFactor()
639 double new_content_scale_factor
= wxGLCanvas::GetContentScaleFactor();
640 if (new_content_scale_factor
== content_scale_factor
) return;
642 content_scale_factor
= new_content_scale_factor
;
643 for (auto& i
: drawing_lists
) {
644 i
.invalidate_if(INVALIDATE_ON_HIDPI
);
648 void GLACanvas::OnMove(wxMoveEvent
& event
)
650 UpdateContentScaleFactor();
655 void GLACanvas::OnSize(wxSizeEvent
& event
)
657 UpdateContentScaleFactor();
659 wxSize size
= event
.GetSize();
661 int new_w
= size
.GetWidth() * content_scale_factor
;
662 int new_h
= size
.GetHeight() * content_scale_factor
;
663 // The width and height go to zero when the panel is dragged right
664 // across so we clamp them to be at least 1 to avoid problems.
665 if (new_w
< 1) new_w
= 1;
666 if (new_h
< 1) new_h
= 1;
667 unsigned int mask
= 0;
668 if (new_w
!= x_size
) mask
|= INVALIDATE_ON_X_RESIZE
;
669 if (new_h
!= y_size
) mask
|= INVALIDATE_ON_Y_RESIZE
;
673 for (auto& i
: drawing_lists
) {
674 i
.invalidate_if(mask
);
680 if (!opengl_initialised
) return;
683 glViewport(0, 0, x_size
, y_size
);
684 CHECK_GL_ERROR("OnSize", "glViewport");
687 void GLACanvas::AddTranslationScreenCoordinates(int dx
, int dy
)
689 // Translate the data by a given amount, specified in screen coordinates.
691 // Find out how far the translation takes us in data coordinates.
696 gluUnProject(0.0, 0.0, 0.0, modelview_matrix
, projection_matrix
, viewport
,
698 CHECK_GL_ERROR("AddTranslationScreenCoordinates", "gluUnProject");
699 gluUnProject(dx
, -dy
, 0.0, modelview_matrix
, projection_matrix
, viewport
,
701 CHECK_GL_ERROR("AddTranslationScreenCoordinates", "gluUnProject (2)");
703 // Apply the translation.
704 AddTranslation(Vector3(x
- x0
, y
- y0
, z
- z0
));
707 void GLACanvas::SetVolumeDiameter(glaCoord diameter
)
709 // Set the size of the data drawing volume by giving the diameter of the
710 // smallest sphere containing it.
712 m_VolumeDiameter
= max(glaCoord(1.0), diameter
);
715 void GLACanvas::StartDrawing()
717 // Prepare for a redraw operation.
719 ctx
.SetCurrent(*this);
720 glDepthMask(GL_TRUE
);
722 if (!save_hints
) return;
724 // We want to check on the second redraw.
725 static int draw_count
= 2;
726 if (--draw_count
!= 0) return;
728 if (cross_method
!= LINES
) {
729 SetColour(col_WHITE
);
733 DrawCross(-m_Translation
.GetX(), -m_Translation
.GetY(), -m_Translation
.GetZ());
735 static const unsigned char expected_cross
[64 * 3] = {
737 #define I 255,255,255
742 if (!CheckVisualFidelity(expected_cross
)) {
743 cross_method
= LINES
;
748 if (blob_method
!= LINES
) {
749 SetColour(col_WHITE
);
753 DrawBlob(-m_Translation
.GetX(), -m_Translation
.GetY(), -m_Translation
.GetZ());
755 static const unsigned char expected_blob
[64 * 3] = {
757 #define I 255,255,255
762 if (!CheckVisualFidelity(expected_blob
)) {
768 wxConfigBase
* cfg
= wxConfigBase::Get();
769 cfg
->Write(wxT("opengl_survex"), wxT(VERSION
));
770 cfg
->Write(wxT("opengl_vendor"), vendor
);
771 cfg
->Write(wxT("opengl_renderer"), renderer
);
772 cfg
->Write(wxT("blob_method"), blob_method
);
773 cfg
->Write(wxT("cross_method"), cross_method
);
778 void GLACanvas::EnableSmoothPolygons(bool filled
)
780 // Prepare for drawing smoothly-shaded polygons.
781 // Only use this when required (in particular lines in lists may not be
782 // coloured correctly when this is enabled).
784 glPushAttrib(GL_ENABLE_BIT
|GL_LIGHTING_BIT
|GL_POLYGON_BIT
);
786 glShadeModel(GL_SMOOTH
);
787 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
789 glDisable(GL_LINE_SMOOTH
);
790 glDisable(GL_TEXTURE_2D
);
791 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
793 CHECK_GL_ERROR("EnableSmoothPolygons", "glPolygonMode");
795 if (filled
&& m_SmoothShading
) {
796 static const GLfloat mat_specular
[] = { 0.2, 0.2, 0.2, 1.0 };
797 static const GLfloat light_position
[] = { -1.0, -1.0, -1.0, 0.0 };
798 static const GLfloat light_ambient
[] = { 0.3, 0.3, 0.3, 1.0 };
799 static const GLfloat light_diffuse
[] = { 0.7, 0.7, 0.7, 1.0 };
800 glEnable(GL_COLOR_MATERIAL
);
801 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, mat_specular
);
802 glMaterialf(GL_FRONT_AND_BACK
, GL_SHININESS
, 10.0);
803 glLightfv(GL_LIGHT0
, GL_AMBIENT
, light_ambient
);
804 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light_diffuse
);
805 glLightfv(GL_LIGHT0
, GL_POSITION
, light_position
);
806 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
807 glEnable(GL_LIGHTING
);
812 void GLACanvas::DisableSmoothPolygons()
817 void GLACanvas::PlaceNormal(const Vector3
&v
)
819 // Add a normal (for polygons etc.)
821 glNormal3d(v
.GetX(), v
.GetY(), v
.GetZ());
824 void GLACanvas::SetDataTransform()
827 glMatrixMode(GL_PROJECTION
);
828 CHECK_GL_ERROR("SetDataTransform", "glMatrixMode");
830 CHECK_GL_ERROR("SetDataTransform", "glLoadIdentity");
832 double aspect
= double(y_size
) / double(x_size
);
834 GLdouble near_plane
= 1.0;
836 GLdouble lr
= near_plane
* tan(rad(25.0));
837 GLdouble far_plane
= m_VolumeDiameter
* 5 + near_plane
; // FIXME: work out properly
838 GLdouble tb
= lr
* aspect
;
839 glFrustum(-lr
, lr
, -tb
, tb
, near_plane
, far_plane
);
840 CHECK_GL_ERROR("SetViewportAndProjection", "glFrustum");
843 assert(m_Scale
!= 0.0);
844 GLdouble lr
= m_VolumeDiameter
/ m_Scale
* 0.5;
845 GLdouble far_plane
= m_VolumeDiameter
+ near_plane
;
852 glOrtho(-lr
, lr
, -tb
, tb
, near_plane
, far_plane
);
853 CHECK_GL_ERROR("SetViewportAndProjection", "glOrtho");
856 // Set the modelview transform for drawing data.
857 glMatrixMode(GL_MODELVIEW
);
858 CHECK_GL_ERROR("SetDataTransform", "glMatrixMode");
860 CHECK_GL_ERROR("SetDataTransform", "glLoadIdentity");
862 glTranslated(0.0, 0.0, -near_plane
);
864 glTranslated(0.0, 0.0, -0.5 * m_VolumeDiameter
);
866 CHECK_GL_ERROR("SetDataTransform", "glTranslated");
867 // Get axes the correct way around (z upwards, y into screen)
868 glRotated(-90.0, 1.0, 0.0, 0.0);
869 CHECK_GL_ERROR("SetDataTransform", "glRotated");
870 glRotated(-m_Tilt
, 1.0, 0.0, 0.0);
871 CHECK_GL_ERROR("SetDataTransform", "glRotated");
872 glRotated(m_Pan
, 0.0, 0.0, 1.0);
873 CHECK_GL_ERROR("SetDataTransform", "CopyToOpenGL");
875 glTranslated(m_Translation
.GetX(),
876 m_Translation
.GetY(),
877 m_Translation
.GetZ());
878 CHECK_GL_ERROR("SetDataTransform", "glTranslated");
881 // Save projection matrix.
882 glGetDoublev(GL_PROJECTION_MATRIX
, projection_matrix
);
883 CHECK_GL_ERROR("SetDataTransform", "glGetDoublev");
885 // Save viewport coordinates.
886 glGetIntegerv(GL_VIEWPORT
, viewport
);
887 CHECK_GL_ERROR("SetDataTransform", "glGetIntegerv");
889 // Save modelview matrix.
890 glGetDoublev(GL_MODELVIEW_MATRIX
, modelview_matrix
);
891 CHECK_GL_ERROR("SetDataTransform", "glGetDoublev");
893 if (!m_Perspective
) {
894 // Adjust the translation so we don't change the Z position of the model
896 gluProject(m_Translation
.GetX(),
897 m_Translation
.GetY(),
898 m_Translation
.GetZ(),
899 modelview_matrix
, projection_matrix
, viewport
,
902 gluUnProject(X
, Y
, 0.5, modelview_matrix
, projection_matrix
, viewport
,
904 glTranslated(Tx
, Ty
, Tz
);
905 CHECK_GL_ERROR("SetDataTransform", "glTranslated");
906 glGetDoublev(GL_MODELVIEW_MATRIX
, modelview_matrix
);
909 glEnable(GL_DEPTH_TEST
);
910 CHECK_GL_ERROR("SetDataTransform", "glEnable GL_DEPTH_TEST");
913 glBindTexture(GL_TEXTURE_2D
, m_Texture
);
914 glEnable(GL_TEXTURE_2D
);
915 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
916 CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_WRAP_S");
917 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
918 CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_WRAP_T");
919 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
920 CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_MAG_FILTER");
921 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
922 GL_LINEAR_MIPMAP_LINEAR
);
923 CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_MIN_FILTER");
924 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
);
926 glDisable(GL_TEXTURE_2D
);
929 glFogf(GL_FOG_START
, near_plane
);
930 glFogf(GL_FOG_END
, near_plane
+ m_VolumeDiameter
);
937 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
939 glEnable(GL_LINE_SMOOTH
);
941 glDisable(GL_LINE_SMOOTH
);
945 void GLACanvas::SetIndicatorTransform()
947 list_flags
|= NEVER_CACHE
;
949 // Set the modelview transform and projection for drawing indicators.
951 glDisable(GL_DEPTH_TEST
);
952 CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_DEPTH_TEST");
954 CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_FOG");
956 // Just a simple 2D projection.
957 glMatrixMode(GL_PROJECTION
);
958 CHECK_GL_ERROR("SetIndicatorTransform", "glMatrixMode");
960 CHECK_GL_ERROR("SetIndicatorTransform", "glLoadIdentity (2)");
961 gluOrtho2D(0, x_size
, 0, y_size
);
962 CHECK_GL_ERROR("SetIndicatorTransform", "gluOrtho2D");
964 // No modelview transform.
965 glMatrixMode(GL_MODELVIEW
);
966 CHECK_GL_ERROR("SetIndicatorTransform", "glMatrixMode");
968 CHECK_GL_ERROR("SetIndicatorTransform", "glLoadIdentity");
970 glDisable(GL_TEXTURE_2D
);
971 CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_TEXTURE_2D");
973 CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_BLEND");
974 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
975 CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_WRAP_S");
976 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
977 CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_WRAP_T");
978 glAlphaFunc(GL_GREATER
, 0.5f
);
979 CHECK_GL_ERROR("SetIndicatorTransform", "glAlphaFunc");
980 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
981 CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_MAG_FILTER");
982 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
983 CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_MIN_FILTER");
984 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
985 CHECK_GL_ERROR("SetIndicatorTransform", "glHint");
988 void GLACanvas::FinishDrawing()
990 // Complete a redraw operation.
992 if (double_buffered
) {
996 CHECK_GL_ERROR("FinishDrawing", "glFlush");
1000 void GLACanvas::DrawList(unsigned int l
)
1002 // FIXME: uncomment to disable use of lists for debugging:
1003 // GenerateList(l); return;
1004 if (l
>= drawing_lists
.size()) drawing_lists
.resize(l
+ 1);
1006 // We generate the OpenGL lists lazily to minimise delays on startup.
1007 // So check if we need to generate the OpenGL list now.
1008 if (drawing_lists
[l
].need_to_generate()) {
1009 // Clear list_flags so that we can note what conditions to invalidate
1010 // the cached OpenGL list on.
1014 printf("generating list #%u... ", l
);
1019 printf("done (%d vertices)\n", m_Vertices
);
1021 drawing_lists
[l
].finalise(list_flags
);
1024 if (!drawing_lists
[l
].DrawList()) {
1025 // That list isn't cached (which means it probably can't usefully be
1031 void GLACanvas::DrawListZPrepass(unsigned int l
)
1033 glColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1035 glDepthMask(GL_FALSE
);
1036 glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1037 glDepthFunc(GL_EQUAL
);
1039 glDepthMask(GL_TRUE
);
1040 glDepthFunc(GL_LESS
);
1043 void GLACanvas::DrawList2D(unsigned int l
, glaCoord x
, glaCoord y
, double rotation
)
1045 glMatrixMode(GL_PROJECTION
);
1046 CHECK_GL_ERROR("DrawList2D", "glMatrixMode");
1048 CHECK_GL_ERROR("DrawList2D", "glPushMatrix");
1049 glTranslated(x
, y
, 0);
1050 CHECK_GL_ERROR("DrawList2D", "glTranslated");
1051 if (rotation
!= 0.0) {
1052 glRotated(rotation
, 0, 0, -1);
1053 CHECK_GL_ERROR("DrawList2D", "glRotated");
1056 glMatrixMode(GL_PROJECTION
);
1057 CHECK_GL_ERROR("DrawList2D", "glMatrixMode 2");
1059 CHECK_GL_ERROR("DrawList2D", "glPopMatrix");
1062 void GLACanvas::SetColour(const GLAPen
& pen
, double rgb_scale
)
1064 // Set the colour for subsequent operations.
1065 glColor4f(pen
.GetRed() * rgb_scale
, pen
.GetGreen() * rgb_scale
,
1066 pen
.GetBlue() * rgb_scale
, alpha
);
1069 void GLACanvas::SetColour(const GLAPen
& pen
)
1071 // Set the colour for subsequent operations.
1072 glColor4d(pen
.components
[0], pen
.components
[1], pen
.components
[2], alpha
);
1075 void GLACanvas::SetColour(gla_colour colour
, double rgb_scale
)
1077 // Set the colour for subsequent operations.
1079 glColor4f(COLOURS
[colour
].r
* rgb_scale
,
1080 COLOURS
[colour
].g
* rgb_scale
,
1081 COLOURS
[colour
].b
* rgb_scale
,
1085 void GLACanvas::SetColour(gla_colour colour
)
1087 // Set the colour for subsequent operations.
1089 glColor3ubv(&COLOURS
[colour
].r
);
1091 glColor4ub(COLOURS
[colour
].r
,
1094 (unsigned char)(255 * alpha
));
1098 void GLACanvas::DrawText(glaCoord x
, glaCoord y
, glaCoord z
, const wxString
& str
)
1100 // Draw a text string on the current buffer in the current font.
1101 glRasterPos3d(x
, y
, z
);
1102 CHECK_GL_ERROR("DrawText", "glRasterPos3d");
1103 m_Font
.write_string(str
.data(), str
.size());
1106 void GLACanvas::DrawIndicatorText(int x
, int y
, const wxString
& str
)
1108 glRasterPos2d(x
, y
);
1109 CHECK_GL_ERROR("DrawIndicatorText", "glRasterPos2d");
1110 m_Font
.write_string(str
.data(), str
.size());
1113 void GLACanvas::GetTextExtent(const wxString
& str
, int * x_ext
, int * y_ext
) const
1115 m_Font
.get_text_extent(str
.data(), str
.size(), x_ext
, y_ext
);
1118 void GLACanvas::BeginQuadrilaterals()
1120 // Commence drawing of quadrilaterals.
1125 void GLACanvas::EndQuadrilaterals()
1127 // Finish drawing of quadrilaterals.
1130 CHECK_GL_ERROR("EndQuadrilaterals", "glEnd GL_QUADS");
1133 void GLACanvas::BeginLines()
1135 // Commence drawing of a set of lines.
1140 void GLACanvas::EndLines()
1142 // Finish drawing of a set of lines.
1145 CHECK_GL_ERROR("EndLines", "glEnd GL_LINES");
1148 void GLACanvas::BeginTriangles()
1150 // Commence drawing of a set of triangles.
1152 glBegin(GL_TRIANGLES
);
1155 void GLACanvas::EndTriangles()
1157 // Finish drawing of a set of triangles.
1160 CHECK_GL_ERROR("EndTriangles", "glEnd GL_TRIANGLES");
1163 void GLACanvas::BeginTriangleStrip()
1165 // Commence drawing of a triangle strip.
1167 glBegin(GL_TRIANGLE_STRIP
);
1170 void GLACanvas::EndTriangleStrip()
1172 // Finish drawing of a triangle strip.
1175 CHECK_GL_ERROR("EndTriangleStrip", "glEnd GL_TRIANGLE_STRIP");
1178 void GLACanvas::BeginPolyline()
1180 // Commence drawing of a polyline.
1182 glBegin(GL_LINE_STRIP
);
1185 void GLACanvas::EndPolyline()
1187 // Finish drawing of a polyline.
1190 CHECK_GL_ERROR("EndPolyline", "glEnd GL_LINE_STRIP");
1193 void GLACanvas::BeginPolyloop()
1195 // Commence drawing of a polyloop.
1197 glBegin(GL_LINE_LOOP
);
1200 void GLACanvas::EndPolyloop()
1202 // Finish drawing of a polyloop.
1205 CHECK_GL_ERROR("EndPolyloop", "glEnd GL_LINE_LOOP");
1208 void GLACanvas::BeginPolygon()
1210 // Commence drawing of a polygon.
1212 glBegin(GL_POLYGON
);
1215 void GLACanvas::EndPolygon()
1217 // Finish drawing of a polygon.
1220 CHECK_GL_ERROR("EndPolygon", "glEnd GL_POLYGON");
1223 void GLACanvas::BeginPoints()
1225 // Commence drawing points.
1227 glPushAttrib(GL_POINT_BIT
);
1228 CHECK_GL_ERROR("BeginPoints", "glPushAttrib");
1230 CHECK_GL_ERROR("BeginPoints", "glPointSize");
1234 void GLACanvas::EndPoints()
1236 // Finish drawing points.
1239 CHECK_GL_ERROR("EndPoints", "glEnd GL_POINTS");
1241 CHECK_GL_ERROR("EndPoints", "glPopAttrib");
1244 void GLACanvas::PlaceVertex(glaCoord x
, glaCoord y
, glaCoord z
)
1246 // Place a vertex for the current object being drawn.
1251 glVertex3d(x
, y
, z
);
1254 void GLACanvas::PlaceVertex(glaCoord x
, glaCoord y
, glaCoord z
,
1255 glaTexCoord tex_x
, glaTexCoord tex_y
)
1257 // Place a vertex for the current object being drawn.
1262 glTexCoord2f(tex_x
, tex_y
);
1263 glVertex3d(x
, y
, z
);
1266 void GLACanvas::PlaceIndicatorVertex(glaCoord x
, glaCoord y
)
1268 // Place a vertex for the current indicator object being drawn.
1270 PlaceVertex(x
, y
, 0.0);
1273 void GLACanvas::BeginBlobs()
1275 // Commence drawing of a set of blobs.
1276 if (blob_method
== SPRITE
) {
1277 glPushAttrib(GL_ENABLE_BIT
|GL_POINT_BIT
);
1278 CHECK_GL_ERROR("BeginBlobs", "glPushAttrib");
1279 glBindTexture(GL_TEXTURE_2D
, m_BlobTexture
);
1280 CHECK_GL_ERROR("BeginBlobs", "glBindTexture");
1281 glEnable(GL_ALPHA_TEST
);
1282 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_ALPHA_TEST");
1284 CHECK_GL_ERROR("BeginBlobs", "glPointSize");
1285 glTexEnvi(GL_POINT_SPRITE
, GL_COORD_REPLACE
, GL_TRUE
);
1286 CHECK_GL_ERROR("BeginBlobs", "glTexEnvi GL_POINT_SPRITE");
1287 glEnable(GL_TEXTURE_2D
);
1288 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_TEXTURE_2D");
1289 glEnable(GL_POINT_SPRITE
);
1290 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_POINT_SPRITE");
1292 } else if (blob_method
== POINT
) {
1293 glPushAttrib(GL_ENABLE_BIT
);
1294 CHECK_GL_ERROR("BeginBlobs", "glPushAttrib");
1295 glEnable(GL_ALPHA_TEST
);
1296 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_ALPHA_TEST");
1297 glEnable(GL_POINT_SMOOTH
);
1298 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_POINT_SMOOTH");
1301 glPushAttrib(GL_TRANSFORM_BIT
|GL_VIEWPORT_BIT
|GL_ENABLE_BIT
);
1302 CHECK_GL_ERROR("BeginBlobs", "glPushAttrib");
1303 SetIndicatorTransform();
1304 glEnable(GL_DEPTH_TEST
);
1305 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_DEPTH_TEST");
1310 void GLACanvas::EndBlobs()
1312 // Finish drawing of a set of blobs.
1314 if (blob_method
!= LINES
) {
1315 CHECK_GL_ERROR("EndBlobs", "glEnd GL_POINTS");
1317 CHECK_GL_ERROR("EndBlobs", "glEnd GL_LINES");
1320 CHECK_GL_ERROR("EndBlobs", "glPopAttrib");
1323 void GLACanvas::DrawBlob(glaCoord x
, glaCoord y
, glaCoord z
)
1325 if (blob_method
!= LINES
) {
1327 PlaceVertex(x
, y
, z
);
1330 if (!Transform(Vector3(x
, y
, z
), &X
, &Y
, &Z
)) {
1331 printf("bad transform\n");
1334 // Stuff behind us (in perspective view) will get clipped,
1335 // but we can save effort with a cheap check here.
1338 X
-= BLOB_DIAMETER
* 0.5;
1339 Y
-= BLOB_DIAMETER
* 0.5;
1341 PlaceVertex(X
, Y
+ 1, Z
);
1342 PlaceVertex(X
, Y
+ (BLOB_DIAMETER
- 1), Z
);
1344 for (int i
= 1; i
< (BLOB_DIAMETER
- 1); ++i
) {
1345 PlaceVertex(X
+ i
, Y
, Z
);
1346 PlaceVertex(X
+ i
, Y
+ BLOB_DIAMETER
, Z
);
1349 PlaceVertex(X
+ (BLOB_DIAMETER
- 1), Y
+ 1, Z
);
1350 PlaceVertex(X
+ (BLOB_DIAMETER
- 1), Y
+ (BLOB_DIAMETER
- 1), Z
);
1357 void GLACanvas::DrawBlob(glaCoord x
, glaCoord y
)
1359 if (blob_method
!= LINES
) {
1361 PlaceVertex(x
, y
, 0);
1363 x
-= BLOB_DIAMETER
* 0.5;
1364 y
-= BLOB_DIAMETER
* 0.5;
1366 PlaceVertex(x
, y
+ 1, 0);
1367 PlaceVertex(x
, y
+ (BLOB_DIAMETER
- 1), 0);
1369 for (int i
= 1; i
< (BLOB_DIAMETER
- 1); ++i
) {
1370 PlaceVertex(x
+ i
, y
, 0);
1371 PlaceVertex(x
+ i
, y
+ BLOB_DIAMETER
, 0);
1374 PlaceVertex(x
+ (BLOB_DIAMETER
- 1), y
+ 1, 0);
1375 PlaceVertex(x
+ (BLOB_DIAMETER
- 1), y
+ (BLOB_DIAMETER
- 1), 0);
1382 void GLACanvas::BeginCrosses()
1385 if (cross_method
== SPRITE
) {
1386 list_flags
|= NEVER_CACHE
;
1388 glPushAttrib(GL_ENABLE_BIT
|GL_POINT_BIT
);
1389 CHECK_GL_ERROR("BeginCrosses", "glPushAttrib");
1390 glBindTexture(GL_TEXTURE_2D
, m_CrossTexture
);
1391 CHECK_GL_ERROR("BeginCrosses", "glBindTexture");
1392 glEnable(GL_ALPHA_TEST
);
1393 CHECK_GL_ERROR("BeginCrosses", "glEnable GL_ALPHA_TEST");
1395 CHECK_GL_ERROR("BeginCrosses", "glPointSize");
1396 glTexEnvi(GL_POINT_SPRITE
, GL_COORD_REPLACE
, GL_TRUE
);
1397 CHECK_GL_ERROR("BeginCrosses", "glTexEnvi GL_POINT_SPRITE");
1398 glEnable(GL_TEXTURE_2D
);
1399 CHECK_GL_ERROR("BeginCrosses", "glEnable GL_TEXTURE_2D");
1400 glEnable(GL_POINT_SPRITE
);
1401 CHECK_GL_ERROR("BeginCrosses", "glEnable GL_POINT_SPRITE");
1404 // To get the crosses to appear at a constant size and orientation on
1405 // screen, we plot them in the Indicator transform coordinates (which
1406 // unfortunately means they can't be usefully put in an opengl display
1408 glPushAttrib(GL_TRANSFORM_BIT
|GL_VIEWPORT_BIT
|GL_ENABLE_BIT
);
1409 CHECK_GL_ERROR("BeginCrosses", "glPushAttrib 2");
1410 SetIndicatorTransform();
1411 // Align line drawing to pixel centres to get pixel-perfect rendering
1412 // (graphics card and driver bugs aside).
1413 glTranslated(-0.5, -0.5, 0);
1414 CHECK_GL_ERROR("BeginCrosses", "glTranslated");
1415 glEnable(GL_DEPTH_TEST
);
1416 CHECK_GL_ERROR("BeginCrosses", "glEnable GL_DEPTH_TEST");
1421 void GLACanvas::EndCrosses()
1424 if (cross_method
== SPRITE
) {
1425 CHECK_GL_ERROR("EndCrosses", "glEnd GL_POINTS");
1427 CHECK_GL_ERROR("EndCrosses", "glEnd GL_LINES");
1430 CHECK_GL_ERROR("EndCrosses", "glPopAttrib");
1433 void GLACanvas::DrawCross(glaCoord x
, glaCoord y
, glaCoord z
)
1435 if (cross_method
== SPRITE
) {
1437 PlaceVertex(x
, y
, z
);
1440 if (!Transform(Vector3(x
, y
, z
), &X
, &Y
, &Z
)) {
1441 printf("bad transform\n");
1444 // Stuff behind us (in perspective view) will get clipped,
1445 // but we can save effort with a cheap check here.
1448 // Round to integers before adding on the offsets for the
1449 // cross arms to avoid uneven crosses.
1452 // Need to extend lines by an extra pixel (which shouldn't get drawn by
1453 // the diamond-exit rule).
1454 PlaceVertex(X
- 3, Y
- 3, Z
);
1455 PlaceVertex(X
+ 4, Y
+ 4, Z
);
1456 PlaceVertex(X
- 3, Y
+ 3, Z
);
1457 PlaceVertex(X
+ 4, Y
- 4, Z
);
1464 void GLACanvas::DrawRing(glaCoord x
, glaCoord y
)
1466 // Draw an unfilled circle of radius 4
1468 // Round to integers to get an even ring.
1472 glBegin(GL_LINE_LOOP
);
1473 PlaceIndicatorVertex(x
+ 3.5, y
- 1.5);
1474 PlaceIndicatorVertex(x
+ 1.5, y
- 3.5);
1475 PlaceIndicatorVertex(x
- 1.5, y
- 3.5);
1476 PlaceIndicatorVertex(x
- 3.5, y
- 1.5);
1477 PlaceIndicatorVertex(x
- 3.5, y
+ 1.5);
1478 PlaceIndicatorVertex(x
- 1.5, y
+ 3.5);
1479 PlaceIndicatorVertex(x
+ 1.5, y
+ 3.5);
1480 PlaceIndicatorVertex(x
+ 3.5, y
+ 1.5);
1482 CHECK_GL_ERROR("DrawRing", "glEnd GL_LINE_LOOP");
1485 void GLACanvas::DrawRectangle(gla_colour fill
, gla_colour edge
,
1486 glaCoord x0
, glaCoord y0
, glaCoord w
, glaCoord h
)
1488 // Draw a filled rectangle with an edge in the indicator plane.
1489 // (x0, y0) specify the bottom-left corner of the rectangle and (w, h) the
1493 BeginQuadrilaterals();
1494 PlaceIndicatorVertex(x0
, y0
);
1495 PlaceIndicatorVertex(x0
+ w
, y0
);
1496 PlaceIndicatorVertex(x0
+ w
, y0
+ h
);
1497 PlaceIndicatorVertex(x0
, y0
+ h
);
1498 EndQuadrilaterals();
1503 PlaceIndicatorVertex(x0
, y0
);
1504 PlaceIndicatorVertex(x0
+ w
, y0
);
1505 PlaceIndicatorVertex(x0
+ w
, y0
+ h
);
1506 PlaceIndicatorVertex(x0
, y0
+ h
);
1507 PlaceIndicatorVertex(x0
, y0
);
1513 GLACanvas::DrawShadedRectangle(const GLAPen
& fill_bot
, const GLAPen
& fill_top
,
1514 glaCoord x0
, glaCoord y0
,
1515 glaCoord w
, glaCoord h
)
1517 // Draw a graduated filled rectangle in the indicator plane.
1518 // (x0, y0) specify the bottom-left corner of the rectangle and (w, h) the
1521 glShadeModel(GL_SMOOTH
);
1522 CHECK_GL_ERROR("DrawShadedRectangle", "glShadeModel GL_SMOOTH");
1523 BeginQuadrilaterals();
1524 SetColour(fill_bot
);
1525 PlaceIndicatorVertex(x0
, y0
);
1526 PlaceIndicatorVertex(x0
+ w
, y0
);
1527 SetColour(fill_top
);
1528 PlaceIndicatorVertex(x0
+ w
, y0
+ h
);
1529 PlaceIndicatorVertex(x0
, y0
+ h
);
1530 EndQuadrilaterals();
1531 glShadeModel(GL_FLAT
);
1532 CHECK_GL_ERROR("DrawShadedRectangle", "glShadeModel GL_FLAT");
1535 void GLACanvas::DrawCircle(gla_colour edge
, gla_colour fill
,
1536 glaCoord cx
, glaCoord cy
, glaCoord radius
)
1538 // Draw a filled circle with an edge.
1540 glMatrixMode(GL_MODELVIEW
);
1541 CHECK_GL_ERROR("DrawCircle", "glMatrixMode");
1543 CHECK_GL_ERROR("DrawCircle", "glPushMatrix");
1544 glTranslated(cx
, cy
, 0.0);
1545 CHECK_GL_ERROR("DrawCircle", "glTranslated");
1547 gluDisk(m_Quadric
, 0.0, radius
, 36, 1);
1548 CHECK_GL_ERROR("DrawCircle", "gluDisk");
1550 gluDisk(m_Quadric
, radius
- 1.0, radius
, 36, 1);
1551 CHECK_GL_ERROR("DrawCircle", "gluDisk (2)");
1553 CHECK_GL_ERROR("DrawCircle", "glPopMatrix");
1556 void GLACanvas::DrawSemicircle(gla_colour edge
, gla_colour fill
,
1557 glaCoord cx
, glaCoord cy
,
1558 glaCoord radius
, glaCoord start
)
1560 // Draw a filled semicircle with an edge.
1561 // The semicircle extends from "start" deg to "start"+180 deg (increasing
1562 // clockwise, 0 deg upwards).
1564 glMatrixMode(GL_MODELVIEW
);
1565 CHECK_GL_ERROR("DrawSemicircle", "glMatrixMode");
1567 CHECK_GL_ERROR("DrawSemicircle", "glPushMatrix");
1568 glTranslated(cx
, cy
, 0.0);
1569 CHECK_GL_ERROR("DrawSemicircle", "glTranslated");
1571 gluPartialDisk(m_Quadric
, 0.0, radius
, 36, 1, start
, 180.0);
1572 CHECK_GL_ERROR("DrawSemicircle", "gluPartialDisk");
1574 gluPartialDisk(m_Quadric
, radius
- 1.0, radius
, 36, 1, start
, 180.0);
1575 CHECK_GL_ERROR("DrawSemicircle", "gluPartialDisk (2)");
1577 CHECK_GL_ERROR("DrawSemicircle", "glPopMatrix");
1580 void GLACanvas::EnableDashedLines()
1582 // Enable dashed lines, and start drawing in them.
1584 glLineStipple(1, 0x3333);
1585 CHECK_GL_ERROR("EnableDashedLines", "glLineStipple");
1586 glEnable(GL_LINE_STIPPLE
);
1587 CHECK_GL_ERROR("EnableDashedLines", "glEnable GL_LINE_STIPPLE");
1590 void GLACanvas::DisableDashedLines()
1592 glDisable(GL_LINE_STIPPLE
);
1593 CHECK_GL_ERROR("DisableDashedLines", "glDisable GL_LINE_STIPPLE");
1596 bool GLACanvas::Transform(const Vector3
& v
,
1597 glaCoord
* x_out
, glaCoord
* y_out
, glaCoord
* z_out
) const
1599 // Convert from data coordinates to screen coordinates.
1601 // Perform the projection.
1602 return gluProject(v
.GetX(), v
.GetY(), v
.GetZ(),
1603 modelview_matrix
, projection_matrix
, viewport
,
1604 x_out
, y_out
, z_out
);
1607 void GLACanvas::ReverseTransform(double x
, double y
,
1608 glaCoord
* x_out
, glaCoord
* y_out
, glaCoord
* z_out
) const
1610 // Convert from screen coordinates to data coordinates.
1612 // Perform the projection.
1613 gluUnProject(x
, y
, 0.0, modelview_matrix
, projection_matrix
, viewport
,
1614 x_out
, y_out
, z_out
);
1615 CHECK_GL_ERROR("ReverseTransform", "gluUnProject");
1618 double GLACanvas::SurveyUnitsAcrossViewport() const
1620 // Measure the current viewport in survey units, taking into account the
1621 // current display scale.
1623 assert(m_Scale
!= 0.0);
1624 list_flags
|= INVALIDATE_ON_SCALE
;
1625 double result
= m_VolumeDiameter
/ m_Scale
;
1626 if (y_size
< x_size
) {
1627 result
= result
* x_size
/ y_size
;
1632 void GLACanvas::ToggleSmoothShading()
1634 m_SmoothShading
= !m_SmoothShading
;
1637 void GLACanvas::ToggleTextured()
1639 m_Textured
= !m_Textured
;
1640 if (m_Textured
&& m_Texture
== 0) {
1641 glGenTextures(1, &m_Texture
);
1642 CHECK_GL_ERROR("ToggleTextured", "glGenTextures");
1644 glBindTexture(GL_TEXTURE_2D
, m_Texture
);
1645 CHECK_GL_ERROR("ToggleTextured", "glBindTexture");
1647 ::wxInitAllImageHandlers();
1650 wxString
texture(wmsg_cfgpth());
1651 texture
+= wxCONFIG_PATH_SEPARATOR
;
1652 texture
+= wxT("images");
1653 texture
+= wxCONFIG_PATH_SEPARATOR
;
1654 texture
+= wxT("texture.png");
1655 if (!img
.LoadFile(texture
, wxBITMAP_TYPE_PNG
)) {
1657 fprintf(stderr
, "Couldn't load image.\n");
1661 // Generate mipmaps.
1662 gluBuild2DMipmaps(GL_TEXTURE_2D
, GL_RGB
, // was GL_LUMINANCE
1663 img
.GetWidth(), img
.GetHeight(),
1664 GL_RGB
, GL_UNSIGNED_BYTE
, img
.GetData());
1665 CHECK_GL_ERROR("ToggleTextured", "gluBuild2DMipmaps");
1667 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
1668 CHECK_GL_ERROR("ToggleTextured", "glTexEnvi");
1672 bool GLACanvas::SaveScreenshot(const wxString
& fnm
, wxBitmapType type
) const
1674 const int width
= x_size
;
1675 const int height
= y_size
;
1676 unsigned char *pixels
= (unsigned char *)malloc(3 * width
* (height
+ 1));
1677 if (!pixels
) return false;
1678 glReadPixels(0, 0, width
, height
, GL_RGB
, GL_UNSIGNED_BYTE
, (GLvoid
*)pixels
);
1679 CHECK_GL_ERROR("SaveScreenshot", "glReadPixels");
1680 unsigned char * tmp_row
= pixels
+ 3 * width
* height
;
1681 // We need to flip the image vertically - this approach should be more
1682 // efficient than using wxImage::Mirror(false) as that creates a new
1684 for (int y
= height
/ 2 - 1; y
>= 0; --y
) {
1685 unsigned char * upper
= pixels
+ 3 * width
* y
;
1686 unsigned char * lower
= pixels
+ 3 * width
* (height
- y
- 1);
1687 memcpy(tmp_row
, upper
, 3 * width
);
1688 memcpy(upper
, lower
, 3 * width
);
1689 memcpy(lower
, tmp_row
, 3 * width
);
1691 // NB wxImage constructor calls free(pixels) for us.
1692 wxImage
grab(width
, height
, pixels
);
1693 return grab
.SaveFile(fnm
, type
);
1696 bool GLACanvas::CheckVisualFidelity(const unsigned char * target
) const
1698 unsigned char pixels
[3 * 8 * 8];
1699 if (double_buffered
) {
1700 glReadBuffer(GL_BACK
);
1701 CHECK_GL_ERROR("FirstShow", "glReadBuffer");
1703 glReadPixels(x_size
/ 2 - 4, y_size
/ 2 - 5, 8, 8,
1704 GL_RGB
, GL_UNSIGNED_BYTE
, (GLvoid
*)pixels
);
1705 CHECK_GL_ERROR("CheckVisualFidelity", "glReadPixels");
1706 if (double_buffered
) {
1707 glReadBuffer(GL_FRONT
);
1708 CHECK_GL_ERROR("FirstShow", "glReadBuffer");
1711 // Show what got drawn and what was expected for debugging.
1712 for (int y
= 0; y
< 8; ++y
) {
1713 for (int x
= 0; x
< 8; ++x
) {
1714 int o
= (y
* 8 + x
) * 3;
1715 printf("%c", pixels
[o
] ? 'X' : '.');
1718 for (int x
= 0; x
< 8; ++x
) {
1719 int o
= (y
* 8 + x
) * 3;
1720 printf("%c", target
[o
] ? 'X' : '.');
1725 return (memcmp(pixels
, target
, sizeof(pixels
)) == 0);
1728 void GLACanvas::ReadPixels(int width
, int height
, unsigned char * buf
) const
1730 CHECK_GL_ERROR("ReadPixels", "glReadPixels");
1731 glReadPixels(0, 0, width
, height
, GL_RGB
, GL_UNSIGNED_BYTE
, (GLvoid
*)buf
);
1734 void GLACanvas::PolygonOffset(bool on
) const
1737 glPolygonOffset(1.0, 1.0);
1738 glEnable(GL_POLYGON_OFFSET_FILL
);
1740 glDisable(GL_POLYGON_OFFSET_FILL
);