;; this assumes tris (triangles)
;; note that this does not account for winding order!
(bind-func generate_tri_normals
(lambda (verts:float* vsize:i64 normals:float* nsize:i64)
(if (<> (/ vsize 4) (/ nsize 3))
(println "Error: verts must be vec4 4, and normals must be vec3")
(let ((elts (/ vsize 4))
(tris (/ elts 3))
(i 0) (j 0) (k 0) (l 0)
(p1:float* null) (p2:float* null)
(p3:float* (alloc 4)) (p4:float* (alloc 4)))
(dotimes (i tris)
(set! k (* i 4 3)) ;; start of vert tri
(set! j (* i 3 3)) ;; start of normal tri
(set! p1 (pref-ptr verts k)) ;; vert 1
(set! p2 (pref-ptr verts (+ k 4))) ;; vert 2
(vvcross p1 p2 p3)
(vnormalise p3 3 p4)
(vcopy p4 3 (pref-ptr normals j))
(vcopy p4 3 (pref-ptr normals (+ j 3)))
(vcopy p4 3 (pref-ptr normals (+ j 6))))
void))))