3D: texturas e materiais (albedo) nos 3 renderizadores

Fase 2 da API de 3D: agora dá para criar uma textura a partir de uma
imagem, juntar num material (com cor) e usar no objeto. O albedo é
amostrado no uv (bilinear, uv repetido) e a amostra chega ao shader com
o novo bloco "textura do pixel".

- 3D: "crie a textura %1 da imagem %2", "crie o material %1",
  "no material %1 use a textura %2 como albedo" (e "como normal", já
  guardado), "no material %1 use a cor %2" e "use o material %1 no
  objeto %2". A textura converte a imagem para RGBA8 e mantém um cache
  por nome; o material tem albedo + normal map + tint (a cor multiplica).
- Tb3dDraw ganhou albedo/normaltex; Tb3dTexture (id + version + RGBA).
- Shader: a assinatura tb_shade/tb_vert passou a ter vec3 tex, e o
  bloco Shader.Texture ("textura do pixel") expõe o albedo à pilha.
- Software, OpenGL e Vulkan amostram o mesmo albedo (branco quando não
  há textura); no Vulkan entram descriptor set para o sampler e um
  staging buffer por textura. TB_3D_RENDERER_VERSION 5 -> 6.
- test-3d: novo caso de textura (imagem 2x2 vermelho/azul no quad);
  os três renderizadores batem (diferença média 0.016).
- O editor passa a carregar PPM/BMP/PNG/Imagem por padrão (a 3D usa
  imagens) e o link ganhou -lpng.
- Exemplo examples/jogo-3d/textura.sb (cubo com tijolo.png) e docs.

O normal map ainda não muda a iluminação: fica para a fase do relevo.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5 committed 2026-10-29 01:07:18 -03:00
1 parent d5c16a5237
commit 0c4c107244
20 files changed
+664 -127

No files matched your search

