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