forked from christopher-besch/opengl_reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cpp
64 lines (51 loc) · 2.45 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
#include "Texture.h"
#include "vendor/stb_image/stb_image.h"
Texture::Texture(const std::string& path)
: m_renderer_id(0), m_file_path(path), m_local_buffer(nullptr), m_width(0), m_height(0), m_bpp(0)
{
// the texture has to be flipped <- OpenGL expects the start to be in the bottom whereas pngs start at the top
stbi_set_flip_vertically_on_load(1);
// <- how many channels? 4 <- RGBA
m_local_buffer = stbi_load(path.c_str(), &m_width, &m_height, &m_bpp, 4);
glGenTextures(1, &m_renderer_id);
glBindTexture(GL_TEXTURE_2D, m_renderer_id);
// linear resample on minification filter
// <- type of data (integer)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// magnification filter
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// wrap mode
// <- horizontal wrap
// <- clamp -> don't extend the area (no tiling)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// same with vertical wrap
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// <- not a multi-level texture
// <- internal format -> how OpenGL stores texture data
// <- bits per chanel
// <- border thickness
// <- format -> format of the data OpenGL is provided with (m_local_buffer)
// <- type of the data
// <- pointer to pixels (can be nullptr -> only create buffer and later on load pixels)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_local_buffer);
glBindTexture(GL_TEXTURE_2D, 0);
// fre the memory after use
if (m_local_buffer)
{
stbi_image_free(m_local_buffer);
}
}
Texture::~Texture()
{
glDeleteTextures(1, &m_renderer_id);
}
void Texture::bind(const unsigned int slot) const
{
// <- texture slot (OpenGL supports up to 31 but the platform used might not)
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, m_renderer_id);
}
void Texture::unbind() const
{
glBindTexture(GL_TEXTURE_2D, 0);
}