+51 -6
View File
@@ -45,8 +45,14 @@ static size_t s_rows_cap;
typedef struct { GLuint vao, vbo, ebo; int icount; unsigned version; } GpuMesh;
static GpuMesh s_mesh[256]; /* pelo id da malha (1..255) */
/* Texturas (cache pelo id) e a textura branca de 1x1 que substitui quem
nao tem material com albedo */
typedef struct { GLuint tex; int id; unsigned version; } GpuTex;
static GpuTex s_tex[128];
static GLuint s_white;
/* Programas dos shaders feitos com blocos: cache pelo id (id % 64) */
typedef struct { GLuint prog; int id; GLint mvp, model, nrm, color, light, ambient, time; } GpuShader;
typedef struct { GLuint prog; int id; GLint mvp, model, nrm, color, light, ambient, time, albedo; } GpuShader;
static GpuShader s_shd[64];
static void fail(const char* fmt, const char* a) { snprintf(G.err, sizeof(G.err), fmt, a); }
@@ -62,17 +68,21 @@ static const char* VS =
"uniform vec3 u_light;\n"
"uniform float u_ambient;\n"
"out vec3 v_col;\n"
"out vec2 v_uv;\n"
"void main() {\n"
" gl_Position = u_mvp * vec4(a_pos, 1.0);\n"
" vec3 n = normalize(u_nrm * a_nrm);\n"
" float I = u_ambient + (1.0 - u_ambient) * max(dot(n, u_light), 0.0);\n"
" v_col = u_color.rgb * I;\n"
" v_uv = a_uv;\n"
"}\n";
static const char* FS =
"#version 330 core\n"
"in vec3 v_col;\n"
"in vec2 v_uv;\n"
"uniform sampler2D u_albedo;\n"
"out vec4 o_color;\n"
"void main() { o_color = vec4(min(v_col, vec3(1.0)), 1.0); }\n";
"void main() { o_color = vec4(min(v_col * texture(u_albedo, v_uv).rgb, vec3(1.0)), 1.0); }\n";
static const char* VS_VERT_HEAD =
"#version 330 core\n"
@@ -94,7 +104,7 @@ static const char* VS_VERT_MAIN =
"void main() {\n"
" vec3 n = normalize(u_nrm * a_nrm);\n"
" float light = u_ambient + (1.0 - u_ambient) * max(dot(n, u_light), 0.0);\n"
" vec3 P = tb_vert(a_pos, a_nrm, a_pos, a_uv, u_color.rgb, light, u_time);\n"
" vec3 P = tb_vert(a_pos, a_nrm, a_pos, a_uv, u_color.rgb, vec3(1.0), light, u_time);\n"
" gl_Position = u_mvp * vec4(P, 1.0);\n"
" v_pos = (u_model * vec4(P, 1.0)).xyz;\n"
" v_nrm = u_nrm * a_nrm;\n"
@@ -102,9 +112,9 @@ static const char* VS_VERT_MAIN =
" v_uv = a_uv;\n"
"}\n";
static const char* DEFAULT_VERT =
"vec3 tb_vert(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, float light, float time) { return P; }\n";
"vec3 tb_vert(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, vec3 tex, float light, float time) { return P; }\n";
static const char* DEFAULT_FRAG =
"vec3 tb_shade(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, float light, float time) { return base * light; }\n";
"vec3 tb_shade(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, vec3 tex, float light, float time) { return base * light; }\n";
static const char* FS_HEAD =
"#version 330 core\n"
"in vec3 v_pos;\n"
@@ -115,12 +125,14 @@ static const char* FS_HEAD =
"uniform vec3 u_light;\n"
"uniform float u_ambient;\n"
"uniform float u_time;\n"
"uniform sampler2D u_albedo;\n"
"out vec4 o_color;\n";
static const char* FS_MAIN =
"void main() {\n"
" vec3 N = normalize(v_nrm);\n"
" float light = u_ambient + (1.0 - u_ambient) * max(dot(N, u_light), 0.0);\n"
" o_color = vec4(tb_shade(v_pos, N, v_local, v_uv, u_color.rgb, light, u_time), 1.0);\n"
" vec3 tex = texture(u_albedo, v_uv).rgb;\n"
" o_color = vec4(tb_shade(v_pos, N, v_local, v_uv, u_color.rgb, tex, light, u_time), 1.0);\n"
"}\n";
static GLuint shader(GLenum kind, const char* src) {
@@ -228,6 +240,7 @@ static GpuShader* gpu_shader(const Tb3dShader* sh) {
g->light = glGetUniformLocation(g->prog, "u_light");
g->ambient = glGetUniformLocation(g->prog, "u_ambient");
g->time = glGetUniformLocation(g->prog, "u_time");
g->albedo = glGetUniformLocation(g->prog, "u_albedo");
return g;
}
@@ -268,6 +281,12 @@ static bool gl_setup(void) {
u_color = glGetUniformLocation(s_prog, "u_color");
u_light = glGetUniformLocation(s_prog, "u_light");
u_ambient = glGetUniformLocation(s_prog, "u_ambient");
glGenTextures(1, &s_white);
glBindTexture(GL_TEXTURE_2D, s_white);
static const uint8_t branco[4] = { 255, 255, 255, 255 };
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, branco);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenFramebuffers(1, &s_fbo);
glGenRenderbuffers(1, &s_color);
glGenRenderbuffers(1, &s_depth);
@@ -315,6 +334,24 @@ static GpuMesh* gpu_mesh(const Tb3dMesh* m) {
return g;
}
/* A textura na GPU (cache pelo id): sobe RGBA8 com filtro linear e uv repetido */
static GLuint gpu_tex(const Tb3dTexture* t) {
if (!t || !t->rgba || t->id <= 0 || t->id >= 128) return s_white;
GpuTex* g = &s_tex[t->id];
if (g->tex && g->version == t->version) return g->tex;
if (!g->tex) glGenTextures(1, &g->tex);
glBindTexture(GL_TEXTURE_2D, g->tex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, t->width, t->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, t->rgba);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
g->id = t->id;
g->version = t->version;
return g->tex;
}
static bool draw(const Tb3dFrame* f, uint8_t* out) {
if (!target(f->width, f->height)) return false;
glBindFramebuffer(GL_FRAMEBUFFER, s_fbo);
@@ -328,10 +365,13 @@ static bool draw(const Tb3dFrame* f, uint8_t* out) {
glUseProgram(s_prog);
glUniform3fv(u_light, 1, f->light_dir);
glUniform1f(u_ambient, f->ambient);
glActiveTexture(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(s_prog, "u_albedo"), 0);
for (int i = 0; i < f->draw_count; i++) {
const Tb3dDraw* d = &f->draws[i];
GpuMesh* g = gpu_mesh(d->mesh);
if (!g) continue;
glBindTexture(GL_TEXTURE_2D, gpu_tex(d->albedo));
if (d->shader) { /* shader feito com blocos */
GpuShader* sh = gpu_shader(d->shader);
if (!sh) return false; /* a 3d desenha esse quadro em software */
@@ -343,6 +383,7 @@ static bool draw(const Tb3dFrame* f, uint8_t* out) {
glUniform3fv(sh->light, 1, f->light_dir);
glUniform1f(sh->ambient, f->ambient);
glUniform1f(sh->time, f->time);
glUniform1i(sh->albedo, 0);
glBindVertexArray(g->vao);
glDrawElements(GL_TRIANGLES, g->icount, GL_UNSIGNED_INT, (void*)0);
glUseProgram(s_prog);
@@ -377,6 +418,10 @@ static void cleanup(void) {
glDeleteBuffers(1, &s_mesh[i].ebo);
}
memset(s_mesh, 0, sizeof(s_mesh));
for (int i = 0; i < 128; i++) if (s_tex[i].tex) glDeleteTextures(1, &s_tex[i].tex);
memset(s_tex, 0, sizeof(s_tex));
if (s_white) glDeleteTextures(1, &s_white);
s_white = 0;
for (int i = 0; i < 64; i++) if (s_shd[i].prog) glDeleteProgram(s_shd[i].prog);
memset(s_shd, 0, sizeof(s_shd));
if (s_prog) glDeleteProgram(s_prog);