-
Notifications
You must be signed in to change notification settings - Fork 0
/
math3d.ash
378 lines (251 loc) · 12.9 KB
/
math3d.ash
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// new module header
#define MAX_CELL_COUNT 16
enum MatrixType {
eMT_None=0,
eMT_Identity=1,
};
import float Abs(float f);
import String[] Split(this String*, String token);
import int CountToken(this String*, String token);
managed struct Vec3;
managed struct Quat {
/// x of the quaternion (x,y,z,w).
float x;
/// y of the quaternion (x,y,z,w).
float y;
/// z of the quaternion (x,y,z,w).
float z;
/// w of the quaternion (x,y,z,w).
float w;
/// Creates a Quaternion.
import static Quat* Create(float x=0, float y=0, float z=0, float w=0);
/// Returns a string "(x, y, z, w)" for printing purposes.
import readonly attribute String AsString;
import String get_AsString(); // $AUTOCOMPLETEIGNORE$
/// Returns a Vec3 representation of the Quaternion.
import readonly attribute Vec3* AsVec3;
/// Sets manually the values of the quaternion.
import Quat* Set(float x=0, float y=0, float z=0, float w=0);
/// Creates a new quaternion that is a copy of the cloned one.
import Quat* Clone();
/// Returns a new quaternion which is the sum of this quaternion with quaternion q.
import Quat* Add(Quat* q);
/// Returns a new quaternion which is the subtraction of this quaternion with quaternion q.
import Quat* Sub(Quat* q);
/// Returns a new quaternion which is the multiplication of this quaternion with quaternion q.
import Quat* Mul(Quat* q);
/// Returns a new quaternion which is the multiplication of this quaternion by a scalar s.
import Quat* Scale(float s);
/// Returns the Euclidean length of the quaternion.
import readonly attribute float Length;
import float get_Length(); // $AUTOCOMPLETEIGNORE$
/// Returns a normalized copy of this quaternion. Normalize quaternion has length 1 but the same rotation.
import readonly attribute Quat* Normalize;
import Quat* get_Normalize(); // $AUTOCOMPLETEIGNORE$
/// Returns the distance between this quaternion with quaternion q.
import float Distance(Quat* q);
/// Returns the dot multiplication of this quaternion with quaternion q.
import float Dot(Quat* q);
/// Returns the angle between this quaternion and quaternion q in radians.
import float Angle(Quat* q);
/// Linear interpolation by t percent of this quaternion and quaternion q.
import Quat* Lerp(Quat* q, float t);
/// Returns a new quaternion with specified angle, maintaining axis.
import Quat* setAngleAxis(float angle);
/// Returns a new quaternion that is a copy of the current, normalized with x set to angle.
import Quat* getAngleAxis();
};
managed struct Matrix {
/// Don't modify this number directly. Each element of a matrix.
float v[MAX_CELL_COUNT];
/// Don't modify this number directly. The number of rows of a matrix.
int row_count;
/// Don't modify this number directly. The number of columns of a matrix.
int column_count;
/// Don't modify this number directly. The number of elements of a matrix.
int cell_count;
/// Sets the value of a specific row and column (y,x). This function modifyies the matrix you apply it directly.
import Matrix* Set(int row, int column, float value);
/// Returns the value of a specific row and column (similar to y,x position of a cell).
import float Get(int row, int column);
/// Returns a new matrix with defined rows and columns. You can set a value for all elements or make this matrix identity.
import static Matrix* Create(int rows, int columns, MatrixType type=0, float value=0);
/// Returns a new 4x4 identity matrix. Functions preceeded with M44 require a matrix created with this method.
import static Matrix* CreateM44();
/// Reads a matrix as a string for printing purposes.
import readonly attribute String AsString;
import String get_AsString(); // $AUTOCOMPLETEIGNORE$
/// Creates a matrix from a string like "{{0,4},{5,2}}".
import static Matrix* CreateFromString(String s);
/// Returns a new matrix that is a copy of this one.
import Matrix* Clone();
/// Returns true if all elements of the matrix m are equal to this one.
import bool isEqual(Matrix* m);
/// Returns a new matrix that is the sum of this matrix with a matrix m.
import Matrix* Add(Matrix* m);
/// Returns a new matrix that is the subtraction of this matrix with a matrix m.
import Matrix* Sub(Matrix* m);
/// Returns a new matrix that is the multiplication of this matrix with a matrix m.
import Matrix* Mul(Matrix* m);
/// Returns a new matrix that is the multiplication of this matrix with a scalar f.
import Matrix* MulNum(float f);
/// Returns a new matrix that is the division of this matrix with a scalar f.
import Matrix* DivNum(float f);
/// Returns a new matrix that is this matrix multiplied by itself n times. Matrix must be square.
import Matrix* Pow(int n);
/// Returns a the determinant of this matrix.
import float Determinant();
/// Returns the value of the biggest element of the matrix.
import float MaxCell();
/// Returns the value of the smallest element of the matrix.
import float MinCell();
/// Returns a point in homogeneous coordinates transformed. Has to be a 4x4 matrix.
import Quat* M44_DoTransform(float px, float py, float pz, float pw);
/// Returns a point in homogeneous coordinates transformed. Has to be a 4x4 matrix.
import Quat* M44_DoTransformQuat(Quat* q);
/// Returns the inverse of this matrix, as a new matrix. Has to be a 4x4 matrix.
import Matrix* M44_Invert();
/// Returns a identity matrix, as a new matrix. Has to be a 4x4 matrix.
import Matrix* M44_SetIdentity();
/// Returns copy of current matrix with translate set to passed position. Has to be a 4x4 matrix.
import Matrix* M44_SetTranslate(float x, float y, float z);
/// Returns copy current matrix with set scaling. Has to be a 4x4 matrix.
import Matrix* M44_SetScale(float x, float y, float z);
/// Returns copy current matrix with rotate set as passed. Has to be a 4x4 matrix.
import Matrix* M44_SetRotateEuler(float x, float y, float z);
/// Returns copy current matrix with translate, rotate and scale set as passed. Has to be a 4x4 matrix.
import Matrix* M44_SetFullTransform(float x, float y, float z, float sx, float sy, float sz, float rx, float ry, float rz);
/// Returns orthographic projection matrix with passed parameters. Has to be a 4x4 matrix.
import Matrix* M44_SetOrthographicProjection(float left, float right, float bottom, float top, float near, float far);
/// Returns perspective projection matrix with passed parameters. Has to be a 4x4 matrix.
import Matrix* M44_SetPerspectiveProjection(float fovx, float fovy, float near, float far);
};
managed struct Vec3 {
/// x of the triplet (x,y,z).
float x;
/// y of the triplet (x,y,z).
float y;
/// z of the triplet (x,y,z).
float z;
/// Creates a triplet (x,y,z) vector.
import static Vec3* Create(float x=0, float y=0, float z=0);
/// Returns a string "(x, y, z)" for printing purposes.
import readonly attribute String AsString;
import String get_AsString(); // $AUTOCOMPLETEIGNORE$
/// Casts this triplet as a quaternion (w=1).
import readonly attribute Quat* AsQuat;
/// Set the values of the triplet.
import Vec3* Set(float x=0, float y=0, float z=0);
/// Creates a new vector triplet that is a copy of the cloned one.
import Vec3* Clone();
/// Returns a new vector triplet which is the sum of this vector with a vector v.
import Vec3* Add(Vec3* v);
/// Returns a new vector triplet which is the sum of this vector with a quaternion q.
import Vec3* AddQuat(Quat* q);
/// Returns a new vector triplet which is the subtraction of this vector with a vector v.
import Vec3* Sub(Vec3* v);
/// Returns a new vector triplet which is the subtraction of this vector with a quaternion q.
import Vec3* SubQuat(Quat* q);
/// Returns a new vector triplet which is the multiplication of this vector with a vector v.
import Vec3* Mul(Vec3* v);
/// Returns a new vector triplet which is the multiplication of this vector with a quaternion q.
import Vec3* MulQuat(Quat* q);
/// Returns a new vector triplet which is the division of this vector with a vector v.
import Vec3* Div(Vec3* v);
/// Returns a new vector triplet which is the division of this vector with a quaternion q.
import Vec3* DivQuat(Quat* q);
/// Returns a new quaternion which is the multiplication of this quaternion by a scalar s.
import Vec3* Scale(float s);
/// Returns the length (distance to origin) of this vector.
import readonly attribute float Length;
import float get_Length(); // $AUTOCOMPLETEIGNORE$
/// Returns a copy of this vector, normalized.
import readonly attribute Vec3* Normalize;
import Vec3* get_Normalize(); // $AUTOCOMPLETEIGNORE$
/// Returns the distance between this vector and a vector v.
import float Distance(Vec3* v);
/// Returns the distance between this vector and a quaternion q.
import float DistanceQuat(Quat* q);
/// Returns the dot multiplication of this vector and a vector v.
import float Dot(Vec3* v);
/// Returns the dot multiplication of this vector and a quaternion q.
import float DotQuat(Quat* v);
/// Returns the angle between this vector and a vector v.
import float Angle(Vec3* v);
/// Returns the angle between this vector and a quaternion q.
import float AngleQuat(Quat* q);
/// Returns a vector which is the cross multiplication of this vector with a vector v.
import Vec3* Cross(Vec3* v);
/// Returns a vector which is the cross multiplication of this vector with a quaternion q.
import Vec3* CrossQuat(Quat* q);
/// Returns a new vector which is the interpolation of percent t between this vector and a vector v.
import Vec3* Lerp(Vec3* v, float t);
/// Returns a new vector that is equivalent of the projection of this vector on a vector v.
import Vec3* Project(Vec3* v);
/// Returns a new vector which is the rotate vector of this by a quaternion q.
import Vec3* Rotate(Quat* q);
};
/// Casts a quaternion to a Vec3. You almost never wants to do this.
import Vec3* get_AsVec3(this Quat*);
/// Casts a Vec3 to quaternion. You almost never wants to do this.
import Quat* get_AsQuat(this Vec3*);
/// Sums a Vec3 from this quaternion. You almost never wants to do this.
import Quat* AddVec3(this Quat*, Vec3* v);
/// Subtracts a Vec3 from this quaternion. You almost never wants to do this.
import Quat* SubVec3(this Quat*, Vec3* v);
/// Multiplies this quaternion by a Vec3. You almost never wants to do this.
import Quat* MulVec3(this Quat*, Vec3* v);
/// Returns distance from this quaternion to a Vec3. You almost never wants to do this.
import float DistanceVec3(this Quat*, Vec3* v);
/// Returns distance the dot product between this quaternion and a Vec3. You almost never wants to do this.
import float DotVec3(this Quat*, Vec3* v);
/// Returns distance the angle between this quaternion and a Vec3. You almost never wants to do this.
import float AngleVec3(this Quat*, Vec3* v);
/// Returns interpolation between this quaternion and a Vec3. You almost never wants to do this.
import Quat* LerpVec3(this Quat*, Vec3* v, float t);
managed struct ScreenPoint{
int x;
int y;
float z;
float w;
bool is_visible;
};
struct Transform3D {
int X;
int Y;
int Width;
int Height;
/// The height to section the top of the frustum geometric shape. Default is 1.0.
float frustrum_near;
/// The height to section the base of the frustum geometric shape. Default is 1000.0.
float frustrum_far;
/// The surface size height. Usually matches final viewport height.
float surfsize_h;
/// The surface size width. Usually matches final viewport width.
float surfsize_w;
/// The normalized device coordinates height. Default is 2.0.
float ndcSize_h;
/// The normalized device coordinates width. Default is 2.0.
float ndcSize_w;
/// The camera to world coordinates tranformation matrix.
Matrix* CamToWorld;
/// The world to camera coordinates tranformation matrix.
Matrix* WorldToCam;
/// The projection matrix.
Matrix* ProjectMtx;
/// Initialize a Transform3D object with default values. Call this after istanciating.
import void Init();
/// Sets the position of the viewport on screen.
import void SetPosition( int x, int y, int width, int height);
/// Configures the camera transform, positioning the camera, scaling and rotating it.
import void SetCameraTransform( Vec3* cam_pos, Vec3* cam_scale, Vec3* cam_rot);
/// Sets the projection matrix for orthogonal projection.
import void SetOrthoProjection(float width, float height, float near, float far);
/// Sets the projection matrix for perspective projection. Field of View is in angle and specifies the frustum.
import void SetPerspectiveProjection(float fov, float near, float far);
/// Sets the surface size for the view used. This should match the viewport to avoid stretching the resulting image.
import void SetSurfaceSize(float width, float height);
/// Returns a world point converted to screen coordinates.
import ScreenPoint* WorldToScreen(float x, float y, float z);
//ScreenToWorld(x, y, z, result);
};