1 | == gl-math |
---|
2 | [[toc:]] |
---|
3 | A small math library aimed at gamedev that provides 4x4 float matrix, vector, and quaternion operations. Uses the [[https://github.com/AlexCharlton/hypermath|hypermath]] library. |
---|
4 | |
---|
5 | |
---|
6 | === Requirements |
---|
7 | * matchable |
---|
8 | |
---|
9 | |
---|
10 | === Documentation |
---|
11 | gl-math provides a number of functions for working with 4x4 matrices (plus a handful of others). The functionality is similar to what can be found in the [[http://wiki.call-cc.org/eggref/4/glm|glm egg]], but with some notable differences: |
---|
12 | |
---|
13 | * Matrix functions only operate on 4x4 matrices |
---|
14 | * Matrix functions can accept either f32vectors or pointers |
---|
15 | * No container is used to represent matrices |
---|
16 | |
---|
17 | Additionally, gl-math is one fifth the compiled size of glm, has a more straight-forward code-base, and complete documentation. |
---|
18 | |
---|
19 | gl-math expects matrices, vectors, and quaternions to be f32vectors or pointers. f32vectors must be 16 elements long, 3 elements long, or 4 elements long for matrices, vectors, or quaternions, respectively. The memory pointed to should likewise be an array of 16, 3, or 4 floats . If a function accepts more than one matrix, vector, or quaternion, all must be of the same type. |
---|
20 | |
---|
21 | gl-math operates on matrices in a column-major fashion in correspondence with OpenGL (e.g. translation components are at indices 12, 13, and 14). Vectors are arranged as ({{(x y z)}}), and quaternions as ({{(x y z w)}}). |
---|
22 | |
---|
23 | |
---|
24 | ==== Matrix operations |
---|
25 | <procedure> (print-mat4 MATRIX)</procedure> |
---|
26 | |
---|
27 | Prints the given {{MATRIX}} to {{(current-output-port)}}. |
---|
28 | |
---|
29 | <procedure> (copy-mat4 MATRIX [RESULT])</procedure> |
---|
30 | |
---|
31 | Make a copy of {{MATRIX}}. If the matrix {{RESULT}} is given, it will be modified to contain the contents of {{MATRIX}}. If {{RESULT}} is {{#t}}, {{MATRIX}} must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, {{MATRIX}} must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory. |
---|
32 | |
---|
33 | <procedure> (m* A B [RESULT])</procedure> |
---|
34 | |
---|
35 | Multiply matrix {{A}} by matrix {{B}}. If the matrix {{RESULT}} is given, it will be modified to contain the results of the multiplication. If {{RESULT}} is {{#t}}, {{A}} and {{B}} must be f32vectors and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, {{A}} and {{B}} must be f32vectors and the returned value will be an f32vector located in normal garbage collected memory. |
---|
36 | |
---|
37 | <procedure> (mat4-identity [RESULT])</procedure> |
---|
38 | |
---|
39 | Return an identity matrix. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
40 | |
---|
41 | <procedure> (translation VECTOR [RESULT])</procedure> |
---|
42 | |
---|
43 | Return the translation matrix given by {{VECTOR}}. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
44 | |
---|
45 | <procedure> (translate VECTOR MATRIX)</procedure> |
---|
46 | |
---|
47 | Translate {{MATRIX}} by {{VECTOR}}. |
---|
48 | |
---|
49 | <procedure> (x-rotation ANGLE [RESULT])</procedure> |
---|
50 | |
---|
51 | Return the rotation matrix given by a rotation of {{ANGLE}} radians around the x-axis. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
52 | |
---|
53 | <procedure> (rotate-x ANGLE MATRIX)</procedure> |
---|
54 | |
---|
55 | Rotate {{MATRIX}} around the x-axis by {{ANGLE}} radians. |
---|
56 | |
---|
57 | <procedure> (y-rotation ANGLE [RESULT])</procedure> |
---|
58 | |
---|
59 | Return the rotation matrix given by a rotation of {{ANGLE}} radians around the y-axis. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
60 | |
---|
61 | <procedure> (rotate-y ANGLE MATRIX)</procedure> |
---|
62 | |
---|
63 | Rotate {{MATRIX}} around the y-axis by {{ANGLE}} radians. |
---|
64 | |
---|
65 | <procedure> (z-rotation ANGLE [RESULT])</procedure> |
---|
66 | |
---|
67 | Return the rotation matrix given by a rotation of {{ANGLE}} radians around the z-axis. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
68 | |
---|
69 | <procedure> (rotate-z ANGLE MATRIX)</procedure> |
---|
70 | |
---|
71 | Rotate {{MATRIX}} around the z-axis by {{ANGLE}} radians. |
---|
72 | |
---|
73 | <procedure> (axis-angle-rotation AXIS ANGLE [RESULT])</procedure> |
---|
74 | |
---|
75 | Return the rotation matrix given by a rotation of {{ANGLE}} radians around the vector {{AXIS}}. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
76 | |
---|
77 | <procedure> (rotate-axis-angle AXIS ANGLE MATRIX)</procedure> |
---|
78 | |
---|
79 | Rotate {{MATRIX}} around the vector {{AXIS}} by {{ANGLE}} radians. |
---|
80 | |
---|
81 | <procedure> (quaternion-rotation Q [RESULT])</procedure> |
---|
82 | |
---|
83 | Return the rotation matrix given by the quaternion {{Q}}. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
84 | |
---|
85 | <procedure> (rotate-quaternion Q MATRIX)</procedure> |
---|
86 | |
---|
87 | Rotate {{MATRIX}} by the quaternion {{Q}}. |
---|
88 | |
---|
89 | <procedure> (ypr-rotation YAW PITCH ROLL [RESULT])</procedure> |
---|
90 | |
---|
91 | Return the rotation matrix given by rotating by {{ROLL}} radians around the z-axis followed by {{PITCH}} radians around the x-axis followed by {{YAW}} radians around the y-axis. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
92 | |
---|
93 | <procedure> (rotate-ypr YAW PITCH ROLL MATRIX)</procedure> |
---|
94 | |
---|
95 | Rotate {{MATRIX}} by {{ROLL}} radians around the z-axis followed by {{PITCH}} radians around the x-axis followed by {{YAW}} radians around the y-axis. |
---|
96 | |
---|
97 | <procedure> (2d-scaling SCALE-X SCALE-Y [RESULT])</procedure> |
---|
98 | |
---|
99 | Return the matrix created by scaling the x and y axes by {{SCALE-X}} and {{SCALE-Y}}. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
100 | |
---|
101 | <procedure> (scale-2d SCALE-X SCALE-Y MATRIX)</procedure> |
---|
102 | |
---|
103 | Scale the x and y axis of {{MATRIX}} by {{SCALE-X}} and {{SCALE-Y}}. |
---|
104 | |
---|
105 | <procedure> (3d-scaling SCALE-X SCALE-Y SCALE-Z [RESULT])</procedure> |
---|
106 | |
---|
107 | Return the matrix created by scaling the x, y and z axes by {{SCALE-X}}, {{SCALE-Y}}, and {{SCALE-Z}}. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
108 | |
---|
109 | <procedure> (scale-3d SCALE-X SCALE-Y SCALE-Z MATRIX)</procedure> |
---|
110 | |
---|
111 | Scale the x, y, and z axis of {{MATRIX}} by {{SCALE-X}}, {{SCALE-Y}}, and {{SCALE-Z}}. |
---|
112 | |
---|
113 | <procedure> (scaling SCALE [RESULT])</procedure> |
---|
114 | |
---|
115 | Return the matrix created by scaling the x, y and z axes by {{SCALE}}. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
116 | |
---|
117 | <procedure> (scale SCALE MATRIX)</procedure> |
---|
118 | |
---|
119 | Scale the x, y, and z axis of {{MATRIX}} by {{SCALE}}. |
---|
120 | |
---|
121 | <procedure> (flip-x MATRIX)</procedure> |
---|
122 | |
---|
123 | Flip (mirror) {{MATRIX}} along the x-axis. |
---|
124 | |
---|
125 | <procedure> (flip-y MATRIX)</procedure> |
---|
126 | |
---|
127 | Flip (mirror) {{MATRIX}} along the y-axis. |
---|
128 | |
---|
129 | <procedure> (flip-z MATRIX)</procedure> |
---|
130 | |
---|
131 | Flip (mirror) {{MATRIX}} along the z-axis. |
---|
132 | |
---|
133 | <procedure> (translate-rotate-scale-2d VECTOR ANGLE SCALE [RESULT])</procedure> |
---|
134 | |
---|
135 | Efficiently create a matrix translated by {{VECTOR}}, rotated around the z-axis by {{ANGLE}} radians, then scaled by {{SCALE}}. If the matrix {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
136 | |
---|
137 | <procedure> (transpose MATRIX [RESULT])</procedure> |
---|
138 | |
---|
139 | Transpose {{MATRIX}}. If the matrix {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}}, {{MATRIX}} must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, {{MATRIX}} must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory. |
---|
140 | |
---|
141 | <procedure> (inverse MATRIX [RESULT])</procedure> |
---|
142 | |
---|
143 | Invert {{MATRIX}}. If the matrix {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}}, {{MATRIX}} must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, {{MATRIX}} must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory. |
---|
144 | |
---|
145 | |
---|
146 | ==== Perspective matrices |
---|
147 | <procedure> (ortho WIDTH HEIGHT NEAR FAR [RESULT])</procedure> |
---|
148 | |
---|
149 | Create an orthographic projection matrix. If the matrix {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
150 | |
---|
151 | <procedure> (perspective WIDTH HEIGHT NEAR FAR ANGLE [RESULT])</procedure> |
---|
152 | |
---|
153 | Create an perspective projection matrix. If the matrix {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
154 | |
---|
155 | <procedure> (frustum LEFT RIGHT BOTTOM TOP NEAR FAR [RESULT])</procedure> |
---|
156 | |
---|
157 | Create a view-frustum matrix. If the matrix {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
158 | |
---|
159 | |
---|
160 | ==== Camera functions |
---|
161 | <procedure> (look-at EYE OBJ UP [RESULT])</procedure> |
---|
162 | |
---|
163 | Create a âlook-atâ style camera matrix. The camera is positioned at point {{EYE}}, pointing towards the point {{OBJ}}. {{UP}} defines the cameraâs up vector. If the matrix {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}}, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
164 | |
---|
165 | <procedure> (camera-inverse CAMERA [RESULT])</procedure> |
---|
166 | |
---|
167 | Invert {{CAMERA}} in an efficient fashion. This allows the camera to be constructed in an intuitive fashion by translating and rotating before inverting in order to position the scene properly. This function is far faster than the general {{inverse}} function, but the matrix {{CAMERA}} must only be a matrix representing a translation and a rotation (no scaling). If the matrix {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}}, {{CAMERA}} must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, {{CAMERA}} must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory. |
---|
168 | |
---|
169 | |
---|
170 | ==== Vector operations |
---|
171 | <procedure> (make-point X Y Z [NON-GC?])</procedure> |
---|
172 | <procedure> (point-x POINT)</procedure> |
---|
173 | <procedure> (point-y POINT)</procedure> |
---|
174 | <procedure> (point-z POINT)</procedure> |
---|
175 | <procedure> (point-x-set! POINT)</procedure> |
---|
176 | <procedure> (point-y-set! POINT)</procedure> |
---|
177 | <procedure> (point-z-set! POINT)</procedure> |
---|
178 | |
---|
179 | Vector constructor, getters, and setters. If {{NON-GC}} is {{#t}}, the point is created in a non-garbage-collected area (the memory will still be freed when there are no more references to the vector). |
---|
180 | |
---|
181 | <procedure> (v+ A B [RESULT])</procedure> |
---|
182 | |
---|
183 | Return the result of the addition of vectors {{A}} and {{B}}. If the vector {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
184 | |
---|
185 | <procedure> (v- A B [RESULT])</procedure> |
---|
186 | |
---|
187 | Return the result of the subtraction of vector {{B}} from {{A}}. If the vector {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
188 | |
---|
189 | <procedure> (v* V S [RESULT])</procedure> |
---|
190 | |
---|
191 | Return the result of the multiplication of vector {{A}} with scalar {{S}}. If the vector {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
192 | |
---|
193 | <procedure> (cross-product A B [RESULT])</procedure> |
---|
194 | |
---|
195 | Return the result of the cross product between the vectors {{A}} and {{B}}. If the vector {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
196 | |
---|
197 | <procedure> (dot-product A B)</procedure> |
---|
198 | |
---|
199 | Return the result of the dot product between the vectors {{A}} and {{B}}. |
---|
200 | |
---|
201 | <procedure> (vector-magnitude V)</procedure> |
---|
202 | |
---|
203 | Return the magnitude of vector {{V}}. |
---|
204 | |
---|
205 | <procedure> (normalize! V)</procedure> |
---|
206 | |
---|
207 | Destructively normalize the vector {{V}}. |
---|
208 | |
---|
209 | <procedure> (m*vector! MATRIX VECTOR)</procedure> |
---|
210 | |
---|
211 | Destructively multiply {{VECTOR}} by {{MATRIX}}. |
---|
212 | |
---|
213 | <procedure> (m*vector-array! MATRIX VECTORS stride: [STRIDE] length: [LENGTH])</procedure> |
---|
214 | |
---|
215 | Destructively multiply the array of 3 element floats {{VECTORS}} by {{MATRIX}}. {{VECTORS}} may be given as an f32vector or a pointer. The keyword {{STRIDE}} specifies the number of elements between consecutive vectors, given in number of floats (which must be at least 3) when {{VECTORS}} is an f32vector and in bytes when {{VECTORS}} is a pointer. When {{VECTORS}} is given as a pointer, the keyword {{LENGTH}} must be provided, specifying the number of vectors in {{VECTORS}}. |
---|
216 | |
---|
217 | <procedure> (lerp A B T [RESULT])</procedure> |
---|
218 | |
---|
219 | Linear interpolation between the points {{A}} and {{B}} with the interpolation parameter {{T}} which must be between 0 and 1. If the vector {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
220 | |
---|
221 | |
---|
222 | ==== Quaternion operations |
---|
223 | Quaternions are expected to be normalized before they are used in certain functions ({{quaternion-normalize}} may be used to do so). All the provided functions that create quaternions, create unit quaternions. |
---|
224 | |
---|
225 | The order of quaternion cross-multiplication is the inverse of the âstandardâ order, so a quaternion that has undergone a series or rotations will represent the same rotation as a marix that has gone through the same series, in the same order. |
---|
226 | |
---|
227 | <procedure> (make-quaternion X Y Z W [NON-GC?])</procedure> |
---|
228 | <procedure> (quaternion-x POINT)</procedure> |
---|
229 | <procedure> (quaternion-y POINT)</procedure> |
---|
230 | <procedure> (quaternion-z POINT)</procedure> |
---|
231 | <procedure> (quaternion-w POINT)</procedure> |
---|
232 | <procedure> (quaternion-x-set! POINT)</procedure> |
---|
233 | <procedure> (quaternion-y-set! POINT)</procedure> |
---|
234 | <procedure> (quaternion-z-set! POINT)</procedure> |
---|
235 | <procedure> (quaternion-w-set! POINT)</procedure> |
---|
236 | |
---|
237 | Quaternion constructor, getters, and setters. If {{NON-GC}} is {{#t}}, the quaternion is created in a non-garbage-collected area (the memory will still be freed when there are no more references to the quaternion). |
---|
238 | |
---|
239 | <procedure> (quaternion-normalize! Q)</procedure> |
---|
240 | |
---|
241 | Destructively normalize the quaternion {{Q}}. |
---|
242 | |
---|
243 | <procedure> (quaternion-inverse Q [RESULT])</procedure> |
---|
244 | |
---|
245 | Return the inverse of the unit quaternion {{Q}}. If the quaternion {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
246 | |
---|
247 | <procedure> (quaternion-cross-product A B [RESULT])</procedure> |
---|
248 | |
---|
249 | Return the cross-product of the quaternions {{A}} and {{B}}. If the quaternion {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
250 | |
---|
251 | <procedure> (quaternion-axis-angle-rotation AXIS ANGLE [RESULT])</procedure> |
---|
252 | |
---|
253 | Return the quaternion corresponding to a rotation of {{ANGLE}} radians around the vector {{AXIS}}. If the quaternion {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
254 | |
---|
255 | <procedure> (quaternion-rotate-axis-angle AXIS ANGLE Q)</procedure> |
---|
256 | |
---|
257 | Rotate the quaternion {{Q}} by a rotation of {{ANGLE}} radians around the vector {{AXIS}}. |
---|
258 | |
---|
259 | <procedure> (quaternion-x-rotation ANGLE [RESULT])</procedure> |
---|
260 | |
---|
261 | Return the quaternion corresponding to a rotation of {{ANGLE}} radians around the x-axis. If the quaternion {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
262 | |
---|
263 | <procedure> (quaternion-rotate-x ANGLE Q)</procedure> |
---|
264 | |
---|
265 | Rotate the quaternion {{Q}} by a rotation of {{ANGLE}} radians around the x-axis. |
---|
266 | |
---|
267 | <procedure> (quaternion-y-rotation ANGLE [RESULT])</procedure> |
---|
268 | |
---|
269 | Return the quaternion corresponding to a rotation of {{ANGLE}} radians around the y-axis. If the quaternion {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
270 | |
---|
271 | <procedure> (quaternion-rotate-y ANGLE Q)</procedure> |
---|
272 | |
---|
273 | Rotate the quaternion {{Q}} by a rotation of {{ANGLE}} radians around the y-axis. |
---|
274 | |
---|
275 | <procedure> (quaternion-z-rotation ANGLE [RESULT])</procedure> |
---|
276 | |
---|
277 | Return the quaternion corresponding to a rotation of {{ANGLE}} radians around the z-axis. If the quaternion {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
278 | |
---|
279 | <procedure> (quaternion-rotate-z ANGLE Q)</procedure> |
---|
280 | |
---|
281 | Rotate the quaternion {{Q}} by a rotation of {{ANGLE}} radians around the z-axis. |
---|
282 | |
---|
283 | <procedure> (quaternion-ypr-rotation YAW PITCH ROLL [RESULT])</procedure> |
---|
284 | |
---|
285 | Return the quaternion corresponding to a rotation of {{ROLL}} radians around the z-axis followed by {{PITCH}} radians around the x-axis followed by {{YAW}} radians around the y-axis. If the quaternion {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
286 | |
---|
287 | <procedure> (quaternion-rotate-ypr YAW PITCH ROLL Q)</procedure> |
---|
288 | |
---|
289 | Rotate the quaternion {{Q}} by {{ROLL}} radians around the z-axis followed by {{PITCH}} radians around the x-axis followed by {{YAW}} radians around the y-axis. |
---|
290 | |
---|
291 | <procedure> (quaternion-rotate-point! Q P)</procedure> |
---|
292 | |
---|
293 | Destructively rotate the point {{P}} by the unit quaternion {{Q}}. |
---|
294 | |
---|
295 | <procedure> (slerp A B T [RESULT])</procedure> |
---|
296 | |
---|
297 | Spherical linear interpolation between the quaternions {{A}} and {{B}} with the interpolation parameter {{T}} which must be between 0 and 1. If the quaternion {{RESULT}} is given, it will be modified to contain the result. If {{RESULT}} is {{#t}} the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If {{RESULT}} is not provided, the returned value will be an f32vector located in normal garbage collected memory. |
---|
298 | |
---|
299 | |
---|
300 | ==== Angle operations |
---|
301 | <procedure> (degrees->radians ANGLE)</procedure> |
---|
302 | |
---|
303 | Converts {{ANGLE}} from degrees to radians. |
---|
304 | |
---|
305 | <procedure> (radians->degrees ANGLE)</procedure> |
---|
306 | |
---|
307 | Converts {{ANGLE}} from radians to degrees. |
---|
308 | |
---|
309 | <constant> pi</constant> |
---|
310 | <constant> pi/2</constant> |
---|
311 | |
---|
312 | |
---|
313 | === Example |
---|
314 | <enscript highlight="scheme"> |
---|
315 | (import chicken scheme) |
---|
316 | (use gl-math) |
---|
317 | |
---|
318 | (define projection-matrix |
---|
319 | (perspective 640 480 0.1 100 70)) |
---|
320 | |
---|
321 | (define view-matrix |
---|
322 | (look-at (make-point 1 0 3) |
---|
323 | (make-point 0 0 0) |
---|
324 | (make-point 0 1 0))) |
---|
325 | |
---|
326 | (define model-matrix (mat4-identity)) |
---|
327 | |
---|
328 | (print-mat4 (m* projection-matrix |
---|
329 | (m* view-matrix model-matrix))) |
---|
330 | </enscript> |
---|
331 | |
---|
332 | |
---|
333 | === Version history |
---|
334 | |
---|
335 | ==== Version 0.5.2 |
---|
336 | 10 September 2014 |
---|
337 | |
---|
338 | * {{m*vector-array!}}: Stride is given in bytes when vector is a pointer |
---|
339 | |
---|
340 | '''Version 0.5.0''' |
---|
341 | |
---|
342 | 2 September 2014 |
---|
343 | |
---|
344 | * Many new vector and quaternion functions |
---|
345 | * Functions that previously accepted vectors as individual floats, now accept them as f32vectors |
---|
346 | |
---|
347 | |
---|
348 | ==== Version 0.4.1 |
---|
349 | 30 August 2014 |
---|
350 | |
---|
351 | * Fix {{m*vector-array!}} |
---|
352 | |
---|
353 | '''Version 0.4.0''' |
---|
354 | |
---|
355 | 27 July 2014 |
---|
356 | |
---|
357 | * Add {{copy-mat4}} |
---|
358 | |
---|
359 | |
---|
360 | ==== Version 0.3.2 |
---|
361 | 21 July 2014 |
---|
362 | |
---|
363 | * Allow pointer to array of vectors to be passed to {{m*vector-array!}} |
---|
364 | * Fix error forms |
---|
365 | |
---|
366 | '''Version 0.3.1''' |
---|
367 | |
---|
368 | 23 June 2014 |
---|
369 | |
---|
370 | * Matrix vector multiplication |
---|
371 | |
---|
372 | |
---|
373 | ==== Version 0.2.0 |
---|
374 | 21 June 2014 |
---|
375 | |
---|
376 | * Each transformation function now has two variants: one that initializes a matrix, and one that operates on a matrix |
---|
377 | * Provide {{pi/2}} |
---|
378 | * Provide quaternion and YPR rotation |
---|
379 | * Remove unhelpful composite operations |
---|
380 | * Fix optional arguments for matrix operations |
---|
381 | * Fix a bug in {{look-at}} |
---|
382 | |
---|
383 | |
---|
384 | ==== Version 0.1.0 |
---|
385 | 17 June 2014 |
---|
386 | |
---|
387 | * Initial release |
---|
388 | |
---|
389 | |
---|
390 | === Source repository |
---|
391 | Source available on [[https://github.com/AlexCharlton/gl-math|GitHub]]. |
---|
392 | |
---|
393 | Bug reports and patches welcome! Bugs can be reported via GitHub or to alex.n.charlton at gmail. |
---|
394 | |
---|
395 | |
---|
396 | === Author |
---|
397 | Alex Charlton |
---|
398 | |
---|
399 | |
---|
400 | === Licence |
---|
401 | BSD |
---|