Compare commits
No commits in common. "6f5c094f49fce3a9bc11098cfdc4a1763be017e2" and "4f58fc48033ea7066f3c40ad38ae1dcc480b8f3d" have entirely different histories.
6f5c094f49
...
4f58fc4803
@ -25,7 +25,7 @@ internal V3f g_ambient_light = {0.3f, 0.75f, 0.15f};
|
||||
internal DirectionalLight g_directional_light = {
|
||||
.diffuse_intensity = {0.9f, 0.8f, 0.6f},
|
||||
.specular_intensity = {0.5f, 0.8f, 2.0f},
|
||||
.direction = {0.0f, 0.0f, 1.4f},
|
||||
.direction = {0.0f, 0.0f, 1.0f},
|
||||
};
|
||||
internal V3f g_eye = {0.2f, 0.1f, 0.75f};
|
||||
internal V3f g_target = {0};
|
||||
@ -88,32 +88,17 @@ internal FragmentResult phong_shader_fragment(void *shader,
|
||||
const FragmentData *data,
|
||||
const Colour *colour,
|
||||
const Model *model) {
|
||||
Shader *shader_ptr = (Shader *)shader;
|
||||
|
||||
V4f hnorm;
|
||||
V3f norm;
|
||||
if (model->normal) {
|
||||
u64 nm_x = data->tex_coords.u * model->normal->width;
|
||||
u64 nm_y = (1.0f - data->tex_coords.v) * model->normal->height;
|
||||
|
||||
Colour pixel = get_pixel(Colour, model->normal, nm_x, nm_y);
|
||||
hnorm = (V4f){
|
||||
.x = pixel.r,
|
||||
.y = pixel.g,
|
||||
.z = pixel.b,
|
||||
.w = 1.0f,
|
||||
};
|
||||
} else {
|
||||
hnorm = V3_to_V4(data->normal);
|
||||
}
|
||||
|
||||
M4x4f matrix = mat4x4_mul(shader_ptr->projection, shader_ptr->model_view);
|
||||
M4x4f transposed = mat4x4_transpose(matrix);
|
||||
M4x4f inv_transpose = mat4x4_inv(transposed);
|
||||
|
||||
hnorm = mat4x4_mul_vec4(inv_transpose, hnorm);
|
||||
norm = project_vec4(hnorm);
|
||||
norm = (V3f){.x = pixel.r, .y = pixel.g, .z = pixel.b};
|
||||
normalise_v3(norm);
|
||||
} else {
|
||||
norm = data->normal;
|
||||
}
|
||||
|
||||
Colour output;
|
||||
if (model->texture) {
|
||||
@ -130,12 +115,8 @@ internal FragmentResult phong_shader_fragment(void *shader,
|
||||
// Ambient term
|
||||
V3f intensity = num_mul_v3(g_ambient_light, model->material.ambient);
|
||||
|
||||
V4f hdir = V3_to_V4(g_directional_light.direction);
|
||||
hdir = mat4x4_mul_vec4(matrix, hdir);
|
||||
V3f light_dir = project_vec4(hdir);
|
||||
|
||||
// Diffuse term
|
||||
f32 l_dot_n = dot_v3(norm, light_dir);
|
||||
f32 l_dot_n = dot_v3(norm, g_directional_light.direction);
|
||||
if (l_dot_n <= 0.0f) {
|
||||
goto RETURN_OUTPUT_COLOUR;
|
||||
}
|
||||
@ -146,7 +127,7 @@ internal FragmentResult phong_shader_fragment(void *shader,
|
||||
|
||||
// Specular term
|
||||
V3f _2_l_dot_n_norm = num_mul_v3(norm, 2.0f * l_dot_n);
|
||||
V3f reflected = sub_v3(_2_l_dot_n_norm, light_dir);
|
||||
V3f reflected = sub_v3(_2_l_dot_n_norm, g_directional_light.direction);
|
||||
f32 r_dot_v = dot_v3(reflected, data->position);
|
||||
if (r_dot_v <= 0.0f) {
|
||||
goto RETURN_OUTPUT_COLOUR;
|
||||
|
@ -55,78 +55,3 @@ M4x4f viewport(f32 x, f32 y, u64 w, u64 h) {
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
M4x4f mat4x4_inv(M4x4f matrix) {
|
||||
f32 mat[4][4] = mat4x4_to_array(matrix);
|
||||
f32 dest[4][4] = {0};
|
||||
|
||||
f32 t[6];
|
||||
f32 det;
|
||||
f32 a = mat[0][0], b = mat[0][1], c = mat[0][2], d = mat[0][3], e = mat[1][0],
|
||||
f = mat[1][1], g = mat[1][2], h = mat[1][3], i = mat[2][0], j = mat[2][1],
|
||||
k = mat[2][2], l = mat[2][3], m = mat[3][0], n = mat[3][1], o = mat[3][2],
|
||||
p = mat[3][3];
|
||||
|
||||
t[0] = k * p - o * l;
|
||||
t[1] = j * p - n * l;
|
||||
t[2] = j * o - n * k;
|
||||
t[3] = i * p - m * l;
|
||||
t[4] = i * o - m * k;
|
||||
t[5] = i * n - m * j;
|
||||
|
||||
dest[0][0] = f * t[0] - g * t[1] + h * t[2];
|
||||
dest[1][0] = -(e * t[0] - g * t[3] + h * t[4]);
|
||||
dest[2][0] = e * t[1] - f * t[3] + h * t[5];
|
||||
dest[3][0] = -(e * t[2] - f * t[4] + g * t[5]);
|
||||
|
||||
dest[0][1] = -(b * t[0] - c * t[1] + d * t[2]);
|
||||
dest[1][1] = a * t[0] - c * t[3] + d * t[4];
|
||||
dest[2][1] = -(a * t[1] - b * t[3] + d * t[5]);
|
||||
dest[3][1] = a * t[2] - b * t[4] + c * t[5];
|
||||
|
||||
t[0] = g * p - o * h;
|
||||
t[1] = f * p - n * h;
|
||||
t[2] = f * o - n * g;
|
||||
t[3] = e * p - m * h;
|
||||
t[4] = e * o - m * g;
|
||||
t[5] = e * n - m * f;
|
||||
|
||||
dest[0][2] = b * t[0] - c * t[1] + d * t[2];
|
||||
dest[1][2] = -(a * t[0] - c * t[3] + d * t[4]);
|
||||
dest[2][2] = a * t[1] - b * t[3] + d * t[5];
|
||||
dest[3][2] = -(a * t[2] - b * t[4] + c * t[5]);
|
||||
|
||||
t[0] = g * l - k * h;
|
||||
t[1] = f * l - j * h;
|
||||
t[2] = f * k - j * g;
|
||||
t[3] = e * l - i * h;
|
||||
t[4] = e * k - i * g;
|
||||
t[5] = e * j - i * f;
|
||||
|
||||
dest[0][3] = -(b * t[0] - c * t[1] + d * t[2]);
|
||||
dest[1][3] = a * t[0] - c * t[3] + d * t[4];
|
||||
dest[2][3] = -(a * t[1] - b * t[3] + d * t[5]);
|
||||
dest[3][3] = a * t[2] - b * t[4] + c * t[5];
|
||||
|
||||
det = 1.0f /
|
||||
(a * dest[0][0] + b * dest[1][0] + c * dest[2][0] + d * dest[3][0]);
|
||||
|
||||
dest[0][0] *= det;
|
||||
dest[0][1] *= det;
|
||||
dest[0][2] *= det;
|
||||
dest[0][3] *= det;
|
||||
dest[1][0] *= det;
|
||||
dest[1][1] *= det;
|
||||
dest[1][2] *= det;
|
||||
dest[1][3] *= det;
|
||||
dest[2][0] *= det;
|
||||
dest[2][1] *= det;
|
||||
dest[2][2] *= det;
|
||||
dest[2][3] *= det;
|
||||
dest[3][0] *= det;
|
||||
dest[3][1] *= det;
|
||||
dest[3][2] *= det;
|
||||
dest[3][3] *= det;
|
||||
|
||||
return array_to_mat4x4(dest);
|
||||
}
|
||||
|
@ -85,8 +85,6 @@ MAKE_LIST_TYPE(V2f);
|
||||
((T){(ELEM_T)X1 - (ELEM_T)X0, (ELEM_T)Y1 - (ELEM_T)Y0, \
|
||||
(ELEM_T)Z1 - (ELEM_T)Z0})
|
||||
|
||||
#define V3_to_V4(V3) ((V4f){.x = V3.x, .y = V3.y, .z = V3.z, .w = 1.0f})
|
||||
|
||||
#define dot_v2(V1, V2) ((f32)V1.x * (f32)V2.x + (f32)V1.y * (f32)V2.y)
|
||||
|
||||
#define dot_v3(V1, V2) \
|
||||
@ -142,42 +140,6 @@ MAKE_LIST_TYPE(V2f);
|
||||
.row3 = {0.0f, 0.0f, 0.0f, 1.0f}, \
|
||||
})
|
||||
|
||||
#define mat4x4_to_array(MAT) \
|
||||
((f32[4][4]){ \
|
||||
{MAT.row0.x, MAT.row0.y, MAT.row0.z, MAT.row0.w}, \
|
||||
{MAT.row1.x, MAT.row1.y, MAT.row1.z, MAT.row1.w}, \
|
||||
{MAT.row2.x, MAT.row2.y, MAT.row2.z, MAT.row2.w}, \
|
||||
{MAT.row3.x, MAT.row3.y, MAT.row3.z, MAT.row3.w}, \
|
||||
})
|
||||
|
||||
#define array_to_mat4x4(arr) \
|
||||
((M4x4f){ \
|
||||
.row0 = {arr[0][0], arr[0][1], arr[0][2], arr[0][3]}, \
|
||||
.row1 = {arr[1][0], arr[1][1], arr[1][2], arr[1][3]}, \
|
||||
.row2 = {arr[2][0], arr[2][1], arr[2][2], arr[2][3]}, \
|
||||
.row3 = {arr[3][0], arr[3][1], arr[3][2], arr[3][3]}, \
|
||||
})
|
||||
|
||||
#define mat4x4_transpose(MAT) \
|
||||
((M4x4f){ \
|
||||
.row0 = {.x = MAT.row0.x, \
|
||||
.y = MAT.row1.x, \
|
||||
.z = MAT.row2.x, \
|
||||
.w = MAT.row3.x}, \
|
||||
.row1 = {.x = MAT.row0.y, \
|
||||
.y = MAT.row1.y, \
|
||||
.z = MAT.row2.y, \
|
||||
.w = MAT.row3.y}, \
|
||||
.row2 = {.x = MAT.row0.z, \
|
||||
.y = MAT.row1.z, \
|
||||
.z = MAT.row2.z, \
|
||||
.w = MAT.row3.z}, \
|
||||
.row3 = {.x = MAT.row0.w, \
|
||||
.y = MAT.row1.w, \
|
||||
.z = MAT.row2.w, \
|
||||
.w = MAT.row3.w}, \
|
||||
})
|
||||
|
||||
#define mat4x4_mul(MAT1, MAT2) \
|
||||
((M4x4f){ \
|
||||
.row0.x = MAT1.row0.x * MAT2.row0.x + MAT1.row0.y * MAT2.row1.x + \
|
||||
@ -231,6 +193,5 @@ MAKE_LIST_TYPE(V2f);
|
||||
M4x4f lookat(V3f eye, V3f target, V3f up);
|
||||
M4x4f projection(f32 coeff);
|
||||
M4x4f viewport(f32 x, f32 y, u64 w, u64 h);
|
||||
M4x4f mat4x4_inv(M4x4f matrix);
|
||||
|
||||
#endif // VEC_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user