-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetTypes.h
605 lines (476 loc) · 10.6 KB
/
AssetTypes.h
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
enum EntityType // warning gets used in files
{
Entity_None,
Entity_Dude,
Entity_Wall,
Entity_Bit,
Entity_Count,
};
enum EntityFlags
{
EntityFlag_SupportsUnit = 0x1,
EntityFlag_BlocksUnit = 0x2,
EntityFlag_Interactable = 0x4,
EntityFlag_IsDynamic = 0x200,
// dynamic flags
EntityFlag_IsMoving = 0x20,
EntityFlag_InTree = 0x40,
EntityFlag_FrameResetFlag = EntityFlag_IsMoving,
};
struct Entity
{
// general entity data header if you will
EntityType type;
u32 serial;
// render stuff
u32 meshId;
f32 scale;
Quaternion orientation;
v4 color;
v4 frameColor;
v3 visualPos;
// simulation stuff, also needed for render
v3i physicalPos;
v3i initialPos;
u64 flags;
};
typedef Entity* EntityPtr;
DefineDynamicArray(Entity);
DefineDynamicArray(EntityPtr);
DefineArray(Entity);
DefineArray(EntityPtr);
DefineDFArray(EntityPtr);
struct EntityData
{
Quaternion orientation;
v3i physicalPos;
v3 offset;
v4 color;
f32 scale;
EntityType type;
u64 flags; // this is kinda useless
u32 meshId;
};
static EntityData EntityToData(Entity e)
{
EntityData ret;
ret.color = e.color;
ret.flags = e.flags;
ret.orientation = e.orientation;
ret.meshId = e.meshId;
ret.scale = e.scale;
ret.type = e.type;
ret.physicalPos = e.physicalPos;
ret.offset = e.visualPos - V3(e.physicalPos);
return ret;
}
static void ApplyEntityDataToEntity(Entity *e, EntityData *data)
{
e->orientation = data->orientation;
e->physicalPos = data->physicalPos;
e->initialPos = data->physicalPos;
e->visualPos = V3(data->physicalPos) + data->offset;
e->color = data->color;
e->scale = data->scale;
e->type = data->type;
e->flags = data->flags;
e->meshId = data->meshId;
}
DefineArray(EntityData);
DefineDynamicArray(EntityData);
struct LightSource
{
v3 pos;
Quaternion orientation;
v3 color;
};
// todo right now I do not see a reason why this should be an asset, so where should it live?
struct Level
{
String name;
Camera camera;
LightSource lightSource;
EntityDataArray entities;
u32 blocksNeeded;
//u32 amountOfDudes; right now not used.
};
struct AABBi
{
v3i minDim;
v3i maxDim;
};
static AABBi CreateAABBi(v3i minDim, v3i maxDim)
{
AABBi ret;
ret.minDim = minDim;
ret.maxDim = maxDim;
return ret;
}
static b32 PointInAABBi(AABBi aabb, v3i point)
{
return
(
aabb.minDim.x < point.x &&
aabb.minDim.y < point.y &&
aabb.minDim.z < point.z &&
aabb.maxDim.x >= point.x &&
aabb.maxDim.y >= point.y &&
aabb.maxDim.z >= point.z
);
}
struct TextureIndex
{
u32 index;
};
static TextureIndex CreateTextureIndex(u32 index)
{
return {index};
}
struct Bitmap
{
u32 *pixels;
u32 width;
u32 height;
};
static void UpdateWrapingTexture(TextureIndex textureIndex, u32 width, u32 height, u32 *pixels);
static u32 *GetPixel(Bitmap bitmap, u32 x, u32 y)
{
Assert(x < bitmap.width && y < bitmap.height);
u32 shift = y * bitmap.width + x;
u32 *pixP = bitmap.pixels + shift;
return pixP;
}
struct CharInfo
{
v2 minUV;
v2 maxUV;
f32 xAdvance;
u32 width;
u32 height;
f32 xOff;
f32 yOff;
};
typedef CharInfo CharData;
DefineArray(CharInfo);
struct Font
{
void *mem;
u32 textureId;
CharInfoArray charData;
f32 charHeight;
};
static Font globalFont;
static f32 GetActualStringLength(String string, f32 fontSize, Font font)
{
f32 ret = 0.0f;
float fScale = fontSize / (f32)font.charHeight;
For(string)
{
if (*it < font.charData.amount)
{
CharData data = font.charData[*it];
//f32 scaledWidth = fScale * (f32)data.width;
float actualFloatWidth = data.xAdvance * fScale;
ret += actualFloatWidth;
}
else
{
Assert(!"Not handled font symbol");
}
}
return ret;
}
struct Material
{
f32 specularExponent; // Ns
v3 ka; // ambientColor
v3 kd; // diffuseColor
v3 ks; // specularColor
v3 ke; // emmissionColor
f32 indexOfRefraction; // Ni ("optical density") 0.001 - 10
f32 dissolved; // d 0 - 1
//v3 transmissionFilter; // Tf
u32 illuminationModel; // illum 0 - 10
u32 bitmapID;
String name;
String texturePath;
};
DefineDFArray(Material);
DefineDynamicArray(Material);
struct IndexSet
{
u32 amount;
u32 offset;
Material mat;
};
DefineArray(IndexSet);
DefineDynamicArray(IndexSet);
struct WeightData
{
f32 weight;
u32 boneIndex;
};
DefineArray(WeightData);
DefineArray(WeightDataArray);
// todo where should these sort of mathy types live?
struct AABB
{
v3 minDim;
v3 maxDim;
};
static AABB CreateAABB(v3 minDim, v3 maxDim)
{
// todo assert if we have volume?
return { minDim, maxDim };
}
static AABB InvertedInfinityAABB()
{
AABB ret;
ret.maxDim = V3(MINF32, MINF32, MINF32);
ret.minDim = V3(MAXF32, MAXF32, MAXF32);
return ret;
}
static b32 PointInAABB(AABB target, v3 point)
{
return
(
target.minDim.x <= point.x + 0.001f &&
target.minDim.y <= point.y + 0.001f &&
target.minDim.z <= point.z + 0.001f &&
target.maxDim.x + 0.001f >= point.x &&
target.maxDim.y + 0.001f >= point.y &&
target.maxDim.z + 0.001f >= point.z
);
}
inline bool v3InAABB(v3 pos, AABB aabb)
{
return
aabb.minDim.x <= pos.x &&
aabb.minDim.y <= pos.y &&
aabb.minDim.z <= pos.z &&
aabb.maxDim.x >= pos.x &&
aabb.maxDim.y >= pos.y &&
aabb.maxDim.z >= pos.z;
}
struct InterpolationData // naming?
{
v3 translation;
Quaternion orientation;
v3 scale;
};
DefineArray(InterpolationData);
static InterpolationData operator*(InterpolationData a, InterpolationData b)
{
m3x3 mat = QuaternionToMatrix3(a.orientation);
InterpolationData ret;
v3 gScale = a.scale * (mat* b.scale);
ret.scale = V3(Abs(gScale.x), Abs(gScale.y), Abs(gScale.z));
ret.translation = a.translation + a.scale * (mat * b.translation);
ret.orientation = a.orientation * b.orientation;
return ret;
}
// inverse almost as fast
static m4x4 InterpolationDataToMatrix(v3 translation, Quaternion orientation, f32 scale)
{
m4x4 ret;
ret = QuaternionToMatrix4(orientation);
ret = Translate(ret, translation);
ret.a[0][0] *= scale;
ret.a[0][1] *= scale;
ret.a[0][2] *= scale;
ret.a[1][0] *= scale;
ret.a[1][1] *= scale;
ret.a[1][2] *= scale;
ret.a[2][0] *= scale;
ret.a[2][1] *= scale;
ret.a[2][2] *= scale;
return ret;
}
static InterpolationData MatrixToInterpolationData(m4x4 mat)
{
// copied from (XForm states : GetFromMatrix in jBlow: Animation Playback part 2 37 min.), slightly altered
InterpolationData ret;
v3 mx = GetRow(mat, 0);
v3 my = GetRow(mat, 1);
v3 mz = GetRow(mat, 2);
f32 normx = Norm(mx);
f32 normy = Norm(my);
f32 normz = Norm(mz);
ret.scale = V3(normx, normy, normz);
ret.translation = GetColumn(mat, 3);
mx /= normx;
my /= normy;
mz /= normz;
f32 coef[3][3];
coef[0][0] = mx.x;
coef[0][1] = mx.y;
coef[0][2] = mx.z;
coef[1][0] = my.x;
coef[1][1] = my.y;
coef[1][2] = my.z;
coef[2][0] = mz.x;
coef[2][1] = mz.y;
coef[2][2] = mz.z;
f32 trace = coef[0][0] + coef[1][1] + coef[2][2];
Quaternion q;
if (trace > 0.0f)
{
// |w| > 1/2
f32 s = Sqrt(trace + 1.0f); // 2w
q.w = s * 0.5f;
s = 0.5f / s; // 1/(4f)
q.x = (coef[2][1] - coef[1][2]) * s;
q.y = (coef[0][2] - coef[2][0]) * s;
q.z = (coef[1][0] - coef[0][1]) * s;
}
else
{
// |w| <= 1/2
i32 i = 0;
if (coef[1][1] > coef[0][0]) i = 1;
if (coef[2][2] > coef[i][i]) i = 2;
i32 j = (1 << i) & 3; // i+1 mod 3.
i32 k = (1 << j) & 3; // j+1 mod 3.
f32 s = Sqrt(coef[i][i] - coef[j][j] - coef[k][k] + 1.0f);
q.component[i] = s * 0.5f;
s = 0.5f / s;
q.component[j] = (coef[i][j] + coef[j][i]) * s;
q.component[k] = (coef[k][i] + coef[i][k]) * s;
q.w = (coef[k][j] - coef[j][k]) * s;
}
ret.orientation = q;
f32 norm = Norm(q);
Assert(0.99f < norm && norm < 1.01f);
return ret;
}
static m4x4 InterpolationDataToMatrix(InterpolationData a)
{
m4x4 ret;
ret = QuaternionToMatrix4(a.orientation);
ret = Translate(ret, a.translation);
ret.a[0][0] *= a.scale.x;
ret.a[0][1] *= a.scale.x;
ret.a[0][2] *= a.scale.x;
ret.a[1][0] *= a.scale.y;
ret.a[1][1] *= a.scale.y;
ret.a[1][2] *= a.scale.y;
ret.a[2][0] *= a.scale.z;
ret.a[2][1] *= a.scale.z;
ret.a[2][2] *= a.scale.z;
return ret;
}
struct KeyFrame
{
f32 t;
InterpolationDataArray boneStates; // something
};
DefineArray(KeyFrame);
// mesh name right now is on the 2nd node of the visual scene, which we otherwise do not use.
struct KeyFramedAnimation
{
String meshName;
String animationName;
KeyFrameArray keyFrames;
f32 length;
};
DefineDynamicArray(KeyFramedAnimation);
struct Bone
{
String name;
u32 parentIndex;
m4x4 inverseBindShapeMatrix;
InterpolationData interp; // do I need this? yes for IK
};
DefineArray(Bone);
struct Skeleton
{
u16Array vertexMap; // from flattend to not flattend
v3Array vertices; // not flattend
WeightDataArrayArray vertexToWeightsMap; // from not flattend
BoneArray bones;
// m4x4 bindShapeMatrix; // this should be premultiplied into the bindshape matricies above? and is now.
};
enum TriangleMeshType
{
TriangleMeshType_List,
TrianlgeMeshType_Strip,
};
enum VertexFormatType
{
VertexFormat_PC,
VertexFormat_PCU,
VertexFormat_PCUN,
VertexFormat_PCUNBD,
};
// NOTE WARNING The identifiers have to be unique, st this dumb hack works
// this is used to simplify the OffsetOf situation for setting up Attrib arrays
struct VertexFormatTemplate
{
v3 p;
u32 c;
v2 uv;
union
{
v3 n;
u16 textureIndex;
};
v4i bi;
v4 bw;
};
struct VertexFormatPC
{
v3 p;
u32 c;
};
struct VertexFormatPCU
{
v3 p;
u32 c;
v2 uv;
};
DefineArray(VertexFormatPC);
struct VertexFormatPCUI
{
v3 p;
u32 c;
v2 uv;
u16 textureIndex;
};
struct VertexFormatPCUN
{
v3 p;
u32 c;
v2 uv;
v3 n;
};
DefineArray(VertexFormatPCUN);
struct VertexFormatPCUNBD
{
v3 p;
u32 c;
v2 uv;
v3 n;
v4i bi;
v4 bw;
};
struct TriangleMesh
{
String name;
u32 type;
// these are allready linearized, but we pack em in RegisterTriangleMesh
v3Array positions;
u32Array colors;
v2Array uvs;
v3Array normals;
u16Array indices;
IndexSetArray indexSets;
Skeleton skeleton;
AABB aabb;
// OpenGL draw stuff
u32 vertexFormatSize;
u32 vertexVBO; // to init this call glBufferData and glBindData
u32 indexVBO;
};
DefineArray(TriangleMesh);
DefineDynamicArray(TriangleMesh);
static void RegisterTriangleMesh(TriangleMesh *mesh);