improve 3d renderer
This commit is contained in:
parent
f19b06d705
commit
59be43d80e
17 changed files with 556 additions and 486 deletions
129
src/pxl8_math.c
129
src/pxl8_math.c
|
|
@ -96,13 +96,13 @@ pxl8_mat4 pxl8_mat4_identity(void) {
|
|||
pxl8_mat4 pxl8_mat4_multiply(pxl8_mat4 a, pxl8_mat4 b) {
|
||||
pxl8_mat4 mat = {0};
|
||||
|
||||
for (i32 i = 0; i < 4; i++) {
|
||||
for (i32 j = 0; j < 4; j++) {
|
||||
mat.m[i * 4 + j] =
|
||||
a.m[i * 4 + 0] * b.m[0 * 4 + j] +
|
||||
a.m[i * 4 + 1] * b.m[1 * 4 + j] +
|
||||
a.m[i * 4 + 2] * b.m[2 * 4 + j] +
|
||||
a.m[i * 4 + 3] * b.m[3 * 4 + j];
|
||||
for (i32 col = 0; col < 4; col++) {
|
||||
for (i32 row = 0; row < 4; row++) {
|
||||
mat.m[col * 4 + row] =
|
||||
a.m[0 * 4 + row] * b.m[col * 4 + 0] +
|
||||
a.m[1 * 4 + row] * b.m[col * 4 + 1] +
|
||||
a.m[2 * 4 + row] * b.m[col * 4 + 2] +
|
||||
a.m[3 * 4 + row] * b.m[col * 4 + 3];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -111,19 +111,19 @@ pxl8_mat4 pxl8_mat4_multiply(pxl8_mat4 a, pxl8_mat4 b) {
|
|||
|
||||
pxl8_vec4 pxl8_mat4_multiply_vec4(pxl8_mat4 m, pxl8_vec4 v) {
|
||||
return (pxl8_vec4){
|
||||
.x = m.m[0] * v.x + m.m[1] * v.y + m.m[2] * v.z + m.m[3] * v.w,
|
||||
.y = m.m[4] * v.x + m.m[5] * v.y + m.m[6] * v.z + m.m[7] * v.w,
|
||||
.z = m.m[8] * v.x + m.m[9] * v.y + m.m[10] * v.z + m.m[11] * v.w,
|
||||
.w = m.m[12] * v.x + m.m[13] * v.y + m.m[14] * v.z + m.m[15] * v.w,
|
||||
.x = m.m[0] * v.x + m.m[4] * v.y + m.m[8] * v.z + m.m[12] * v.w,
|
||||
.y = m.m[1] * v.x + m.m[5] * v.y + m.m[9] * v.z + m.m[13] * v.w,
|
||||
.z = m.m[2] * v.x + m.m[6] * v.y + m.m[10] * v.z + m.m[14] * v.w,
|
||||
.w = m.m[3] * v.x + m.m[7] * v.y + m.m[11] * v.z + m.m[15] * v.w,
|
||||
};
|
||||
}
|
||||
|
||||
pxl8_mat4 pxl8_mat4_translate(f32 x, f32 y, f32 z) {
|
||||
pxl8_mat4 mat = pxl8_mat4_identity();
|
||||
|
||||
mat.m[3] = x;
|
||||
mat.m[7] = y;
|
||||
mat.m[11] = z;
|
||||
mat.m[12] = x;
|
||||
mat.m[13] = y;
|
||||
mat.m[14] = z;
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
|
@ -134,8 +134,8 @@ pxl8_mat4 pxl8_mat4_rotate_x(f32 angle) {
|
|||
f32 s = sinf(angle);
|
||||
|
||||
mat.m[5] = c;
|
||||
mat.m[6] = -s;
|
||||
mat.m[9] = s;
|
||||
mat.m[9] = -s;
|
||||
mat.m[6] = s;
|
||||
mat.m[10] = c;
|
||||
|
||||
return mat;
|
||||
|
|
@ -147,8 +147,8 @@ pxl8_mat4 pxl8_mat4_rotate_y(f32 angle) {
|
|||
f32 s = sinf(angle);
|
||||
|
||||
mat.m[0] = c;
|
||||
mat.m[2] = s;
|
||||
mat.m[8] = -s;
|
||||
mat.m[8] = s;
|
||||
mat.m[2] = -s;
|
||||
mat.m[10] = c;
|
||||
|
||||
return mat;
|
||||
|
|
@ -160,8 +160,8 @@ pxl8_mat4 pxl8_mat4_rotate_z(f32 angle) {
|
|||
f32 s = sinf(angle);
|
||||
|
||||
mat.m[0] = c;
|
||||
mat.m[1] = -s;
|
||||
mat.m[4] = s;
|
||||
mat.m[4] = -s;
|
||||
mat.m[1] = s;
|
||||
mat.m[5] = c;
|
||||
|
||||
return mat;
|
||||
|
|
@ -183,9 +183,9 @@ pxl8_mat4 pxl8_mat4_ortho(f32 left, f32 right, f32 bottom, f32 top, f32 near, f3
|
|||
mat.m[0] = 2.0f / (right - left);
|
||||
mat.m[5] = 2.0f / (top - bottom);
|
||||
mat.m[10] = -2.0f / (far - near);
|
||||
mat.m[3] = -(right + left) / (right - left);
|
||||
mat.m[7] = -(top + bottom) / (top - bottom);
|
||||
mat.m[11] = -(far + near) / (far - near);
|
||||
mat.m[12] = -(right + left) / (right - left);
|
||||
mat.m[13] = -(top + bottom) / (top - bottom);
|
||||
mat.m[14] = -(far + near) / (far - near);
|
||||
mat.m[15] = 1.0f;
|
||||
|
||||
return mat;
|
||||
|
|
@ -198,8 +198,8 @@ pxl8_mat4 pxl8_mat4_perspective(f32 fov, f32 aspect, f32 near, f32 far) {
|
|||
mat.m[0] = 1.0f / (aspect * tan_half_fov);
|
||||
mat.m[5] = 1.0f / tan_half_fov;
|
||||
mat.m[10] = -(far + near) / (far - near);
|
||||
mat.m[11] = -(2.0f * far * near) / (far - near);
|
||||
mat.m[14] = -1.0f;
|
||||
mat.m[14] = -(2.0f * far * near) / (far - near);
|
||||
mat.m[11] = -1.0f;
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
|
@ -211,17 +211,17 @@ pxl8_mat4 pxl8_mat4_lookat(pxl8_vec3 eye, pxl8_vec3 center, pxl8_vec3 up) {
|
|||
pxl8_vec3 u = pxl8_vec3_cross(s, f);
|
||||
|
||||
mat.m[0] = s.x;
|
||||
mat.m[1] = s.y;
|
||||
mat.m[2] = s.z;
|
||||
mat.m[4] = u.x;
|
||||
mat.m[4] = s.y;
|
||||
mat.m[8] = s.z;
|
||||
mat.m[1] = u.x;
|
||||
mat.m[5] = u.y;
|
||||
mat.m[6] = u.z;
|
||||
mat.m[8] = -f.x;
|
||||
mat.m[9] = -f.y;
|
||||
mat.m[9] = u.z;
|
||||
mat.m[2] = -f.x;
|
||||
mat.m[6] = -f.y;
|
||||
mat.m[10] = -f.z;
|
||||
mat.m[3] = -pxl8_vec3_dot(s, eye);
|
||||
mat.m[7] = -pxl8_vec3_dot(u, eye);
|
||||
mat.m[11] = pxl8_vec3_dot(f, eye);
|
||||
mat.m[12] = -pxl8_vec3_dot(s, eye);
|
||||
mat.m[13] = -pxl8_vec3_dot(u, eye);
|
||||
mat.m[14] = pxl8_vec3_dot(f, eye);
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
|
@ -230,35 +230,35 @@ pxl8_frustum pxl8_frustum_from_matrix(pxl8_mat4 vp) {
|
|||
pxl8_frustum frustum;
|
||||
const f32* m = vp.m;
|
||||
|
||||
frustum.planes[0].normal.x = m[12] - m[0];
|
||||
frustum.planes[0].normal.y = m[13] - m[1];
|
||||
frustum.planes[0].normal.z = m[14] - m[2];
|
||||
frustum.planes[0].distance = m[15] - m[3];
|
||||
frustum.planes[0].normal.x = m[3] - m[0];
|
||||
frustum.planes[0].normal.y = m[7] - m[4];
|
||||
frustum.planes[0].normal.z = m[11] - m[8];
|
||||
frustum.planes[0].distance = m[15] - m[12];
|
||||
|
||||
frustum.planes[1].normal.x = m[12] + m[0];
|
||||
frustum.planes[1].normal.y = m[13] + m[1];
|
||||
frustum.planes[1].normal.z = m[14] + m[2];
|
||||
frustum.planes[1].distance = m[15] + m[3];
|
||||
frustum.planes[1].normal.x = m[3] + m[0];
|
||||
frustum.planes[1].normal.y = m[7] + m[4];
|
||||
frustum.planes[1].normal.z = m[11] + m[8];
|
||||
frustum.planes[1].distance = m[15] + m[12];
|
||||
|
||||
frustum.planes[2].normal.x = m[12] + m[4];
|
||||
frustum.planes[2].normal.y = m[13] + m[5];
|
||||
frustum.planes[2].normal.z = m[14] + m[6];
|
||||
frustum.planes[2].distance = m[15] + m[7];
|
||||
frustum.planes[2].normal.x = m[3] + m[1];
|
||||
frustum.planes[2].normal.y = m[7] + m[5];
|
||||
frustum.planes[2].normal.z = m[11] + m[9];
|
||||
frustum.planes[2].distance = m[15] + m[13];
|
||||
|
||||
frustum.planes[3].normal.x = m[12] - m[4];
|
||||
frustum.planes[3].normal.y = m[13] - m[5];
|
||||
frustum.planes[3].normal.z = m[14] - m[6];
|
||||
frustum.planes[3].distance = m[15] - m[7];
|
||||
frustum.planes[3].normal.x = m[3] - m[1];
|
||||
frustum.planes[3].normal.y = m[7] - m[5];
|
||||
frustum.planes[3].normal.z = m[11] - m[9];
|
||||
frustum.planes[3].distance = m[15] - m[13];
|
||||
|
||||
frustum.planes[4].normal.x = m[12] - m[8];
|
||||
frustum.planes[4].normal.y = m[13] - m[9];
|
||||
frustum.planes[4].normal.z = m[14] - m[10];
|
||||
frustum.planes[4].distance = m[15] - m[11];
|
||||
frustum.planes[4].normal.x = m[3] - m[2];
|
||||
frustum.planes[4].normal.y = m[7] - m[6];
|
||||
frustum.planes[4].normal.z = m[11] - m[10];
|
||||
frustum.planes[4].distance = m[15] - m[14];
|
||||
|
||||
frustum.planes[5].normal.x = m[12] + m[8];
|
||||
frustum.planes[5].normal.y = m[13] + m[9];
|
||||
frustum.planes[5].normal.z = m[14] + m[10];
|
||||
frustum.planes[5].distance = m[15] + m[11];
|
||||
frustum.planes[5].normal.x = m[3] + m[2];
|
||||
frustum.planes[5].normal.y = m[7] + m[6];
|
||||
frustum.planes[5].normal.z = m[11] + m[10];
|
||||
frustum.planes[5].distance = m[15] + m[14];
|
||||
|
||||
for (i32 i = 0; i < 6; i++) {
|
||||
f32 len = pxl8_vec3_length(frustum.planes[i].normal);
|
||||
|
|
@ -283,22 +283,11 @@ bool pxl8_frustum_test_aabb(const pxl8_frustum* frustum, pxl8_vec3 min, pxl8_vec
|
|||
(normal.z >= 0.0f) ? max.z : min.z
|
||||
};
|
||||
|
||||
pxl8_vec3 n_vertex = {
|
||||
(normal.x >= 0.0f) ? min.x : max.x,
|
||||
(normal.y >= 0.0f) ? min.y : max.y,
|
||||
(normal.z >= 0.0f) ? min.z : max.z
|
||||
};
|
||||
|
||||
f32 p_dist = pxl8_vec3_dot(normal, p_vertex) + d;
|
||||
f32 n_dist = pxl8_vec3_dot(normal, n_vertex) + d;
|
||||
|
||||
if (p_dist < -0.1f) {
|
||||
if (p_dist < 0.0f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (n_dist > 0.1f) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue