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