GCC Code Coverage Report


Directory: ../src/
File: /home/joels/Current/lispbm/src/extensions/tiny3d_extensions.c
Date: 2026-09-05 08:38:23
Exec Total Coverage
Lines: 25 415 6.0%
Functions: 1 38 2.6%
Branches: 0 282 0.0%

Line Branch Exec Source
1 /*
2 Copyright 2026 Joel Svensson svenssonjoel@yahoo.se
3
4 This file is part of LispBM.
5
6 LispBM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 LispBM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <extensions/tiny3d_extensions.h>
21 #include <extensions/display_extensions.h>
22 #include <lbm_utils.h>
23 #include <math.h>
24
25 #define TINY3D_MESH_MAGIC ((uint32_t)0x33444D00) // "3DM\0"
26 #define TINY3D_INSTANCE_MAGIC ((uint32_t)0x33444900) // "3DI\0"
27 #define TINY3D_STATE_MAGIC ((uint32_t)0x33445300) // "3DS\0"
28
29 // Symbols
30 static lbm_uint symbol_filled = 0;
31 static lbm_uint symbol_no_backface_cull = 0;
32 static lbm_uint symbol_light_source = 0;
33 static lbm_uint symbol_ambient = 0;
34
35 typedef struct {
36 uint32_t magic;
37 uint16_t vertex_count;
38 uint16_t triangle_count;
39 int32_t bounding_radius;
40 } tiny3d_mesh_header_t;
41
42 ✗ static bool is_mesh(const uint8_t *data, lbm_uint size) {
43 ✗ if (size < sizeof(tiny3d_mesh_header_t)) return false;
44 ✗ const tiny3d_mesh_header_t *hdr = (const tiny3d_mesh_header_t*)data;
45 ✗ if (hdr->magic != TINY3D_MESH_MAGIC) return false;
46 ✗ lbm_uint expected = sizeof(tiny3d_mesh_header_t)
47 ✗ + (lbm_uint)hdr->vertex_count * sizeof(tiny3d_vec_t)
48 ✗ + (lbm_uint)hdr->triangle_count * sizeof(tiny3d_triangle_t);
49 ✗ return size == expected;
50 }
51
52 ✗ static tiny3d_mesh_header_t *get_mesh_header(lbm_value v) {
53 ✗ lbm_array_header_t *arr = lbm_dec_array_r(v);
54 ✗ if (arr && is_mesh((const uint8_t*)arr->data, arr->size)) {
55 ✗ return (tiny3d_mesh_header_t*)arr->data;
56 }
57 ✗ return NULL;
58 }
59
60 ✗ static tiny3d_mesh_t to_mesh(tiny3d_mesh_header_t *hdr) {
61 ✗ uint8_t *base = (uint8_t*)hdr;
62 tiny3d_mesh_t m;
63 ✗ m.vertices = (const tiny3d_vec_t*)(base + sizeof(tiny3d_mesh_header_t));
64 ✗ m.vertex_count = hdr->vertex_count;
65 ✗ m.triangles = (const tiny3d_triangle_t*)
66 ✗ (base + sizeof(tiny3d_mesh_header_t) + (lbm_uint)hdr->vertex_count * sizeof(tiny3d_vec_t));
67 ✗ m.triangle_count = hdr->triangle_count;
68 ✗ m.bounding_radius = hdr->bounding_radius;
69 ✗ return m;
70 }
71
72 ✗ static bool validate_vertex_list(lbm_value verts_list, lbm_uint *out_count) {
73 ✗ lbm_uint count = 0;
74 ✗ lbm_value curr = verts_list;
75 ✗ while (lbm_is_cons(curr)) {
76 ✗ lbm_value v = lbm_car(curr);
77 ✗ if (lbm_list_length(v) != 3) return false;
78 ✗ if (!lbm_is_number(lbm_car(v)) || !lbm_is_number(lbm_cadr(v)) ||
79 ✗ !lbm_is_number(lbm_car(lbm_cddr(v)))) {
80 ✗ return false;
81 }
82 ✗ count++;
83 ✗ curr = lbm_cdr(curr);
84 }
85 ✗ if (curr != ENC_SYM_NIL) return false; // improper list
86 ✗ *out_count = count;
87 ✗ return true;
88 }
89
90 ✗ static bool validate_index_triangle_list(lbm_value tris_list, lbm_uint vertex_count, lbm_uint *out_count) {
91 ✗ lbm_uint count = 0;
92 ✗ lbm_value curr = tris_list;
93 ✗ while (lbm_is_cons(curr)) {
94 ✗ lbm_value tri = lbm_car(curr);
95 ✗ if (lbm_list_length(tri) != 4) return false;
96 ✗ lbm_value e = tri;
97 ✗ for (int i = 0; i < 4; i++) {
98 ✗ if (!lbm_is_number(lbm_car(e))) return false;
99 ✗ e = lbm_cdr(e);
100 }
101 ✗ uint32_t i0 = lbm_dec_as_u32(lbm_car(tri));
102 ✗ uint32_t i1 = lbm_dec_as_u32(lbm_cadr(tri));
103 ✗ uint32_t i2 = lbm_dec_as_u32(lbm_car(lbm_cddr(tri)));
104 ✗ if (i0 >= vertex_count || i1 >= vertex_count || i2 >= vertex_count) return false;
105 ✗ count++;
106 ✗ curr = lbm_cdr(curr);
107 }
108 ✗ if (curr != ENC_SYM_NIL) return false; // improper list
109 ✗ *out_count = count;
110 ✗ return true;
111 }
112
113 // (tiny3d-mesh vertices triangles)
114 // vertices: list of (x y z), one entry per unique vertex.
115 // triangles: list of (i0 i1 i2 color) - indices into vertices, so
116 // vertices are shared between triangles.
117 // Precomputes the one_over_abs_n value per triangle.
118 ✗ static lbm_value ext_tiny3d_mesh(lbm_value *args, lbm_uint argn) {
119 ✗ if (argn != 2 || !lbm_is_list(args[0]) || !lbm_is_list(args[1])) return ENC_SYM_TERROR;
120
121 lbm_uint vert_count;
122 ✗ if (!validate_vertex_list(args[0], &vert_count) || vert_count == 0 || vert_count > 0xFFFF) {
123 ✗ return ENC_SYM_TERROR;
124 }
125 lbm_uint tri_count;
126 ✗ if (!validate_index_triangle_list(args[1], vert_count, &tri_count) || tri_count == 0 || tri_count > 0xFFFF) {
127 ✗ return ENC_SYM_TERROR;
128 }
129
130 ✗ lbm_uint size = sizeof(tiny3d_mesh_header_t)
131 ✗ + vert_count * sizeof(tiny3d_vec_t)
132 ✗ + tri_count * sizeof(tiny3d_triangle_t);
133
134 ✗ uint8_t *buf = lbm_malloc(size);
135 ✗ if (!buf) return ENC_SYM_MERROR;
136
137 ✗ tiny3d_vec_t *verts = (tiny3d_vec_t*)(buf + sizeof(tiny3d_mesh_header_t));
138 ✗ tiny3d_triangle_t *tris = (tiny3d_triangle_t*)
139 ✗ (buf + sizeof(tiny3d_mesh_header_t) + vert_count * sizeof(tiny3d_vec_t));
140
141 // Populate the vertices and keep track of the distance to the point
142 // furthest away from 0,0,0 local coord which is used as mesh bounding sphere radius.
143 ✗ float max_r = 0.0f;
144 ✗ lbm_uint vi = 0;
145 ✗ lbm_value curr = args[0];
146 ✗ while (lbm_is_cons(curr)) {
147 ✗ lbm_value v = lbm_car(curr);
148 ✗ float x = lbm_dec_as_float(lbm_car(v));
149 ✗ float y = lbm_dec_as_float(lbm_cadr(v));
150 ✗ float z = lbm_dec_as_float(lbm_car(lbm_cddr(v)));
151 ✗ verts[vi].x = (int32_t)llroundf(x * 65536.0f);
152 ✗ verts[vi].y = (int32_t)llroundf(y * 65536.0f);
153 ✗ verts[vi].z = (int32_t)llroundf(z * 65536.0f);
154 ✗ float r = sqrtf(x * x + y * y + z * z);
155 ✗ if (r > max_r) max_r = r;
156 ✗ vi++;
157 ✗ curr = lbm_cdr(curr);
158 }
159
160 // Populate the triangles and compute the (1/|N|) where N is
161 // is the triangles normal length.
162 ✗ lbm_uint ti = 0;
163 ✗ curr = args[1];
164 ✗ while (lbm_is_cons(curr)) {
165 ✗ lbm_value tri = lbm_car(curr);
166 ✗ uint16_t i0 = (uint16_t)lbm_dec_as_u32(lbm_car(tri));
167 ✗ uint16_t i1 = (uint16_t)lbm_dec_as_u32(lbm_cadr(tri));
168 ✗ uint16_t i2 = (uint16_t)lbm_dec_as_u32(lbm_car(lbm_cddr(tri)));
169 ✗ uint32_t color = lbm_dec_as_u32(lbm_car(lbm_cdr(lbm_cddr(tri))));
170
171 // compute triangle normal length.
172 ✗ float x0 = (float)verts[i0].x / 65536.0f; // re-float it
173 ✗ float y0 = (float)verts[i0].y / 65536.0f;
174 ✗ float z0 = (float)verts[i0].z / 65536.0f;
175
176 ✗ float x1 = (float)verts[i1].x / 65536.0f;
177 ✗ float y1 = (float)verts[i1].y / 65536.0f;
178 ✗ float z1 = (float)verts[i1].z / 65536.0f;
179
180 ✗ float x2 = (float)verts[i2].x / 65536.0f;
181 ✗ float y2 = (float)verts[i2].y / 65536.0f;
182 ✗ float z2 = (float)verts[i2].z / 65536.0f;
183
184 ✗ float e0x = x1 - x0;
185 ✗ float e0y = y1 - y0;
186 ✗ float e0z = z1 - z0;
187
188 ✗ float e1x = x2 - x0;
189 ✗ float e1y = y2 - y0;
190 ✗ float e1z = z2 - z0;
191
192 ✗ float nx = e0y * e1z - e0z * e1y;
193 ✗ float ny = e0z * e1x - e0x * e1z;
194 ✗ float nz = e0x * e1y - e0y * e1x;
195
196 ✗ float len = sqrtf(nx*nx + ny*ny + nz*nz);
197 ✗ float inv_len = 1.0f / len;
198 ✗ if (inv_len > 30000.0f) inv_len = 30000.0f;
199
200 ✗ tris[ti].i0 = i0;
201 ✗ tris[ti].i1 = i1;
202 ✗ tris[ti].i2 = i2;
203 ✗ tris[ti]._pad = 0;
204 ✗ tris[ti].color = color;
205 ✗ tris[ti].one_over_abs_n = (int32_t)llroundf(inv_len * 65536.0f); // Q16.16
206 ✗ ti++;
207 ✗ curr = lbm_cdr(curr);
208 }
209
210 ✗ tiny3d_mesh_header_t *hdr = (tiny3d_mesh_header_t*)buf;
211 ✗ hdr->magic = TINY3D_MESH_MAGIC;
212 ✗ hdr->vertex_count = (uint16_t)vert_count;
213 ✗ hdr->triangle_count = (uint16_t)tri_count;
214 ✗ hdr->bounding_radius = (int32_t)llroundf(max_r * 65536.0f);
215
216 lbm_value res;
217 ✗ if (!lbm_lift_array(&res, (char*)buf, size)) {
218 ✗ lbm_free(buf);
219 ✗ return ENC_SYM_MERROR;
220 }
221 ✗ return res;
222 }
223
224 ✗ static lbm_value ext_tiny3d_is_mesh(lbm_value *args, lbm_uint argn) {
225 ✗ if (argn != 1) return ENC_SYM_TERROR;
226 ✗ return get_mesh_header(args[0]) ? ENC_SYM_TRUE : ENC_SYM_NIL;
227 }
228
229 // //////////////////////////////////////////////////
230 // Instance: [magic][pos][orient][scale]
231 //
232 // An instance of a mesh, this is so that several
233 // "things" in the world can reuse one set of vertices and triangles.
234 //
235 // The instance_blob can be updated destructively.
236 // On the lisp side a full represntation of an instance is a (instance-blob . mesh) pair
237
238 typedef struct {
239 uint32_t magic;
240 tiny3d_pos_t pos;
241 tiny3d_orient_t orient;
242 int32_t scale;
243 } tiny3d_instance_blob_t;
244
245 ✗ static tiny3d_instance_blob_t *get_instance_blob(lbm_value v) {
246 ✗ lbm_array_header_t *arr = lbm_dec_array_r(v);
247 ✗ if (!arr || arr->size != sizeof(tiny3d_instance_blob_t)) return NULL;
248 ✗ tiny3d_instance_blob_t *blob = (tiny3d_instance_blob_t*)arr->data;
249 ✗ if (blob->magic != TINY3D_INSTANCE_MAGIC) return NULL;
250 ✗ return blob;
251 }
252
253 // Setters accept either the bare instance value or an (instance-blob . mesh)
254 // pair
255 ✗ static tiny3d_instance_blob_t *resolve_instance(lbm_value v) {
256 ✗ if (lbm_is_cons(v)) v = lbm_car(v);
257 ✗ return get_instance_blob(v);
258 }
259
260 static bool decode_vec3(lbm_value v, tiny3d_vec_t *out);
261 static bool decode_orient(lbm_value v, tiny3d_orient_t *out);
262 static bool decode_light_direction(lbm_value v, tiny3d_vec_t *out);
263 static lbm_value encode_vec3(tiny3d_vec_t v);
264 static lbm_value encode_orient(tiny3d_orient_t o);
265
266 // (tiny3d-instance mesh pos orient opt-scale)
267 ✗ static lbm_value ext_tiny3d_instance(lbm_value *args, lbm_uint argn) {
268 ✗ if ((argn != 3 && argn != 4) || !get_mesh_header(args[0])) return ENC_SYM_TERROR;
269 ✗ if (argn == 4 && !lbm_is_number(args[3])) return ENC_SYM_TERROR;
270
271 ✗ tiny3d_instance_blob_t *blob = lbm_malloc(sizeof(tiny3d_instance_blob_t));
272 ✗ if (!blob) return ENC_SYM_MERROR;
273
274 ✗ blob->magic = TINY3D_INSTANCE_MAGIC;
275 ✗ blob->scale = (argn == 4)
276 ✗ ? (int32_t)llround((double)lbm_dec_as_float(args[3]) * 65536.0)
277 ✗ : TINY3D_SCALE_ONE;
278 ✗ if (!decode_vec3(args[1], &blob->pos) || !decode_orient(args[2], &blob->orient)) {
279 ✗ lbm_free(blob);
280 ✗ return ENC_SYM_TERROR;
281 }
282
283 lbm_value inst_val;
284 ✗ if (!lbm_lift_array(&inst_val, (char*)blob, sizeof(tiny3d_instance_blob_t))) {
285 ✗ lbm_free(blob);
286 ✗ return ENC_SYM_MERROR;
287 }
288 ✗ return lbm_cons(inst_val, args[0]);
289 }
290
291 ✗ static lbm_value ext_tiny3d_is_instance(lbm_value *args, lbm_uint argn) {
292 ✗ if (argn != 1) return ENC_SYM_TERROR;
293 ✗ return resolve_instance(args[0]) ? ENC_SYM_TRUE : ENC_SYM_NIL;
294 }
295
296 ✗ static lbm_value ext_tiny3d_instance_set_pos(lbm_value *args, lbm_uint argn) {
297 ✗ if (argn != 2) return ENC_SYM_TERROR;
298 ✗ tiny3d_instance_blob_t *blob = resolve_instance(args[0]);
299 ✗ if (!blob || !decode_vec3(args[1], &blob->pos)) return ENC_SYM_TERROR;
300 ✗ return ENC_SYM_TRUE;
301 }
302
303 ✗ static lbm_value ext_tiny3d_instance_set_orient(lbm_value *args, lbm_uint argn) {
304 ✗ if (argn != 2) return ENC_SYM_TERROR;
305 ✗ tiny3d_instance_blob_t *blob = resolve_instance(args[0]);
306 ✗ if (!blob || !decode_orient(args[1], &blob->orient)) return ENC_SYM_TERROR;
307 ✗ return ENC_SYM_TRUE;
308 }
309
310 ✗ static lbm_value ext_tiny3d_instance_set_transform(lbm_value *args, lbm_uint argn) {
311 ✗ if (argn != 3) return ENC_SYM_TERROR;
312 ✗ tiny3d_instance_blob_t *blob = resolve_instance(args[0]);
313 ✗ if (!blob) return ENC_SYM_TERROR;
314 ✗ if (!decode_vec3(args[1], &blob->pos)) return ENC_SYM_TERROR;
315 ✗ if (!decode_orient(args[2], &blob->orient)) return ENC_SYM_TERROR;
316 ✗ return ENC_SYM_TRUE;
317 }
318
319 ✗ static lbm_value ext_tiny3d_instance_pos(lbm_value *args, lbm_uint argn) {
320 ✗ if (argn != 1) return ENC_SYM_TERROR;
321 ✗ tiny3d_instance_blob_t *blob = resolve_instance(args[0]);
322 ✗ if (!blob) return ENC_SYM_TERROR;
323 ✗ return encode_vec3(blob->pos);
324 }
325
326 ✗ static lbm_value ext_tiny3d_instance_orient(lbm_value *args, lbm_uint argn) {
327 ✗ if (argn != 1) return ENC_SYM_TERROR;
328 ✗ tiny3d_instance_blob_t *blob = resolve_instance(args[0]);
329 ✗ if (!blob) return ENC_SYM_TERROR;
330 ✗ return encode_orient(blob->orient);
331 }
332
333 // (tiny3d-instance-set-scale obj scale)
334 ✗ static lbm_value ext_tiny3d_instance_set_scale(lbm_value *args, lbm_uint argn) {
335 ✗ if (argn != 2 || !lbm_is_number(args[1])) return ENC_SYM_TERROR;
336 ✗ tiny3d_instance_blob_t *blob = resolve_instance(args[0]);
337 ✗ if (!blob) return ENC_SYM_TERROR;
338 ✗ blob->scale = (int32_t)llround((double)lbm_dec_as_float(args[1]) * 65536.0);
339 ✗ return ENC_SYM_TRUE;
340 }
341
342 ✗ static lbm_value ext_tiny3d_instance_scale(lbm_value *args, lbm_uint argn) {
343 ✗ if (argn != 1) return ENC_SYM_TERROR;
344 ✗ tiny3d_instance_blob_t *blob = resolve_instance(args[0]);
345 ✗ if (!blob) return ENC_SYM_TERROR;
346 ✗ return lbm_enc_float((float)((double)blob->scale / 65536.0));
347 }
348
349 ✗ static lbm_value ext_tiny3d_mesh_vertex_count(lbm_value *args, lbm_uint argn) {
350 ✗ if (argn != 1) return ENC_SYM_TERROR;
351 ✗ tiny3d_mesh_header_t *mh = get_mesh_header(args[0]);
352 ✗ if (!mh) return ENC_SYM_TERROR;
353 ✗ return lbm_enc_i(mh->vertex_count);
354 }
355
356 ✗ static lbm_value ext_tiny3d_mesh_triangle_count(lbm_value *args, lbm_uint argn) {
357 ✗ if (argn != 1) return ENC_SYM_TERROR;
358 ✗ tiny3d_mesh_header_t *mh = get_mesh_header(args[0]);
359 ✗ if (!mh) return ENC_SYM_TERROR;
360 ✗ return lbm_enc_i(mh->triangle_count);
361 }
362
363 ✗ static lbm_value ext_tiny3d_mesh_bounding_radius(lbm_value *args, lbm_uint argn) {
364 ✗ if (argn != 1) return ENC_SYM_TERROR;
365 ✗ tiny3d_mesh_header_t *mh = get_mesh_header(args[0]);
366 ✗ if (!mh) return ENC_SYM_TERROR;
367 ✗ return lbm_enc_float((float)mh->bounding_radius / 65536.0f);
368 }
369
370
371 ✗ static bool decode_image_buffer(lbm_value v, image_buffer_t *out) {
372 ✗ lbm_array_header_t *arr = get_image_buffer(v);
373 ✗ if (!arr) return false;
374 ✗ uint8_t *data = (uint8_t*)arr->data;
375 ✗ out->width = image_buffer_width(data);
376 ✗ out->height = image_buffer_height(data);
377 ✗ out->fmt = (color_format_t)image_buffer_format(data);
378 ✗ out->data = image_buffer_data(data);
379 ✗ out->mem_base = data;
380 ✗ return true;
381 }
382
383 // //////////////////////////////////////////////////
384 // State: [magic][image_buffer_t][tiny3d_state_t][tiny3d_camera_tri_t x cap]
385
386 typedef struct {
387 uint32_t magic;
388 image_buffer_t img;
389 tiny3d_state_t state;
390 tiny3d_vec_t light_source_storage;
391 tiny3d_camera_tri_t tri_buffer_data[];
392 } tiny3d_state_blob_t;
393
394 ✗ static tiny3d_state_blob_t *get_state_blob(lbm_value v) {
395 ✗ lbm_array_header_t *arr = lbm_dec_array_r(v);
396 ✗ if (!arr || arr->size < sizeof(tiny3d_state_blob_t)) return NULL;
397 ✗ tiny3d_state_blob_t *blob = (tiny3d_state_blob_t*)arr->data;
398 ✗ if (blob->magic != TINY3D_STATE_MAGIC) return NULL;
399 ✗ return blob;
400 }
401
402 // On the lispbm side, the "tiny3d state" is a (state-blob . image-buffer)
403 // pair. This is to ensure that the lifetime of the image buffer
404 // is the same as the lifetime of the state-blob.
405 // The state-blob contains raw pointers into the image buffer that
406 // are used by the Tiny3d C code without it knowing anything about
407 // lisp and its GC.
408 ✗ static tiny3d_state_blob_t *resolve_state(lbm_value v) {
409 ✗ if (!lbm_is_cons(v)) return NULL;
410 ✗ return get_state_blob(lbm_car(v));
411 }
412
413 // (tiny3d-state-create img max-tris-per-object near far fov-degrees cull-margin opt-attrs...)
414 // opt-attrs:
415 // '(filled) - solid triangles instead of the default wireframe
416 // '(no-backface-cull) - keep back-facing triangles (culled by default)
417 // '(light-source x y z) - enable flat directional lighting; x y z need not
418 // be pre-normalized. Implies backface culling
419 // regardless of '(no-backface-cull) appearing
420 // anywhere in the attribute list.
421 // '(ambient a) - base brightness floor in [0,1], blended with the
422 // directional term (0: fully off, unlit faces are
423 // black; 1: fully lit regardless of angle).
424 // Requires '(light-source ...) also be given.
425 // Returns (state . img)
426 ✗ static lbm_value ext_tiny3d_state_create(lbm_value *args, lbm_uint argn) {
427 ✗ if (argn < 6) return ENC_SYM_TERROR;
428 ✗ if (!lbm_is_number(args[1]) || !lbm_is_number(args[2]) || !lbm_is_number(args[3]) ||
429 ✗ !lbm_is_number(args[4]) || !lbm_is_number(args[5])) {
430 ✗ return ENC_SYM_TERROR;
431 }
432 ✗ bool filled = false;
433 ✗ bool cull_backfaces = true;
434 ✗ bool has_light = false;
435 ✗ bool has_ambient = false;
436 ✗ tiny3d_vec_t light_source_vec = {0};
437 ✗ float ambient_val = 0.0f;
438 ✗ for (lbm_uint i = 6; i < argn; i++) {
439 ✗ if (!lbm_is_cons(args[i]) || !lbm_is_symbol(lbm_car(args[i]))) {
440 ✗ return ENC_SYM_TERROR;
441 }
442 ✗ lbm_uint s = lbm_dec_sym(lbm_car(args[i]));
443 ✗ if (s == symbol_filled) {
444 ✗ if (lbm_list_length(args[i]) != 1) return ENC_SYM_TERROR;
445 ✗ filled = true;
446 ✗ } else if (s == symbol_no_backface_cull) {
447 ✗ if (lbm_list_length(args[i]) != 1) return ENC_SYM_TERROR;
448 ✗ cull_backfaces = false;
449 ✗ } else if (s == symbol_light_source) {
450 ✗ if (!decode_light_direction(lbm_cdr(args[i]), &light_source_vec)) return ENC_SYM_TERROR;
451 ✗ has_light = true;
452 ✗ } else if (s == symbol_ambient) {
453 ✗ if (lbm_list_length(args[i]) != 2 || !lbm_is_number(lbm_cadr(args[i]))) return ENC_SYM_TERROR;
454 ✗ ambient_val = lbm_dec_as_float(lbm_cadr(args[i]));
455 ✗ has_ambient = true;
456 } else {
457 ✗ return ENC_SYM_TERROR;
458 }
459 }
460 ✗ if (has_ambient && !has_light) return ENC_SYM_TERROR;
461 ✗ if (has_light) cull_backfaces = true;
462
463 image_buffer_t img;
464 ✗ if (!decode_image_buffer(args[0], &img)) return ENC_SYM_TERROR;
465
466 ✗ uint32_t max_tris = lbm_dec_as_u32(args[1]);
467 ✗ if (max_tris == 0 || max_tris > 0xFFFF) return ENC_SYM_TERROR;
468
469 ✗ int32_t near = (int32_t)llround((double)lbm_dec_as_float(args[2]) * 65536.0);
470 ✗ int32_t far = (int32_t)llround((double)lbm_dec_as_float(args[3]) * 65536.0);
471 ✗ float fov_degrees = lbm_dec_as_float(args[4]);
472 ✗ int32_t cull_margin = (int32_t)llround((double)lbm_dec_as_float(args[5]) * 65536.0);
473 ✗ bool wireframe = !filled;
474
475 ✗ lbm_uint size = sizeof(tiny3d_state_blob_t) + (lbm_uint)max_tris * sizeof(tiny3d_camera_tri_t);
476 ✗ uint8_t *buf = lbm_malloc(size);
477 ✗ if (!buf) return ENC_SYM_MERROR;
478
479 ✗ tiny3d_state_blob_t *blob = (tiny3d_state_blob_t*)buf;
480 ✗ blob->magic = TINY3D_STATE_MAGIC;
481 ✗ blob->img = img;
482 ✗ blob->light_source_storage = light_source_vec;
483
484 ✗ int32_t ambient_q = (int32_t)llround((double)ambient_val * 65536.0);
485 ✗ bool ok = tiny3d_init(&blob->state, &blob->img,
486 ✗ blob->tri_buffer_data, max_tris * (uint32_t)sizeof(tiny3d_camera_tri_t),
487 near, far, fov_degrees, cull_margin, wireframe, cull_backfaces,
488 has_light ? &blob->light_source_storage : NULL, ambient_q);
489 ✗ if (!ok) {
490 ✗ lbm_free(buf);
491 ✗ return ENC_SYM_TERROR;
492 }
493
494 lbm_value state_val;
495 ✗ if (!lbm_lift_array(&state_val, (char*)buf, size)) {
496 ✗ lbm_free(buf);
497 ✗ return ENC_SYM_MERROR;
498 }
499 ✗ return lbm_cons(state_val, args[0]);
500 }
501
502 ✗ static lbm_value ext_tiny3d_set_light_vec(lbm_value *args, lbm_uint argn) {
503 ✗ if (argn != 2) return ENC_SYM_TERROR;
504 ✗ tiny3d_state_blob_t *blob = resolve_state(args[0]);
505 ✗ if (!blob) return ENC_SYM_TERROR;
506 ✗ if (!blob->state.light_source) return ENC_SYM_NIL;
507 ✗ if (!decode_light_direction(args[1], &blob->light_source_storage)) return ENC_SYM_TERROR;
508 ✗ return ENC_SYM_TRUE;
509 }
510
511 ✗ static lbm_value ext_tiny3d_set_ambiance(lbm_value *args, lbm_uint argn) {
512 ✗ if (argn != 2 || !lbm_is_number(args[1])) return ENC_SYM_TERROR;
513 ✗ tiny3d_state_blob_t *blob = resolve_state(args[0]);
514 ✗ if (!blob) return ENC_SYM_TERROR;
515 ✗ if (!blob->state.light_source) return ENC_SYM_NIL;
516 ✗ int32_t ambient = (int32_t)llround((double)lbm_dec_as_float(args[1]) * 65536.0);
517 ✗ if (ambient < 0) ambient = 0;
518 ✗ if (ambient > TINY3D_SCALE_ONE) ambient = TINY3D_SCALE_ONE;
519 ✗ blob->state.ambient = ambient;
520 ✗ return ENC_SYM_TRUE;
521 }
522
523 // //////////////////////////////////////////////////
524 // Render
525
526 ✗ static uint16_t degrees_to_phase(double deg) {
527 ✗ int64_t raw = llround(deg / 360.0 * 65536.0);
528 ✗ int64_t wrapped = raw % 65536;
529 ✗ if (wrapped < 0) wrapped += 65536;
530 ✗ return (uint16_t)wrapped;
531 }
532
533 ✗ static bool decode_vec3(lbm_value v, tiny3d_vec_t *out) {
534 ✗ if (lbm_list_length(v) != 3) return false;
535 ✗ out->x = (int32_t)llround((double)lbm_dec_as_float(lbm_car(v)) * 65536.0);
536 ✗ out->y = (int32_t)llround((double)lbm_dec_as_float(lbm_cadr(v)) * 65536.0);
537 ✗ out->z = (int32_t)llround((double)lbm_dec_as_float(lbm_car(lbm_cddr(v))) * 65536.0);
538 ✗ return true;
539 }
540
541 ✗ static bool decode_orient(lbm_value v, tiny3d_orient_t *out) {
542 ✗ if (lbm_list_length(v) != 3) return false;
543 ✗ out->ang_x = degrees_to_phase((double)lbm_dec_as_float(lbm_car(v)));
544 ✗ out->ang_y = degrees_to_phase((double)lbm_dec_as_float(lbm_cadr(v)));
545 ✗ out->ang_z = degrees_to_phase((double)lbm_dec_as_float(lbm_car(lbm_cddr(v))));
546 ✗ return true;
547 }
548
549 // A light direction is stored as a unit vector - the caller may pass any
550 // non-zero vector, e.g. '(light-source 1.0 1.0 0.0), and it gets
551 // normalized here (float, one-time setup, not the per-frame hot path)
552 // so the lighting dot product downstream is a proper cosine term
553 // regardless of what magnitude was typed in. A zero/degenerate vector
554 // is rejected rather than silently producing a NaN/garbage direction.
555 ✗ static bool decode_light_direction(lbm_value v, tiny3d_vec_t *out) {
556 ✗ if (lbm_list_length(v) != 3) return false;
557 ✗ float x = lbm_dec_as_float(lbm_car(v));
558 ✗ float y = lbm_dec_as_float(lbm_cadr(v));
559 ✗ float z = lbm_dec_as_float(lbm_car(lbm_cddr(v)));
560 ✗ float len = sqrtf(x * x + y * y + z * z);
561 ✗ if (len < 0.000001f) return false;
562 ✗ x /= len; y /= len; z /= len;
563 ✗ out->x = (int32_t)llroundf(x * 65536.0f);
564 ✗ out->y = (int32_t)llroundf(y * 65536.0f);
565 ✗ out->z = (int32_t)llroundf(z * 65536.0f);
566 ✗ return true;
567 }
568
569 ✗ static lbm_value encode_vec3(tiny3d_vec_t v) {
570 ✗ lbm_value x = lbm_enc_float((float)((double)v.x / 65536.0));
571 ✗ lbm_value y = lbm_enc_float((float)((double)v.y / 65536.0));
572 ✗ lbm_value z = lbm_enc_float((float)((double)v.z / 65536.0));
573 ✗ return lbm_cons(x, lbm_cons(y, lbm_cons(z, ENC_SYM_NIL)));
574 }
575
576 ✗ static lbm_value encode_orient(tiny3d_orient_t o) {
577 ✗ lbm_value x = lbm_enc_float((float)((double)o.ang_x / 65536.0 * 360.0));
578 ✗ lbm_value y = lbm_enc_float((float)((double)o.ang_y / 65536.0 * 360.0));
579 ✗ lbm_value z = lbm_enc_float((float)((double)o.ang_z / 65536.0 * 360.0));
580 ✗ return lbm_cons(x, lbm_cons(y, lbm_cons(z, ENC_SYM_NIL)));
581 }
582
583 typedef struct {
584 lbm_value cursor;
585 tiny3d_mesh_t current_mesh;
586 } render_ctx_t;
587
588 ✗ static tiny3d_mesh_t render_ctx_get_mesh(uint16_t index, void *ctx_v) {
589 (void)index;
590 ✗ return ((render_ctx_t*)ctx_v)->current_mesh;
591 }
592
593 // Objects list elements are (instance-blob . mesh) pairs
594 ✗ static bool render_ctx_next_instance(void *ctx_v, tiny3d_instance_t *out) {
595 ✗ render_ctx_t *ctx = (render_ctx_t*)ctx_v;
596 ✗ if (!lbm_is_cons(ctx->cursor)) return false;
597
598 ✗ lbm_value obj = lbm_car(ctx->cursor);
599 ✗ if (!lbm_is_cons(obj)) return false;
600
601 ✗ tiny3d_instance_blob_t *inst_blob = get_instance_blob(lbm_car(obj));
602 ✗ tiny3d_mesh_header_t *mh = get_mesh_header(lbm_cdr(obj));
603 ✗ if (!inst_blob || !mh) return false;
604
605 ✗ tiny3d_instance_t inst = {0};
606 ✗ inst.pos = inst_blob->pos;
607 ✗ inst.orient = inst_blob->orient;
608 ✗ inst.scale = inst_blob->scale;
609
610 ✗ ctx->current_mesh = to_mesh(mh);
611 ✗ *out = inst;
612 ✗ ctx->cursor = lbm_cdr(ctx->cursor);
613 ✗ return true;
614 }
615
616 // (tiny3d-render state objects cam-pos cam-orient)
617 // state: the (state . img) pair returned by tiny3d-state-create.
618 // objects: list of (instance-blob . mesh) pairs, as returned by tiny3d-instance.
619 // cam-pos/cam-orient: (list x y z) / (list ax ay az) in world units / degrees.
620 ✗ static lbm_value ext_tiny3d_render(lbm_value *args, lbm_uint argn) {
621 ✗ if (argn != 4) return ENC_SYM_TERROR;
622
623 ✗ tiny3d_state_blob_t *blob = resolve_state(args[0]);
624 ✗ if (!blob) return ENC_SYM_TERROR;
625 ✗ if (!lbm_is_list(args[1])) return ENC_SYM_TERROR;
626
627 tiny3d_pos_t cam_pos;
628 tiny3d_orient_t cam_orient;
629 ✗ if (!decode_vec3(args[2], &cam_pos)) return ENC_SYM_TERROR;
630 ✗ if (!decode_orient(args[3], &cam_orient)) return ENC_SYM_TERROR;
631
632 ✗ render_ctx_t ctx = { .cursor = args[1] };
633 ✗ tiny3d_render(&blob->state,
634 render_ctx_get_mesh, &ctx,
635 render_ctx_next_instance, &ctx,
636 cam_pos, cam_orient);
637 ✗ return ENC_SYM_TRUE;
638 }
639
640 // Culls a single object.
641 // TODO: Maybe an extension that loops through an entire list of objects
642 // and returns a list of those that survive the culling.
643 ✗ static lbm_value ext_tiny3d_cull(lbm_value *args, lbm_uint argn) {
644 ✗ if (argn != 4) return ENC_SYM_TERROR;
645
646 ✗ tiny3d_state_blob_t *blob = resolve_state(args[0]);
647 ✗ if (!blob) return ENC_SYM_TERROR;
648
649 ✗ if (!lbm_is_cons(args[1])) return ENC_SYM_TERROR;
650 ✗ tiny3d_instance_blob_t *inst_blob = get_instance_blob(lbm_car(args[1]));
651 ✗ tiny3d_mesh_header_t *mh = get_mesh_header(lbm_cdr(args[1]));
652 ✗ if (!inst_blob || !mh) return ENC_SYM_TERROR;
653
654 ✗ tiny3d_instance_t inst = {0};
655 ✗ inst.pos = inst_blob->pos;
656 ✗ inst.orient = inst_blob->orient;
657 ✗ inst.scale = inst_blob->scale;
658
659 tiny3d_pos_t cam_pos;
660 tiny3d_orient_t cam_orient;
661 ✗ if (!decode_vec3(args[2], &cam_pos)) return ENC_SYM_TERROR;
662 ✗ if (!decode_orient(args[3], &cam_orient)) return ENC_SYM_TERROR;
663
664 int32_t depth;
665 ✗ bool visible = tiny3d_transform_cull(&blob->state, inst, mh->bounding_radius,
666 cam_pos, cam_orient, &depth);
667 ✗ if (!visible) return ENC_SYM_NIL;
668 ✗ return lbm_enc_float((float)depth / 65536.0f);
669 }
670
671 714 void lbm_tiny3d_extensions_init(void) {
672 714 lbm_add_symbol_const("filled", &symbol_filled);
673 714 lbm_add_symbol_const("no-backface-cull", &symbol_no_backface_cull);
674 714 lbm_add_symbol_const("light-source", &symbol_light_source);
675 714 lbm_add_symbol_const("ambient", &symbol_ambient);
676
677 714 lbm_add_extension("tiny3d-mesh", ext_tiny3d_mesh);
678 714 lbm_add_extension("tiny3d-mesh?", ext_tiny3d_is_mesh);
679 714 lbm_add_extension("tiny3d-instance", ext_tiny3d_instance);
680 714 lbm_add_extension("tiny3d-instance?", ext_tiny3d_is_instance);
681 714 lbm_add_extension("tiny3d-instance-set-pos", ext_tiny3d_instance_set_pos);
682 714 lbm_add_extension("tiny3d-instance-set-orient", ext_tiny3d_instance_set_orient);
683 714 lbm_add_extension("tiny3d-instance-set-transform", ext_tiny3d_instance_set_transform);
684 714 lbm_add_extension("tiny3d-instance-set-scale", ext_tiny3d_instance_set_scale);
685 714 lbm_add_extension("tiny3d-instance-pos", ext_tiny3d_instance_pos);
686 714 lbm_add_extension("tiny3d-instance-orient", ext_tiny3d_instance_orient);
687 714 lbm_add_extension("tiny3d-instance-scale", ext_tiny3d_instance_scale);
688 714 lbm_add_extension("tiny3d-mesh-vertex-count", ext_tiny3d_mesh_vertex_count);
689 714 lbm_add_extension("tiny3d-mesh-triangle-count", ext_tiny3d_mesh_triangle_count);
690 714 lbm_add_extension("tiny3d-mesh-bounding-radius", ext_tiny3d_mesh_bounding_radius);
691 714 lbm_add_extension("tiny3d-state-create", ext_tiny3d_state_create);
692 714 lbm_add_extension("tiny3d-set-light-vec", ext_tiny3d_set_light_vec);
693 714 lbm_add_extension("tiny3d-set-ambiance", ext_tiny3d_set_ambiance);
694 714 lbm_add_extension("tiny3d-render", ext_tiny3d_render);
695 714 lbm_add_extension("tiny3d-cull", ext_tiny3d_cull);
696 714 }
697