27 lines
971 B
C++
27 lines
971 B
C++
#include "Texture.h"
|
|
#include <iostream>
|
|
#include "../lib/loaders/stb_image.h"
|
|
#include "../lib/glad/glad.h"
|
|
|
|
Texture createTexture(const char* source_path) {
|
|
Texture result = {0};
|
|
glGenTextures(1, &result.tex_id);
|
|
glBindTexture(GL_TEXTURE_2D, result.tex_id);
|
|
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 nr_channels;
|
|
stbi_uc *data = stbi_load(source_path, &result.width, &result.height, &nr_channels, 0);
|
|
if (data) {
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, result.width, result.height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
} else {
|
|
std::cout << "Failed to load texture." << std::endl;
|
|
}
|
|
stbi_image_free(data);
|
|
|
|
return result;
|
|
}
|