-
Notifications
You must be signed in to change notification settings - Fork 0
/
Texture.cpp
119 lines (104 loc) · 3.65 KB
/
Texture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "Texture.h"
#include <iostream>
#include "src/vendor/stb_image/stb_image.h"
Texture::Texture(const std::string& path)
: m_FilePath(path), m_LocalBuffer(nullptr),
m_Width(0), m_Height(0), m_BPP(0)
{
stbi_set_flip_vertically_on_load(1);
m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);
GLCall(glGenTextures(1, &m_RendererID));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
//GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer));
//GLCall(glBindTexture(GL_TEXTURE_2D, 0));
if (m_LocalBuffer)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(m_LocalBuffer);
}
else
{
std::cout << "\nError: Failed to load texture" << std::endl;
std::cout << stbi_failure_reason() << std::endl;
__debugbreak();
}
}
Texture::Texture()
{
}
Texture::~Texture()
{
GLCall(glDeleteTextures(1, &m_RendererID));
}
void Texture::Bind(unsigned int slot) const
{
GLCall(glActiveTexture(GL_TEXTURE0 + slot))
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
}
void Texture::Unbind()
{
GLCall(glBindTexture(GL_TEXTURE_2D, 0));
}
Texture::Texture(const Texture& other)
: m_FilePath(other.m_FilePath), m_LocalBuffer(nullptr),
m_Width(other.m_Width), m_Height(other.m_Height), m_BPP(other.m_BPP)
{
stbi_set_flip_vertically_on_load(1);
m_LocalBuffer = stbi_load(m_FilePath.c_str(), &m_Width, &m_Height, &m_BPP, 4);
GLCall(glGenTextures(1, &m_RendererID));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
if (m_LocalBuffer)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(m_LocalBuffer);
}
else
{
std::cout << "\nError: Failed to load texture" << std::endl;
std::cout << stbi_failure_reason() << std::endl;
__debugbreak();
}
}
//Texture& Texture::operator=(const Texture& other)
//{
// m_FilePath = other.m_FilePath;
// m_LocalBuffer = nullptr,
// m_Width = other.m_Width;
// m_Height = other.m_Height;
// m_BPP = other.m_BPP;
//
// stbi_set_flip_vertically_on_load(1);
// m_LocalBuffer = stbi_load(other.m_FilePath.c_str(), &m_Width, &m_Height, &m_BPP, 4);
//
// GLCall(glGenTextures(1, &m_RendererID));
// GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
//
// GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
// GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
// GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
// GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
//
// if (m_LocalBuffer)
// {
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer);
// glBindTexture(GL_TEXTURE_2D, 0);
// stbi_image_free(m_LocalBuffer);
// }
// else
// {
// std::cout << "\nError: Failed to load texture" << std::endl;
// std::cout << stbi_failure_reason() << std::endl;
// __debugbreak();
// }
// return *this;
//}