-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstl.lisp
More file actions
181 lines (158 loc) · 7.37 KB
/
stl.lisp
File metadata and controls
181 lines (158 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
;; stl.lisp
;;
;; Copyright (c) 2022 Jeremiah LaRocco <jeremiah_larocco@fastmail.com>
(in-package #:simple-gl)
(defclass stl-file (instanced-opengl-object)
((styles :initform (list (cons :stl-style
(make-style-from-files "stl-plastic-vertex.glsl" "stl-plastic-fragment.glsl"))))
(file-name :initarg :file-name :type (or string path))
(tri-count :initform 0 :type (unsigned-byte 32))))
(declaim (ftype (function
((simple-array (unsigned-byte 8)) fixnum)
(unsigned-byte 16))
get-u2)
(inline get-u2))
(defun get-u2 (arr idx)
"Interpret two bytes in arr as an '(unsigned-byte 32)"
(declare
(optimize (speed 3) (space 0) (safety 0) (debug 0))
(type (simple-array (unsigned-byte 8)) arr))
(the (unsigned-byte 16) (+ (* (aref arr (1+ idx)) 256) (aref arr idx))))
(declaim (ftype (function
((simple-array (unsigned-byte 8)) fixnum)
(unsigned-byte 32))
get-u4)
(inline get-u4))
(defun get-u4 (arr idx)
"Interpret the four bytes in arr as an '(unsigned-byte 32)"
(declare
(optimize (speed 3) (space 0) (safety 0) (debug 0))
(type (simple-array (unsigned-byte 8)) arr))
(the (unsigned-byte 32) (+ (* (+ (* (+ (* (aref arr (+ 3 idx)) 256)
(aref arr (+ 2 idx))) 256)
(aref arr (+ 1 idx))) 256)
(aref arr idx))))
(declaim (ftype (function
((simple-array (unsigned-byte 8)) fixnum)
(signed-byte 32))
get-s4)
(inline get-s4))
(defun get-s4 (arr idx)
"Interpret four bytes in arr as an '(signed-byte 32)"
(declare
(optimize (speed 3) (space 0) (safety 0) (debug 0))
(type (simple-array (unsigned-byte 8)) arr))
(the (signed-byte 32) (+ (* (+ (* (+ (* (aref arr (+ 3 idx)) 256)
(aref arr (+ 2 idx))) 256)
(aref arr (+ 1 idx))) 256)
(aref arr idx))))
(declaim (ftype (function
((simple-array (unsigned-byte 8)) fixnum)
single-float)
get-float)
(inline get-float))
(defun get-float (arr idx)
"Interpret four bytes in arr as a single-float."
(declare
(optimize (speed 3) (space 0) (safety 0) (debug 0))
(type (simple-array (unsigned-byte 8)) arr))
(let ((x (get-u4 arr idx)))
#+(and :little-endian :ieee-floating-point :sbcl)
(if (>= x #x80000000)
(sb-kernel:make-single-float (- x #x100000000))
(sb-kernel:make-single-float x))
#-(and :little-endian :ieee-floating-point :sbcl)
(ieee-floats:decode-float32 x)))
(defparameter *float-byte-size* 4
"Size of an STL float in bytes.")
(defparameter *point-byte-size* (* 3 *float-byte-size*)
"Size of an STL point in bytes.")
(defparameter *triangle-byte-size* (+ 2 (* 4 *point-byte-size*))
"Size of an STL triangle in bytes.")
(defun get-point (arr idx)
"Create a point using x, y, and z values read from arr."
(declare
(optimize (speed 3) (space 0) (safety 0) (debug 0))
(type (simple-array (unsigned-byte 8) *) arr))
(vec3 (get-float arr idx)
(get-float arr (+ idx *float-byte-size*))
(get-float arr (+ idx (* 2 *float-byte-size*)))))
(declaim (type (unsigned-byte 32) *point-byte-size*))
(defun read-triangle (arr idx)
"Read a triangle from arr."
(declare
(optimize (speed 3) (space 0) (safety 0) (debug 0))
(type (simple-array (unsigned-byte 8)) arr)
(type (unsigned-byte 32) idx)
(type (unsigned-byte 32) *point-byte-size*))
(values
;; Point 0
(get-point arr idx)
;; Point 1
(get-point arr (+ (* 1 *point-byte-size*) idx))
;; Point 2
(get-point arr (+ (* 2 *point-byte-size*) idx))
;; Color
(get-point arr (+ (* 3 *point-byte-size*) idx))
;; Tag
(coerce (get-u2 arr (+ (* 4 *point-byte-size*) idx)) 'single-float)))
(defun get-stl-info (stl-obj)
(declare
(optimize (speed 3) (space 0) (safety 0) (debug 0)))
(with-slots (file-name tri-count) stl-obj
(with-open-file (inf file-name :element-type '(unsigned-byte 8))
(let ((header (make-array 80 :element-type '(unsigned-byte 8)))
(triangle-count-buffer (make-array 4 :element-type '(unsigned-byte 8))))
(read-sequence header inf)
(read-sequence triangle-count-buffer inf)
(format t "STL File has ~a triangles!~%" tri-count)
(values (get-u4 triangle-count-buffer 0)
header
(babel:octets-to-string header :end (position 0 header)))))))
(defmethod initialize-buffers ((obj stl-file) &key)
(declare
(optimize (speed 3) (space 0) (safety 0) (debug 0)))
(with-slots (file-name tri-count) obj
(let ((vertices )
(buffer (make-array *triangle-byte-size* :element-type '(unsigned-byte 8)))
(cur-offset 0))
(with-open-file (inf file-name :element-type '(unsigned-byte 8))
(let ((header (make-array 80 :element-type '(unsigned-byte 8)))
(triangle-count-buffer (make-array 4 :element-type '(unsigned-byte 8))))
(read-sequence header inf)
(read-sequence triangle-count-buffer inf)
(setf tri-count (get-u4 triangle-count-buffer 0))
(format t "STL File has ~a triangles!~%" tri-count)
(setf vertices (gl:alloc-gl-array :float( * tri-count (* 3 6))))
(loop
:for idx fixnum :below tri-count
:for last-idx fixnum = (read-sequence buffer inf)
:while (= last-idx *triangle-byte-size*)
:do
(multiple-value-bind (norm p1 p2 p3) (read-triangle buffer 0)
(when (v= (vec3 0 0 0) norm)
(setf norm (vunit (vc (v- p1 p2) (v- p1 p3) ))))
(setf cur-offset (fill-pointer-offset p1 vertices cur-offset))
(setf cur-offset (fill-pointer-offset norm vertices cur-offset))
(setf cur-offset (fill-pointer-offset p2 vertices cur-offset))
(setf cur-offset (fill-pointer-offset norm vertices cur-offset))
(setf cur-offset (fill-pointer-offset p3 vertices cur-offset))
(setf cur-offset (fill-pointer-offset norm vertices cur-offset))))))
(set-buffer obj :vertices (make-instance 'attribute-buffer
:pointer vertices
:attributes '(("in_position" . :vec3)
("in_normal" . :vec3))
:free t))
(set-buffer obj :indices (constant-index-buffer (* 3 tri-count)))
(with-slots (instance-count max-instances) obj
(setf instance-count 1)
(set-buffer obj :colors (make-instance
'instance-buffer
:pointer (to-gl-array :float (* 4 max-instances) (vec4 0.1 0.8 0.2 1.0))
:attributes '(("obj_color" . :vec4))
:free nil))
(set-buffer obj :transforms (list (make-instance
'instance-buffer
:pointer (to-gl-array :float (* 16 max-instances) (meye 4))
:attributes '(("obj_transform" . :mat4))
:free nil)))))))