Skip to content

Commit

Permalink
rendering scene with 1 color (#45)
Browse files Browse the repository at this point in the history
* rendering scene with 1 color

* adding rturn value to shader_fragment, removing unused var
  • Loading branch information
marsp0 authored Oct 6, 2024
1 parent 045e4d9 commit 75187f8
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
83 changes: 82 additions & 1 deletion src/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "linux/input.h"
#include "time_utils.h"
#include "rasterizer.h"
#include "shader.h"

/********************
* Notes
Expand Down Expand Up @@ -81,7 +82,87 @@ static void renderer_draw_utilities()

static void renderer_draw_mesh(mesh_t* mesh)
{
printf("mesh draw called %p\n", (void*)mesh);

uint32_t i0;
uint32_t i1;
uint32_t i2;

vec4_t v0;
vec4_t v1;
vec4_t v2;

// vec2_t t0;
// vec2_t t1;
// vec2_t t2;

// vec4_t n0;
// vec4_t n1;
// vec4_t n2;

float w_over_2 = (float)width * 0.5f;
float h_over_2 = (float)height * 0.5f;

camera_t* cam = scene->camera;

uint32_t indices_size = mesh->indices_size;
uint32_t* indices = mesh->indices;

vec4_t* vertices = mesh->vertices;

// uint32_t texcoords_size = mesh->texcoords_size;
// vec2_t* texcoords = mesh->texcoords;

// uint32_t normals_size = mesh->normals_size;
// vec4_t* normals = mesh->normals;

uint32_t colors[4] = {0x00000000,
0x0000FF00,
0x00FF0000,
0xFF000000};

for (uint32_t i = 0; i < indices_size; i += 3)
{
i0 = indices[i + 0];
i1 = indices[i + 1];
i2 = indices[i + 2];

v0 = vertices[i0];
v1 = vertices[i1];
v2 = vertices[i2];

// t0 = texcoords[i0];
// t1 = texcoords[i1];
// t2 = texcoords[i2];

// n0 = normals[i0];
// n1 = normals[i1];
// n2 = normals[i2];

// TODO: backface culling

// set shader utils
shader_set_camera(cam);

// run vertex shader
v0 = shader_vertex(v0);
v1 = shader_vertex(v1);
v2 = shader_vertex(v2);

// persp divide
v0 = vec4_scale(v0, 1.f / v0.w);
v1 = vec4_scale(v1, 1.f / v1.w);
v2 = vec4_scale(v2, 1.f / v2.w);

// viewport transform
v0.x = (v0.x + 1.f) * w_over_2;
v0.y = (v0.y + 1.f) * h_over_2;
v1.x = (v1.x + 1.f) * w_over_2;
v1.y = (v1.y + 1.f) * h_over_2;
v2.x = (v2.x + 1.f) * w_over_2;
v2.y = (v2.y + 1.f) * h_over_2;

rasterizer_draw_triangle(v0, v1, v2, colors[i%5], current, depthbuffer);
}
}

static void renderer_draw()
Expand Down
43 changes: 43 additions & 0 deletions src/shader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "shader.h"

/********************
* Notes
*
********************/

/********************/
/* defines */
/********************/

/********************/
/* static variables */
/********************/

static camera_t* cam = NULL;

/********************/
/* static functions */
/********************/

/********************/
/* public functions */
/********************/


void shader_set_camera(camera_t* camera)
{
cam = camera;
}

vec4_t shader_vertex(vec4_t v)
{
mat_t PV = mat_mul_mat(camera_proj_transform(cam),
camera_view_transform(cam));

return mat_mul_vec(PV, v);
}

uint32_t shader_fragment()
{
return 0;
}
7 changes: 7 additions & 0 deletions src/shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "camera.h"

void shader_set_camera(camera_t* cam);
vec4_t shader_vertex(vec4_t v);
uint32_t shader_fragment();

0 comments on commit 75187f8

Please sign in to comment.