-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture_loader.cpp
More file actions
47 lines (36 loc) · 1.41 KB
/
Copy pathtexture_loader.cpp
File metadata and controls
47 lines (36 loc) · 1.41 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
// texture_loader.cpp
#include "texture_loader.h"
TextureLoader::TextureLoader(int textureOffset):
textureOffset(textureOffset) {
stbi_set_flip_vertically_on_load(true);
}
TextureLoader::~TextureLoader() {}
void TextureLoader::setTextureParameters() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
unsigned int TextureLoader::loadTexture(const std::string& filename) {
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
setTextureParameters();
int width, height, nrChannels;
unsigned char* data = stbi_load(("../textures/" + filename).c_str(), &width, &height, &nrChannels, 0);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cout << "Failed to load texture: " << filename << std::endl;
}
stbi_image_free(data);
return textureID;
}
std::vector<unsigned int> TextureLoader::loadTextures(const std::vector<std::string>& textureFiles) {
std::vector<unsigned int> textureIDs(textureFiles.size());
for (size_t i = 0; i < textureFiles.size(); i++) {
glActiveTexture(GL_TEXTURE0 + i + textureOffset);
textureIDs[i] = loadTexture(textureFiles[i]);
}
return textureIDs;
}