//
// Created by sebastian on 30.07.26.
//

#include "TextureUploader.h"

#include "glad/glad.h"

engine::Texture2D engine::TextureUploader::upload(const RawTextureData &data) {
    GLuint id;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);

    GLenum format = data.channels == 4 ? GL_RGBA : data.channels == 3 ? GL_RGB : GL_RED;
    glTexImage2D(GL_TEXTURE_2D, 0, format, data.width, data.height, 0, format, GL_UNSIGNED_BYTE, data.pixels.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);

    return {id, data.width, data.height, data.channels}; // privater Ctor, TextureUploader ist friend
}
