3D: malha personalizada e UV (base para ler .obj)

- malha personalizada pelos blocos: "crie a malha", "na malha adicione o
  vértice / a normal / a texcoord", "adicione o triângulo" e "finalize a
  malha"; as normais que faltam saem das faces e a caixa (AABB) é
  calculada ao finalizar
- uv no vértice (8 floats: x y z nx ny nz u v) no software, no OpenGL e
  no Vulkan; Tb3dMesh ganha "version" para recarregar a malha quando muda
- shader: "x/y/z do vértice" (espaço do objeto) e "u/v do uv"; tb_shade
  vira (P, N, L, UV, base, light, time)
- GL/VK: tabela de malhas 32 -> 256, atributo 2 = uv; SPIR-V regerado
- teste: quad personalizado pintado pelo uv, igual nos 3 renderizadores
- docs/blocos.md regerado (436 tipos)

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5 committed 2026-10-29 00:47:08 -03:00
1 parent 545aa86a5a
commit 67ba86601c
12 files changed
+408 -109

No files matched your search

+52
View File
@@ -209,6 +209,58 @@ int main(void) {
tb_image_release(img);
}
}
/* -- malha personalizada + uv: um quadrado pintado pelo uv -- */
{
RUN("3D.Scene", NULL);
RUN("3D.MeshCreate", "quad");
const char* vx[4][3] = { {"-1","-1","0"}, {"1","-1","0"}, {"1","1","0"}, {"-1","1","0"} };
const char* tx[4][2] = { {"0","0"}, {"1","0"}, {"1","1"}, {"0","1"} };
for (int i = 0; i < 4; i++) {
RUN("3D.MeshVertex", "quad", vx[i][0], vx[i][1], vx[i][2]);
RUN("3D.MeshUV", "quad", tx[i][0], tx[i][1]);
}
RUN("3D.MeshTri", "quad", "1", "2", "3");
RUN("3D.MeshTri", "quad", "1", "3", "4");
RUN("3D.MeshFinish", "quad");
CHECK(!tinyblocks_last_error(rt)[0], "malha personalizada: monta sem erro (normais calculadas)");
CHECK(RUN("3D.Count", NULL)->outputs[0].as.integer == 1, "a malha entra na cena como objeto");
CHECK(RUN("3D.X", "quad")->outputs[0].as.real == 0, "mova/gire valem para a malha também");
RUN("3D.Camera", "0", "0", "4", "0", "0", "0");
const char* uvsh = "[" BLK("Shader.Pixel", BLK("Shader.Rgb", BLK0("Shader.U") "," BLK0("Shader.V") "," "0")) "]";
TinyBlocks_Block* su = compile_shader(uvsh);
CHECK(su != NULL, "shader com u do uv / v do uv compila");
if (su) {
TinyBlocks_Block* g = run_with_shader("3D.ShaderGlsl", su, NULL);
CHECK(g->outputs[0].kind == TB_VALUE_TEXT && strstr(g->outputs[0].as.text, "UV.x") &&
strstr(g->outputs[0].as.text, "UV.y"), "o GLSL gerado usa o uv");
run_with_shader("3D.ShaderUse", su, "quad");
}
TinyBlocks_Image* ui = render_with("software");
CHECK(ui != NULL, "a malha personalizada desenha");
if (ui) {
uint8_t l[4], r[4];
tb_image_get_pixel(ui, 125, 120, &l[0], &l[1], &l[2], &l[3]);
tb_image_get_pixel(ui, 195, 120, &r[0], &r[1], &r[2], &r[3]);
printf(" quad por uv: esquerda r=%d direita r=%d\n", l[0], r[0]);
CHECK(r[0] > 150 && l[0] < 100, "o uv pinta da esquerda (u=0) para a direita (u=1)");
for (int i = 0; i < 2; i++) {
const char* lib = i ? "tinyblocks-vk" : "tinyblocks-gl";
if (!tb_rt_find_library(rt, lib)) continue;
TinyBlocks_Image* gi = render_with(gpu[i]);
TinyBlocks_Block* gr = RUN("3D.Renderer", NULL);
const char* used = gr->outputs[0].kind == TB_VALUE_TEXT ? gr->outputs[0].as.text : "";
if (strncmp(used, gpu[i], strlen(gpu[i])) != 0) { tb_image_release(gi); continue; }
double mean = 0, bad = 1;
if (gi) compare(ui, gi, &mean, &bad);
printf(" malha %s x software: diferença média %.3f, pixels muito diferentes %.2f%%\n", gpu[i], mean, bad * 100);
char m[96];
snprintf(m, sizeof(m), "a malha personalizada com uv desenha igual no %s", gpu[i]);
CHECK(gi && mean < 4.0 && bad < 0.02, m);
tb_image_release(gi);
}
}
tb_image_release(ui);
}
tb_image_release(sw);
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);