Added camera
Moved some math types Added some more vector types Did the camera matrix calulations Updated shaders to take push constants
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
#include "impl/arena.c"
|
||||
#include "impl/string.c"
|
||||
#include "impl/math.c"
|
||||
|
||||
@@ -6,5 +6,6 @@
|
||||
#include "macros.h"
|
||||
#include "arena.h"
|
||||
#include "string.h"
|
||||
#include "math.h"
|
||||
|
||||
#endif // LD_CORE_CORE_H_
|
||||
|
||||
143
code/core/impl/math.c
Normal file
143
code/core/impl/math.c
Normal file
@@ -0,0 +1,143 @@
|
||||
V3f V3f_Neg(V3f x) {
|
||||
V3f result = { -x.x, -x.y, -x.z };
|
||||
return result;
|
||||
}
|
||||
|
||||
V3f V3f_Scale(V3f x, F32 s) {
|
||||
V3f result = { s * x.x, s * x.y, s * x.z };
|
||||
return result;
|
||||
}
|
||||
|
||||
F32 V3f_Dot(V3f a, V3f b) {
|
||||
F32 result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
|
||||
return result;
|
||||
}
|
||||
|
||||
F32 V4f_Dot(V4f a, V4f b) {
|
||||
F32 result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w);
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4F M4x4F_Rows(V3f x, V3f y, V3f z) {
|
||||
Mat4x4F result = {
|
||||
x.x, x.y, x.z, 0,
|
||||
y.x, y.y, y.z, 0,
|
||||
z.x, z.y, z.z, 0,
|
||||
0, 0, 0, 1
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4F M4x4F_Columns(V3f x, V3f y, V3f z) {
|
||||
Mat4x4F result = {
|
||||
x.x, y.x, z.x, 0,
|
||||
x.y, y.y, z.y, 0,
|
||||
x.z, y.z, z.z, 0,
|
||||
0, 0, 0, 1
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4F M4x4F_Mul(Mat4x4F a, Mat4x4F b) {
|
||||
Mat4x4F result;
|
||||
|
||||
for (U32 r = 0; r < 4; ++r) {
|
||||
for (U32 c = 0; c < 4; ++c) {
|
||||
result.m[r][c] =
|
||||
(a.m[r][0] * b.m[0][c]) + (a.m[r][1] * b.m[1][c]) +
|
||||
(a.m[r][2] * b.m[2][c]) + (a.m[r][3] * b.m[3][c]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
V4f M4x4F_VMul4(Mat4x4F m, V4f v) {
|
||||
V4f result;
|
||||
result.x = V4f_Dot(m.r[0], v);
|
||||
result.y = V4f_Dot(m.r[1], v);
|
||||
result.z = V4f_Dot(m.r[2], v);
|
||||
result.w = V4f_Dot(m.r[3], v);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
V3f M4x4F_VMul3(Mat4x4F m, V3f v) {
|
||||
V4f tx;
|
||||
tx.xyz = v;
|
||||
tx.w = 1.0f;
|
||||
|
||||
V3f result = M4x4F_VMul4(m, tx).xyz;
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4FInv M4x4F_Perspective(F32 fov, F32 aspect, F32 nearp, F32 farp) {
|
||||
F32 focal_length = 1.0f / tanf(0.5f * (PI_F32 * (fov / 360.0f)));
|
||||
|
||||
F32 a = focal_length;
|
||||
F32 b = focal_length * aspect;
|
||||
|
||||
F32 c = -(nearp + farp) / (farp - nearp);
|
||||
F32 d = -(2.0f * nearp * farp) / (farp - nearp);
|
||||
|
||||
Mat4x4FInv result = {
|
||||
// fwd
|
||||
{
|
||||
a, 0, 0, 0,
|
||||
0, b, 0, 0,
|
||||
0, 0, c, d,
|
||||
0, 0, -1, 0
|
||||
},
|
||||
// inv
|
||||
{
|
||||
(1 / a), 0, 0, 0,
|
||||
0, (1 / b), 0, 0,
|
||||
0, 0, 0, -1,
|
||||
0, 0, (1/ d), (c / d)
|
||||
}
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4FInv M4x4F_CameraView(V3f x, V3f y, V3f z, V3f p) {
|
||||
Mat4x4FInv result;
|
||||
|
||||
// Construct orthonomal basis from axes
|
||||
//
|
||||
result.fwd = M4x4F_Rows(x, y, z);
|
||||
V3f txp = V3f_Neg(M4x4F_VMul3(result.fwd, p));
|
||||
|
||||
// Translate by txp
|
||||
//
|
||||
result.fwd.r[0].w += txp.x;
|
||||
result.fwd.r[1].w += txp.y;
|
||||
result.fwd.r[2].w += txp.z;
|
||||
|
||||
// Calculate inverse axes
|
||||
//
|
||||
V3f ix = V3f_Scale(x, 1.0f / V3f_Dot(x, x));
|
||||
V3f iy = V3f_Scale(y, 1.0f / V3f_Dot(y, y));
|
||||
V3f iz = V3f_Scale(z, 1.0f / V3f_Dot(z, y));
|
||||
|
||||
// Calculate inverse position
|
||||
//
|
||||
V3f ip;
|
||||
ip.x = (txp.x * ix.x) + (txp.y * iy.x) + (txp.z * iz.x);
|
||||
ip.y = (txp.x * ix.y) + (txp.y * iy.y) + (txp.z * iz.y);
|
||||
ip.z = (txp.x * ix.z) + (txp.y * iy.z) + (txp.z * iz.z);
|
||||
|
||||
result.inv = M4x4F_Columns(ix, iy, iz);
|
||||
|
||||
// Translate by ip
|
||||
//
|
||||
result.inv.r[0].w -= ip.x;
|
||||
result.inv.r[1].w -= ip.y;
|
||||
result.inv.r[2].w -= ip.z;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
105
code/core/math.h
Normal file
105
code/core/math.h
Normal file
@@ -0,0 +1,105 @@
|
||||
#if !defined(LD_CORE_MATH_H_)
|
||||
#define LD_CORE_MATH_H_
|
||||
|
||||
#define PI_F32 (3.14159265358979323846264338f)
|
||||
#define TAU_F32 (2.0f * PI_F32)
|
||||
|
||||
typedef union V2f V2f;
|
||||
union V2f {
|
||||
struct {
|
||||
F32 x, y;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 u, v;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 w, h;
|
||||
};
|
||||
|
||||
F32 e[2];
|
||||
};
|
||||
|
||||
typedef union V2i V2i;
|
||||
union V2i {
|
||||
struct {
|
||||
U32 x, y;
|
||||
};
|
||||
|
||||
struct {
|
||||
U32 w, h;
|
||||
};
|
||||
|
||||
U32 e[2];
|
||||
};
|
||||
|
||||
typedef union V3f V3f;
|
||||
union V3f {
|
||||
struct {
|
||||
F32 x, y, z;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 r, g, b;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 w, h, d;
|
||||
};
|
||||
|
||||
struct {
|
||||
V2f xy;
|
||||
F32 _z;
|
||||
};
|
||||
|
||||
F32 e[3];
|
||||
};
|
||||
|
||||
typedef union V4f V4f;
|
||||
union V4f {
|
||||
struct {
|
||||
F32 x, y, z, w;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 r, g, b, a;
|
||||
};
|
||||
|
||||
struct {
|
||||
V3f xyz;
|
||||
F32 _w;
|
||||
};
|
||||
|
||||
F32 e[4];
|
||||
};
|
||||
|
||||
typedef union Mat4x4F Mat4x4F;
|
||||
union Mat4x4F {
|
||||
F32 m[4][4];
|
||||
F32 e[16];
|
||||
V4f r[4];
|
||||
};
|
||||
|
||||
typedef struct Mat4x4FInv Mat4x4FInv;
|
||||
struct Mat4x4FInv {
|
||||
Mat4x4F fwd;
|
||||
Mat4x4F inv;
|
||||
};
|
||||
|
||||
function V3f V3f_Neg(V3f x);
|
||||
function V3f V3f_Scale(V3f x, F32 s);
|
||||
|
||||
function F32 V3f_Dot(V3f a, V3f b);
|
||||
function F32 V4f_Dot(V4f a, V4f b);
|
||||
|
||||
function Mat4x4F M4x4F_Rows(V3f x, V3f y, V3f z);
|
||||
function Mat4x4F M4x4F_Columns(V3f x, V3f y, V3f z);
|
||||
|
||||
function V4f M4x4F_VMul4(Mat4x4F m, V4f v);
|
||||
function V3f M4x4F_VMul3(Mat4x4F m, V3f v);
|
||||
|
||||
function Mat4x4FInv M4x4F_Perspective(F32 fov, F32 aspect, F32 nearp, F32 farp);
|
||||
function Mat4x4FInv M4x4F_CameraView(V3f x, V3f y, V3f z, V3f p);
|
||||
|
||||
#endif // LD_CORE_MATH_H_
|
||||
@@ -29,18 +29,6 @@ struct Str8 {
|
||||
U8 *data;
|
||||
};
|
||||
|
||||
typedef struct V2f V2f;
|
||||
struct V2f {
|
||||
F32 x;
|
||||
F32 y;
|
||||
};
|
||||
|
||||
typedef struct V2i V2i;
|
||||
struct V2i {
|
||||
U32 x;
|
||||
U32 y;
|
||||
};
|
||||
|
||||
#define U8_MAX ((U8) -1)
|
||||
#define U16_MAX ((U16) -1)
|
||||
#define U32_MAX ((U32) -1)
|
||||
|
||||
Reference in New Issue
Block a user