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