RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
Texture.cpp
Go to the documentation of this file.
1#include "Texture.h"
2#define STB_IMAGE_IMPLEMENTATION
3#include "stb_image.h"
4
5Texture::Texture(const std::string& path) :
6 m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0)
7{
8
9 stbi_set_flip_vertically_on_load(1);
10 m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);
11 if (m_LocalBuffer == NULL)
12 {
13 std::cout << stbi_failure_reason();
14 }
15 GLCall(glGenTextures(1, &m_RendererID));
16 GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
17
18 GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer));
19 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
20 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
21 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
22 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
23
24 GLCall(glBindTexture(GL_TEXTURE_2D, 0));
25
26 if (m_LocalBuffer)
27 stbi_image_free(m_LocalBuffer);
28 stbi_set_flip_vertically_on_load(0);
29}
30
32{
33 GLCall(glDeleteTextures(1, &m_RendererID));
34}
35
37{
38 return m_RendererID;
39}
40
41void Texture::SetFilePath(const std::string& path)
42{
43 m_FilePath = path;
44}
45
46void Texture::Bind(unsigned int slot /*= 0*/) const
47{
48
49 GLCall(glActiveTexture(GL_TEXTURE0 + slot));
50 GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
51}
52
53void Texture::Unbind() const
54{
55 GLCall(glBindTexture(GL_TEXTURE_2D, 0));
56}
#define GLCall(x)
Definition Renderer.h:12
~Texture()
Definition Texture.cpp:31
void Unbind() const
Definition Texture.cpp:53
void Bind(unsigned int slot=0) const
Definition Texture.cpp:46
Texture(const std::string &path)
Definition Texture.cpp:5
void SetFilePath(const std::string &path)
Definition Texture.cpp:41
int m_BPP
Definition Texture.h:9
int m_Height
Definition Texture.h:9
std::string m_FilePath
Definition Texture.h:13
unsigned int m_RendererID
Definition Texture.h:10
unsigned char * m_LocalBuffer
Definition Texture.h:8
int m_Width
Definition Texture.h:9
unsigned int GetRendererID()
Definition Texture.cpp:36