3D: névoa e transparência nos 3 renderizadores

- Tb3dFrame ganha fog_color/fog_start/fog_end e opaque_count; versão 8.
- Os draws saem com os opacos na frente e os transparentes (color[3] < 1)
  depois, do mais longe ao mais perto.
- Software mistura por alfa e não grava o z; OpenGL e Vulkan usam um
  pipeline de blend separado (o alfa mistura a cor, sem gravar o z).
- Blocos novos: "mude a opacidade de %1 para %2" (3D.Opacity) e
  "névoa de %1 até %2" (3D.Fog).
- d3_test compara névoa e transparência nos três renderizadores.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5 committed 2026-10-30 00:55:03 -03:00
1 parent cea7cb5ba4
commit 7b3cf8ce9b
11 files changed
+829 -647

No files matched your search

+64 -6
View File
@@ -343,6 +343,7 @@ static struct {
int n, cap;
float eye[3], at[3], fov;
float light[3], ambient, sky[4];
float fog_start, fog_end; /* nevoa (fim <= inicio = sem) */
char want[16]; /* "" = automatico */
char used[64], err[160];
double t0; /* "nova cena 3D" (o "tempo" dos shaders) */
@@ -359,6 +360,7 @@ static void scene_reset(void) {
v3_norm(S.light);
S.ambient = 0.35f;
S.sky[0] = 0.53f; S.sky[1] = 0.78f; S.sky[2] = 0.98f; S.sky[3] = 1;
S.fog_start = S.fog_end = 0;
S.t0 = now_s();
}
@@ -435,6 +437,13 @@ static void obj_aabb(const Obj* o, float lo[3], float hi[3]) {
static Tb3dDraw* s_draws;
static int s_draws_cap;
/* Distancia (ao quadrado) do centro do objeto ao olho: ordem dos transparentes */
static float draw_depth(const Tb3dDraw* d, const float eye[3]) {
float c[3];
for (int k = 0; k < 3; k++) c[k] = d->model[12 + k] - eye[k];
return c[0] * c[0] + c[1] * c[1] + c[2] * c[2];
}
static bool frame_build(Tb3dFrame* f, int w, int h) {
meshes_build();
memset(f, 0, sizeof(*f));
@@ -447,6 +456,9 @@ static bool frame_build(Tb3dFrame* f, int w, int h) {
memcpy(f->light_dir, S.light, sizeof(f->light_dir));
f->ambient = S.ambient;
f->time = (float)(now_s() - S.t0);
memcpy(f->fog_color, S.sky, sizeof(f->fog_color)); /* a nevoa some no ceu */
f->fog_start = S.fog_start;
f->fog_end = S.fog_end;
if (S.n > s_draws_cap) {
Tb3dDraw* d = realloc(s_draws, (size_t)S.n * sizeof(*d));
if (!d) return false;
@@ -472,8 +484,23 @@ static bool frame_build(Tb3dFrame* f, int w, int h) {
if (o->material)
for (int k = 0; k < 3; k++) d->color[k] = o->color[k] * o->material->tint[k];
}
/* opacos primeiro; os transparentes no fim, do mais longe ao mais perto */
int no = 0;
for (int i = 0; i < n; i++)
if (s_draws[i].color[3] >= 0.999f) {
Tb3dDraw t = s_draws[i]; s_draws[i] = s_draws[no]; s_draws[no] = t;
no++;
}
for (int i = no + 1; i < n; i++) { /* insercao: poucos transparentes */
Tb3dDraw t = s_draws[i];
float dt = draw_depth(&t, S.eye);
int j = i - 1;
while (j >= no && draw_depth(&s_draws[j], S.eye) < dt) { s_draws[j + 1] = s_draws[j]; j--; }
s_draws[j + 1] = t;
}
f->draws = s_draws;
f->draw_count = n;
f->opaque_count = no;
return true;
}
@@ -492,6 +519,8 @@ static const Tb3dFrame* s_frame;
static float s_base[3];
static float s_nmat[9]; /* matriz das normais (objeto -> mundo) */
static float s_alpha = 1.f; /* opacidade do objeto (< 1: mistura, sem z) */
static float* s_zbuf;
static size_t s_zcap;
@@ -561,7 +590,7 @@ static void raster_tri(const CV* a, const CV* b, const CV* c, int W, int H, uint
if (z < -1.f || z > 1.f) continue;
float* zb = &s_zbuf[(size_t)y * W + x];
if (z >= *zb) continue;
*zb = z;
if (s_alpha >= 0.999f) *zb = z;
uint8_t* p = px + ((size_t)y * W + x) * 4;
float r, g, bl;
float q0 = w0 / a->w, q1 = w1 / b->w, q2 = w2 / c->w, qs = 1.f / (q0 + q1 + q2);
@@ -609,9 +638,23 @@ static void raster_tri(const CV* a, const CV* b, const CV* c, int W, int H, uint
g = (w0 * a->g + w1 * b->g + w2 * c->g) * I * tex[1];
bl = (w0 * a->b + w1 * b->b + w2 * c->b) * I * tex[2];
}
p[0] = (uint8_t)(fminf(r, 1.f) * 255.f + .5f);
p[1] = (uint8_t)(fminf(g, 1.f) * 255.f + .5f);
p[2] = (uint8_t)(fminf(bl, 1.f) * 255.f + .5f);
r = fminf(r, 1.f); g = fminf(g, 1.f); bl = fminf(bl, 1.f);
if (s_frame->fog_end > s_frame->fog_start) { /* nevoa pela profundidade (w do clip) */
float t = (qs - s_frame->fog_start) / (s_frame->fog_end - s_frame->fog_start);
t = t < 0 ? 0 : t > 1 ? 1 : t;
r += (s_frame->fog_color[0] - r) * t;
g += (s_frame->fog_color[1] - g) * t;
bl += (s_frame->fog_color[2] - bl) * t;
}
if (s_alpha < 0.999f) {
float a = s_alpha < 0 ? 0 : s_alpha;
r = r * a + p[0] / 255.f * (1 - a);
g = g * a + p[1] / 255.f * (1 - a);
bl = bl * a + p[2] / 255.f * (1 - a);
}
p[0] = (uint8_t)(r * 255.f + .5f);
p[1] = (uint8_t)(g * 255.f + .5f);
p[2] = (uint8_t)(bl * 255.f + .5f);
p[3] = 255;
}
}
@@ -705,6 +748,7 @@ static bool sw_render(const Tb3dFrame* f, uint8_t* px) {
s_normal = d->normaltex;
memcpy(s_nmat, d->normal, sizeof(s_nmat));
s_frame = f;
s_alpha = d->color[3];
memcpy(s_base, d->color, sizeof(s_base));
for (int i = 0; i < m->vcount; i++) shade(f, d, m->verts + (size_t)i * 8, &cv[i]);
for (int t = 0; t + 2 < m->icount; t += 3) {
@@ -716,6 +760,7 @@ static bool sw_render(const Tb3dFrame* f, uint8_t* px) {
free(cv);
s_prog = s_vprog = NULL;
s_albedo = s_normal = NULL;
s_alpha = 1.f;
return true;
}
@@ -938,8 +983,12 @@ OBJ_CMD(eval_rot_by, vec_in(self, rt, 1, v); for (int k = 0; k < 3; k++) o->r
OBJ_CMD(eval_scale, vec_in(self, rt, 1, v); memcpy(o->scale, v, sizeof(v));)
OBJ_CMD(eval_show, o->visible = true;)
OBJ_CMD(eval_hide, o->visible = false;)
OBJ_CMD(eval_color, if (!d3_color_of(name_in(self, rt, 1), o->color))
tinyblocks_set_error(rt, "cor desconhecida '%s' (use um nome ou #rrggbb)", name_in(self, rt, 1));)
OBJ_CMD(eval_color, float a = o->color[3];
if (!d3_color_of(name_in(self, rt, 1), o->color))
tinyblocks_set_error(rt, "cor desconhecida '%s' (use um nome ou #rrggbb)", name_in(self, rt, 1));
o->color[3] = a;)
OBJ_CMD(eval_opacity, double a = tb_block_input_number(self, rt, 1, 1.0);
o->color[3] = a < 0 ? 0.f : a > 1 ? 1.f : (float)a;)
static void eval_delete(TinyBlocks_Block* self, TinyBlocks* rt) {
self->dirty = false;
@@ -1021,6 +1070,13 @@ static void eval_sky(TinyBlocks_Block* self, TinyBlocks* rt) {
if (!d3_color_of(name_in(self, rt, 0), S.sky)) tinyblocks_set_error(rt, "cor desconhecida '%s'", name_in(self, rt, 0));
}
static void eval_fog(TinyBlocks_Block* self, TinyBlocks* rt) {
self->dirty = false;
double a = tb_block_input_number(self, rt, 0, 20), b = tb_block_input_number(self, rt, 1, 60);
S.fog_start = a < 0 ? 0.f : (float)a;
S.fog_end = (float)b;
}
static void eval_use(TinyBlocks_Block* self, TinyBlocks* rt) {
self->dirty = false;
const char* w = name_in(self, rt, 0);
@@ -1298,6 +1354,7 @@ static const Spec s_specs[] = {
{ "3D.RotateBy", "gire %1 por x %2 y %3 z %4 graus", "jogador|0|5|0", 4, {T,R,R,R}, O_NONE, -1, S_(eval_rot_by), "Soma à rotação do objeto" },
{ "3D.Scale", "mude o tamanho de %1 para x %2 y %3 z %4", "chão|20|1|20", 4, {T,R,R,R}, O_NONE, -1, S_(eval_scale), "Escala do objeto em cada eixo" },
{ "3D.Color", "mude a cor de %1 para %2", "jogador|vermelho", 2, {T,T}, O_NONE, -1, S_(eval_color), "Cor por nome (vermelho, azul, dourado...) ou #rrggbb" },
{ "3D.Opacity", "mude a opacidade de %1 para %2", "água|0.6", 2, {T,R}, O_NONE, -1, S_(eval_opacity), "0 = invisível, 1 = sólido (água, vidro, fantasmas)" },
{ "3D.Show", "mostre o objeto %1", "jogador", 1, {T}, O_NONE, -1, S_(eval_show), "Volta a desenhar o objeto" },
{ "3D.Hide", "esconda o objeto %1", "jogador", 1, {T}, O_NONE, -1, S_(eval_hide), "Deixa de desenhar o objeto (e ele não encosta)" },
{ "3D.Delete", "apague o objeto %1", "jogador", 1, {T}, O_NONE, -1, S_(eval_delete), "Tira o objeto da cena" },
@@ -1312,6 +1369,7 @@ static const Spec s_specs[] = {
{ "3D.Light", "luz vindo de x %1 y %2 z %3", "0.4|1|0.6", 3, {R,R,R}, O_NONE, -1, S_(eval_light), "Direção de onde vem a luz do sol" },
{ "3D.Ambient", "luz ambiente %1", "0.35", 1, {R}, O_NONE, -1, S_(eval_ambient), "Claridade das partes na sombra (0 a 1)" },
{ "3D.Sky", "cor do céu %1", "#87c7fa", 1, {T}, O_NONE, -1, S_(eval_sky), "Cor do fundo" },
{ "3D.Fog", "névoa de %1 até %2", "20|60", 2, {R,R}, O_NONE, -1, S_(eval_fog), "Longe da câmera tudo some na cor do céu: começa na 1a distância e cobre tudo na 2a (0 e 0 = sem névoa)" },
{ "3D.Image", "imagem da cena de %1 por %2", "480|360", 2, {I,I}, O_IMAGE, -1, S_(eval_image), "Desenha a cena numa imagem (use com \"desenhe\")" },
{ "3D.Use", "use o renderizador %1", "automático", 1, {T}, O_NONE, -1, S_(eval_use), "vulkan, opengl, software ou automático" },
{ "3D.Renderer", "renderizador da cena", "", 0, {T}, O_TEXT, -1, S_(eval_renderer), "Quem desenhou o último quadro (e em qual placa)" },