| 1 | // SPDX-FileCopyrightText: 2000 David Megginson |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "GL_utils.hxx" |
| 5 | #include "FGTexturedLayer.hxx" |
| 6 | |
| 7 | GLuint FGTexturedLayer::Textured_Layer_Program_Object (0); |
| 8 | GLint FGTexturedLayer::Textured_Layer_Position_Loc (0); |
| 9 | GLint FGTexturedLayer::Textured_Layer_Tex_Coord_Loc (0); |
| 10 | GLint FGTexturedLayer::Textured_Layer_MVP_Loc (0); |
| 11 | GLint FGTexturedLayer::Textured_Layer_Sampler_Loc (0); |
| 12 | |
| 13 | void |
| 14 | FGTexturedLayer::Init (const GLuint Program_Object, |
| 15 | const GLint Position_Loc, |
| 16 | const GLint Tex_Coord_Loc, |
| 17 | const GLint MVP_Loc, |
| 18 | const GLint Sampler_Loc) { |
| 19 | Textured_Layer_Program_Object = Program_Object; |
| 20 | Textured_Layer_Position_Loc = Position_Loc; |
| 21 | Textured_Layer_Tex_Coord_Loc = Tex_Coord_Loc; |
| 22 | Textured_Layer_MVP_Loc = MVP_Loc; |
| 23 | Textured_Layer_Sampler_Loc = Sampler_Loc; |
| 24 | } |
| 25 | |
| 26 | FGTexturedLayer::FGTexturedLayer (const int w, const int h) : |
| 27 | FGInstrumentLayer (w, h) { |
| 28 | } |
| 29 | |
| 30 | FGTexturedLayer::FGTexturedLayer (const FGCroppedTexture_ptr texture, const int w, const int h) : |
| 31 | FGInstrumentLayer (w, h), |
| 32 | m_texture (texture), |
| 33 | m_emissive (false) { |
| 34 | } |
| 35 | |
| 36 | FGTexturedLayer::~FGTexturedLayer () { |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | FGTexturedLayer::draw () { |
| 41 | if (test ()) { |
| 42 | transform (); |
| 43 | getDisplayList (); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | FGTexturedLayer::setTexture (const FGCroppedTexture_ptr texture) { |
| 49 | m_texture = texture; |
| 50 | } |
| 51 | |
| 52 | FGCroppedTexture_ptr |
| 53 | FGTexturedLayer::getTexture () const { |
| 54 | return m_texture; |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | FGTexturedLayer::setEmissive (const bool emissive) { |
| 59 | m_emissive = emissive; |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | FGTexturedLayer::getDisplayList () { |
| 64 | int w2 = m_w / 2; |
| 65 | int h2 = m_h / 2; |
| 66 | |
| 67 | glUseProgram (Textured_Layer_Program_Object); |
| 68 | m_texture->bind (Textured_Layer_Sampler_Loc); |
| 69 | const GLfloat v_Vertices[] = { |
| 70 | float (-w2), float (-h2), 0.0f, // Position 0 (bottom left corner) |
| 71 | m_texture->getMinX (), m_texture->getMinY (), // TexCoord 0 (bottom left corner) |
| 72 | float ( w2), float (-h2), 0.0f, // Position 1 (bottom right corner) |
| 73 | m_texture->getMaxX (), m_texture->getMinY (), // TexCoord 1 (bottom right corner) |
| 74 | float ( w2), float ( h2), 0.0f, // Position 2 (top right corner) |
| 75 | m_texture->getMaxX (), m_texture->getMaxY (), // TexCoord 2 (top right corner) |
| 76 | float (-w2), float ( h2), 0.0f, // Position 3 (top left corner) |
| 77 | m_texture->getMinX (), m_texture->getMaxY () }; // TexCoord 3 (top left corner) |
| 78 | glVertexAttribPointer (Textured_Layer_Position_Loc, |
| 79 | 3, |
| 80 | GL_FLOAT, |
| 81 | GL_FALSE, |
| 82 | 5 * sizeof (GLfloat), |
| 83 | v_Vertices); |
| 84 | glVertexAttribPointer (Textured_Layer_Tex_Coord_Loc, |
| 85 | 2, |
| 86 | GL_FLOAT, |
| 87 | GL_FALSE, |
| 88 | 5 * sizeof (GLfloat), |
| 89 | &v_Vertices[3]); |
| 90 | |
| 91 | glEnableVertexAttribArray (Textured_Layer_Position_Loc); |
| 92 | glEnableVertexAttribArray (Textured_Layer_Tex_Coord_Loc); |
| 93 | |
| 94 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_PROJECTION); |
| 95 | GL_utils::instance ().glPushMatrix (); |
| 96 | GL_utils::instance ().glMultMatrixf |
| 97 | (*reinterpret_cast <GLfloat (*)[4][4]> |
| 98 | (GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_MODELVIEW))); |
| 99 | glUniformMatrix4fv (Textured_Layer_MVP_Loc, |
| 100 | 1, |
| 101 | GL_FALSE, |
| 102 | GL_utils::instance ().get_top_matrix (GL_utils::GL_UTILS_PROJECTION)); |
| 103 | |
| 104 | const GLushort indices[] = {0, 1, 2, 0, 2, 3}; |
| 105 | glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); |
| 106 | GL_utils::instance ().glPopMatrix (); |
| 107 | GL_utils::instance ().glMatrixMode (GL_utils::GL_UTILS_MODELVIEW); |
| 108 | } |