-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cpp
64 lines (50 loc) · 2.24 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"
Texture::Texture(int width, int height, GLenum access) : m_texAccess(access), m_width(width), m_height(height) {
glGenTextures(1, &id);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, this->m_width, this->m_height, 0, GL_RGBA, GL_FLOAT, nullptr);
std::cout << this->id << std::endl;
}
Texture::Texture(const std::string &fileName, GLenum access, GLuint imageChannels) {
glGenTextures(1, &this->id);
glBindTexture(GL_TEXTURE_2D, this->id);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// 7/28 unused for now, changed to a nullptr since it is unused.
// 7/31 new nrChannels to determine the image channels, 1 GL_R, 2 GL_RG, 3 GL_RGB, 4 GL_RGBA
int nrChannels;
// Implement a get filesystem path.
unsigned char *data = stbi_load(fileName.c_str(), &this->m_width, &this->m_height, /*nrChannels*/ nullptr, 0);
this->checkErrors(data, imageChannels);
stbi_image_free(data);
}
void Texture::use() {
glBindImageTexture(0, id, 0, GL_FALSE, 0, this->m_texAccess, GL_RGBA32F);
}
void Texture::checkErrors(unsigned char *data, GLuint imageChannels) {
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, imageChannels, this->m_width, this->m_height, 0, imageChannels, GL_UNSIGNED_BYTE,
data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
std::cout << "Failed to load texture" << std::endl;
}
}
int Texture::getWidth() const {
return this->m_width;
}
int Texture::getHeight() const {
return this->m_height;
}
void *Texture::getImGuiTextureId() const {
// Cast to size_t to avoid compiler warnings.
return (void *) (size_t) this->id;
}