-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cpp
60 lines (52 loc) · 1.7 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
#include <iostream>
#include <utility>
#include <stb/stb_image.h>
#include "Texture.h"
Texture::Texture(std::string name, std::string imagePath, unsigned int index) :
name(std::move(name)), imagePath(std::move(imagePath)), index(index) {
handle = 0;
isLoaded = false;
}
bool Texture::load() {
glGenTextures(1, &handle);
use();
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_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width = 0;
int height = 0;
int channelsCount = 0;
stbi_set_flip_vertically_on_load(true);
unsigned char *imageData = stbi_load(imagePath.c_str(), &width, &height, &channelsCount, 0);
if (imageData == nullptr) {
std::cout << "Error: unable to load image file \"" << imagePath << "\"." << std::endl;
destroy();
return false;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, channelsCount == 4 ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE,
imageData);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(imageData);
isLoaded = true;
return true;
}
void Texture::attach(const Shader &shader) const {
shader.setInteger(name, (int) index);
}
void Texture::use() const {
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(GL_TEXTURE_2D, handle);
}
void Texture::dispose() {
if (isLoaded) {
destroy();
}
}
void Texture::destroy() {
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(GL_TEXTURE_2D, 0);
glDeleteTextures(1, &handle);
handle = 0;
isLoaded = false;
}