4 // OpenGL implementation for the GLA abstraction layer.
6 // Copyright (C) 2002-2003,2005 Mark R. Shinwell
7 // Copyright (C) 2003,2004,2005,2006,2007,2010,2011,2012,2013,2014,2015,2017,2018 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
28 #include <wx/confbase.h>
35 #include "gllogerror.h"
41 #elif defined HAVE_OPENGL_GL_H
42 # include <OpenGL/gl.h>
45 #ifdef HAVE_GL_GLEXT_H
46 # include <GL/glext.h>
47 #elif defined HAVE_OPENGL_GLEXT_H
48 # include <OpenGL/glext.h>
51 #ifndef GL_POINT_SIZE_MAX
52 #define GL_POINT_SIZE_MAX 0x8127
54 #ifndef GL_POINT_SPRITE
55 #define GL_POINT_SPRITE 0x8861
57 #ifndef GL_COORD_REPLACE
58 #define GL_COORD_REPLACE 0x8862
60 // GL_POINT_SIZE_RANGE is deprecated in OpenGL 1.2 and later, and replaced by
61 // GL_SMOOTH_POINT_SIZE_RANGE.
62 #ifndef GL_SMOOTH_POINT_SIZE_RANGE
63 #define GL_SMOOTH_POINT_SIZE_RANGE GL_POINT_SIZE_RANGE
65 // GL_POINT_SIZE_GRANULARITY is deprecated in OpenGL 1.2 and later, and
66 // replaced by GL_SMOOTH_POINT_SIZE_GRANULARITY.
67 #ifndef GL_SMOOTH_POINT_SIZE_GRANULARITY
68 #define GL_SMOOTH_POINT_SIZE_GRANULARITY GL_POINT_SIZE_GRANULARITY
70 // GL_ALIASED_POINT_SIZE_RANGE was added in OpenGL 1.2.
71 #ifndef GL_ALIASED_POINT_SIZE_RANGE
72 #define GL_ALIASED_POINT_SIZE_RANGE 0x846D
77 const int BLOB_DIAMETER
= 5;
79 #define BLOB_TEXTURE \
80 o, o, o, o, o, o, o, o,\
81 o, o, o, o, o, o, o, o,\
82 o, o, I, I, I, o, o, o,\
83 o, I, I, I, I, I, o, o,\
84 o, I, I, I, I, I, o, o,\
85 o, I, I, I, I, I, o, o,\
86 o, o, I, I, I, o, o, o,\
87 o, o, o, o, o, o, o, o
89 #define CROSS_TEXTURE \
90 o, o, o, o, o, o, o, o,\
91 I, o, o, o, o, o, I, o,\
92 o, I, o, o, o, I, o, o,\
93 o, o, I, o, I, o, o, o,\
94 o, o, o, I, o, o, o, o,\
95 o, o, I, o, I, o, o, o,\
96 o, I, o, o, o, I, o, o,\
97 I, o, o, o, o, o, I, o
99 // Declared in gllogerror.h.
100 bool opengl_initialised
= false;
102 static bool double_buffered
= false;
104 static const int* wx_gl_attribs
= NULL
;
107 GLACanvas::check_visual()
109 static const int wx_gl_attribs_full
[] = {
112 WX_GL_DEPTH_SIZE
, 16,
116 // Use a double-buffered visual if available, as it will give much smoother
118 double_buffered
= true;
119 wx_gl_attribs
= wx_gl_attribs_full
;
120 if (!IsDisplaySupported(wx_gl_attribs
)) {
122 if (!IsDisplaySupported(wx_gl_attribs
)) {
125 double_buffered
= false;
130 string
GetGLSystemDescription()
132 // If OpenGL isn't initialised we may get a SEGV from glGetString.
133 if (!opengl_initialised
)
134 return "No OpenGL information available yet - try opening a file.";
135 const char *p
= (const char*)glGetString(GL_VERSION
);
137 return "Couldn't read OpenGL version!";
143 info
+= (const char*)glGetString(GL_VENDOR
);
145 info
+= (const char*)glGetString(GL_RENDERER
);
146 #if defined __WXGTK__ || defined __WXX11__ || defined __WXMOTIF__
147 info
+= string_format("\nGLX %0.1f\n", wxGLCanvas::GetGLXVersion() * 0.1);
152 GLint red
, green
, blue
;
153 glGetIntegerv(GL_RED_BITS
, &red
);
154 glGetIntegerv(GL_GREEN_BITS
, &green
);
155 glGetIntegerv(GL_BLUE_BITS
, &blue
);
156 GLint max_texture_size
;
157 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &max_texture_size
);
158 GLint max_viewport
[2];
159 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, max_viewport
);
160 GLdouble point_size_range
[2];
161 glGetDoublev(GL_SMOOTH_POINT_SIZE_RANGE
, point_size_range
);
162 GLdouble point_size_granularity
;
163 glGetDoublev(GL_SMOOTH_POINT_SIZE_GRANULARITY
, &point_size_granularity
);
164 info
+= string_format("R%dG%dB%d\n"
165 "Max Texture size: %dx%d\n"
166 "Max Viewport size: %dx%d\n"
167 "Smooth Point Size %.3f-%.3f (granularity %.3f)",
168 (int)red
, (int)green
, (int)blue
,
169 (int)max_texture_size
, (int)max_texture_size
,
170 (int)max_viewport
[0], (int)max_viewport
[1],
171 point_size_range
[0], point_size_range
[1],
172 point_size_granularity
);
173 glGetDoublev(GL_ALIASED_POINT_SIZE_RANGE
, point_size_range
);
174 if (glGetError() != GL_INVALID_ENUM
) {
175 info
+= string_format("\nAliased point size %.3f-%.3f",
176 point_size_range
[0], point_size_range
[1]);
179 info
+= "\nDouble buffered: ";
185 const GLubyte
* gl_extensions
= glGetString(GL_EXTENSIONS
);
186 if (*gl_extensions
) {
188 info
+= (const char*)gl_extensions
;
194 glpoint_sprite_works()
196 // Point sprites provide an easy, fast way for us to draw crosses by
197 // texture mapping GL points.
199 // If we have OpenGL >= 2.0 then we definitely have GL_POINT_SPRITE.
200 // Otherwise see if we have the GL_ARB_point_sprite or GL_NV_point_sprite
203 // The symbolic constants GL_POINT_SPRITE, GL_POINT_SPRITE_ARB, and
204 // GL_POINT_SPRITE_NV all give the same number so it doesn't matter
206 static bool glpoint_sprite
= false;
207 static bool checked
= false;
209 float maxSize
= 0.0f
;
210 glGetFloatv(GL_POINT_SIZE_MAX
, &maxSize
);
212 glpoint_sprite
= (atoi((const char *)glGetString(GL_VERSION
)) >= 2);
213 if (!glpoint_sprite
) {
214 const char * p
= (const char *)glGetString(GL_EXTENSIONS
);
217 if (memcmp(p
, "GL_ARB_point_sprite", 19) == 0) {
219 } else if (memcmp(p
, "GL_NV_point_sprite", 18) == 0) {
224 if (*p
== '\0' || *p
== ' ') {
225 glpoint_sprite
= true;
229 p
= strchr(p
+ 1, ' ');
237 return glpoint_sprite
;
241 log_gl_error(const wxChar
* str
, GLenum error_code
)
244 switch (error_code
) {
245 case GL_INVALID_ENUM
:
246 msg
= "Invalid OpenGL enumerated value";
248 case GL_INVALID_VALUE
:
249 msg
= "Invalid OpenGL numeric argument value";
251 case GL_INVALID_OPERATION
:
252 msg
= "Invalid OpenGL operation";
254 case GL_INVALID_FRAMEBUFFER_OPERATION
:
255 msg
= "Invalid OpenGL framebuffer operation";
257 case GL_OUT_OF_MEMORY
:
258 msg
= wmsg(/*Out of memory*/389);
260 case GL_STACK_UNDERFLOW
:
261 msg
= "OpenGL stack underflow";
263 case GL_STACK_OVERFLOW
:
264 msg
= "OpenGL stack overflow";
267 msg
.Format("Unknown OpenGL error code: %d", int(error_code
));
270 wxLogError(str
, msg
);
279 components
[0] = components
[1] = components
[2] = 0.0;
282 void GLAPen::SetColour(double red
, double green
, double blue
)
285 components
[1] = green
;
286 components
[2] = blue
;
289 double GLAPen::GetRed() const
291 return components
[0];
294 double GLAPen::GetGreen() const
296 return components
[1];
299 double GLAPen::GetBlue() const
301 return components
[2];
304 void GLAPen::Interpolate(const GLAPen
& pen
, double how_far
)
306 components
[0] += how_far
* (pen
.GetRed() - components
[0]);
307 components
[1] += how_far
* (pen
.GetGreen() - components
[1]);
308 components
[2] += how_far
* (pen
.GetBlue() - components
[2]);
311 struct ColourTriple
{
312 // RGB triple: values are from 0-255 inclusive for each component.
313 unsigned char r
, g
, b
;
316 // Order must match that in enum gla_colour[] in gla.h.
317 static const ColourTriple COLOURS
[] = {
318 { 0, 0, 0 }, // black
319 { 100, 100, 100 }, // grey
320 { 180, 180, 180 }, // light grey
321 { 140, 140, 140 }, // light grey 2
322 { 90, 90, 90 }, // dark grey
323 { 255, 255, 255 }, // white
324 { 0, 100, 255}, // turquoise
325 { 0, 255, 40 }, // green
326 { 150, 205, 224 }, // indicator 1
327 { 114, 149, 160 }, // indicator 2
328 { 255, 255, 0 }, // yellow
329 { 255, 0, 0 }, // red
330 { 40, 40, 255 }, // blue
331 { 255, 0, 255 }, // magenta
334 bool GLAList::need_to_generate() {
335 // Bail out if the list is already cached, or can't usefully be cached.
336 if (flags
& (GLACanvas::CACHED
|GLACanvas::NEVER_CACHE
))
339 // Create a new OpenGL list to hold this sequence of drawing
342 gl_list
= glGenLists(1);
343 CHECK_GL_ERROR("GLAList::need_to_generate", "glGenLists");
345 printf("glGenLists(1) returned %u\n", (unsigned)gl_list
);
348 // If we can't create a list for any reason, fall back to just
349 // drawing directly, and flag the list as NEVER_CACHE as there's
350 // unlikely to be much point calling glGenLists() again.
351 flags
= GLACanvas::NEVER_CACHE
;
355 // We should have 256 lists for font drawing and a dozen or so for 2D
356 // and 3D lists. So something is amiss if we've generated 1000 lists,
357 // probably a infinite loop in the lazy list mechanism.
358 assert(gl_list
< 1000);
360 // https://www.opengl.org/resources/faq/technical/displaylist.htm advises:
362 // "Stay away from GL_COMPILE_AND_EXECUTE mode. Instead, create the
363 // list using GL_COMPILE mode, then execute it with glCallList()."
364 glNewList(gl_list
, GL_COMPILE
);
365 CHECK_GL_ERROR("GLAList::need_to_generate", "glNewList");
369 void GLAList::finalise(unsigned int list_flags
)
372 CHECK_GL_ERROR("GLAList::finalise", "glEndList");
373 if (list_flags
& GLACanvas::NEVER_CACHE
) {
374 glDeleteLists(gl_list
, 1);
375 CHECK_GL_ERROR("GLAList::finalise", "glDeleteLists");
377 flags
= GLACanvas::NEVER_CACHE
;
379 flags
= list_flags
| GLACanvas::CACHED
;
383 bool GLAList::DrawList() const {
384 if ((flags
& GLACanvas::CACHED
) == 0)
387 CHECK_GL_ERROR("GLAList::DrawList", "glCallList");
395 BEGIN_EVENT_TABLE(GLACanvas
, wxGLCanvas
)
396 EVT_SIZE(GLACanvas::OnSize
)
399 // Pass wxWANTS_CHARS so that the window gets cursor keys on MS Windows.
400 GLACanvas::GLACanvas(wxWindow
* parent
, int id
)
401 : wxGLCanvas(parent
, id
, wx_gl_attribs
, wxDefaultPosition
,
402 wxDefaultSize
, wxWANTS_CHARS
),
403 ctx(this), m_Translation(), blob_method(UNKNOWN
), cross_method(UNKNOWN
),
412 m_VolumeDiameter
= 1.0;
413 m_SmoothShading
= false;
416 m_Perspective
= false;
423 GLACanvas::~GLACanvas()
428 gluDeleteQuadric(m_Quadric
);
429 CHECK_GL_ERROR("~GLACanvas", "gluDeleteQuadric");
433 void GLACanvas::FirstShow()
435 // Update our record of the client area size and centre.
436 GetClientSize(&x_size
, &y_size
);
437 if (x_size
< 1) x_size
= 1;
438 if (y_size
< 1) y_size
= 1;
440 ctx
.SetCurrent(*this);
441 opengl_initialised
= true;
443 // Set the background colour of the canvas to black.
444 glClearColor(0.0, 0.0, 0.0, 1.0);
445 CHECK_GL_ERROR("FirstShow", "glClearColor");
448 glViewport(0, 0, x_size
, y_size
);
449 CHECK_GL_ERROR("FirstShow", "glViewport");
453 vendor
= wxString((const char *)glGetString(GL_VENDOR
), wxConvUTF8
);
454 renderer
= wxString((const char *)glGetString(GL_RENDERER
), wxConvUTF8
);
456 wxConfigBase
* cfg
= wxConfigBase::Get();
458 if (cfg
->Read(wxT("opengl_survex"), &s
, wxString()) && s
== wxT(VERSION
) &&
459 cfg
->Read(wxT("opengl_vendor"), &s
, wxString()) && s
== vendor
&&
460 cfg
->Read(wxT("opengl_renderer"), &s
, wxString()) && s
== renderer
) {
461 // The survex version, vendor and renderer are the same as those
462 // we cached hints for, so use those hints.
464 if (cfg
->Read(wxT("blob_method"), &v
, 0) &&
465 (v
== SPRITE
|| v
== POINT
|| v
== LINES
)) {
466 // How to draw blobs.
469 if (cfg
->Read(wxT("cross_method"), &v
, 0) &&
470 (v
== SPRITE
|| v
== LINES
)) {
471 // How to draw crosses.
477 if (m_Quadric
) return;
478 // One time initialisation follows.
480 m_Quadric
= gluNewQuadric();
481 CHECK_GL_ERROR("FirstShow", "gluNewQuadric");
483 abort(); // FIXME need to cope somehow
486 glShadeModel(GL_FLAT
);
487 CHECK_GL_ERROR("FirstShow", "glShadeModel");
488 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
); // So text works.
489 CHECK_GL_ERROR("FirstShow", "glPolygonMode");
490 //glAlphaFunc(GL_GREATER, 0.5f);
491 //CHECK_GL_ERROR("FirstShow", "glAlphaFunc");
493 // We want glReadPixels() to read from the front buffer (which is the
494 // default for single-buffered displays).
495 if (double_buffered
) {
496 glReadBuffer(GL_FRONT
);
497 CHECK_GL_ERROR("FirstShow", "glReadBuffer");
501 GLfloat fogcolour
[4] = { 0.5, 0.5, 0.5, 1.0 };
502 glFogfv(GL_FOG_COLOR
, fogcolour
);
503 CHECK_GL_ERROR("FirstShow", "glFogfv");
506 glFogi(GL_FOG_MODE
, GL_LINEAR
);
507 CHECK_GL_ERROR("FirstShow", "glFogi");
509 // Optimise for speed (compute fog per vertex).
510 glHint(GL_FOG_HINT
, GL_FASTEST
);
511 CHECK_GL_ERROR("FirstShow", "glHint");
513 // No padding on pixel packing and unpacking (default is to pad each
514 // line to a multiple of 4 bytes).
515 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1); // For setting texture maps.
516 CHECK_GL_ERROR("FirstShow", "glPixelStorei GL_UNPACK_ALIGNMENT");
517 glPixelStorei(GL_PACK_ALIGNMENT
, 1); // For screengrabs and movies.
518 CHECK_GL_ERROR("FirstShow", "glPixelStorei GL_PACK_ALIGNMENT");
521 wxString path
= wmsg_cfgpth();
522 path
+= wxCONFIG_PATH_SEPARATOR
;
523 path
+= wxT("unifont.pixelfont");
524 if (!m_Font
.load(path
)) {
525 // FIXME: do something better.
526 // We have this message available: Error in format of font file “%s”
527 fprintf(stderr
, "Failed to parse compiled-in font data\n");
531 if (blob_method
== UNKNOWN
) {
532 // Check if we can use GL_POINTS to plot blobs at stations.
533 GLdouble point_size_range
[2];
534 glGetDoublev(GL_SMOOTH_POINT_SIZE_RANGE
, point_size_range
);
535 CHECK_GL_ERROR("FirstShow", "glGetDoublev GL_SMOOTH_POINT_SIZE_RANGE");
536 if (point_size_range
[0] <= BLOB_DIAMETER
&&
537 point_size_range
[1] >= BLOB_DIAMETER
) {
540 blob_method
= glpoint_sprite_works() ? SPRITE
: LINES
;
545 if (blob_method
== POINT
) {
546 glPointSize(BLOB_DIAMETER
);
547 CHECK_GL_ERROR("FirstShow", "glPointSize");
550 if (cross_method
== UNKNOWN
) {
551 cross_method
= glpoint_sprite_works() ? SPRITE
: LINES
;
555 if (cross_method
== SPRITE
) {
556 glGenTextures(1, &m_CrossTexture
);
557 CHECK_GL_ERROR("FirstShow", "glGenTextures");
558 glBindTexture(GL_TEXTURE_2D
, m_CrossTexture
);
559 CHECK_GL_ERROR("FirstShow", "glBindTexture");
560 // Cross image for drawing crosses using texture mapped point sprites.
561 const unsigned char crossteximage
[128] = {
568 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
569 CHECK_GL_ERROR("FirstShow", "glPixelStorei");
570 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
571 CHECK_GL_ERROR("FirstShow", "glTexEnvi");
572 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
573 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_S");
574 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
575 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_T");
576 glTexImage2D(GL_TEXTURE_2D
, 0, GL_LUMINANCE_ALPHA
, 8, 8, 0, GL_LUMINANCE_ALPHA
, GL_UNSIGNED_BYTE
, (GLvoid
*)crossteximage
);
577 CHECK_GL_ERROR("FirstShow", "glTexImage2D");
578 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
579 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MAG_FILTER");
580 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
581 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MIN_FILTER");
584 if (blob_method
== SPRITE
) {
585 glGenTextures(1, &m_BlobTexture
);
586 CHECK_GL_ERROR("FirstShow", "glGenTextures");
587 glBindTexture(GL_TEXTURE_2D
, m_BlobTexture
);
588 CHECK_GL_ERROR("FirstShow", "glBindTexture");
589 // Image for drawing blobs using texture mapped point sprites.
590 const unsigned char blobteximage
[128] = {
597 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
598 CHECK_GL_ERROR("FirstShow", "glPixelStorei");
599 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
600 CHECK_GL_ERROR("FirstShow", "glTexEnvi");
601 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
602 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_S");
603 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
604 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_WRAP_T");
605 glTexImage2D(GL_TEXTURE_2D
, 0, GL_LUMINANCE_ALPHA
, 8, 8, 0, GL_LUMINANCE_ALPHA
, GL_UNSIGNED_BYTE
, (GLvoid
*)blobteximage
);
606 CHECK_GL_ERROR("FirstShow", "glTexImage2D");
607 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
608 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MAG_FILTER");
609 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
610 CHECK_GL_ERROR("FirstShow", "glTexParameteri GL_TEXTURE_MIN_FILTER");
614 void GLACanvas::Clear()
618 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
619 CHECK_GL_ERROR("Clear", "glClear");
622 void GLACanvas::SetScale(Double scale
)
624 if (scale
!= m_Scale
) {
625 vector
<GLAList
>::iterator i
;
626 for (i
= drawing_lists
.begin(); i
!= drawing_lists
.end(); ++i
) {
627 i
->invalidate_if(INVALIDATE_ON_SCALE
);
634 void GLACanvas::OnSize(wxSizeEvent
& event
)
636 wxSize size
= event
.GetSize();
638 unsigned int mask
= 0;
639 if (size
.GetWidth() != x_size
) mask
|= INVALIDATE_ON_X_RESIZE
;
640 if (size
.GetHeight() != y_size
) mask
|= INVALIDATE_ON_Y_RESIZE
;
642 vector
<GLAList
>::iterator i
;
643 for (i
= drawing_lists
.begin(); i
!= drawing_lists
.end(); ++i
) {
644 i
->invalidate_if(mask
);
647 // The width and height go to zero when the panel is dragged right
648 // across so we clamp them to be at least 1 to avoid problems.
649 x_size
= size
.GetWidth();
650 y_size
= size
.GetHeight();
651 if (x_size
< 1) x_size
= 1;
652 if (y_size
< 1) y_size
= 1;
657 if (!opengl_initialised
) return;
660 glViewport(0, 0, x_size
, y_size
);
661 CHECK_GL_ERROR("OnSize", "glViewport");
664 void GLACanvas::AddTranslationScreenCoordinates(int dx
, int dy
)
666 // Translate the data by a given amount, specified in screen coordinates.
668 // Find out how far the translation takes us in data coordinates.
673 gluUnProject(0.0, 0.0, 0.0, modelview_matrix
, projection_matrix
, viewport
,
675 CHECK_GL_ERROR("AddTranslationScreenCoordinates", "gluUnProject");
676 gluUnProject(dx
, -dy
, 0.0, modelview_matrix
, projection_matrix
, viewport
,
678 CHECK_GL_ERROR("AddTranslationScreenCoordinates", "gluUnProject (2)");
680 // Apply the translation.
681 AddTranslation(Vector3(x
- x0
, y
- y0
, z
- z0
));
684 void GLACanvas::SetVolumeDiameter(glaCoord diameter
)
686 // Set the size of the data drawing volume by giving the diameter of the
687 // smallest sphere containing it.
689 m_VolumeDiameter
= max(glaCoord(1.0), diameter
);
692 void GLACanvas::StartDrawing()
694 // Prepare for a redraw operation.
696 ctx
.SetCurrent(*this);
697 glDepthMask(GL_TRUE
);
699 if (!save_hints
) return;
701 // We want to check on the second redraw.
702 static int draw_count
= 2;
703 if (--draw_count
!= 0) return;
705 if (cross_method
!= LINES
) {
706 SetColour(col_WHITE
);
710 DrawCross(-m_Translation
.GetX(), -m_Translation
.GetY(), -m_Translation
.GetZ());
712 static const unsigned char expected_cross
[64 * 3] = {
714 #define I 255,255,255
719 if (!CheckVisualFidelity(expected_cross
)) {
720 cross_method
= LINES
;
725 if (blob_method
!= LINES
) {
726 SetColour(col_WHITE
);
730 DrawBlob(-m_Translation
.GetX(), -m_Translation
.GetY(), -m_Translation
.GetZ());
732 static const unsigned char expected_blob
[64 * 3] = {
734 #define I 255,255,255
739 if (!CheckVisualFidelity(expected_blob
)) {
745 wxConfigBase
* cfg
= wxConfigBase::Get();
746 cfg
->Write(wxT("opengl_survex"), wxT(VERSION
));
747 cfg
->Write(wxT("opengl_vendor"), vendor
);
748 cfg
->Write(wxT("opengl_renderer"), renderer
);
749 cfg
->Write(wxT("blob_method"), blob_method
);
750 cfg
->Write(wxT("cross_method"), cross_method
);
755 void GLACanvas::EnableSmoothPolygons(bool filled
)
757 // Prepare for drawing smoothly-shaded polygons.
758 // Only use this when required (in particular lines in lists may not be
759 // coloured correctly when this is enabled).
761 glPushAttrib(GL_ENABLE_BIT
|GL_LIGHTING_BIT
|GL_POLYGON_BIT
);
763 glShadeModel(GL_SMOOTH
);
764 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
766 glDisable(GL_LINE_SMOOTH
);
767 glDisable(GL_TEXTURE_2D
);
768 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
770 CHECK_GL_ERROR("EnableSmoothPolygons", "glPolygonMode");
772 if (filled
&& m_SmoothShading
) {
773 static const GLfloat mat_specular
[] = { 0.2, 0.2, 0.2, 1.0 };
774 static const GLfloat light_position
[] = { -1.0, -1.0, -1.0, 0.0 };
775 static const GLfloat light_ambient
[] = { 0.3, 0.3, 0.3, 1.0 };
776 static const GLfloat light_diffuse
[] = { 0.7, 0.7, 0.7, 1.0 };
777 glEnable(GL_COLOR_MATERIAL
);
778 glMaterialfv(GL_FRONT_AND_BACK
, GL_SPECULAR
, mat_specular
);
779 glMaterialf(GL_FRONT_AND_BACK
, GL_SHININESS
, 10.0);
780 glLightfv(GL_LIGHT0
, GL_AMBIENT
, light_ambient
);
781 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, light_diffuse
);
782 glLightfv(GL_LIGHT0
, GL_POSITION
, light_position
);
783 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
784 glEnable(GL_LIGHTING
);
789 void GLACanvas::DisableSmoothPolygons()
794 void GLACanvas::PlaceNormal(const Vector3
&v
)
796 // Add a normal (for polygons etc.)
798 glNormal3d(v
.GetX(), v
.GetY(), v
.GetZ());
801 void GLACanvas::SetDataTransform()
804 glMatrixMode(GL_PROJECTION
);
805 CHECK_GL_ERROR("SetDataTransform", "glMatrixMode");
807 CHECK_GL_ERROR("SetDataTransform", "glLoadIdentity");
809 double aspect
= double(y_size
) / double(x_size
);
811 Double near_plane
= 1.0;
813 Double lr
= near_plane
* tan(rad(25.0));
814 Double far_plane
= m_VolumeDiameter
* 5 + near_plane
; // FIXME: work out properly
815 Double tb
= lr
* aspect
;
816 glFrustum(-lr
, lr
, -tb
, tb
, near_plane
, far_plane
);
817 CHECK_GL_ERROR("SetViewportAndProjection", "glFrustum");
820 assert(m_Scale
!= 0.0);
821 Double lr
= m_VolumeDiameter
/ m_Scale
* 0.5;
822 Double far_plane
= m_VolumeDiameter
+ near_plane
;
829 glOrtho(-lr
, lr
, -tb
, tb
, near_plane
, far_plane
);
830 CHECK_GL_ERROR("SetViewportAndProjection", "glOrtho");
833 // Set the modelview transform for drawing data.
834 glMatrixMode(GL_MODELVIEW
);
835 CHECK_GL_ERROR("SetDataTransform", "glMatrixMode");
837 CHECK_GL_ERROR("SetDataTransform", "glLoadIdentity");
839 glTranslated(0.0, 0.0, -near_plane
);
841 glTranslated(0.0, 0.0, -0.5 * m_VolumeDiameter
);
843 CHECK_GL_ERROR("SetDataTransform", "glTranslated");
844 // Get axes the correct way around (z upwards, y into screen)
845 glRotated(-90.0, 1.0, 0.0, 0.0);
846 CHECK_GL_ERROR("SetDataTransform", "glRotated");
847 glRotated(-m_Tilt
, 1.0, 0.0, 0.0);
848 CHECK_GL_ERROR("SetDataTransform", "glRotated");
849 glRotated(m_Pan
, 0.0, 0.0, 1.0);
850 CHECK_GL_ERROR("SetDataTransform", "CopyToOpenGL");
852 glTranslated(m_Translation
.GetX(),
853 m_Translation
.GetY(),
854 m_Translation
.GetZ());
855 CHECK_GL_ERROR("SetDataTransform", "glTranslated");
858 // Save projection matrix.
859 glGetDoublev(GL_PROJECTION_MATRIX
, projection_matrix
);
860 CHECK_GL_ERROR("SetDataTransform", "glGetDoublev");
862 // Save viewport coordinates.
863 glGetIntegerv(GL_VIEWPORT
, viewport
);
864 CHECK_GL_ERROR("SetDataTransform", "glGetIntegerv");
866 // Save modelview matrix.
867 glGetDoublev(GL_MODELVIEW_MATRIX
, modelview_matrix
);
868 CHECK_GL_ERROR("SetDataTransform", "glGetDoublev");
870 if (!m_Perspective
) {
871 // Adjust the translation so we don't change the Z position of the model
873 gluProject(m_Translation
.GetX(),
874 m_Translation
.GetY(),
875 m_Translation
.GetZ(),
876 modelview_matrix
, projection_matrix
, viewport
,
879 gluUnProject(X
, Y
, 0.5, modelview_matrix
, projection_matrix
, viewport
,
881 glTranslated(Tx
, Ty
, Tz
);
882 CHECK_GL_ERROR("SetDataTransform", "glTranslated");
883 glGetDoublev(GL_MODELVIEW_MATRIX
, modelview_matrix
);
886 glEnable(GL_DEPTH_TEST
);
887 CHECK_GL_ERROR("SetDataTransform", "glEnable GL_DEPTH_TEST");
890 glBindTexture(GL_TEXTURE_2D
, m_Texture
);
891 glEnable(GL_TEXTURE_2D
);
892 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
893 CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_WRAP_S");
894 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
895 CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_WRAP_T");
896 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
897 CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_MAG_FILTER");
898 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
899 GL_LINEAR_MIPMAP_LINEAR
);
900 CHECK_GL_ERROR("ToggleTextured", "glTexParameteri GL_TEXTURE_MIN_FILTER");
901 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
);
903 glDisable(GL_TEXTURE_2D
);
906 glFogf(GL_FOG_START
, near_plane
);
907 glFogf(GL_FOG_END
, near_plane
+ m_VolumeDiameter
);
914 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
916 glEnable(GL_LINE_SMOOTH
);
918 glDisable(GL_LINE_SMOOTH
);
922 void GLACanvas::SetIndicatorTransform()
924 list_flags
|= NEVER_CACHE
;
926 // Set the modelview transform and projection for drawing indicators.
928 glDisable(GL_DEPTH_TEST
);
929 CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_DEPTH_TEST");
931 CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_FOG");
933 // Just a simple 2D projection.
934 glMatrixMode(GL_PROJECTION
);
935 CHECK_GL_ERROR("SetIndicatorTransform", "glMatrixMode");
937 CHECK_GL_ERROR("SetIndicatorTransform", "glLoadIdentity (2)");
938 gluOrtho2D(0, x_size
, 0, y_size
);
939 CHECK_GL_ERROR("SetIndicatorTransform", "gluOrtho2D");
941 // No modelview transform.
942 glMatrixMode(GL_MODELVIEW
);
943 CHECK_GL_ERROR("SetIndicatorTransform", "glMatrixMode");
945 CHECK_GL_ERROR("SetIndicatorTransform", "glLoadIdentity");
947 glDisable(GL_TEXTURE_2D
);
948 CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_TEXTURE_2D");
950 CHECK_GL_ERROR("SetIndicatorTransform", "glDisable GL_BLEND");
951 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
952 CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_WRAP_S");
953 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
954 CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_WRAP_T");
955 glAlphaFunc(GL_GREATER
, 0.5f
);
956 CHECK_GL_ERROR("SetIndicatorTransform", "glAlphaFunc");
957 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
958 CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_MAG_FILTER");
959 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
960 CHECK_GL_ERROR("SetIndicatorTransform", "glTexParameteri GL_TEXTURE_MIN_FILTER");
961 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
962 CHECK_GL_ERROR("SetIndicatorTransform", "glHint");
965 void GLACanvas::FinishDrawing()
967 // Complete a redraw operation.
969 if (double_buffered
) {
973 CHECK_GL_ERROR("FinishDrawing", "glFlush");
977 void GLACanvas::DrawList(unsigned int l
)
979 // FIXME: uncomment to disable use of lists for debugging:
980 // GenerateList(l); return;
981 if (l
>= drawing_lists
.size()) drawing_lists
.resize(l
+ 1);
983 // We generate the OpenGL lists lazily to minimise delays on startup.
984 // So check if we need to generate the OpenGL list now.
985 if (drawing_lists
[l
].need_to_generate()) {
986 // Clear list_flags so that we can note what conditions to invalidate
987 // the cached OpenGL list on.
991 printf("generating list #%u... ", l
);
996 printf("done (%d vertices)\n", m_Vertices
);
998 drawing_lists
[l
].finalise(list_flags
);
1001 if (!drawing_lists
[l
].DrawList()) {
1002 // That list isn't cached (which means it probably can't usefully be
1008 void GLACanvas::DrawListZPrepass(unsigned int l
)
1010 glColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1012 glDepthMask(GL_FALSE
);
1013 glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1014 glDepthFunc(GL_EQUAL
);
1016 glDepthMask(GL_TRUE
);
1017 glDepthFunc(GL_LESS
);
1020 void GLACanvas::DrawList2D(unsigned int l
, glaCoord x
, glaCoord y
, Double rotation
)
1022 glMatrixMode(GL_PROJECTION
);
1023 CHECK_GL_ERROR("DrawList2D", "glMatrixMode");
1025 CHECK_GL_ERROR("DrawList2D", "glPushMatrix");
1026 glTranslated(x
, y
, 0);
1027 CHECK_GL_ERROR("DrawList2D", "glTranslated");
1028 if (rotation
!= 0.0) {
1029 glRotated(rotation
, 0, 0, -1);
1030 CHECK_GL_ERROR("DrawList2D", "glRotated");
1033 glMatrixMode(GL_PROJECTION
);
1034 CHECK_GL_ERROR("DrawList2D", "glMatrixMode 2");
1036 CHECK_GL_ERROR("DrawList2D", "glPopMatrix");
1039 void GLACanvas::SetColour(const GLAPen
& pen
, double rgb_scale
)
1041 // Set the colour for subsequent operations.
1042 glColor4f(pen
.GetRed() * rgb_scale
, pen
.GetGreen() * rgb_scale
,
1043 pen
.GetBlue() * rgb_scale
, alpha
);
1046 void GLACanvas::SetColour(const GLAPen
& pen
)
1048 // Set the colour for subsequent operations.
1049 glColor4d(pen
.components
[0], pen
.components
[1], pen
.components
[2], alpha
);
1052 void GLACanvas::SetColour(gla_colour colour
, double rgb_scale
)
1054 // Set the colour for subsequent operations.
1056 glColor4f(COLOURS
[colour
].r
* rgb_scale
,
1057 COLOURS
[colour
].g
* rgb_scale
,
1058 COLOURS
[colour
].b
* rgb_scale
,
1062 void GLACanvas::SetColour(gla_colour colour
)
1064 // Set the colour for subsequent operations.
1066 glColor3ubv(&COLOURS
[colour
].r
);
1068 glColor4ub(COLOURS
[colour
].r
,
1071 (unsigned char)(255 * alpha
));
1075 void GLACanvas::DrawText(glaCoord x
, glaCoord y
, glaCoord z
, const wxString
& str
)
1077 // Draw a text string on the current buffer in the current font.
1078 glRasterPos3d(x
, y
, z
);
1079 CHECK_GL_ERROR("DrawText", "glRasterPos3d");
1080 m_Font
.write_string(str
.data(), str
.size());
1083 void GLACanvas::DrawIndicatorText(int x
, int y
, const wxString
& str
)
1085 glRasterPos2d(x
, y
);
1086 CHECK_GL_ERROR("DrawIndicatorText", "glRasterPos2d");
1087 m_Font
.write_string(str
.data(), str
.size());
1090 void GLACanvas::GetTextExtent(const wxString
& str
, int * x_ext
, int * y_ext
) const
1092 m_Font
.get_text_extent(str
.data(), str
.size(), x_ext
, y_ext
);
1095 void GLACanvas::BeginQuadrilaterals()
1097 // Commence drawing of quadrilaterals.
1102 void GLACanvas::EndQuadrilaterals()
1104 // Finish drawing of quadrilaterals.
1107 CHECK_GL_ERROR("EndQuadrilaterals", "glEnd GL_QUADS");
1110 void GLACanvas::BeginLines()
1112 // Commence drawing of a set of lines.
1117 void GLACanvas::EndLines()
1119 // Finish drawing of a set of lines.
1122 CHECK_GL_ERROR("EndLines", "glEnd GL_LINES");
1125 void GLACanvas::BeginTriangles()
1127 // Commence drawing of a set of triangles.
1129 glBegin(GL_TRIANGLES
);
1132 void GLACanvas::EndTriangles()
1134 // Finish drawing of a set of triangles.
1137 CHECK_GL_ERROR("EndTriangles", "glEnd GL_TRIANGLES");
1140 void GLACanvas::BeginTriangleStrip()
1142 // Commence drawing of a triangle strip.
1144 glBegin(GL_TRIANGLE_STRIP
);
1147 void GLACanvas::EndTriangleStrip()
1149 // Finish drawing of a triangle strip.
1152 CHECK_GL_ERROR("EndTriangleStrip", "glEnd GL_TRIANGLE_STRIP");
1155 void GLACanvas::BeginPolyline()
1157 // Commence drawing of a polyline.
1159 glBegin(GL_LINE_STRIP
);
1162 void GLACanvas::EndPolyline()
1164 // Finish drawing of a polyline.
1167 CHECK_GL_ERROR("EndPolyline", "glEnd GL_LINE_STRIP");
1170 void GLACanvas::BeginPolyloop()
1172 // Commence drawing of a polyloop.
1174 glBegin(GL_LINE_LOOP
);
1177 void GLACanvas::EndPolyloop()
1179 // Finish drawing of a polyloop.
1182 CHECK_GL_ERROR("EndPolyloop", "glEnd GL_LINE_LOOP");
1185 void GLACanvas::BeginPolygon()
1187 // Commence drawing of a polygon.
1189 glBegin(GL_POLYGON
);
1192 void GLACanvas::EndPolygon()
1194 // Finish drawing of a polygon.
1197 CHECK_GL_ERROR("EndPolygon", "glEnd GL_POLYGON");
1200 void GLACanvas::PlaceVertex(glaCoord x
, glaCoord y
, glaCoord z
)
1202 // Place a vertex for the current object being drawn.
1207 glVertex3d(x
, y
, z
);
1210 void GLACanvas::PlaceVertex(glaCoord x
, glaCoord y
, glaCoord z
,
1211 glaTexCoord tex_x
, glaTexCoord tex_y
)
1213 // Place a vertex for the current object being drawn.
1218 glTexCoord2f(tex_x
, tex_y
);
1219 glVertex3d(x
, y
, z
);
1222 void GLACanvas::PlaceIndicatorVertex(glaCoord x
, glaCoord y
)
1224 // Place a vertex for the current indicator object being drawn.
1226 PlaceVertex(x
, y
, 0.0);
1229 void GLACanvas::BeginBlobs()
1231 // Commence drawing of a set of blobs.
1232 if (blob_method
== SPRITE
) {
1233 glPushAttrib(GL_ENABLE_BIT
|GL_POINT_BIT
);
1234 CHECK_GL_ERROR("BeginBlobs", "glPushAttrib");
1235 glBindTexture(GL_TEXTURE_2D
, m_BlobTexture
);
1236 CHECK_GL_ERROR("BeginBlobs", "glBindTexture");
1237 glEnable(GL_ALPHA_TEST
);
1238 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_ALPHA_TEST");
1240 CHECK_GL_ERROR("BeginBlobs", "glPointSize");
1241 glTexEnvi(GL_POINT_SPRITE
, GL_COORD_REPLACE
, GL_TRUE
);
1242 CHECK_GL_ERROR("BeginBlobs", "glTexEnvi GL_POINT_SPRITE");
1243 glEnable(GL_TEXTURE_2D
);
1244 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_TEXTURE_2D");
1245 glEnable(GL_POINT_SPRITE
);
1246 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_POINT_SPRITE");
1248 } else if (blob_method
== POINT
) {
1249 glPushAttrib(GL_ENABLE_BIT
);
1250 CHECK_GL_ERROR("BeginBlobs", "glPushAttrib");
1251 glEnable(GL_ALPHA_TEST
);
1252 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_ALPHA_TEST");
1253 glEnable(GL_POINT_SMOOTH
);
1254 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_POINT_SMOOTH");
1257 glPushAttrib(GL_TRANSFORM_BIT
|GL_VIEWPORT_BIT
|GL_ENABLE_BIT
);
1258 CHECK_GL_ERROR("BeginBlobs", "glPushAttrib");
1259 SetIndicatorTransform();
1260 glEnable(GL_DEPTH_TEST
);
1261 CHECK_GL_ERROR("BeginBlobs", "glEnable GL_DEPTH_TEST");
1266 void GLACanvas::EndBlobs()
1268 // Finish drawing of a set of blobs.
1270 if (blob_method
!= LINES
) {
1271 CHECK_GL_ERROR("EndBlobs", "glEnd GL_POINTS");
1273 CHECK_GL_ERROR("EndBlobs", "glEnd GL_LINES");
1276 CHECK_GL_ERROR("EndBlobs", "glPopAttrib");
1279 void GLACanvas::DrawBlob(glaCoord x
, glaCoord y
, glaCoord z
)
1281 if (blob_method
!= LINES
) {
1283 PlaceVertex(x
, y
, z
);
1286 if (!Transform(Vector3(x
, y
, z
), &X
, &Y
, &Z
)) {
1287 printf("bad transform\n");
1290 // Stuff behind us (in perspective view) will get clipped,
1291 // but we can save effort with a cheap check here.
1294 X
-= BLOB_DIAMETER
* 0.5;
1295 Y
-= BLOB_DIAMETER
* 0.5;
1297 PlaceVertex(X
, Y
+ 1, Z
);
1298 PlaceVertex(X
, Y
+ (BLOB_DIAMETER
- 1), Z
);
1300 for (int i
= 1; i
< (BLOB_DIAMETER
- 1); ++i
) {
1301 PlaceVertex(X
+ i
, Y
, Z
);
1302 PlaceVertex(X
+ i
, Y
+ BLOB_DIAMETER
, Z
);
1305 PlaceVertex(X
+ (BLOB_DIAMETER
- 1), Y
+ 1, Z
);
1306 PlaceVertex(X
+ (BLOB_DIAMETER
- 1), Y
+ (BLOB_DIAMETER
- 1), Z
);
1313 void GLACanvas::DrawBlob(glaCoord x
, glaCoord y
)
1315 if (blob_method
!= LINES
) {
1317 PlaceVertex(x
, y
, 0);
1319 x
-= BLOB_DIAMETER
* 0.5;
1320 y
-= BLOB_DIAMETER
* 0.5;
1322 PlaceVertex(x
, y
+ 1, 0);
1323 PlaceVertex(x
, y
+ (BLOB_DIAMETER
- 1), 0);
1325 for (int i
= 1; i
< (BLOB_DIAMETER
- 1); ++i
) {
1326 PlaceVertex(x
+ i
, y
, 0);
1327 PlaceVertex(x
+ i
, y
+ BLOB_DIAMETER
, 0);
1330 PlaceVertex(x
+ (BLOB_DIAMETER
- 1), y
+ 1, 0);
1331 PlaceVertex(x
+ (BLOB_DIAMETER
- 1), y
+ (BLOB_DIAMETER
- 1), 0);
1338 void GLACanvas::BeginCrosses()
1341 if (cross_method
== SPRITE
) {
1342 glPushAttrib(GL_ENABLE_BIT
|GL_POINT_BIT
);
1343 CHECK_GL_ERROR("BeginCrosses", "glPushAttrib");
1344 glBindTexture(GL_TEXTURE_2D
, m_CrossTexture
);
1345 CHECK_GL_ERROR("BeginCrosses", "glBindTexture");
1346 glEnable(GL_ALPHA_TEST
);
1347 CHECK_GL_ERROR("BeginCrosses", "glEnable GL_ALPHA_TEST");
1349 CHECK_GL_ERROR("BeginCrosses", "glPointSize");
1350 glTexEnvi(GL_POINT_SPRITE
, GL_COORD_REPLACE
, GL_TRUE
);
1351 CHECK_GL_ERROR("BeginCrosses", "glTexEnvi GL_POINT_SPRITE");
1352 glEnable(GL_TEXTURE_2D
);
1353 CHECK_GL_ERROR("BeginCrosses", "glEnable GL_TEXTURE_2D");
1354 glEnable(GL_POINT_SPRITE
);
1355 CHECK_GL_ERROR("BeginCrosses", "glEnable GL_POINT_SPRITE");
1358 // To get the crosses to appear at a constant size and orientation on
1359 // screen, we plot them in the Indicator transform coordinates (which
1360 // unfortunately means they can't be usefully put in an opengl display
1362 glPushAttrib(GL_TRANSFORM_BIT
|GL_VIEWPORT_BIT
|GL_ENABLE_BIT
);
1363 CHECK_GL_ERROR("BeginCrosses", "glPushAttrib 2");
1364 SetIndicatorTransform();
1365 // Align line drawing to pixel centres to get pixel-perfect rendering
1366 // (graphics card and driver bugs aside).
1367 glTranslated(-0.5, -0.5, 0);
1368 CHECK_GL_ERROR("BeginCrosses", "glTranslated");
1369 glEnable(GL_DEPTH_TEST
);
1370 CHECK_GL_ERROR("BeginCrosses", "glEnable GL_DEPTH_TEST");
1375 void GLACanvas::EndCrosses()
1378 if (cross_method
== SPRITE
) {
1379 CHECK_GL_ERROR("EndCrosses", "glEnd GL_POINTS");
1381 CHECK_GL_ERROR("EndCrosses", "glEnd GL_LINES");
1384 CHECK_GL_ERROR("EndCrosses", "glPopAttrib");
1387 void GLACanvas::DrawCross(glaCoord x
, glaCoord y
, glaCoord z
)
1389 if (cross_method
== SPRITE
) {
1391 PlaceVertex(x
, y
, z
);
1394 if (!Transform(Vector3(x
, y
, z
), &X
, &Y
, &Z
)) {
1395 printf("bad transform\n");
1398 // Stuff behind us (in perspective view) will get clipped,
1399 // but we can save effort with a cheap check here.
1402 // Round to integers before adding on the offsets for the
1403 // cross arms to avoid uneven crosses.
1406 // Need to extend lines by an extra pixel (which shouldn't get drawn by
1407 // the diamond-exit rule).
1408 PlaceVertex(X
- 3, Y
- 3, Z
);
1409 PlaceVertex(X
+ 4, Y
+ 4, Z
);
1410 PlaceVertex(X
- 3, Y
+ 3, Z
);
1411 PlaceVertex(X
+ 4, Y
- 4, Z
);
1418 void GLACanvas::DrawRing(glaCoord x
, glaCoord y
)
1420 // Draw an unfilled circle of radius 4
1422 // Round to integers to get an even ring.
1426 glBegin(GL_LINE_LOOP
);
1427 PlaceIndicatorVertex(x
+ 3.5, y
- 1.5);
1428 PlaceIndicatorVertex(x
+ 1.5, y
- 3.5);
1429 PlaceIndicatorVertex(x
- 1.5, y
- 3.5);
1430 PlaceIndicatorVertex(x
- 3.5, y
- 1.5);
1431 PlaceIndicatorVertex(x
- 3.5, y
+ 1.5);
1432 PlaceIndicatorVertex(x
- 1.5, y
+ 3.5);
1433 PlaceIndicatorVertex(x
+ 1.5, y
+ 3.5);
1434 PlaceIndicatorVertex(x
+ 3.5, y
+ 1.5);
1436 CHECK_GL_ERROR("DrawRing", "glEnd GL_LINE_LOOP");
1439 void GLACanvas::DrawRectangle(gla_colour fill
, gla_colour edge
,
1440 glaCoord x0
, glaCoord y0
, glaCoord w
, glaCoord h
)
1442 // Draw a filled rectangle with an edge in the indicator plane.
1443 // (x0, y0) specify the bottom-left corner of the rectangle and (w, h) the
1447 BeginQuadrilaterals();
1448 PlaceIndicatorVertex(x0
, y0
);
1449 PlaceIndicatorVertex(x0
+ w
, y0
);
1450 PlaceIndicatorVertex(x0
+ w
, y0
+ h
);
1451 PlaceIndicatorVertex(x0
, y0
+ h
);
1452 EndQuadrilaterals();
1457 PlaceIndicatorVertex(x0
, y0
);
1458 PlaceIndicatorVertex(x0
+ w
, y0
);
1459 PlaceIndicatorVertex(x0
+ w
, y0
+ h
);
1460 PlaceIndicatorVertex(x0
, y0
+ h
);
1461 PlaceIndicatorVertex(x0
, y0
);
1467 GLACanvas::DrawShadedRectangle(const GLAPen
& fill_bot
, const GLAPen
& fill_top
,
1468 glaCoord x0
, glaCoord y0
,
1469 glaCoord w
, glaCoord h
)
1471 // Draw a graduated filled rectangle in the indicator plane.
1472 // (x0, y0) specify the bottom-left corner of the rectangle and (w, h) the
1475 glShadeModel(GL_SMOOTH
);
1476 CHECK_GL_ERROR("DrawShadedRectangle", "glShadeModel GL_SMOOTH");
1477 BeginQuadrilaterals();
1478 SetColour(fill_bot
);
1479 PlaceIndicatorVertex(x0
, y0
);
1480 PlaceIndicatorVertex(x0
+ w
, y0
);
1481 SetColour(fill_top
);
1482 PlaceIndicatorVertex(x0
+ w
, y0
+ h
);
1483 PlaceIndicatorVertex(x0
, y0
+ h
);
1484 EndQuadrilaterals();
1485 glShadeModel(GL_FLAT
);
1486 CHECK_GL_ERROR("DrawShadedRectangle", "glShadeModel GL_FLAT");
1489 void GLACanvas::DrawCircle(gla_colour edge
, gla_colour fill
,
1490 glaCoord cx
, glaCoord cy
, glaCoord radius
)
1492 // Draw a filled circle with an edge.
1494 glMatrixMode(GL_MODELVIEW
);
1495 CHECK_GL_ERROR("DrawCircle", "glMatrixMode");
1497 CHECK_GL_ERROR("DrawCircle", "glPushMatrix");
1498 glTranslated(cx
, cy
, 0.0);
1499 CHECK_GL_ERROR("DrawCircle", "glTranslated");
1501 gluDisk(m_Quadric
, 0.0, radius
, 36, 1);
1502 CHECK_GL_ERROR("DrawCircle", "gluDisk");
1504 gluDisk(m_Quadric
, radius
- 1.0, radius
, 36, 1);
1505 CHECK_GL_ERROR("DrawCircle", "gluDisk (2)");
1507 CHECK_GL_ERROR("DrawCircle", "glPopMatrix");
1510 void GLACanvas::DrawSemicircle(gla_colour edge
, gla_colour fill
,
1511 glaCoord cx
, glaCoord cy
,
1512 glaCoord radius
, glaCoord start
)
1514 // Draw a filled semicircle with an edge.
1515 // The semicircle extends from "start" deg to "start"+180 deg (increasing
1516 // clockwise, 0 deg upwards).
1518 glMatrixMode(GL_MODELVIEW
);
1519 CHECK_GL_ERROR("DrawSemicircle", "glMatrixMode");
1521 CHECK_GL_ERROR("DrawSemicircle", "glPushMatrix");
1522 glTranslated(cx
, cy
, 0.0);
1523 CHECK_GL_ERROR("DrawSemicircle", "glTranslated");
1525 gluPartialDisk(m_Quadric
, 0.0, radius
, 36, 1, start
, 180.0);
1526 CHECK_GL_ERROR("DrawSemicircle", "gluPartialDisk");
1528 gluPartialDisk(m_Quadric
, radius
- 1.0, radius
, 36, 1, start
, 180.0);
1529 CHECK_GL_ERROR("DrawSemicircle", "gluPartialDisk (2)");
1531 CHECK_GL_ERROR("DrawSemicircle", "glPopMatrix");
1535 GLACanvas::DrawTriangle(gla_colour edge
, gla_colour fill
,
1536 const Vector3
&p0
, const Vector3
&p1
, const Vector3
&p2
)
1538 // Draw a filled triangle with an edge.
1542 PlaceIndicatorVertex(p0
.GetX(), p0
.GetY());
1543 PlaceIndicatorVertex(p1
.GetX(), p1
.GetY());
1544 PlaceIndicatorVertex(p2
.GetX(), p2
.GetY());
1548 glBegin(GL_LINE_STRIP
);
1549 PlaceIndicatorVertex(p0
.GetX(), p0
.GetY());
1550 PlaceIndicatorVertex(p1
.GetX(), p1
.GetY());
1551 PlaceIndicatorVertex(p2
.GetX(), p2
.GetY());
1553 CHECK_GL_ERROR("DrawTriangle", "glEnd GL_LINE_STRIP");
1556 void GLACanvas::EnableDashedLines()
1558 // Enable dashed lines, and start drawing in them.
1560 glLineStipple(1, 0x3333);
1561 CHECK_GL_ERROR("EnableDashedLines", "glLineStipple");
1562 glEnable(GL_LINE_STIPPLE
);
1563 CHECK_GL_ERROR("EnableDashedLines", "glEnable GL_LINE_STIPPLE");
1566 void GLACanvas::DisableDashedLines()
1568 glDisable(GL_LINE_STIPPLE
);
1569 CHECK_GL_ERROR("DisableDashedLines", "glDisable GL_LINE_STIPPLE");
1572 bool GLACanvas::Transform(const Vector3
& v
,
1573 double* x_out
, double* y_out
, double* z_out
) const
1575 // Convert from data coordinates to screen coordinates.
1577 // Perform the projection.
1578 return gluProject(v
.GetX(), v
.GetY(), v
.GetZ(),
1579 modelview_matrix
, projection_matrix
, viewport
,
1580 x_out
, y_out
, z_out
);
1583 void GLACanvas::ReverseTransform(Double x
, Double y
,
1584 double* x_out
, double* y_out
, double* z_out
) const
1586 // Convert from screen coordinates to data coordinates.
1588 // Perform the projection.
1589 gluUnProject(x
, y
, 0.0, modelview_matrix
, projection_matrix
, viewport
,
1590 x_out
, y_out
, z_out
);
1591 CHECK_GL_ERROR("ReverseTransform", "gluUnProject");
1594 Double
GLACanvas::SurveyUnitsAcrossViewport() const
1596 // Measure the current viewport in survey units, taking into account the
1597 // current display scale.
1599 assert(m_Scale
!= 0.0);
1600 list_flags
|= INVALIDATE_ON_SCALE
;
1601 Double result
= m_VolumeDiameter
/ m_Scale
;
1602 if (y_size
< x_size
) {
1603 result
= result
* x_size
/ y_size
;
1608 void GLACanvas::ToggleSmoothShading()
1610 m_SmoothShading
= !m_SmoothShading
;
1613 void GLACanvas::ToggleTextured()
1615 m_Textured
= !m_Textured
;
1616 if (m_Textured
&& m_Texture
== 0) {
1617 glGenTextures(1, &m_Texture
);
1618 CHECK_GL_ERROR("ToggleTextured", "glGenTextures");
1620 glBindTexture(GL_TEXTURE_2D
, m_Texture
);
1621 CHECK_GL_ERROR("ToggleTextured", "glBindTexture");
1623 ::wxInitAllImageHandlers();
1626 wxString
texture(wmsg_cfgpth());
1627 texture
+= wxCONFIG_PATH_SEPARATOR
;
1628 texture
+= wxT("images");
1629 texture
+= wxCONFIG_PATH_SEPARATOR
;
1630 texture
+= wxT("texture.png");
1631 if (!img
.LoadFile(texture
, wxBITMAP_TYPE_PNG
)) {
1633 fprintf(stderr
, "Couldn't load image.\n");
1637 // Generate mipmaps.
1638 gluBuild2DMipmaps(GL_TEXTURE_2D
, GL_RGB
, // was GL_LUMINANCE
1639 img
.GetWidth(), img
.GetHeight(),
1640 GL_RGB
, GL_UNSIGNED_BYTE
, img
.GetData());
1641 CHECK_GL_ERROR("ToggleTextured", "gluBuild2DMipmaps");
1643 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
1644 CHECK_GL_ERROR("ToggleTextured", "glTexEnvi");
1648 bool GLACanvas::SaveScreenshot(const wxString
& fnm
, wxBitmapType type
) const
1650 const int width
= x_size
;
1651 const int height
= y_size
;
1652 unsigned char *pixels
= (unsigned char *)malloc(3 * width
* (height
+ 1));
1653 if (!pixels
) return false;
1654 glReadPixels(0, 0, width
, height
, GL_RGB
, GL_UNSIGNED_BYTE
, (GLvoid
*)pixels
);
1655 CHECK_GL_ERROR("SaveScreenshot", "glReadPixels");
1656 unsigned char * tmp_row
= pixels
+ 3 * width
* height
;
1657 // We need to flip the image vertically - this approach should be more
1658 // efficient than using wxImage::Mirror(false) as that creates a new
1660 for (int y
= height
/ 2 - 1; y
>= 0; --y
) {
1661 unsigned char * upper
= pixels
+ 3 * width
* y
;
1662 unsigned char * lower
= pixels
+ 3 * width
* (height
- y
- 1);
1663 memcpy(tmp_row
, upper
, 3 * width
);
1664 memcpy(upper
, lower
, 3 * width
);
1665 memcpy(lower
, tmp_row
, 3 * width
);
1667 // NB wxImage constructor calls free(pixels) for us.
1668 wxImage
grab(width
, height
, pixels
);
1669 return grab
.SaveFile(fnm
, type
);
1672 bool GLACanvas::CheckVisualFidelity(const unsigned char * target
) const
1674 unsigned char pixels
[3 * 8 * 8];
1675 if (double_buffered
) {
1676 glReadBuffer(GL_BACK
);
1677 CHECK_GL_ERROR("FirstShow", "glReadBuffer");
1679 glReadPixels(x_size
/ 2 - 4, y_size
/ 2 - 5, 8, 8,
1680 GL_RGB
, GL_UNSIGNED_BYTE
, (GLvoid
*)pixels
);
1681 CHECK_GL_ERROR("CheckVisualFidelity", "glReadPixels");
1682 if (double_buffered
) {
1683 glReadBuffer(GL_FRONT
);
1684 CHECK_GL_ERROR("FirstShow", "glReadBuffer");
1687 // Show what got drawn and what was expected for debugging.
1688 for (int y
= 0; y
< 8; ++y
) {
1689 for (int x
= 0; x
< 8; ++x
) {
1690 int o
= (y
* 8 + x
) * 3;
1691 printf("%c", pixels
[o
] ? 'X' : '.');
1694 for (int x
= 0; x
< 8; ++x
) {
1695 int o
= (y
* 8 + x
) * 3;
1696 printf("%c", target
[o
] ? 'X' : '.');
1701 return (memcmp(pixels
, target
, sizeof(pixels
)) == 0);
1704 void GLACanvas::ReadPixels(int width
, int height
, unsigned char * buf
) const
1706 CHECK_GL_ERROR("ReadPixels", "glReadPixels");
1707 glReadPixels(0, 0, width
, height
, GL_RGB
, GL_UNSIGNED_BYTE
, (GLvoid
*)buf
);
1710 void GLACanvas::PolygonOffset(bool on
) const
1713 glPolygonOffset(1.0, 1.0);
1714 glEnable(GL_POLYGON_OFFSET_FILL
);
1716 glDisable(GL_POLYGON_OFFSET_FILL
);