Update shaders and ensure coordinates are within buffer bounds
This commit is contained in:
parent
6239be219a
commit
690eb3d317
@ -13,12 +13,9 @@
|
|||||||
#define SIZE 1200
|
#define SIZE 1200
|
||||||
#define RESOURCE(NAME) "resources/" NAME
|
#define RESOURCE(NAME) "resources/" NAME
|
||||||
|
|
||||||
extern ShaderID phong;
|
extern ShaderID perspective_phong;
|
||||||
extern ShaderID perspective_lit_textured;
|
|
||||||
extern ShaderID perspective_lit_coloured;
|
|
||||||
extern ShaderID perspective_albedo;
|
extern ShaderID perspective_albedo;
|
||||||
extern ShaderID orthographic_lit_textured;
|
extern ShaderID orthographic_phong;
|
||||||
extern ShaderID orthographic_lit_coloured;
|
|
||||||
extern ShaderID orthographic_albedo;
|
extern ShaderID orthographic_albedo;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
@ -51,7 +48,7 @@ int main(void) {
|
|||||||
load_shaders();
|
load_shaders();
|
||||||
|
|
||||||
clear_buffer(&(render.img), &bg);
|
clear_buffer(&(render.img), &bg);
|
||||||
render_model(&obj, &render, phong, RENDER_TYPE_SHADED, teal);
|
render_model(&obj, &render, perspective_phong, RENDER_TYPE_SHADED, teal);
|
||||||
save_image(&(render.img), "result.pam");
|
save_image(&(render.img), "result.pam");
|
||||||
|
|
||||||
wapp_mem_arena_destroy(&arena);
|
wapp_mem_arena_destroy(&arena);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "img.h"
|
#include "img.h"
|
||||||
#include "render.h"
|
#include "render.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
#include "utils.h"
|
||||||
#include "vec.h"
|
#include "vec.h"
|
||||||
|
|
||||||
typedef struct shader Shader;
|
typedef struct shader Shader;
|
||||||
@ -9,22 +10,19 @@ struct shader {
|
|||||||
M4x4f projection;
|
M4x4f projection;
|
||||||
};
|
};
|
||||||
|
|
||||||
Shader perspective = {0};
|
internal Shader perspective = {0};
|
||||||
Shader orthographic = {0};
|
internal Shader orthographic = {0};
|
||||||
|
|
||||||
ShaderID phong = {0};
|
ShaderID perspective_phong = {0};
|
||||||
ShaderID perspective_lit_textured = {0};
|
|
||||||
ShaderID perspective_lit_coloured = {0};
|
|
||||||
ShaderID perspective_albedo = {0};
|
ShaderID perspective_albedo = {0};
|
||||||
ShaderID orthographic_lit_textured = {0};
|
ShaderID orthographic_phong = {0};
|
||||||
ShaderID orthographic_lit_coloured = {0};
|
|
||||||
ShaderID orthographic_albedo = {0};
|
ShaderID orthographic_albedo = {0};
|
||||||
|
|
||||||
V3f g_light_dir = {0.0f, 0.0f, 1.0f};
|
internal V3f g_light_dir = {0.0f, 0.0f, 1.0f};
|
||||||
V3f g_eye = {0.2f, 0.1f, 0.75f};
|
internal V3f g_eye = {0.2f, 0.1f, 0.75f};
|
||||||
V3f g_target = {0};
|
internal V3f g_target = {0};
|
||||||
V3f g_up = {0.0f, 1.0f, 0.0f};
|
internal V3f g_up = {0.0f, 1.0f, 0.0f};
|
||||||
M4x4f g_cam_matrix = mat4x4_identity;
|
internal M4x4f g_cam_matrix = mat4x4_identity;
|
||||||
|
|
||||||
internal V3f general_shader_vertex(void *shader, const V3f *vertex,
|
internal V3f general_shader_vertex(void *shader, const V3f *vertex,
|
||||||
const Buffer *out_buf);
|
const Buffer *out_buf);
|
||||||
@ -32,14 +30,6 @@ internal FragmentResult phong_shader_fragment(void *shader, V3f normal,
|
|||||||
V2f tex_coords,
|
V2f tex_coords,
|
||||||
const Colour *colour,
|
const Colour *colour,
|
||||||
const Model *model);
|
const Model *model);
|
||||||
internal FragmentResult lit_textured_shader_fragment(void *shader, V3f normal,
|
|
||||||
V2f tex_coords,
|
|
||||||
const Colour *colour,
|
|
||||||
const Model *model);
|
|
||||||
internal FragmentResult lit_coloured_shader_fragment(void *shader, V3f normal,
|
|
||||||
V2f tex_coords,
|
|
||||||
const Colour *colour,
|
|
||||||
const Model *model);
|
|
||||||
internal FragmentResult albedo_shader_fragment(void *shader, V3f normal,
|
internal FragmentResult albedo_shader_fragment(void *shader, V3f normal,
|
||||||
V2f tex_coords,
|
V2f tex_coords,
|
||||||
const Colour *colour,
|
const Colour *colour,
|
||||||
@ -58,18 +48,12 @@ void load_shaders(void) {
|
|||||||
perspective.projection = perspective_projection;
|
perspective.projection = perspective_projection;
|
||||||
orthographic.projection = orthographic_projection;
|
orthographic.projection = orthographic_projection;
|
||||||
|
|
||||||
phong =
|
perspective_phong =
|
||||||
create_shader(&perspective, general_shader_vertex, phong_shader_fragment);
|
create_shader(&perspective, general_shader_vertex, phong_shader_fragment);
|
||||||
perspective_lit_textured = create_shader(&perspective, general_shader_vertex,
|
|
||||||
lit_textured_shader_fragment);
|
|
||||||
perspective_lit_coloured = create_shader(&perspective, general_shader_vertex,
|
|
||||||
lit_coloured_shader_fragment);
|
|
||||||
perspective_albedo = create_shader(&perspective, general_shader_vertex,
|
perspective_albedo = create_shader(&perspective, general_shader_vertex,
|
||||||
albedo_shader_fragment);
|
albedo_shader_fragment);
|
||||||
orthographic_lit_textured = create_shader(
|
orthographic_phong = create_shader(&orthographic, general_shader_vertex,
|
||||||
&orthographic, general_shader_vertex, lit_textured_shader_fragment);
|
phong_shader_fragment);
|
||||||
orthographic_lit_coloured = create_shader(
|
|
||||||
&orthographic, general_shader_vertex, lit_coloured_shader_fragment);
|
|
||||||
orthographic_albedo = create_shader(&orthographic, general_shader_vertex,
|
orthographic_albedo = create_shader(&orthographic, general_shader_vertex,
|
||||||
albedo_shader_fragment);
|
albedo_shader_fragment);
|
||||||
}
|
}
|
||||||
@ -85,7 +69,11 @@ internal V3f general_shader_vertex(void *shader, const V3f *vertex,
|
|||||||
vh = mat4x4_mul_vec4(viewport(vh.x, vh.y, out_buf->width, out_buf->height),
|
vh = mat4x4_mul_vec4(viewport(vh.x, vh.y, out_buf->width, out_buf->height),
|
||||||
vh);
|
vh);
|
||||||
|
|
||||||
return project_vec4(vh);
|
V3f output = project_vec4(vh);
|
||||||
|
output.x = clamp(output.x, 0.0f, out_buf->width);
|
||||||
|
output.y = clamp(output.y, 0.0f, out_buf->height);
|
||||||
|
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal FragmentResult phong_shader_fragment(void *shader, V3f normal,
|
internal FragmentResult phong_shader_fragment(void *shader, V3f normal,
|
||||||
@ -122,42 +110,6 @@ internal FragmentResult phong_shader_fragment(void *shader, V3f normal,
|
|||||||
return (FragmentResult){.colour = output};
|
return (FragmentResult){.colour = output};
|
||||||
}
|
}
|
||||||
|
|
||||||
internal FragmentResult lit_textured_shader_fragment(void *shader, V3f normal,
|
|
||||||
V2f tex_coords,
|
|
||||||
const Colour *colour,
|
|
||||||
const Model *model) {
|
|
||||||
if (!(model->texture)) {
|
|
||||||
return DISCARDED_FRAGMENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
f32 intensity = get_intensity(&normal);
|
|
||||||
|
|
||||||
u64 tx_x = tex_coords.u * model->texture->width;
|
|
||||||
u64 tx_y = (1.0f - tex_coords.v) * model->texture->height;
|
|
||||||
|
|
||||||
Colour output = get_pixel(Colour, model->texture, tx_x, tx_y);
|
|
||||||
|
|
||||||
output.r *= intensity;
|
|
||||||
output.g *= intensity;
|
|
||||||
output.b *= intensity;
|
|
||||||
|
|
||||||
return (FragmentResult){.colour = output};
|
|
||||||
}
|
|
||||||
|
|
||||||
internal FragmentResult lit_coloured_shader_fragment(void *shader, V3f normal,
|
|
||||||
V2f tex_coords,
|
|
||||||
const Colour *colour,
|
|
||||||
const Model *model) {
|
|
||||||
f32 intensity = get_intensity(&normal);
|
|
||||||
Colour output = *colour;
|
|
||||||
|
|
||||||
output.r *= intensity;
|
|
||||||
output.g *= intensity;
|
|
||||||
output.b *= intensity;
|
|
||||||
|
|
||||||
return (FragmentResult){.colour = output};
|
|
||||||
}
|
|
||||||
|
|
||||||
internal FragmentResult albedo_shader_fragment(void *shader, V3f normal,
|
internal FragmentResult albedo_shader_fragment(void *shader, V3f normal,
|
||||||
V2f tex_coords,
|
V2f tex_coords,
|
||||||
const Colour *colour,
|
const Colour *colour,
|
||||||
|
Loading…
Reference in New Issue
Block a user