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