| 1 | // SPDX-FileCopyrightText: 2000 David Megginson |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <string> |
| 5 | |
| 6 | #include <simgear/misc/sg_path.hxx> |
| 7 | #include <simgear/props/props.hxx> |
| 8 | |
| 9 | #include "ApplicationProperties.hxx" |
| 10 | #include "FGCroppedTexture.hxx" |
| 11 | |
| 12 | using std::map; |
| 13 | using std::string; |
| 14 | |
| 15 | GLuint FGCroppedTexture::s_current_bound_texture = 0; |
| 16 | map <string, GLuint> FGCroppedTexture::s_cache; |
| 17 | map <string, FGTextureLoaderInterface*> FGCroppedTexture::s_TextureLoader; |
| 18 | FGDummyTextureLoader FGCroppedTexture::s_DummyTextureLoader; |
| 19 | |
| 20 | FGCroppedTexture::FGCroppedTexture (const string &path, |
| 21 | const float minX, const float minY, |
| 22 | const float maxX, const float maxY) : |
| 23 | m_path (path), |
| 24 | m_minX (minX), m_minY (minY), |
| 25 | m_maxX (maxX), m_maxY (maxY), |
| 26 | m_texture(0) { |
| 27 | } |
| 28 | |
| 29 | FGCroppedTexture::~FGCroppedTexture () { |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | FGCroppedTexture::setPath (const string &path) { |
| 34 | m_path = path; |
| 35 | } |
| 36 | |
| 37 | const string & |
| 38 | FGCroppedTexture::getPath () const { |
| 39 | return m_path; |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | FGCroppedTexture::setCrop (const float minX, const float minY, const float maxX, const float maxY) { |
| 44 | m_minX = minX; m_minY = minY; m_maxX = maxX; m_maxY = maxY; |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | FGCroppedTexture::registerTextureLoader (const string &extension, |
| 49 | FGTextureLoaderInterface * const loader) { |
| 50 | if (s_TextureLoader.count (extension) == 0) { |
| 51 | s_TextureLoader[extension] = loader; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | float |
| 56 | FGCroppedTexture::getMinX () const { |
| 57 | return m_minX; |
| 58 | } |
| 59 | |
| 60 | float |
| 61 | FGCroppedTexture::getMinY () const { |
| 62 | return m_minY; |
| 63 | } |
| 64 | |
| 65 | float |
| 66 | FGCroppedTexture::getMaxX () const { |
| 67 | return m_maxX; |
| 68 | } |
| 69 | |
| 70 | float |
| 71 | FGCroppedTexture::getMaxY () const { |
| 72 | return m_maxY; |
| 73 | } |
| 74 | |
| 75 | GLuint |
| 76 | FGCroppedTexture::getTexture () const { |
| 77 | return m_texture; |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | FGCroppedTexture::bind (const GLint Textured_Layer_Sampler_Loc) { |
| 82 | if (m_texture == 0) { |
| 83 | SG_LOG (SG_COCKPIT, |
| 84 | SG_DEBUG, |
| 85 | "First bind of texture " << m_path); |
| 86 | if (s_cache.count (m_path) > 0 ) { |
| 87 | m_texture = s_cache[m_path]; |
| 88 | SG_LOG (SG_COCKPIT, |
| 89 | SG_DEBUG, |
| 90 | "Using texture " << m_path << " from cache (#" << m_texture << ")"); |
| 91 | } else { |
| 92 | const SGPath path (ApplicationProperties::GetRootPath (m_path.c_str ())); |
| 93 | const string extension (path.extension ()); |
| 94 | FGTextureLoaderInterface *loader (&s_DummyTextureLoader); |
| 95 | if (s_TextureLoader.count (extension) == 0) { |
| 96 | SG_LOG (SG_COCKPIT, |
| 97 | SG_ALERT, |
| 98 | "Can't handle textures of type " << extension); |
| 99 | } else { |
| 100 | loader = s_TextureLoader[extension]; |
| 101 | } |
| 102 | |
| 103 | m_texture = loader->loadTexture (path.local8BitStr ()); |
| 104 | SG_LOG (SG_COCKPIT, |
| 105 | SG_DEBUG, |
| 106 | "Texture " << path << " loaded from file as #" << m_texture); |
| 107 | s_cache[m_path] = m_texture; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (s_current_bound_texture != m_texture) { |
| 112 | glActiveTexture (GL_TEXTURE0); |
| 113 | glBindTexture (GL_TEXTURE_2D, m_texture); |
| 114 | glUniform1i (Textured_Layer_Sampler_Loc, 0); |
| 115 | s_current_bound_texture = m_texture; |
| 116 | } |
| 117 | } |