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