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