| 1 | // SPDX-FileCopyrightText: 2000 David Megginson |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef HAVE_CONFIG_H |
| 5 | # include <config.h> |
| 6 | #endif |
| 7 | |
| 8 | #ifdef HAVE_WINDOWS_H |
| 9 | # include <windows.h> |
| 10 | #endif |
| 11 | |
| 12 | #include <stdio.h> // sprintf |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <simgear/compiler.h> |
| 16 | #include <simgear/debug/logstream.hxx> |
| 17 | #include <simgear/misc/sg_path.hxx> |
| 18 | using std::terminate; |
| 19 | |
| 20 | #if defined (SG_MAC) |
| 21 | #include <OpenGL/gl.h> |
| 22 | #include <GLUT/glut.h> |
| 23 | #elif defined (_GLES2) |
| 24 | #include <GLES2/gl2.h> |
| 25 | #else |
| 26 | #include <GL/glew.h> // Must be included before <GL/gl.h> |
| 27 | #include <GL/gl.h> |
| 28 | #include <GL/glut.h> |
| 29 | #endif |
| 30 | |
| 31 | #include "GL_utils.hxx" |
| 32 | #include "FGPanel.hxx" |
| 33 | #include "FGTextLayer.hxx" |
| 34 | #include "FGTexturedLayer.hxx" |
| 35 | |
| 36 | //////////////////////////////////////////////////////////////////////// |
| 37 | // Local functions. |
| 38 | //////////////////////////////////////////////////////////////////////// |
| 39 | GLuint FGPanel::Textured_Layer_Program_Object (0); |
| 40 | GLint FGPanel::Textured_Layer_Position_Loc (0); |
| 41 | GLint FGPanel::Textured_Layer_Tex_Coord_Loc (0); |
| 42 | GLint FGPanel::Textured_Layer_MVP_Loc (0); |
| 43 | GLint FGPanel::Textured_Layer_Sampler_Loc (0); |
| 44 | |
| 45 | //////////////////////////////////////////////////////////////////////// |
| 46 | // Implementation of FGPanel. |
| 47 | //////////////////////////////////////////////////////////////////////// |
| 48 | |
| 49 | /** |
| 50 | * Constructor. |
| 51 | */ |
| 52 | FGPanel::FGPanel (const SGPropertyNode_ptr root) : |
| 53 | m_flipx (root->getNode ("/sim/panel/flip-x", true)), |
| 54 | m_bg_width (1.0), m_bg_height (1.0) { |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Destructor. |
| 59 | */ |
| 60 | FGPanel::~FGPanel () { |
| 61 | for (instrument_list_type::iterator it = m_instruments.begin (); |
| 62 | it != m_instruments.end (); |
| 63 | ++it) { |
| 64 | delete *it; |
| 65 | *it = 0; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Initialize the panel. |
| 71 | */ |
| 72 | void |
| 73 | FGPanel::init () { |
| 74 | // Textured Layer Shaders |
| 75 | const char V_Textured_Layer_Shader_Str[] = |
| 76 | #ifdef _RPI |
| 77 | "#version 100 \n" |
| 78 | "attribute vec4 a_position; \n" |
| 79 | "attribute vec2 a_tex_coord; \n" |
| 80 | "varying vec2 v_tex_coord; \n" |
| 81 | #else |
| 82 | "#version 130 \n" |
| 83 | "in vec4 a_position; \n" |
| 84 | "in vec2 a_tex_coord; \n" |
| 85 | "out vec2 v_tex_coord; \n" |
| 86 | #endif |
| 87 | "uniform mat4 u_mvp_matrix; \n" |
| 88 | "void main () { \n" |
| 89 | " gl_Position = u_mvp_matrix * a_position; \n" |
| 90 | " v_tex_coord = a_tex_coord; \n" |
| 91 | "} \n"; |
| 92 | |
| 93 | const char F_Textured_Layer_Shader_Str[] = |
| 94 | #ifdef _RPI |
| 95 | "#version 100 \n" |
| 96 | "precision mediump float; \n" |
| 97 | "varying vec2 v_tex_coord; \n" |
| 98 | #else |
| 99 | "#version 130 \n" |
| 100 | "in vec2 v_tex_coord; \n" |
| 101 | #endif |
| 102 | "uniform sampler2D u_texture; \n" |
| 103 | "void main () { \n" |
| 104 | " vec4 base_color = texture2D (u_texture, v_tex_coord); \n" |
| 105 | " if (base_color.a <= 0.1) { \n" |
| 106 | " discard; \n" |
| 107 | " } \n" |
| 108 | " gl_FragColor = base_color; \n" |
| 109 | "} \n"; |
| 110 | |
| 111 | Textured_Layer_Program_Object = GL_utils::instance ().load_program (V_Textured_Layer_Shader_Str, |
| 112 | F_Textured_Layer_Shader_Str); |
| 113 | if (Textured_Layer_Program_Object == 0) { |
| 114 | terminate (); |
| 115 | } |
| 116 | |
| 117 | // Get the attribute locations |
| 118 | Textured_Layer_Position_Loc = glGetAttribLocation (Textured_Layer_Program_Object, "a_position"); |
| 119 | Textured_Layer_Tex_Coord_Loc = glGetAttribLocation (Textured_Layer_Program_Object, "a_tex_coord"); |
| 120 | |
| 121 | // Get the uniform locations |
| 122 | Textured_Layer_MVP_Loc = glGetUniformLocation (Textured_Layer_Program_Object, "u_mvp_matrix"); |
| 123 | |
| 124 | // Get the sampler location |
| 125 | Textured_Layer_Sampler_Loc = glGetUniformLocation (Textured_Layer_Program_Object, "u_texture"); |
| 126 | |
| 127 | FGTexturedLayer::Init (Textured_Layer_Program_Object, |
| 128 | Textured_Layer_Position_Loc, |
| 129 | Textured_Layer_Tex_Coord_Loc, |
| 130 | Textured_Layer_MVP_Loc, |
| 131 | Textured_Layer_Sampler_Loc); |
| 132 | |
| 133 | // Text Layer Shaders |
| 134 | if (!FGTextLayer::Init ()) { |
| 135 | terminate (); |
| 136 | } |
| 137 | |
| 138 | glClearColor (0.0f, 0.0f, 0.0f, 1.0f); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Bind panel properties. |
| 143 | */ |
| 144 | void |
| 145 | FGPanel::bind () { |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Unbind panel properties. |
| 150 | */ |
| 151 | void |
| 152 | FGPanel::unbind () { |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | FGPanel::update (double dt) { |
| 157 | getInitDisplayList (); |
| 158 | // Draw the instruments. |
| 159 | // Syd Adams: added instrument clipping |
| 160 | for (instrument_list_type::const_iterator current = m_instruments.begin (); |
| 161 | current != m_instruments.end (); |
| 162 | ++current) { |
| 163 | FGPanelInstrument *instr = *current; |
| 164 | GL_utils::instance ().glPushMatrix (); |
| 165 | GL_utils::instance ().glTranslatef (instr->getXPos (), instr->getYPos (), 0); |
| 166 | |
| 167 | instr->draw(); |
| 168 | |
| 169 | GL_utils::instance ().glPopMatrix (); |
| 170 | } |
| 171 | #ifndef _GLES2 |
| 172 | // restore some original state |
| 173 | //glPopAttrib (); |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Add an instrument to the panel. |
| 179 | */ |
| 180 | void |
| 181 | FGPanel::addInstrument (FGPanelInstrument * const instrument) { |
| 182 | m_instruments.push_back (instrument); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Set the panel's background texture. |
| 187 | */ |
| 188 | void |
| 189 | FGPanel::setBackground (const FGCroppedTexture_ptr texture) { |
| 190 | m_bg = texture; |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | FGPanel::setBackgroundWidth (const double d) { |
| 195 | m_bg_width = d; |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | FGPanel::setBackgroundHeight (const double d) { |
| 200 | m_bg_height = d; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Set the panel's multiple background textures. |
| 205 | */ |
| 206 | void |
| 207 | FGPanel::setMultiBackground (const FGCroppedTexture_ptr texture, const int idx) { |
| 208 | m_bg = 0; |
| 209 | m_mbg[idx] = texture; |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | FGPanel::setWidth (const int width) { |
| 214 | m_width = width; |
| 215 | } |
| 216 | |
| 217 | int |
| 218 | FGPanel::getWidth () const { |
| 219 | return m_width; |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | FGPanel::setHeight (const int height) { |
| 224 | m_height = height; |
| 225 | } |
| 226 | |
| 227 | int |
| 228 | FGPanel::getHeight () const { |
| 229 | return m_height; |
| 230 | } |
| 231 | |
| 232 | void |
| 233 | FGPanel::getInitDisplayList () { |
| 234 | glUseProgram (Textured_Layer_Program_Object); |
| 235 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_PROJECTION); |
| 236 | GL_utils::instance ().glLoadIdentity (); |
| 237 | if (m_flipx->getBoolValue ()) { |
| 238 | GL_utils::instance ().gluOrtho2D (m_width, 0, m_height, 0); |
| 239 | } else { |
| 240 | GL_utils::instance ().gluOrtho2D (0, m_width, 0, m_height); |
| 241 | } |
| 242 | GL_utils::instance ().glTranslatef (-1.0f, -1.0f, 0.0f); |
| 243 | GL_utils::instance ().glScalef (2.0f / GLfloat (m_width), 2.0f / GLfloat (m_height), 1.0f); |
| 244 | |
| 245 | #ifndef _GLES2 |
| 246 | glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); |
| 247 | #endif |
| 248 | |
| 249 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_MODELVIEW); |
| 250 | GL_utils::instance ().glLoadIdentity (); |
| 251 | glClear (GL_COLOR_BUFFER_BIT); |
| 252 | |
| 253 | glUseProgram (Textured_Layer_Program_Object); |
| 254 | #ifndef _GLES2 |
| 255 | // save some state |
| 256 | //glPushAttrib (GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT | |
| 257 | // GL_TEXTURE_BIT | GL_PIXEL_MODE_BIT | GL_CULL_FACE | |
| 258 | // GL_DEPTH_BUFFER_BIT ); |
| 259 | #endif |
| 260 | // Draw the background |
| 261 | glEnable (GL_TEXTURE_2D); |
| 262 | #ifndef _GLES2 |
| 263 | glDisable (GL_LIGHTING); |
| 264 | #endif |
| 265 | glEnable (GL_BLEND); |
| 266 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 267 | #ifndef _GLES2 |
| 268 | glEnable (GL_COLOR_MATERIAL); |
| 269 | #endif |
| 270 | glEnable (GL_CULL_FACE); |
| 271 | glCullFace (GL_BACK); |
| 272 | glDisable (GL_DEPTH_TEST); |
| 273 | |
| 274 | if (m_bg != NULL) { |
| 275 | m_bg->bind (Textured_Layer_Sampler_Loc); |
| 276 | const GLfloat v_Vertices[] = { |
| 277 | 0.0f, 0.0f, 0.0f, // Position 0 (bottom left corner) |
| 278 | 0.0f, 0.0f, // TexCoord 0 (bottom left corner) |
| 279 | float (m_width), 0.0f, 0.0f, // Position 1 (bottom right corner) |
| 280 | float (m_bg_width), 0.0f, // TexCoord 1 (bottom right corner) |
| 281 | float (m_width), float (m_height), 0.0f, // Position 2 (top right corner) |
| 282 | float (m_bg_width), float (m_bg_height), // TexCoord 2 (top right corner) |
| 283 | 0.0f, float (m_height), 0.0f, // Position 3 (top left corner) |
| 284 | 0.0f, float (m_bg_height) }; // TexCoord 3 (top left corner) |
| 285 | glVertexAttribPointer (Textured_Layer_Position_Loc, |
| 286 | 3, |
| 287 | GL_FLOAT, |
| 288 | GL_FALSE, |
| 289 | 5 * sizeof (GLfloat), |
| 290 | v_Vertices); |
| 291 | glVertexAttribPointer (Textured_Layer_Tex_Coord_Loc, |
| 292 | 2, |
| 293 | GL_FLOAT, |
| 294 | GL_FALSE, |
| 295 | 5 * sizeof (GLfloat), |
| 296 | &v_Vertices[3]); |
| 297 | |
| 298 | glEnableVertexAttribArray (Textured_Layer_Position_Loc); |
| 299 | glEnableVertexAttribArray (Textured_Layer_Tex_Coord_Loc); |
| 300 | |
| 301 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_PROJECTION); |
| 302 | GL_utils::instance ().glPushMatrix (); |
| 303 | GL_utils::instance ().glMultMatrixf |
| 304 | (*reinterpret_cast <GLfloat (*)[4][4]> |
| 305 | (GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_MODELVIEW))); |
| 306 | glUniformMatrix4fv (Textured_Layer_MVP_Loc, |
| 307 | 1, |
| 308 | GL_FALSE, |
| 309 | GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_PROJECTION)); |
| 310 | |
| 311 | const GLushort indices[] = {0, 1, 2, 0, 2, 3}; |
| 312 | glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); |
| 313 | |
| 314 | GL_utils::instance ().glPopMatrix (); |
| 315 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_MODELVIEW); |
| 316 | } else if (m_mbg[0] != NULL) { |
| 317 | for (int i = 0; i < 4; ++i) { |
| 318 | // top row of textures...(1,3,5,7) |
| 319 | m_mbg[i*2]->bind (Textured_Layer_Sampler_Loc); |
| 320 | const GLfloat v_Vertices_Top[] = { |
| 321 | float (i * m_width / 4), float (m_height / 2), 0.0f, // Position 0 (bottom left corner) |
| 322 | 0.0f, 0.0f, // TexCoord 0 (bottom left corner) |
| 323 | float ((i + 1) * m_width / 4), float (m_height / 2), 0.0f, // Position 1 (bottom right corner) |
| 324 | 1.0f, 0.0f, // TexCoord 1 (bottom right corner) |
| 325 | float ((i + 1) * m_width / 4), float (m_height), 0.0f, // Position 2 (top right corner) |
| 326 | 1.0f, 1.0f, // TexCoord 2 (top right corner) |
| 327 | float (i * m_width / 4), float (m_height), 0.0f, // Position 3 (top left corner) |
| 328 | 0.0f, 1.0f }; // TexCoord 3 (top left corner) |
| 329 | glVertexAttribPointer (Textured_Layer_Position_Loc, |
| 330 | 3, |
| 331 | GL_FLOAT, |
| 332 | GL_FALSE, |
| 333 | 5 * sizeof (GLfloat), |
| 334 | v_Vertices_Top); |
| 335 | glVertexAttribPointer (Textured_Layer_Tex_Coord_Loc, |
| 336 | 2, |
| 337 | GL_FLOAT, |
| 338 | GL_FALSE, |
| 339 | 5 * sizeof (GLfloat), |
| 340 | &v_Vertices_Top[3]); |
| 341 | |
| 342 | glEnableVertexAttribArray (Textured_Layer_Position_Loc); |
| 343 | glEnableVertexAttribArray (Textured_Layer_Tex_Coord_Loc); |
| 344 | |
| 345 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_PROJECTION); |
| 346 | GL_utils::instance ().glPushMatrix (); |
| 347 | GL_utils::instance ().glMultMatrixf |
| 348 | (*reinterpret_cast <GLfloat (*)[4][4]> |
| 349 | (GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_MODELVIEW))); |
| 350 | glUniformMatrix4fv (Textured_Layer_MVP_Loc, |
| 351 | 1, |
| 352 | GL_FALSE, |
| 353 | GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_PROJECTION)); |
| 354 | |
| 355 | const GLushort indices[] = {0, 1, 2, 0, 2, 3}; |
| 356 | glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); |
| 357 | |
| 358 | GL_utils::instance ().glPopMatrix (); |
| 359 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_MODELVIEW); |
| 360 | |
| 361 | // bottom row of textures...(2,4,6,8) |
| 362 | m_mbg[i*2+1]->bind (Textured_Layer_Sampler_Loc); |
| 363 | const GLfloat v_Vertices_Bot[] = { |
| 364 | float (i * m_width / 4), 0.0f, 0.0f, // Position 0 (bottom left corner) |
| 365 | 0.0f, 0.0f, // TexCoord 0 (bottom left corner) |
| 366 | float ((i + 1) * m_width / 4), 0.0f, 0.0f, // Position 1 (bottom right corner) |
| 367 | 1.0f, 0.0f, // TexCoord 1 (bottom right corner) |
| 368 | float ((i + 1) * m_width / 4), float (m_height / 2), 0.0f, // Position 2 (top right corner) |
| 369 | 1.0f, 1.0f, // TexCoord 2 (top right corner) |
| 370 | float (i * m_width / 4), float (m_height / 2), 0.0f, // Position 3 (top left corner) |
| 371 | 0.0f, 1.0f }; // TexCoord 3 (top left corner) |
| 372 | glVertexAttribPointer (Textured_Layer_Position_Loc, |
| 373 | 3, |
| 374 | GL_FLOAT, |
| 375 | GL_FALSE, |
| 376 | 5 * sizeof (GLfloat), |
| 377 | v_Vertices_Bot); |
| 378 | glVertexAttribPointer (Textured_Layer_Tex_Coord_Loc, |
| 379 | 2, |
| 380 | GL_FLOAT, |
| 381 | GL_FALSE, |
| 382 | 5 * sizeof (GLfloat), |
| 383 | &v_Vertices_Bot[3]); |
| 384 | |
| 385 | glEnableVertexAttribArray (Textured_Layer_Position_Loc); |
| 386 | glEnableVertexAttribArray (Textured_Layer_Tex_Coord_Loc); |
| 387 | |
| 388 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_PROJECTION); |
| 389 | GL_utils::instance ().glPushMatrix (); |
| 390 | GL_utils::instance ().glMultMatrixf |
| 391 | (*reinterpret_cast <GLfloat (*)[4][4]> |
| 392 | (GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_MODELVIEW))); |
| 393 | glUniformMatrix4fv (Textured_Layer_MVP_Loc, |
| 394 | 1, |
| 395 | GL_FALSE, |
| 396 | GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_PROJECTION)); |
| 397 | |
| 398 | glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); |
| 399 | |
| 400 | GL_utils::instance ().glPopMatrix (); |
| 401 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_MODELVIEW); |
| 402 | } |
| 403 | } else { |
| 404 | #ifndef _GLES2 |
| 405 | glUseProgram (0); |
| 406 | float c[4]; |
| 407 | glGetFloatv (GL_CURRENT_COLOR, c); |
| 408 | glColor4f (1.0, 0.0, 0.0, 1.0); |
| 409 | glBegin (GL_QUADS); |
| 410 | glVertex2f (0, 0); |
| 411 | glVertex2f (m_width, 0); |
| 412 | glVertex2f (m_width, m_height); |
| 413 | glVertex2f (0, m_height); |
| 414 | glEnd (); |
| 415 | glColor4fv (c); |
| 416 | glUseProgram (Textured_Layer_Program_Object); |
| 417 | #endif |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | |
| 422 | // Register the subsystem. |
| 423 | #if 0 |
| 424 | SGSubsystemMgr::Registrant<FGPanel> registrantFGPanel; |
| 425 | #endif |