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
@@ -30,6 +30,7 @@ typedef struct {
|
||||
float P[3], N[3], L[3]; /* ponto/vertice, normal, vertice local */
|
||||
float uv[2]; /* coordenada de textura */
|
||||
float base[3];
|
||||
float tex[3]; /* textura de albedo amostrada no uv */
|
||||
float light, time;
|
||||
} D3Pixel;
|
||||
void d3_shader_run(const D3Program* p, const D3Pixel* in, float out[3]);
|
||||
|
||||
@@ -29,7 +29,7 @@ enum { TF, TV, TB }; /* numero, cor, condicao */
|
||||
|
||||
enum {
|
||||
/* expressoes */
|
||||
E_CONST, E_VAR, E_PIX, E_BASE, E_LIGHT, E_TIME, E_P, E_N, E_L, E_UV, /* canal em k[0] */
|
||||
E_CONST, E_VAR, E_PIX, E_BASE, E_LIGHT, E_TIME, E_P, E_N, E_L, E_UV, E_TEX, /* canal em k[0] */
|
||||
E_RGB, E_ADD, E_SUB, E_MUL, E_DIV, E_MOD, E_MIN, E_MAX, E_POW, E_MIX, E_CLAMP,
|
||||
E_ABS, E_SQRT, E_FLOOR, E_CEIL, E_ROUND, E_SIN, E_COS, E_TAN, E_EXP, E_LN,
|
||||
E_CHAN, E_GRAY, /* E_CHAN: canal em k[0] */
|
||||
@@ -190,6 +190,7 @@ static int expr(C* c, const TinyBlocks_JsonValue* j) {
|
||||
if (!strcmp(t, "Shader.ObjectColor")) return node(c, E_BASE, TV);
|
||||
if (!strcmp(t, "Shader.Light")) return node(c, E_LIGHT, TF);
|
||||
if (!strcmp(t, "Shader.Time")) return node(c, E_TIME, TF);
|
||||
if (!strcmp(t, "Shader.Texture")) return node(c, E_TEX, TV);
|
||||
const char* axes[] = { "Shader.X", "Shader.Y", "Shader.Z", "Shader.NX", "Shader.NY", "Shader.NZ",
|
||||
"Shader.VX", "Shader.VY", "Shader.VZ", "Shader.U", "Shader.V" };
|
||||
for (int k = 0; k < 11; k++)
|
||||
@@ -424,6 +425,7 @@ static void gx(const D3Program* p, Str* o, int i) {
|
||||
case E_N: put(o, "N.%c", ch[(int)x->k[0]]); break;
|
||||
case E_L: put(o, "L.%c", ch[(int)x->k[0]]); break;
|
||||
case E_UV: put(o, "UV.%c", ch[(int)x->k[0]]); break;
|
||||
case E_TEX: put(o, "tex"); break;
|
||||
case E_RGB: put(o, "vec3("); gx(p, o, x->a); put(o, ", "); gx(p, o, x->b); put(o, ", "); gx(p, o, x->c); put(o, ")"); break;
|
||||
case E_ADD: case E_SUB: case E_MUL: case E_DIV:
|
||||
put(o, "("); gx(p, o, x->a);
|
||||
@@ -479,10 +481,10 @@ static void gs(const D3Program* p, Str* o, int i, int depth) {
|
||||
static char* emit_glsl(const D3Program* p) {
|
||||
Str o = {0};
|
||||
if (p->stage == D3_STAGE_VERTEX) {
|
||||
put(&o, "vec3 tb_vert(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, float light, float time) {\n");
|
||||
put(&o, "vec3 tb_vert(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, vec3 tex, float light, float time) {\n");
|
||||
put(&o, " vec3 vpos = P;\n");
|
||||
} else {
|
||||
put(&o, "vec3 tb_shade(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, float light, float time) {\n");
|
||||
put(&o, "vec3 tb_shade(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, vec3 tex, float light, float time) {\n");
|
||||
put(&o, " vec3 pix = base * light;\n");
|
||||
}
|
||||
for (int i = 0; i < p->nl; i++)
|
||||
@@ -540,6 +542,7 @@ static void ev(const D3Program* p, Run* r, int i, float o[3]) {
|
||||
case E_N: o[0] = o[1] = o[2] = r->in->N[(int)x->k[0]]; return;
|
||||
case E_L: o[0] = o[1] = o[2] = r->in->L[(int)x->k[0]]; return;
|
||||
case E_UV: o[0] = o[1] = o[2] = r->in->uv[(int)x->k[0]]; return;
|
||||
case E_TEX: memcpy(o, r->in->tex, sizeof(r->in->tex)); return;
|
||||
case E_RGB: ev(p, r, x->a, a); ev(p, r, x->b, b); ev(p, r, x->c, c); o[0] = a[0]; o[1] = b[0]; o[2] = c[0]; return;
|
||||
case E_CHAN: ev(p, r, x->a, a); o[0] = o[1] = o[2] = a[(int)x->k[0]]; return;
|
||||
case E_GRAY: ev(p, r, x->a, a); o[0] = o[1] = o[2] = a[0] * 0.299f + a[1] * 0.587f + a[2] * 0.114f; return;
|
||||
|
||||
@@ -201,12 +201,15 @@ typedef struct {
|
||||
int* f; int fn, fcap; /* 3 indices por triangulo (1-based) */
|
||||
} MeshBuild;
|
||||
|
||||
typedef struct Mat Mat; /* material (texturas + cor) */
|
||||
|
||||
typedef struct {
|
||||
char name[48];
|
||||
int kind;
|
||||
float pos[3], rot[3], scale[3], color[4];
|
||||
bool visible;
|
||||
struct Shd* shader; /* NULL = luz padrao */
|
||||
Mat* material; /* NULL = sem material (cor pura) */
|
||||
MeshBuild mb; /* malha personalizada (K_CUSTOM) */
|
||||
Tb3dMesh mesh; /* a malha finalizada */
|
||||
float bmin[3], bmax[3]; /* caixa local ("encosta em") */
|
||||
@@ -239,6 +242,95 @@ static void shd_release(Shd* s) {
|
||||
free(s);
|
||||
}
|
||||
|
||||
/* Textura ("crie a textura [nome] da imagem [img]"): guarda a imagem e uma
|
||||
copia RGBA8 (linha 0 no alto) que os renderizadores sobem para a GPU. */
|
||||
typedef struct Tex {
|
||||
Tb3dTexture pub; /* primeiro: o renderizador ve so isto */
|
||||
char name[48];
|
||||
TinyBlocks_Image* img;
|
||||
uint8_t* rgba;
|
||||
int refs;
|
||||
} Tex;
|
||||
struct Mat {
|
||||
char name[48];
|
||||
Tex* albedo;
|
||||
Tex* normal;
|
||||
float tint[3]; /* multiplica a cor do objeto */
|
||||
};
|
||||
|
||||
static struct { Tex** t; int n, cap; } s_tex;
|
||||
static struct { Mat** m; int n, cap; } s_mat;
|
||||
static int s_tex_seq; /* ids unicos por textura */
|
||||
|
||||
static void tex_release(Tex* t) {
|
||||
if (!t || --t->refs > 0) return;
|
||||
if (t->img) tb_image_release(t->img);
|
||||
free(t->rgba);
|
||||
free(t);
|
||||
}
|
||||
|
||||
static void textures_clear(void) {
|
||||
for (int i = 0; i < s_tex.n; i++) tex_release(s_tex.t[i]);
|
||||
s_tex.n = 0;
|
||||
for (int i = 0; i < s_mat.n; i++) {
|
||||
tex_release(s_mat.m[i]->albedo);
|
||||
tex_release(s_mat.m[i]->normal);
|
||||
free(s_mat.m[i]);
|
||||
}
|
||||
s_mat.n = 0;
|
||||
}
|
||||
|
||||
static Mat* mat_get(const char* name, bool create) {
|
||||
for (int i = 0; i < s_mat.n; i++) if (!strcmp(s_mat.m[i]->name, name)) return s_mat.m[i];
|
||||
if (!create) return NULL;
|
||||
if (s_mat.n == s_mat.cap) {
|
||||
int nc = s_mat.cap ? s_mat.cap * 2 : 8;
|
||||
Mat** q = realloc(s_mat.m, (size_t)nc * sizeof(*q));
|
||||
if (!q) return NULL;
|
||||
s_mat.m = q; s_mat.cap = nc;
|
||||
}
|
||||
Mat* m = calloc(1, sizeof(*m));
|
||||
if (!m) return NULL;
|
||||
snprintf(m->name, sizeof(m->name), "%s", name);
|
||||
m->tint[0] = m->tint[1] = m->tint[2] = 1.f;
|
||||
s_mat.m[s_mat.n++] = m;
|
||||
return m;
|
||||
}
|
||||
|
||||
static Tex* tex_get(const char* name) {
|
||||
for (int i = 0; i < s_tex.n; i++) if (!strcmp(s_tex.t[i]->name, name)) return s_tex.t[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Converte a imagem para RGBA8 (linha 0 no alto), como os renderizadores querem */
|
||||
static void tex_from_image(Tex* t, TinyBlocks_Image* img) {
|
||||
if (t->img) tb_image_release(t->img);
|
||||
t->img = NULL;
|
||||
free(t->rgba);
|
||||
t->rgba = NULL;
|
||||
t->pub.rgba = NULL;
|
||||
t->pub.width = t->pub.height = 0;
|
||||
if (!img) return;
|
||||
int w = tb_image_width(img), h = tb_image_height(img);
|
||||
if (w <= 0 || h <= 0) return;
|
||||
uint8_t* p = malloc((size_t)w * h * 4);
|
||||
if (!p) return;
|
||||
for (int y = 0; y < h; y++)
|
||||
for (int x = 0; x < w; x++) {
|
||||
uint8_t r, g, b, a;
|
||||
tb_image_get_pixel(img, x, y, &r, &g, &b, &a);
|
||||
uint8_t* q = p + ((size_t)y * w + x) * 4;
|
||||
q[0] = r; q[1] = g; q[2] = b; q[3] = a;
|
||||
}
|
||||
t->img = img;
|
||||
tb_image_retain(img);
|
||||
t->rgba = p;
|
||||
t->pub.rgba = p;
|
||||
t->pub.width = w;
|
||||
t->pub.height = h;
|
||||
t->pub.version++;
|
||||
}
|
||||
|
||||
static double now_s(void) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
@@ -259,6 +351,7 @@ static struct {
|
||||
static void scene_reset(void) {
|
||||
for (int i = 0; i < S.n; i++) { shd_release(S.o[i].shader); mesh_clear(&S.o[i]); }
|
||||
S.n = 0;
|
||||
textures_clear();
|
||||
S.eye[0] = 0; S.eye[1] = 6; S.eye[2] = 10;
|
||||
S.at[0] = S.at[1] = S.at[2] = 0;
|
||||
S.fov = 60;
|
||||
@@ -374,6 +467,10 @@ static bool frame_build(Tb3dFrame* f, int w, int h) {
|
||||
tb_3d_mat_mul(d->mvp, f->viewproj, d->model);
|
||||
memcpy(d->color, o->color, sizeof(d->color));
|
||||
d->shader = o->shader ? &o->shader->pub : NULL;
|
||||
d->albedo = (o->material && o->material->albedo) ? &o->material->albedo->pub : NULL;
|
||||
d->normaltex = (o->material && o->material->normal) ? &o->material->normal->pub : NULL;
|
||||
if (o->material)
|
||||
for (int k = 0; k < 3; k++) d->color[k] = o->color[k] * o->material->tint[k];
|
||||
}
|
||||
f->draws = s_draws;
|
||||
f->draw_count = n;
|
||||
@@ -389,12 +486,34 @@ typedef struct { float x, y, z, w, r, g, b, P[3], N[3], L[3], uv[2]; } CV;
|
||||
/* Quem o rasterizador esta desenhando agora (shader do objeto) */
|
||||
static const D3Program* s_prog;
|
||||
static const D3Program* s_vprog; /* shader de vertice (ou NULL) */
|
||||
static const Tb3dTexture* s_albedo; /* textura do objeto (ou NULL) */
|
||||
static const Tb3dFrame* s_frame;
|
||||
static float s_base[3];
|
||||
|
||||
static float* s_zbuf;
|
||||
static size_t s_zcap;
|
||||
|
||||
/* Amostra a textura (bilinear, uv repetido); sem textura, devolve branco */
|
||||
static void sample_tex(const Tb3dTexture* t, float u, float v, float out[3]) {
|
||||
out[0] = out[1] = out[2] = 1.f;
|
||||
if (!t || !t->rgba || t->width <= 0 || t->height <= 0) return;
|
||||
int w = t->width, h = t->height;
|
||||
float fu = u - floorf(u), fv = v - floorf(v);
|
||||
float fx = fu * w - 0.5f, fy = fv * h - 0.5f;
|
||||
int x0 = (int)floorf(fx), y0 = (int)floorf(fy);
|
||||
float tx = fx - x0, ty = fy - y0;
|
||||
int x1 = x0 + 1, y1 = y0 + 1;
|
||||
x0 = ((x0 % w) + w) % w; x1 = ((x1 % w) + w) % w;
|
||||
y0 = ((y0 % h) + h) % h; y1 = ((y1 % h) + h) % h;
|
||||
for (int c = 0; c < 3; c++) {
|
||||
float a0 = t->rgba[((size_t)y0 * w + x0) * 4 + c] / 255.f;
|
||||
float b0 = t->rgba[((size_t)y0 * w + x1) * 4 + c] / 255.f;
|
||||
float a1 = t->rgba[((size_t)y1 * w + x0) * 4 + c] / 255.f;
|
||||
float b1 = t->rgba[((size_t)y1 * w + x1) * 4 + c] / 255.f;
|
||||
out[c] = (a0 * (1 - tx) + b0 * tx) * (1 - ty) + (a1 * (1 - tx) + b1 * tx) * ty;
|
||||
}
|
||||
}
|
||||
|
||||
static void raster_tri(const CV* a, const CV* b, const CV* c, int W, int H, uint8_t* px) {
|
||||
float ax = (a->x / a->w * 0.5f + 0.5f) * W, ay = (0.5f - a->y / a->w * 0.5f) * H, az = a->z / a->w;
|
||||
float bx = (b->x / b->w * 0.5f + 0.5f) * W, by = (0.5f - b->y / b->w * 0.5f) * H, bz = b->z / b->w;
|
||||
@@ -423,17 +542,22 @@ static void raster_tri(const CV* a, const CV* b, const CV* c, int W, int H, uint
|
||||
*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);
|
||||
float iu = (q0 * a->uv[0] + q1 * b->uv[0] + q2 * c->uv[0]) * qs;
|
||||
float iv = (q0 * a->uv[1] + q1 * b->uv[1] + q2 * c->uv[1]) * qs;
|
||||
float tex[3];
|
||||
sample_tex(s_albedo, iu, iv, tex);
|
||||
if (s_prog) { /* shader: ponto e normal com perspectiva */
|
||||
float q0 = w0 / a->w, q1 = w1 / b->w, q2 = w2 / c->w, qs = 1.f / (q0 + q1 + q2);
|
||||
D3Pixel in;
|
||||
for (int k = 0; k < 3; k++) {
|
||||
in.P[k] = (q0 * a->P[k] + q1 * b->P[k] + q2 * c->P[k]) * qs;
|
||||
in.N[k] = (q0 * a->N[k] + q1 * b->N[k] + q2 * c->N[k]) * qs;
|
||||
in.L[k] = (q0 * a->L[k] + q1 * b->L[k] + q2 * c->L[k]) * qs;
|
||||
in.base[k] = s_base[k];
|
||||
in.tex[k] = tex[k];
|
||||
}
|
||||
in.uv[0] = (q0 * a->uv[0] + q1 * b->uv[0] + q2 * c->uv[0]) * qs;
|
||||
in.uv[1] = (q0 * a->uv[1] + q1 * b->uv[1] + q2 * c->uv[1]) * qs;
|
||||
in.uv[0] = iu;
|
||||
in.uv[1] = iv;
|
||||
v3_norm(in.N);
|
||||
float lam = v3_dot(in.N, s_frame->light_dir);
|
||||
in.light = s_frame->ambient + (1.f - s_frame->ambient) * (lam > 0 ? lam : 0);
|
||||
@@ -442,9 +566,9 @@ static void raster_tri(const CV* a, const CV* b, const CV* c, int W, int H, uint
|
||||
d3_shader_run(s_prog, &in, o);
|
||||
r = o[0]; g = o[1]; bl = o[2];
|
||||
} else {
|
||||
r = w0 * a->r + w1 * b->r + w2 * c->r;
|
||||
g = w0 * a->g + w1 * b->g + w2 * c->g;
|
||||
bl = w0 * a->b + w1 * b->b + w2 * c->b;
|
||||
r = (w0 * a->r + w1 * b->r + w2 * c->r) * tex[0];
|
||||
g = (w0 * a->g + w1 * b->g + w2 * c->g) * tex[1];
|
||||
bl = (w0 * a->b + w1 * b->b + w2 * c->b) * tex[2];
|
||||
}
|
||||
p[0] = (uint8_t)(fminf(r, 1.f) * 255.f + .5f);
|
||||
p[1] = (uint8_t)(fminf(g, 1.f) * 255.f + .5f);
|
||||
@@ -485,6 +609,7 @@ static void shade(const Tb3dFrame* f, const Tb3dDraw* d, const float* v, CV* o)
|
||||
in.L[0] = v[0]; in.L[1] = v[1]; in.L[2] = v[2];
|
||||
in.uv[0] = v[6]; in.uv[1] = v[7];
|
||||
in.base[0] = d->color[0]; in.base[1] = d->color[1]; in.base[2] = d->color[2];
|
||||
in.tex[0] = in.tex[1] = in.tex[2] = 1.f;
|
||||
in.light = 1.f; in.time = f->time;
|
||||
d3_shader_run_vertex(s_vprog, &in, lp);
|
||||
}
|
||||
@@ -536,6 +661,7 @@ static bool sw_render(const Tb3dFrame* f, uint8_t* px) {
|
||||
const D3Program* prog = d->shader ? ((const Shd*)d->shader)->prog : NULL; /* pub e o 1o campo */
|
||||
s_vprog = (prog && d3_shader_stage(prog) == D3_STAGE_VERTEX) ? prog : NULL;
|
||||
s_prog = (prog && d3_shader_stage(prog) == D3_STAGE_FRAGMENT) ? prog : NULL;
|
||||
s_albedo = d->albedo;
|
||||
s_frame = f;
|
||||
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]);
|
||||
@@ -547,6 +673,7 @@ static bool sw_render(const Tb3dFrame* f, uint8_t* px) {
|
||||
}
|
||||
free(cv);
|
||||
s_prog = s_vprog = NULL;
|
||||
s_albedo = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -998,6 +1125,86 @@ static void eval_shader_glsl(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
tb_block_set_output(self, 0, tb_value_text(sh->pub.glsl));
|
||||
}
|
||||
|
||||
/* -- Texturas e materiais --- */
|
||||
static void eval_texture(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
const char* name = tb_block_input_text(self, rt, 0);
|
||||
if (!name[0]) { tinyblocks_set_error(rt, "dê um nome à textura"); return; }
|
||||
TinyBlocks_Image* img = tb_value_as_image(tb_block_get_input(self, rt, 1));
|
||||
if (!img) { tinyblocks_set_error(rt, "a textura precisa de uma imagem (\"carregue imagem ...\")"); return; }
|
||||
Tex* t = tex_get(name);
|
||||
if (!t) {
|
||||
t = calloc(1, sizeof(*t));
|
||||
if (!t) { tinyblocks_set_error(rt, "sem memória"); return; }
|
||||
t->refs = 1;
|
||||
t->pub.id = ++s_tex_seq;
|
||||
t->pub.version = 1;
|
||||
snprintf(t->name, sizeof(t->name), "%s", name);
|
||||
if (s_tex.n == s_tex.cap) {
|
||||
int nc = s_tex.cap ? s_tex.cap * 2 : 8;
|
||||
Tex** q = realloc(s_tex.t, (size_t)nc * sizeof(*q));
|
||||
if (!q) { free(t); tinyblocks_set_error(rt, "sem memória"); return; }
|
||||
s_tex.t = q; s_tex.cap = nc;
|
||||
}
|
||||
s_tex.t[s_tex.n++] = t;
|
||||
}
|
||||
tex_from_image(t, img);
|
||||
}
|
||||
|
||||
static void eval_material(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
const char* name = tb_block_input_text(self, rt, 0);
|
||||
if (!name[0]) { tinyblocks_set_error(rt, "dê um nome ao material"); return; }
|
||||
if (!mat_get(name, true)) tinyblocks_set_error(rt, "sem memória");
|
||||
}
|
||||
|
||||
static void eval_material_albedo(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
const char* mn = tb_block_input_text(self, rt, 0);
|
||||
Mat* m = mat_get(mn, false);
|
||||
if (!m) { tinyblocks_set_error(rt, "não existe o material '%s'", mn); return; }
|
||||
const char* tn = tb_block_input_text(self, rt, 1);
|
||||
Tex* t = tex_get(tn);
|
||||
if (!t) { tinyblocks_set_error(rt, "não existe a textura '%s'", tn); return; }
|
||||
t->refs++;
|
||||
tex_release(m->albedo);
|
||||
m->albedo = t;
|
||||
}
|
||||
|
||||
static void eval_material_normal(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
const char* mn = tb_block_input_text(self, rt, 0);
|
||||
Mat* m = mat_get(mn, false);
|
||||
if (!m) { tinyblocks_set_error(rt, "não existe o material '%s'", mn); return; }
|
||||
const char* tn = tb_block_input_text(self, rt, 1);
|
||||
Tex* t = tex_get(tn);
|
||||
if (!t) { tinyblocks_set_error(rt, "não existe a textura '%s'", tn); return; }
|
||||
t->refs++;
|
||||
tex_release(m->normal);
|
||||
m->normal = t;
|
||||
}
|
||||
|
||||
static void eval_material_color(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
const char* mn = tb_block_input_text(self, rt, 0);
|
||||
Mat* m = mat_get(mn, false);
|
||||
if (!m) { tinyblocks_set_error(rt, "não existe o material '%s'", mn); return; }
|
||||
const char* cn = tb_block_input_text(self, rt, 1);
|
||||
float c[4];
|
||||
if (!d3_color_of(cn, c)) { tinyblocks_set_error(rt, "cor desconhecida '%s'", cn); return; }
|
||||
m->tint[0] = c[0]; m->tint[1] = c[1]; m->tint[2] = c[2];
|
||||
}
|
||||
|
||||
static void eval_material_use(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
const char* mn = tb_block_input_text(self, rt, 0);
|
||||
Mat* m = mat_get(mn, false);
|
||||
if (!m) { tinyblocks_set_error(rt, "não existe o material '%s'", mn); return; }
|
||||
Obj* o = need(self, rt, 1);
|
||||
if (!o) return;
|
||||
o->material = m;
|
||||
}
|
||||
|
||||
static void eval_renderer(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
if (!S.used[0]) { const Tb3dRenderer* r = pick(rt); snprintf(S.used, sizeof(S.used), "%s (%s)", r->name, r->device()); }
|
||||
@@ -1037,6 +1244,12 @@ static const Spec s_specs[] = {
|
||||
{ "3D.MeshUV", "na malha %1 adicione a texcoord u %2 v %3", "minha malha|0|0", 3, {T,R,R}, O_NONE, -1, S_(eval_mesh_uv), "Empilha a coordenada de textura (0 a 1) de um vértice" },
|
||||
{ "3D.MeshTri", "na malha %1 adicione o triângulo %2 %3 %4", "minha malha|1|2|3", 4, {T,I,I,I}, O_NONE, -1, S_(eval_mesh_tri), "Um triângulo pelos números dos vértices (1 = o primeiro)" },
|
||||
{ "3D.MeshFinish", "finalize a malha %1", "minha malha", 1, {T}, O_NONE, -1, S_(eval_mesh_finish), "Monta a malha (com as normais que faltam) e a deixa pronta para desenhar" },
|
||||
{ "3D.Texture", "crie a textura %1 da imagem %2", "telha|", 2, {T,TB_VALUE_IMAGE}, O_NONE, -1, S_(eval_texture), "Guarda uma textura com esse nome a partir de uma imagem" },
|
||||
{ "3D.Material", "crie o material %1", "meu material", 1, {T}, O_NONE, -1, S_(eval_material), "Começa um material (cor + texturas)" },
|
||||
{ "3D.MaterialAlbedo", "no material %1 use a textura %2 como albedo", "meu material|telha", 2, {T,T}, O_NONE, -1, S_(eval_material_albedo), "A textura que dá a cor do material" },
|
||||
{ "3D.MaterialNormal", "no material %1 use a textura %2 como normal", "meu material|relevo", 2, {T,T}, O_NONE, -1, S_(eval_material_normal), "A textura de relevo (normal map) do material (usada depois)" },
|
||||
{ "3D.MaterialColor", "no material %1 use a cor %2", "meu material|branco", 2, {T,T}, O_NONE, -1, S_(eval_material_color), "A cor que multiplica a textura (padrão branco)" },
|
||||
{ "3D.MaterialUse", "use o material %1 no objeto %2", "meu material|jogador", 2, {T,T}, O_NONE, -1, S_(eval_material_use), "O objeto passa a usar o material (textura e cor)" },
|
||||
{ "3D.MoveTo", "mova %1 para x %2 y %3 z %4", "jogador|0|0.5|0", 4, {T,R,R,R}, O_NONE, -1, S_(eval_move_to), "Põe o objeto nessa posição" },
|
||||
{ "3D.MoveBy", "mova %1 por x %2 y %3 z %4", "jogador|0|0|0.1", 4, {T,R,R,R}, O_NONE, -1, S_(eval_move_by), "Soma à posição do objeto" },
|
||||
{ "3D.RotateTo", "gire %1 para x %2 y %3 z %4 graus", "jogador|0|45|0", 4, {T,R,R,R}, O_NONE, -1, S_(eval_rot_to), "Rotação nos eixos x, y e z (em graus)" },
|
||||
|
||||
Reference in new issue
Block a user