2022-12-12 20:41:53 +08:00

50 lines
1.6 KiB
C++

#pragma once
#include <STBImage/stb_image.h>
#include <qopengl.h>
#include <qopenglcontext.h>
#include "texture.h"
#include "logger.h"
#include "utils.h"
QOpenGLContext* sharedContext;
// Must set context before calling this function
Texture::Texture(TextureType type, std::string path) {
_type = type;
_path = path;
OPENGL_FUNCTIONS->glGenTextures(1, &_id);
OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, _id);
// Set the texture wrapping parameters
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method)
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering parameters
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load image, create texture and generate mipmaps
int width, height, nrChannels;
unsigned char* data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
if (data) {
GLenum format;
if (nrChannels == 1) {
format = GL_RED;
}
else if (nrChannels == 3) {
format = GL_RGB;
}
else if (nrChannels == 4) {
format = GL_RGBA;
}
OPENGL_FUNCTIONS->glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
OPENGL_FUNCTIONS->glGenerateMipmap(GL_TEXTURE_2D);
}
else {
Logger::error("Failed to load texture: " + path);
}
stbi_image_free(data);
}