d1d20bdc04
Funcion impura del registry que carga PNG/JPG/BMP/TGA/HDR a una textura OpenGL lista para sampler2D. Composable con gl_loader, gl_shader, shader_canvas. Genera mipmaps, soporta sRGB y HDR (RGBA16F). API: GlTexture gl_texture_load(path, flip_y=true, srgb=false) GlTexture gl_texture_load_from_memory(data, size, ...) void gl_texture_destroy(tex) const char* gl_texture_last_error() // thread-local void gl_texture_bind_uniform(prog, name, tex, unit) Errores via thread_local string accesible por gl_texture_last_error(). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
45 lines
1.7 KiB
C++
45 lines
1.7 KiB
C++
#pragma once
|
|
// gl_texture_load — carga PNG/JPG/BMP/TGA/HDR desde disco o memoria a una
|
|
// textura OpenGL lista para usar como sampler2D en shaders. Vendorea stb_image
|
|
// en cpp/vendor/stb/. Funcion impura: hace I/O y crea recursos GPU.
|
|
|
|
#include "gl_loader.h"
|
|
|
|
namespace fn {
|
|
|
|
struct GlTexture {
|
|
GLuint id = 0;
|
|
int w = 0;
|
|
int h = 0;
|
|
int channels = 0;
|
|
bool ok() const { return id != 0; }
|
|
};
|
|
|
|
// Carga desde disco. flip_y=true (por defecto) coincide con la convencion
|
|
// de OpenGL (V hacia arriba). srgb=true sube como GL_SRGB8_ALPHA8 (gamma
|
|
// correcto sin pow(c, 2.2) en shader). Si el path termina en .hdr se carga
|
|
// como float (GL_RGBA16F).
|
|
//
|
|
// Si falla, devuelve GlTexture con id=0 (ok() == false). Llama a
|
|
// gl_texture_last_error() para obtener el detalle.
|
|
GlTexture gl_texture_load(const char* path, bool flip_y = true, bool srgb = false);
|
|
|
|
// Igual que arriba pero leyendo de un buffer en memoria (PNG/JPG/etc bytes).
|
|
GlTexture gl_texture_load_from_memory(const unsigned char* data, int size,
|
|
bool flip_y = true, bool srgb = false);
|
|
|
|
// Libera la textura GPU y deja id=0.
|
|
void gl_texture_destroy(GlTexture& tex);
|
|
|
|
// Mensaje de error del ultimo gl_texture_load* en este thread. "" si no hubo
|
|
// fallo. Permanece valido hasta el siguiente gl_texture_load* en el thread.
|
|
const char* gl_texture_last_error();
|
|
|
|
// Helper: bind a una texture unit y subir uniform sampler2D al programa.
|
|
// program debe ser un GLuint valido, name el nombre del uniform sampler2D,
|
|
// unit el slot de textura (0..N).
|
|
void gl_texture_bind_uniform(GLuint program, const char* name,
|
|
const GlTexture& tex, int unit);
|
|
|
|
} // namespace fn
|