-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
67 lines (55 loc) · 1.81 KB
/
Texture.cpp
File metadata and controls
67 lines (55 loc) · 1.81 KB
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
#include "Texture.h"
void texUnit(Shader& shader, const char* uniform, GLint unit)
{
shader.activate();
glUniform1i(glGetUniformLocation(shader.handle, uniform), unit);
}
Texture::Texture(const char* filename, const char* type, GLuint slot, bool flipImage) : type{ type }, slot{ GL_TEXTURE0 + slot }
{
int imgWidth, imgHeight, numColCh;
if (flipImage)
stbi_set_flip_vertically_on_load(true); // remember in .gltfs to not do this
stbi_uc* data = stbi_load(filename, &imgWidth, &imgHeight, &numColCh, 0);
if (!data)
throw std::invalid_argument{ "Failed to load texture" };
glGenTextures(1, &this->handle);
glActiveTexture(this->slot);
glBindTexture(GL_TEXTURE_2D, this->handle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// float flatColor = { 1.0f, 1.0f, 1.0f, 1.0f };
// glTexParameterfv(texType, GL_TEXTURE_BORDER_COLOR, flatColor);
switch (numColCh)
{
case 4:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imgWidth, imgHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
break;
case 3:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
break;
case 1:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, imgWidth, imgHeight, 0, GL_RED, GL_UNSIGNED_BYTE, data);
break;
default:
throw std::invalid_argument{ "Invalid numColCh" };
}
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
glBindTexture(GL_TEXTURE_2D, 0);
}
Texture::~Texture()
{
glDeleteTextures(1, &handle);
}
void Texture::bind()
{
glActiveTexture(slot);
glBindTexture(GL_TEXTURE_2D, handle);
}
void Texture::unbind()
{
glActiveTexture(slot);
glBindTexture(GL_TEXTURE_2D, 0);
}