Files
tinyblocks/examples/editor/main.c
T
mguinhosandClaude Opus 5 0c4c107244 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>
2026-10-29 01:07:18 -03:00

187 lines
7.4 KiB
C

/*
* TinyBlocks - exemplo editor
*
* Abre o editor visual com as libraries de categoria registradas:
* Eventos, Controle, Operadores, Variáveis, Aparência, Caneta, Arquivos e
* Meus Blocos.
*
* As demais (TOML, JSON, XML, CSV, Blueprint, ...) NÃO vêm embutidas: são
* extensões que o usuário carrega pela tela "Escolha uma Extensão".
*
* A janela vem da interface tinyblocks-graphics; o backend padrão é SDL2 e
* pode ser trocado por outro .so:
*
* ./editor [--backend libbackend.so] [library.so ...]
*
* O editor não conhece essas libraries: elas apenas registram seus blocos e
* declaram uma categoria, e o editor monta a paleta a partir disso.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_editor.h>
#include <tb_graphics.h>
#include <tb_events.h>
#include <tb_control.h>
#include <tb_operators.h>
#include <tb_variables.h>
#include <tb_appearance.h>
#include <tb_pen.h>
#include <tb_fs.h>
#include <tb_os.h>
#include <tb_errors.h>
#include <tb_input_lib.h>
#include <tb_number.h>
#include <tb_string.h>
#include <tb_vector.h>
#include <tb_socket.h>
#include <tb_ssl.h>
#include <tb_http.h>
#include <tb_archive.h>
#include <tb_tensor.h>
#include <tb_ai.h>
#include <tb_plot.h>
#include <tb_3d.h>
#include <tb_shader.h>
#if !defined(_WIN32) && !defined(__ANDROID__)
# include <tb_gl.h>
# include <tb_vk.h>
#endif
#include <tb_api.h>
#ifdef __ANDROID__
void tb_android_boot(void); /* cwd, TINYBLOCKS_APPDIR, fonte e certificados (packaging/android) */
#endif
#ifdef TB_STATIC_EXTRAS
/* Build sem dlopen das libraries (Windows): as que no Linux vem como .so
entram no executavel e sao registradas aqui */
# include <tb_base64.h>
# include <tb_curves.h>
# include <tb_vg.h>
# include <tb_svg.h>
# include <tb_regex.h>
# include <tb_markdown.h>
# include <tb_opentype.h>
#endif
#include <tb_ppm.h>
#include <tb_bmp.h>
#include <tb_png.h>
#include <tb_imglib.h>
/* Registra uma library de categoria: se existir o .so dela no disco, carrega
* por ali (assim "make libs" + hot reload recarrega a lista de blocos na
* paleta); senao usa a copia embutida (linkada estaticamente). */
static void reg_core(TinyBlocks* rt, const char* so_name,
const TinyBlocks_LibraryDescriptor* (*get)(void)) {
if (tb_api_load_named(so_name) >= 0) return;
tb_library_register(rt, get());
}
int main(int argc, char** argv) {
#ifdef __ANDROID__
tb_android_boot();
#endif
#ifdef _WIN32
/* Programa de janela (sem console): o stdout nao existe, e o editor
precisa dele para levar o "escreva" ao palco. Aponta para o NUL. */
if (_fileno(stdout) < 0) freopen("NUL", "w", stdout);
if (_fileno(stderr) < 0) freopen("NUL", "w", stderr);
#endif
TinyBlocks* rt = NULL;
if (!tinyblocks_init(&rt, NULL)) return 1;
/* --backend troca o backend gráfico; os demais .so são pré-carregados */
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--backend") == 0 && i + 1 < argc) {
if (!tb_graphics_load_backend(argv[++i])) return 1;
} else {
tb_editor_preload(argv[i]);
}
}
/* O tinyblocks-api faz dlopen das libraries e mantem os .so vigiados
(hot reload da paleta). */
tb_api_init(rt);
/* Libraries de categoria, na ordem em que aparecem na paleta.
Preferimos o .so: recompilar a library recarrega a paleta ao vivo. */
reg_core(rt, "tinyblocks-events", tb_events_get_library);
reg_core(rt, "tinyblocks-control", tb_control_get_library);
reg_core(rt, "tinyblocks-errors", tb_errors_get_library);
reg_core(rt, "tinyblocks-input", tb_input_get_library);
reg_core(rt, "tinyblocks-operators", tb_operators_get_library);
reg_core(rt, "tinyblocks-number", tb_number_get_library);
reg_core(rt, "tinyblocks-string", tb_string_get_library);
reg_core(rt, "tinyblocks-variables", tb_variables_get_library);
reg_core(rt, "tinyblocks-vector", tb_vector_get_library);
reg_core(rt, "tinyblocks-appearance", tb_appearance_get_library);
reg_core(rt, "tinyblocks-ppm", tb_ppm_get_library);
reg_core(rt, "tinyblocks-bmp", tb_bmp_get_library);
reg_core(rt, "tinyblocks-png", tb_png_get_library);
reg_core(rt, "tinyblocks-image", tb_imglib_get_library);
reg_core(rt, "tinyblocks-pen", tb_pen_get_library);
reg_core(rt, "tinyblocks-fs", tb_fs_get_library);
reg_core(rt, "tinyblocks-os", tb_os_get_library);
reg_core(rt, "tinyblocks-socket", tb_socket_get_library);
reg_core(rt, "tinyblocks-ssl", tb_ssl_get_library);
reg_core(rt, "tinyblocks-http", tb_http_get_library);
reg_core(rt, "tinyblocks-archive", tb_archive_get_library);
reg_core(rt, "tinyblocks-tensor", tb_tensor_get_library);
reg_core(rt, "tinyblocks-ai", tb_ai_get_library);
reg_core(rt, "tinyblocks-plot", tb_plot_get_library);
reg_core(rt, "tinyblocks-3d", tb_3d_get_library);
reg_core(rt, "tinyblocks-shader", tb_shader_get_library);
#ifndef _WIN32 /* no Windows a 3d desenha em software */
#if !defined(__ANDROID__)
reg_core(rt, "tinyblocks-gl", tb_gl_get_library);
reg_core(rt, "tinyblocks-vk", tb_vk_get_library);
#endif
#endif
#ifdef TB_STATIC_EXTRAS
{
const TinyBlocks_LibraryDescriptor* (*extras[])(void) = {
tb_base64_get_library, tb_curves_get_library, tb_vg_get_library, tb_svg_get_library,
tb_regex_get_library, tb_markdown_get_library, tb_opentype_get_library };
for (size_t i = 0; i < sizeof(extras) / sizeof(extras[0]); i++) tb_library_register(rt, extras[i]());
}
#endif
/* Interface gráfica: o editor mede texto e cria o palco ao nascer */
tb_library_register(rt, tb_graphics_get_library());
tb_library_register(rt, tb_editor_get_library());
/* Graphics.Input - Editor.Scene - Graphics.Window */
TinyBlocks_TypeBlock* t_input = tb_type_block_find(rt, "Graphics.Input");
TinyBlocks_TypeBlock* t_scene = tb_type_block_find(rt, "Editor.Scene");
TinyBlocks_TypeBlock* t_win = tb_type_block_find(rt, "Graphics.Window");
if (!t_input || !t_scene || !t_win) {
fprintf(stderr, "blocos Graphics.Input / Editor.Scene / Graphics.Window não encontrados\n");
return 1;
}
TinyBlocks_Block* input = tb_block_create(rt, t_input);
TinyBlocks_Block* scene = tb_block_create(rt, t_scene);
TinyBlocks_Block* win = tb_block_create(rt, t_win);
tb_block_set_default(scene, 0, tb_value_block(input));
TinyBlocks_Value title = tb_value_text("TinyBlocks Editor");
tb_block_set_default(win, 0, title); /* set_default clona */
tb_value_free(&title);
/* TINYBLOCKS_WIN_SIZE=LxA abre com outro tamanho (ex.: 400x800 simula celular) */
int win_w = 1280, win_h = 800;
const char* ws = getenv("TINYBLOCKS_WIN_SIZE");
if (ws && sscanf(ws, "%dx%d", &win_w, &win_h) != 2) { win_w = 1280; win_h = 800; }
tb_block_set_default(win, 1, tb_value_integer(win_w));
tb_block_set_default(win, 2, tb_value_integer(win_h));
tb_block_set_default(win, 3, tb_value_block(scene));
tb_block_set_default(win, 4, tb_value_block(input));
tb_block_eval(win, rt); /* abre a janela */
printf("Backend gráfico: %s\n", tb_graphics_backend()->name);
tb_graphics_run(rt); /* loop até a janela principal fechar */
tinyblocks_deinit(&rt);
return 0;
}