Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
Texture.cpp
Go to the documentation of this file.
1#include <filesystem>
2#include <memory>
3
4#include "graphics/Texture.h"
5
6#include "Engine.h"
7#include "Log.h"
8#include "graphics/GraphicsAPI.h"
9
10#define STB_IMAGE_IMPLEMENTATION
11#include <stb_image.h>
12
13namespace mnd
14{
15
16Texture::Texture(int width, int height, int numChannels, unsigned char *data)
17 : m_width(width)
18 , m_height(height)
19 , m_numChannels(numChannels)
20{
21 Init(width, height, numChannels, data);
22}
23
25{
26 if (m_textureID > 0)
27 {
28 glDeleteTextures(1, &m_textureID);
29 }
30}
31
32void Texture::Init(int width, int height, int numChannels, unsigned char *data)
33{
34 glGenTextures(1, &m_textureID);
35 glBindTexture(GL_TEXTURE_2D, m_textureID);
36
37 GLint internalFormat = GL_RGB;
38 GLenum format = GL_RGB;
39
40 if (numChannels == 4)
41 {
42 internalFormat = GL_RGBA;
43 format = GL_RGBA;
44 }
45
46 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, data);
47
48 glGenerateMipmap(GL_TEXTURE_2D);
49
50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
52
53 // PS1-style pixelated sampling: nearest-neighbor, no mip filtering.
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
56}
57
58std::shared_ptr<Texture> TextureManager::GetOrLoadTexture(const std::string &path)
59{
60 auto it = m_textures.find(path);
61 if (it != m_textures.end())
62 {
63 return it->second;
64 }
65
66 auto texture = Texture::Load(path);
67 m_textures[path] = texture;
68 return texture;
69}
70
72{
73 return m_textureID;
74}
75
76std::shared_ptr<Texture> Texture::Load(const std::string &path)
77{
78 int width = 0;
79 int height = 0;
80 int numChannels = 0;
81
83 auto fullPath = fs.GetAssetsFolder() / path;
84
85 if (!std::filesystem::exists(fullPath))
86 {
87 return nullptr;
88 }
89
90 std::shared_ptr<Texture> result;
91
92 unsigned char *data = stbi_load(fullPath.string().c_str(), &width, &height, &numChannels, 0);
93
94 if (data != nullptr)
95 {
96 result = std::make_shared<Texture>(width, height, numChannels, data);
97 LOG_INFO("Image loaded");
98 stbi_image_free(data);
99 }
100
101 return result;
102}
103
104std::shared_ptr<Texture> Texture::LoadFromMemory(const unsigned char *data, int sizeBytes)
105{
106 if (data == nullptr || sizeBytes <= 0)
107 {
108 return nullptr;
109 }
110
111 int width = 0;
112 int height = 0;
113 int numChannels = 0;
114
115 unsigned char *decoded = stbi_load_from_memory(data, sizeBytes, &width, &height, &numChannels, 0);
116 if (decoded == nullptr)
117 {
118 return nullptr;
119 }
120
121 auto result = std::make_shared<Texture>(width, height, numChannels, decoded);
122 stbi_image_free(decoded);
123 return result;
124}
125
126} // namespace mnd
int GLint
Definition GLForward.h:13
unsigned int GLenum
Definition GLForward.h:12
unsigned int GLuint
Definition GLForward.h:11
Console logging macros for all engine and game code.
#define LOG_INFO(fmt,...)
Definition Log.h:79
FileSystem & GetFileSystem()
Returns the file system helper for asset-relative path resolution.
Definition Engine.cpp:453
static Engine & GetInstance()
Returns the single Engine instance (created on first call).
Definition Engine.cpp:59
std::filesystem::path GetAssetsFolder() const
Return the runtime assets directory.
std::shared_ptr< Texture > GetOrLoadTexture(const std::string &path)
Return a cached texture, loading it from disk on first request.
Definition Texture.cpp:58
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
Texture(int width, int height, int numChannels, unsigned char *data)
Upload pixel data to the GPU immediately.
Definition Texture.cpp:16
GLuint GetID() const
Returns the underlying OpenGL texture handle (GL_TEXTURE_2D target).
Definition Texture.cpp:71