81 lines
3.4 KiB
C
81 lines
3.4 KiB
C
#include "shaders.h"
|
|
#include "aliases.h"
|
|
#include "depth_shader.h"
|
|
#include "main_shader.h"
|
|
#include "render.h"
|
|
#include "shader.h"
|
|
#include "vec.h"
|
|
|
|
ShaderID perspective_diffuse = {0};
|
|
ShaderID perspective_albedo = {0};
|
|
ShaderID orthographic_diffuse = {0};
|
|
ShaderID orthographic_albedo = {0};
|
|
ShaderID depth = {0};
|
|
|
|
internal Shader perspective = {0};
|
|
internal Shader orthographic = {0};
|
|
internal DepthShader depth_shader = {0};
|
|
|
|
internal V3f g_ambient_light = {0.1f, 0.1f, 0.1f};
|
|
internal V3f g_eye = {0.2f, -0.1f, 0.5f};
|
|
internal V3f g_target = {0};
|
|
internal V3f g_up = {0.0f, 1.0f, 0.0f};
|
|
internal V3f g_light_dir = {1.0f, -1.0f, 1.0f};
|
|
|
|
internal M4x4f get_projection_matrix(ProjectionType projection_type);
|
|
|
|
void load_shaders(M4x4f vp) {
|
|
M4x4f model_view = lookat(g_eye, g_target, g_up);
|
|
M4x4f orthographic_projection = get_projection_matrix(PROJECTION_TYPE_ORTHOGRAPHIC);
|
|
M4x4f perspective_projection = get_projection_matrix(PROJECTION_TYPE_PERSPECTIVE);
|
|
|
|
// Set up perspective shader
|
|
perspective.proj_mv = mat4x4_mul(perspective_projection, model_view);
|
|
perspective.proj_mv_inv_t = mat4x4_inv(mat4x4_transpose(perspective.proj_mv));
|
|
perspective.viewport = vp;
|
|
perspective.final = mat4x4_mul(perspective.viewport, perspective.proj_mv);
|
|
perspective.light_dir = mat3x3_mul_vec3(perspective.proj_mv, g_light_dir);
|
|
normalise_v3(perspective.light_dir);
|
|
perspective.ambient = g_ambient_light;
|
|
|
|
// Set up orthographic shader
|
|
orthographic.proj_mv = mat4x4_mul(orthographic_projection, model_view);
|
|
orthographic.proj_mv_inv_t = mat4x4_inv(mat4x4_transpose(orthographic.proj_mv));
|
|
orthographic.viewport = vp;
|
|
orthographic.final = mat4x4_mul(orthographic.viewport, orthographic.proj_mv);
|
|
orthographic.light_dir = mat3x3_mul_vec3(orthographic.proj_mv, g_light_dir);
|
|
normalise_v3(orthographic.light_dir);
|
|
orthographic.ambient = g_ambient_light;
|
|
|
|
// Set up depth shader
|
|
M4x4f depth_model_view = lookat(g_light_dir, g_target, g_up);
|
|
M4x4f depth_projection = projection(0.0f);
|
|
|
|
depth_shader.proj_mv = mat4x4_mul(depth_projection, depth_model_view);
|
|
depth_shader.proj_mv_inv_t = mat4x4_inv(mat4x4_transpose(depth_shader.proj_mv));
|
|
depth_shader.viewport = vp;
|
|
depth_shader.final = mat4x4_mul(depth_shader.viewport, depth_shader.proj_mv);
|
|
depth_shader.light_dir = mat3x3_mul_vec3(depth_shader.proj_mv, g_light_dir);
|
|
normalise_v3(depth_shader.light_dir);
|
|
|
|
// Register shaders
|
|
perspective_diffuse = register_shader(&perspective, general_shader_vertex, diffuse_shader_fragment);
|
|
perspective_albedo = register_shader(&perspective, general_shader_vertex, albedo_shader_fragment);
|
|
orthographic_diffuse = register_shader(&orthographic, general_shader_vertex, diffuse_shader_fragment);
|
|
orthographic_albedo = register_shader(&orthographic, general_shader_vertex, albedo_shader_fragment);
|
|
depth = register_shader(&depth_shader, depth_shader_vertex, depth_shader_fragment);
|
|
}
|
|
|
|
internal M4x4f get_projection_matrix(ProjectionType projection_type) {
|
|
if (projection_type == PROJECTION_TYPE_PERSPECTIVE) {
|
|
V3f cam = V3(V3f, f32, g_target.x, g_target.y, g_target.z, g_eye.x, g_eye.y, g_eye.z);
|
|
normalise_v3(cam);
|
|
|
|
f32 coeff = -1.0f / magnitude_v3(cam) * 0.5f;
|
|
|
|
return projection(coeff);
|
|
}
|
|
|
|
return mat4x4_identity;
|
|
}
|