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