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:
1 parent
d5c16a5237
commit
0c4c107244
20 files changed
+664
-127
No files matched your search
@@ -300,6 +300,75 @@ int main(void) {
|
||||
tb_image_release(vi);
|
||||
}
|
||||
}
|
||||
|
||||
/* -- textura + material: um quad pintado por uma imagem 2x2 -- */
|
||||
{
|
||||
TinyBlocks_Image* tex = tb_image_new(2, 2, 4);
|
||||
for (int y = 0; y < 2; y++) {
|
||||
tb_image_set_pixel(tex, 0, y, 255, 0, 0, 255); /* esquerda: vermelho */
|
||||
tb_image_set_pixel(tex, 1, y, 0, 0, 255, 255); /* direita: azul */
|
||||
}
|
||||
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");
|
||||
RUN("3D.Camera", "0", "0", "4", "0", "0", "0");
|
||||
|
||||
TinyBlocks_Block* tb = tb_block_create(rt, tb_type_block_find(rt, "3D.Texture"));
|
||||
TinyBlocks_Value nm = tb_value_text("cores");
|
||||
tb_block_set_default(tb, 0, nm); tb_value_free(&nm);
|
||||
TinyBlocks_Value iv = tb_value_image(tex);
|
||||
tb_block_set_default(tb, 1, iv); tb_value_free(&iv);
|
||||
tb_block_eval(tb, rt);
|
||||
CHECK(!tinyblocks_last_error(rt)[0], "3D.Texture: cria a textura a partir de uma imagem");
|
||||
RUN("3D.Material", "mat");
|
||||
RUN("3D.MaterialAlbedo", "mat", "cores");
|
||||
RUN("3D.MaterialUse", "mat", "quad");
|
||||
CHECK(!tinyblocks_last_error(rt)[0], "3D.Material + albedo + use: sem erro");
|
||||
const char* tsh = "[" BLK("Shader.Pixel", BLK0("Shader.Texture")) "]";
|
||||
TinyBlocks_Block* st = compile_shader(tsh);
|
||||
CHECK(st != NULL, "shader com \"textura do pixel\" compila");
|
||||
if (st) {
|
||||
TinyBlocks_Block* g = run_with_shader("3D.ShaderGlsl", st, NULL);
|
||||
CHECK(g->outputs[0].kind == TB_VALUE_TEXT && strstr(g->outputs[0].as.text, "tex"),
|
||||
"o GLSL gerado usa a textura do pixel");
|
||||
}
|
||||
tb_image_release(tex);
|
||||
|
||||
TinyBlocks_Image* ti = render_with("software");
|
||||
CHECK(ti != NULL, "o objeto com textura desenha");
|
||||
if (ti) {
|
||||
uint8_t l[4], r[4];
|
||||
tb_image_get_pixel(ti, 125, 120, &l[0], &l[1], &l[2], &l[3]);
|
||||
tb_image_get_pixel(ti, 195, 120, &r[0], &r[1], &r[2], &r[3]);
|
||||
printf(" textura: esquerda rgb=(%d,%d,%d) direita rgb=(%d,%d,%d)\n", l[0], l[1], l[2], r[0], r[1], r[2]);
|
||||
CHECK(l[0] > 80 && l[0] > 2 * l[2] && r[2] > 80 && r[2] > 2 * r[0],
|
||||
"o albedo pinta a esquerda de vermelho e a direita de azul");
|
||||
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(ti, gi, &mean, &bad);
|
||||
printf(" textura %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 textura desenha igual no %s", gpu[i]);
|
||||
CHECK(gi && mean < 4.0 && bad < 0.02, m);
|
||||
tb_image_release(gi);
|
||||
}
|
||||
}
|
||||
tb_image_release(ti);
|
||||
}
|
||||
tb_image_release(sw);
|
||||
tinyblocks_deinit(&rt);
|
||||
if (fails) printf("\n%d FALHA(S)\n", fails);
|
||||
|
||||
Reference in new issue
Block a user