79 lines
1.7 KiB
C++
79 lines
1.7 KiB
C++
#pragma once
|
|
|
|
#include "../../assets/dynamic_read_buffers"
|
|
#include "assets/dynamic_data_stores/dynamic_texture_store.hpp"
|
|
#include "opengl/data/texture_data.hpp"
|
|
#include <vector>
|
|
|
|
namespace zgl
|
|
{
|
|
class texture_data_uploader
|
|
{
|
|
|
|
void upload(
|
|
std::span<const dynamic_texture_data> dynamic_data,
|
|
std::span<const dynamic_texture_store::id_type> dynamic_data_ids
|
|
) {
|
|
|
|
std::vector<GLuint> texture_ids;
|
|
|
|
texture_ids.resize(dynamic_data.size());
|
|
|
|
glGenTextures(texture_ids.size(), texture_ids.data());
|
|
|
|
const auto invalid_texture_ids = std::ranges::partition(
|
|
texture_ids,
|
|
[](GLuint texture_id)
|
|
{
|
|
GLenum format;
|
|
switch (texture.components()) {
|
|
using enum components::texture::flags;
|
|
case luminance:
|
|
format = GL_LUMINANCE;
|
|
break;
|
|
case luminance | alpha:
|
|
format = GL_LUMINANCE_ALPHA;
|
|
break;
|
|
case red | green | blue:
|
|
format = GL_RGB;
|
|
break;
|
|
case red | green | blue | alpha:
|
|
format = GL_RGBA;
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture_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);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
glTexImage2D(
|
|
GL_TEXTURE_2D, 0,
|
|
GL_RGBA8,
|
|
texture.width(),
|
|
texture.height(),
|
|
0,
|
|
format,
|
|
GL_UNSIGNED_BYTE,
|
|
texture.data()
|
|
);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
|
|
return true;
|
|
}
|
|
);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glDeleteTextures(invalid_texture_ids.size(), invalid_texture_ids.data());
|
|
|
|
invalid_texture_ids.resize(texture_ids.size() - invalid_texture_ids.size());
|
|
}
|
|
};
|
|
}
|