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