Split obj loading and model rendering code

This commit is contained in:
2024-08-18 17:04:41 +01:00
parent 2e39780272
commit be7b577a0e
8 changed files with 152 additions and 157 deletions

View File

@@ -46,8 +46,7 @@ V3f run_vertex_shader(ShaderID shader, const V3f *vertex,
}
FragmentResult run_fragment_shader(ShaderID shader, V3f normal, V2f tex_coords,
const Colour *colour, const Image *texture,
const Image *normal_map) {
const Colour *colour, const Model *model) {
if (IS_INVALID_SHADER(shader) || shader.id > g_repository.count || !colour) {
return DISCARDED_FRAGMENT;
}
@@ -59,6 +58,5 @@ FragmentResult run_fragment_shader(ShaderID shader, V3f normal, V2f tex_coords,
return DISCARDED_FRAGMENT;
}
return fragment_func(shader_obj, normal, tex_coords, colour, texture,
normal_map);
return fragment_func(shader_obj, normal, tex_coords, colour, model);
}

View File

@@ -3,6 +3,7 @@
#include "aliases.h"
#include "img.h"
#include "obj.h"
#include "vec.h"
typedef struct shader_id ShaderID;
@@ -25,15 +26,13 @@ typedef V3f(VertexShader)(void *shader, const V3f *vertex,
const Buffer *out_buf);
typedef FragmentResult(FragmentShader)(void *shader, V3f normal, V2f tex_coords,
const Colour *colour,
const Image *texture,
const Image *normal_map);
const Model *model);
ShaderID create_shader(void *shader, VertexShader *vertex,
FragmentShader *fragment);
V3f run_vertex_shader(ShaderID shader, const V3f *vertex,
const Buffer *out_buf);
FragmentResult run_fragment_shader(ShaderID shader, V3f normal, V2f tex_coords,
const Colour *colour, const Image *texture,
const Image *normal_map);
const Colour *colour, const Model *model);
#endif // SHADER_H

View File

@@ -1,5 +1,5 @@
#include "img.h"
#include "obj.h"
#include "render.h"
#include "shader.h"
#include "vec.h"
@@ -31,23 +31,19 @@ internal V3f general_shader_vertex(void *shader, const V3f *vertex,
internal FragmentResult phong_shader_fragment(void *shader, V3f normal,
V2f tex_coords,
const Colour *colour,
const Image *texture,
const Image *normal_map);
const Model *model);
internal FragmentResult lit_textured_shader_fragment(void *shader, V3f normal,
V2f tex_coords,
const Colour *colour,
const Image *texture,
const Image *normal_map);
const Model *model);
internal FragmentResult lit_coloured_shader_fragment(void *shader, V3f normal,
V2f tex_coords,
const Colour *colour,
const Image *texture,
const Image *normal_map);
const Model *model);
internal FragmentResult albedo_shader_fragment(void *shader, V3f normal,
V2f tex_coords,
const Colour *colour,
const Image *texture,
const Image *normal_map);
const Model *model);
internal M4x4f get_projection_matrix(ProjectionType projection_type);
internal f32 get_intensity(const V3f *normal);
@@ -95,14 +91,13 @@ internal V3f general_shader_vertex(void *shader, const V3f *vertex,
internal FragmentResult phong_shader_fragment(void *shader, V3f normal,
V2f tex_coords,
const Colour *colour,
const Image *texture,
const Image *normal_map) {
const Model *model) {
f32 intensity = get_intensity(&normal);
if (normal_map) {
u64 nm_x = tex_coords.u * normal_map->width;
u64 nm_y = (1.0f - tex_coords.v) * normal_map->height;
if (model->normal) {
u64 nm_x = tex_coords.u * model->normal->width;
u64 nm_y = (1.0f - tex_coords.v) * model->normal->height;
Colour pixel = get_pixel(Colour, normal_map, nm_x, nm_y);
Colour pixel = get_pixel(Colour, model->normal, nm_x, nm_y);
V3f norm = {.x = pixel.r, .y = pixel.g, .z = pixel.b};
normalise_v3(norm);
@@ -112,10 +107,10 @@ internal FragmentResult phong_shader_fragment(void *shader, V3f normal,
}
Colour output;
if (texture) {
u64 tx_x = tex_coords.u * texture->width;
u64 tx_y = (1.0f - tex_coords.v) * texture->height;
output = get_pixel(Colour, texture, tx_x, tx_y);
if (model->texture) {
u64 tx_x = tex_coords.u * model->texture->width;
u64 tx_y = (1.0f - tex_coords.v) * model->texture->height;
output = get_pixel(Colour, model->texture, tx_x, tx_y);
} else {
output = *colour;
}
@@ -130,18 +125,17 @@ internal FragmentResult phong_shader_fragment(void *shader, V3f normal,
internal FragmentResult lit_textured_shader_fragment(void *shader, V3f normal,
V2f tex_coords,
const Colour *colour,
const Image *texture,
const Image *normal_map) {
if (!texture) {
const Model *model) {
if (!(model->texture)) {
return DISCARDED_FRAGMENT;
}
f32 intensity = get_intensity(&normal);
u64 tx_x = tex_coords.u * texture->width;
u64 tx_y = (1.0f - tex_coords.v) * texture->height;
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, texture, tx_x, tx_y);
Colour output = get_pixel(Colour, model->texture, tx_x, tx_y);
output.r *= intensity;
output.g *= intensity;
@@ -153,8 +147,7 @@ internal FragmentResult lit_textured_shader_fragment(void *shader, V3f normal,
internal FragmentResult lit_coloured_shader_fragment(void *shader, V3f normal,
V2f tex_coords,
const Colour *colour,
const Image *texture,
const Image *normal_map) {
const Model *model) {
f32 intensity = get_intensity(&normal);
Colour output = *colour;
@@ -168,8 +161,7 @@ internal FragmentResult lit_coloured_shader_fragment(void *shader, V3f normal,
internal FragmentResult albedo_shader_fragment(void *shader, V3f normal,
V2f tex_coords,
const Colour *colour,
const Image *texture,
const Image *normal_map) {
const Model *model) {
return (FragmentResult){.colour = *colour};
}