32void Texture::Init(
int width,
int height,
int numChannels,
unsigned char *data)
34 glGenTextures(1, &m_textureID);
35 glBindTexture(GL_TEXTURE_2D, m_textureID);
37 GLint internalFormat = GL_RGB;
42 internalFormat = GL_RGBA;
46 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, data);
48 glGenerateMipmap(GL_TEXTURE_2D);
50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
85 if (!std::filesystem::exists(fullPath))
90 std::shared_ptr<Texture> result;
92 unsigned char *data = stbi_load(fullPath.string().c_str(), &width, &height, &numChannels, 0);
96 result = std::make_shared<Texture>(width, height, numChannels, data);
98 stbi_image_free(data);
106 if (data ==
nullptr || sizeBytes <= 0)
115 unsigned char *decoded = stbi_load_from_memory(data, sizeBytes, &width, &height, &numChannels, 0);
116 if (decoded ==
nullptr)
121 auto result = std::make_shared<Texture>(width, height, numChannels, decoded);
122 stbi_image_free(decoded);
std::shared_ptr< Texture > GetOrLoadTexture(const std::string &path)
Return a cached texture, loading it from disk on first request.
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...
~Texture()
Calls glDeleteTextures() to release the GPU handle.
static std::shared_ptr< Texture > LoadFromMemory(const unsigned char *data, int sizeBytes)
Decode an in-memory image (PNG/JPG/...) into a GPU texture.
static std::shared_ptr< Texture > Load(const std::string &path)
Load a texture from a file path using stb_image.
Texture(int width, int height, int numChannels, unsigned char *data)
Upload pixel data to the GPU immediately.
GLuint GetID() const
Returns the underlying OpenGL texture handle (GL_TEXTURE_2D target).