Vivid
Loading...
Searching...
No Matches
Texture.cpp
1#include "Texture.h"
2
3namespace Vivid
4{
5 Texture::Texture(const std::string& path)
6 : m_RendererID(0)
7 , m_FilePath(path)
8 , m_LocalBuffer(nullptr)
9 , m_Width(0)
10 , m_Height(0)
11 , m_BPP(0)
12 {
13 stbi_set_flip_vertically_on_load(0);
14 stbi_set_flip_vertically_on_load_thread(0);
15 m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);
16
17 GLCall(glGenTextures(1, &m_RendererID));
18 GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
19
20 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
21 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
22 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
23 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
24
25 GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
26 m_LocalBuffer));
27 GLCall(glBindTexture(GL_TEXTURE_2D, 0));
28
29 if (m_LocalBuffer)
30 {
31 stbi_image_free(m_LocalBuffer);
32 }
33
34 m_Name = "Texture" + std::to_string(m_RendererID);
35 }
36
37 Texture::Texture()
38 : m_RendererID(0)
39 , m_LocalBuffer(nullptr)
40 , m_Width(0)
41 , m_Height(0)
42 , m_BPP(0)
43 {
44 GLCall(glGenTextures(1, &m_RendererID));
45 GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
46
47 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
48 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
49 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
50 GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
51
52 GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
53 m_LocalBuffer));
54 GLCall(glBindTexture(GL_TEXTURE_2D, 0));
55 }
56
57 Texture::~Texture()
58 {
59 GLCall(glDeleteTextures(1, &m_RendererID));
60 }
61
62 void Texture::Bind(unsigned int slot) const
63 {
64 GLCall(glActiveTexture(GL_TEXTURE0 + slot));
65 GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
66 }
67
68 void Texture::Unbind() const
69 {
70 GLCall(glBindTexture(GL_TEXTURE_2D, 0));
71 }
72
73}