Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Texture.h
Go to the documentation of this file.
1/**
2 * @file Texture.h
3 * @ingroup mnd_graphics
4 * @brief 2D texture resource and a path-keyed cache (TextureManager).
5 *
6 * ## Loading a texture
7 * Prefer TextureManager::GetOrLoadTexture() over Texture::Load() directly —
8 * the manager returns the same shared_ptr for repeated requests to the same
9 * path, avoiding redundant GPU uploads.
10 *
11 * @code
12 * auto tex = Engine::GetInstance().GetTextureManager()
13 * .GetOrLoadTexture("assets/textures/wall.png");
14 * material->SetParam("uDiffuse", tex);
15 * @endcode
16 *
17 * Textures are uploaded to the GPU in Texture::Init() using stb_image for
18 * decoding. The GL texture object is deleted in the destructor.
19 *
20 * @see Material::SetParam, TextureManager, ShaderProgram::SetTexture
21 */
22
23#pragma once
24
25#include <memory>
26#include <string>
27#include <unordered_map>
28
29#include "graphics/GLForward.h"
30
31namespace mnd
32{
33
34/**
35 * @brief 2D OpenGL texture object wrapping a GL_TEXTURE_2D handle.
36 *
37 * Loaded from disk via stb_image. Once uploaded to the GPU the CPU pixel
38 * data is freed. The GL handle lives until the Texture is destroyed.
39 */
41{
42public:
43 /**
44 * @brief Upload pixel data to the GPU immediately.
45 * @param width Image width in pixels.
46 * @param height Image height in pixels.
47 * @param numChannels Number of channels (1 = greyscale, 3 = RGB, 4 = RGBA).
48 * @param data Raw pixel bytes (stb_image output). May be freed after this call.
49 */
50 Texture(int width, int height, int numChannels, unsigned char *data);
51
52 /// Calls glDeleteTextures() to release the GPU handle.
53 ~Texture();
54
55 /// Returns the underlying OpenGL texture handle (GL_TEXTURE_2D target).
56 [[nodiscard]] GLuint GetID() const;
57
58 /**
59 * @brief (Re-)initialise the texture with new pixel data.
60 * Called by the constructor; can also re-upload changed data.
61 */
62 void Init(int width, int height, int numChannels, unsigned char *data);
63
64 /**
65 * @brief Load a texture from a file path using stb_image.
66 * @param path Filesystem path to a PNG/JPG/BMP/TGA image.
67 * @return Shared pointer to the loaded Texture, or nullptr on failure.
68 */
69 static std::shared_ptr<Texture> Load(const std::string &path);
70
71 /**
72 * @brief Decode an in-memory image (PNG/JPG/...) into a GPU texture.
73 *
74 * Used for textures embedded inside model containers (.glb, FBX with
75 * embedded media), where there's no path to read from disk.
76 */
77 static std::shared_ptr<Texture> LoadFromMemory(const unsigned char *data, int sizeBytes);
78
79private:
80 int m_width = 0; ///< Image width in pixels.
81 int m_height = 0; ///< Image height in pixels.
82 int m_numChannels = 0; ///< Channel count returned by stb_image.
83 GLuint m_textureID = 0; ///< GL texture object handle.
84};
85
86/**
87 * @brief Path-keyed cache that prevents the same image from being uploaded twice.
88 *
89 * Owned by the Engine singleton (Engine::GetTextureManager()). Material setup
90 * code should always go through this manager rather than Texture::Load()
91 * directly, so the same GL texture is reused across multiple materials.
92 */
94{
95public:
96 /**
97 * @brief Return a cached texture, loading it from disk on first request.
98 * @param path Filesystem path used as the cache key.
99 * @return Shared pointer to the texture (never nullptr if the file exists).
100 */
101 std::shared_ptr<Texture> GetOrLoadTexture(const std::string &path);
102
103private:
104 /// In-memory cache: path → shared Texture instance.
105 std::unordered_map<std::string, std::shared_ptr<Texture>> m_textures;
106};
107
108} // namespace mnd
unsigned int GLuint
Definition GLForward.h:11
Path-keyed cache that prevents the same image from being uploaded twice.
Definition Texture.h:94
std::shared_ptr< Texture > GetOrLoadTexture(const std::string &path)
Return a cached texture, loading it from disk on first request.
Definition Texture.cpp:58
2D OpenGL texture object wrapping a GL_TEXTURE_2D handle.
Definition Texture.h:41
void Init(int width, int height, int numChannels, unsigned char *data)
(Re-)initialise the texture with new pixel data. Called by the constructor; can also re-upload change...
Definition Texture.cpp:32
~Texture()
Calls glDeleteTextures() to release the GPU handle.
Definition Texture.cpp:24
static std::shared_ptr< Texture > LoadFromMemory(const unsigned char *data, int sizeBytes)
Decode an in-memory image (PNG/JPG/...) into a GPU texture.
Definition Texture.cpp:104
static std::shared_ptr< Texture > Load(const std::string &path)
Load a texture from a file path using stb_image.
Definition Texture.cpp:76
GLuint GetID() const
Returns the underlying OpenGL texture handle (GL_TEXTURE_2D target).
Definition Texture.cpp:71