-
Notifications
You must be signed in to change notification settings - Fork 0
/
raykit.kit
497 lines (398 loc) · 13.5 KB
/
raykit.kit
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
include "raylib.h";
include "raymath.h";
enum RLightType {
LightDirectional;
LightPoint;
}
struct RLight {
static var lightsCount: Int = 0;
static var maxLights: Int = 4;
var enabled: Bool;
var type: RLightType;
var position: RVector3;
var target: RVector3;
var color: RColor;
var shader: RShader;
var enabledLoc: Int;
var typeLoc: Int;
var posLoc: Int;
var targetLoc: Int;
var colorLoc: Int;
//Light CreateLight(int type, Vector3 pos, Vector3 targ, Color color, Shader shader);
public static function new(type: RLightType, position: RVector3, target: RVector3, color: RColor, shader: RShader): Option[RLight] {
if Self.lightsCount > 4 {
return None;
}
var light = struct RLight {
enabled: true,
type,
position,
target,
color,
shader
};
var lightCount: Int = Self.lightsCount;
var shaderSpot: Ptr[Char] = mallocator.alloc((sizeof Char) * 32);
shaderSpot = sprintf(shaderSpot, "lights[%d].enabled", Self.lightsCount);
light.enabledLoc = shader.getLocation(shaderSpot);
shaderSpot = sprintf(shaderSpot, "lights[%d].type", Self.lightsCount);
light.typeLoc = shader.getLocation(shaderSpot);
shaderSpot = sprintf(shaderSpot, "lights[%d].position", Self.lightsCount);
light.posLoc = shader.getLocation(shaderSpot);
shaderSpot = sprintf(shaderSpot, "lights[%d].target", Self.lightsCount);
light.targetLoc = shader.getLocation(shaderSpot);
shaderSpot = sprintf(shaderSpot, "lights[%d].color", Self.lightsCount);
light.colorLoc = shader.getLocation(shaderSpot);
mallocator.free(shaderSpot);
light.update();
Self.lightsCount++;
return Some(light);
}
public function update() {
this.shader.setValue(this.enabledLoc, &this.enabled, ${UNIFORM_INT: Int});
this.shader.setValue(this.typeLoc, &this.type, ${UNIFORM_INT: Int});
this.shader.setValue(this.posLoc, [this.position.x, this.position.y, this.position.z], ${UNIFORM_VEC3: Int});
this.shader.setValue(this.targetLoc, [this.target.x, this.target.y, this.target.z], ${UNIFORM_VEC3: Int});
//TODO: Refactor this into a function
var color = [(this.color.r / 255), (this.color.g / 255), (this.color.b / 255), (this.color.a / 255)];
this.shader.setValue(this.colorLoc, color, ${UNIFORM_VEC4: Int});
}
}
#[promote] abstract RShader: Shader {
public static function new(vsName: CString, fsName: CString): RShader {
return LoadShader(vsName, fsName);
}
public function getLocation(name: CString): Int {
return GetShaderLocation(this, name);
}
public function setValue(uniformLoc: Int, value: Ptr[Void], uniformType: Int) {
SetShaderValue(this, uniformLoc, value, uniformType);
}
public function delete() {
UnloadShader(this);
}
}
#[promote]
abstract RTransform: Transform {
public static function new(): RTransform {
return struct Transform {
translation: RVector3.zero(),
rotation: RQuaternion.new(),
scale: RVector3.zero()
} as RTransform;
}
}
#[promote]
abstract RMatrix: Matrix {
public static function new(): RMatrix {
return MatrixIdentity();
}
public function getForward(): RVector3 {
return RVector3.new(this.m8, this.m9, this.m10);
}
public function setForward(vector: RVector3): RVector3 {
this.m8 = vector.x;
this.m9 = vector.y;
this.m10 = vector.z;
}
public function getUp(): RVector3 {
return RVector3.new(this.m4, this.m5, this.m6);
}
public function setUp(vector: RVector3): Void {
this.m4 = vector.x;
this.m5 = vector.y;
this.m6 = vector.z;
}
rules{
($this.up) => $this.getUp();
($this.up = $vector) => $this.setUp($vector);
($this.forward) => $this.getForward();
($this.forward = $vector) => $this.setForward($vector);
}
}
#[promote]
abstract RQuaternion: Quaternion {
public static function new(): RQuaternion {
return QuaternionIdentity();
}
public static function fromMatrix(matrix: RMatrix): RQuaternion {
return QuaternionFromMatrix(matrix);
}
}
#[promote]
abstract RModel: Model {
public static function new(modelLoc: CString): RModel {
return LoadModel(modelLoc) as RModel;
}
public static function fromMesh(mesh: Mesh): RModel {
return LoadModelFromMesh(mesh);
}
public function delete(){
UnloadModel(this);
}
}
#[promote]
abstract RMaterial: Material {
//Albedo
public function setAlbedoTexture(texture: RTexture) {
this.maps[${MAP_ALBEDO: Int}].texture = texture;
}
public function setAlbedoColor(color: RColor) {
this.maps[${MAP_ALBEDO: Int}].color = color;
}
public function setAlbedoValue(value: Float) {
this.maps[${MAP_ALBEDO: Int}].value = value;
}
//Normal
public function setNormalTexture(texture: RTexture) {
this.maps[${MAP_NORMAL: Int}].texture = texture;
}
public function setNormalColor(color: RColor) {
this.maps[${MAP_NORMAL: Int}].color = color;
}
public function setNormalValue(value: Float) {
this.maps[${MAP_NORMAL: Int}].value = value;
}
//Metalness
public function setMetalnessTexture(texture: RTexture) {
this.maps[${MAP_METALNESS: Int}].texture = texture;
}
public function setMetalnessColor(color: RColor) {
this.maps[${MAP_METALNESS: Int}].color = color;
}
public function setMetalnessValue(value: Float) {
this.maps[${MAP_METALNESS: Int}].value = value;
}
//Roughness
public function setRoughnessTexture(texture: RTexture) {
this.maps[${MAP_ROUGHNESS: Int}].texture = texture;
}
public function setRoughnessColor(color: RColor) {
this.maps[${MAP_ROUGHNESS: Int}].color = color;
}
public function setRoughnessValue(value: Float) {
this.maps[${MAP_ROUGHNESS: Int}].value = value;
}
//Occlusion
public function setOcclusionTexture(texture: RTexture) {
this.maps[${MAP_OCCLUSION: Int}].texture = texture;
}
public function setOcclusionColor(color: RColor) {
this.maps[${MAP_OCCLUSION: Int}].color = color;
}
public function setOcclusionValue(value: Float) {
this.maps[${MAP_OCCLUSION: Int}].value = value;
}
//Emission
public function setEmissionTexture(texture: RTexture) {
this.maps[${MAP_EMISSION: Int}].texture = texture;
}
public function setEmissionColor(color: RColor) {
this.maps[${MAP_EMISSION: Int}].color = color;
}
public function setEmissionValue(value: Float) {
this.maps[${MAP_EMISSION: Int}].value = value;
}
//Height
public function setHeightTexture(texture: RTexture) {
this.maps[${MAP_HEIGHT: Int}].texture = texture;
}
public function setHeightColor(color: RColor) {
this.maps[${MAP_HEIGHT: Int}].color = color;
}
public function setHeightValue(value: Float) {
this.maps[${MAP_HEIGHT: Int}].value = value;
}
//Irradiance
public function setIrradianceTexture(texture: RTexture) {
this.maps[${MAP_IRRADIANCE: Int}].texture = texture;
}
public function setIrradianceColor(color: RColor) {
this.maps[${MAP_IRRADIANCE: Int}].color = color;
}
public function setIrradianceValue(value: Float) {
this.maps[${MAP_IRRADIANCE: Int}].value = value;
}
//Prefilter
public function setPrefilterTexture(texture: RTexture) {
this.maps[${MAP_PREFILTER: Int}].texture = texture;
}
public function setPrefilterColor(color: RColor) {
this.maps[${MAP_PREFILTER: Int}].color = color;
}
public function setPrefilterValue(value: Float) {
this.maps[${MAP_PREFILTER: Int}].value = value;
}
//Brdf
public function setBrdfTexture(texture: RTexture) {
this.maps[${MAP_BRDF: Int}].texture = texture;
}
public function setBrdfColor(color: RColor) {
this.maps[${MAP_BRDF: Int}].color = color;
}
public function setBrdfValue(value: Float) {
this.maps[${MAP_BRDF: Int}].value = value;
}
}
#[promote]
abstract RTexture: Texture {
public static function new(textureLoc: CString): RTexture {
return LoadTexture(textureLoc);
}
public function setFilter(filter: Int) {
SetTextureFilter(this, filter);
}
public function delete(){
UnloadTexture(this);
}
}
#[promote]
abstract RCamera3D: Camera3D {
public static function new(position: Vector3, target: Vector3, up: Vector3, fovy: Float = 45.0, type: Int = 0): RCamera3D{
return struct RCamera3D {
position: position,
target: target,
up: up,
fovy: fovy,
type: type
};
}
public function setCameraMode(type: Int = 0) {
SetCameraMode(this, type);
}
public function updateCamera() {
UpdateCamera(this);
}
public function forward(): RVector3 {
return RVector3.fromVector3(Vector3Subtract(this.position, this.target));
}
public function right(): RVector3 {
return RVector3.fromVector3(Vector3CrossProduct(this.up, this.forward()));
}
}
#[promote]
abstract RVector2: Vector2 {
public static function new(n1: Float, n2: Float): RVector2{
return struct RVector2 {
x: n1,
y: n2
};
}
public static function zero(): RVector2 {
return Self.new(0, 0);
}
public static function fromVector2(from: Vector2){
return RVector2.new(from.x, from.y);
}
public function add(other: Vector2){
this.x = this.x + other.x;
this.y = this.y + other.y;
}
public function subtract(other: Vector2){
this.x = this.x - other.x;
this.y = this.y - other.y;
}
public function print(){
printf("Vector2 - (%f, %f)\n", this.x, this.y);
}
rules {
($this += $other) => $this.add($other);
($this -= $other) => $this.subtract($other);
($this + $other) => Vector2Add($this, $other);
($this - $other) => Vector2Subtract($this, $other);
}
}
#[promote]
abstract RVector3: Vector3 {
public static function new(n1: Float, n2: Float, n3: Float): RVector3 {
return struct RVector3 {
x: n1,
y: n2,
z: n3
};
}
public static function fromVector3(other: Vector3):RVector3 {
return RVector3.new(other.x, other.y, other.z);
}
public static function zero(): RVector3 {
return RVector3.new(0,0,0);
}
public static function one(): RVector3 {
return RVector3.new(1.0,1.0,1.0);
}
public function copy(): RVector3 {
return struct Self { x: this.x, y: this.y, z: this.z };
}
public function set(newVector: RVector3){
this.x = newVector.x;
this.y = newVector.y;
this.z = newVector.z;
}
public function add(other: RVector3): RVector3{
this.set(Vector3Add(this, other));
return this;
}
public function subtract(other: RVector3): RVector3{
this.set(Vector3Subtract(this, other));
return this;
}
public function crossProduct(other: RVector3): RVector3{
this.set(Vector3CrossProduct(this, other));
return this;
}
public function multiply(scalar: Float): RVector3{
this.set(Vector3Multiply(this, scalar));
return this;
}
public function multiplyVector(other: RVector3): RVector3{
this.set(Vector3MultiplyV(this, other));
return this;
}
public function distance(other: RVector3): Float{
return Vector3Distance(this, other);
}
public function normalize(): RVector3 {
this.set(Vector3Normalize(this));
return this;
}
public function negate(): RVector3 {
this.set(Vector3Negate(this));
return this;
}
public function print(){
printf("Vector3 - (%f, %f, %f)\n", this.x, this.y, this.z);
}
rules {
($this + $other) => Vector3Add($this, $other);
($this - $other) => ($this.copy().subtract($other));
($this += $other) => $this.add($other);
($this -= $other) => $this.subtract($other);
}
}
#[promote]
abstract RColor: Color {
public static function new(red: Uint8 = 0, green: Uint8 = 0, blue: Uint8 = 0, alpha: Uint8 = 1) : RColor {
return struct RColor {
r: red,
g: green,
b: blue,
a: alpha
};
}
public function toString(): Ptr[Char] {
var colorString: Ptr[Char] = malloc((sizeof Char) * 30);
sprintf(colorString, "Color: %d, %d, %d, %d\n", this.r, this.g, this.b, this.a);
return colorString;
}
public static const lightGray = Self.new(200, 200, 200, 255);
public static const gray = Self.new(130, 130, 130, 255);
public static const darkGray = Self.new(80, 80, 80, 255);
public static const yellow = Self.new(253, 249, 0, 255);
public static const gold = Self.new(255, 203, 0, 255);
public static const orange = Self.new();
public static const blue = Self.new(0, 121, 241, 255);
public static const lime = Self.new(0, 158, 47, 255);
public static const maroon = Self.new(190, 33, 55, 255);
public static const white = Self.new(255, 255, 255, 255);
public static const black = Self.new(0, 0, 0, 255);
}