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