YoggieEngine/BarinkEngine/graphics/Texture.cpp

40 lines
1.1 KiB
C++
Raw Normal View History

#include "../Include/Graphics/Texture.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include "Graphics/stb_image.h"
#include <iostream>
Texture::Texture(const std::string texturePath) {
int width, height, channels;
unsigned char* data = stbi_load(texturePath.c_str(), &width, &height, &channels, 0);
std::cout << channels << std::endl;
if (data) {
glGenTextures(1, &Id);
glBindTexture(GL_TEXTURE_2D, Id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
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);
}
else {
spdlog::error("Failed to load image (%s)", texturePath );
}
stbi_image_free(data);
}
void Texture::Bind() {
glBindTexture(GL_TEXTURE_2D, Id);
}
void Texture::Unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}