Move vertex and fragment calculations to the shaders
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
#include "render.h"
|
||||
#include "aliases.h"
|
||||
#include "constants.h"
|
||||
#include "shader.h"
|
||||
#include "typed_list.h"
|
||||
#include "utils.h"
|
||||
#include "vec.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define TRIANGLE_VERTICES 3
|
||||
internal M4x4f g_viewport;
|
||||
|
||||
typedef struct triangle_bbox TriangleBBox;
|
||||
struct triangle_bbox {
|
||||
@@ -20,14 +21,12 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
|
||||
ShaderID shader, Render *render,
|
||||
RenderType render_type, Colour colour);
|
||||
internal void fill_triangle(Render *render, ShaderID shader,
|
||||
V3f vertices[TRIANGLE_VERTICES],
|
||||
V3f normals[TRIANGLE_VERTICES],
|
||||
V2f coordinates[TRIANGLE_VERTICES], Colour colour,
|
||||
const Model *model, RenderType type);
|
||||
VertexData vertices[TRIANGLE_VERTICES],
|
||||
Colour colour, const Model *model, RenderType type);
|
||||
internal TriangleBBox get_triangle_bbox(const Image *img,
|
||||
V3f vertices[TRIANGLE_VERTICES]);
|
||||
internal V3f get_barycentric_coords(V3f a, V3f b, V3f c, V3f p);
|
||||
internal V3f get_viewport_vertex(const V3f *vertex, const Image *img);
|
||||
V2f vertices[TRIANGLE_VERTICES]);
|
||||
internal V3f get_barycentric_coords(V2f a, V2f b, V2f c, V2f p);
|
||||
internal V2f get_viewport_vertex(const V3f *vertex, const Image *img);
|
||||
|
||||
bool init_render(Arena *arena, Render *render, u64 width, u64 height) {
|
||||
render->img = (Image){.width = width, .height = height};
|
||||
@@ -43,6 +42,8 @@ bool init_render(Arena *arena, Render *render, u64 width, u64 height) {
|
||||
f32 inf = -INFINITY;
|
||||
clear_buffer(&(render->depth), &inf);
|
||||
|
||||
g_viewport = viewport(0, 0, width, height);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -59,120 +60,74 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
|
||||
ShaderID shader, Render *render,
|
||||
RenderType render_type, Colour colour) {
|
||||
Image *img = &(render->img);
|
||||
V3f vertices[TRIANGLE_VERTICES] = {
|
||||
list_get(model->vertices, triangle->p0),
|
||||
list_get(model->vertices, triangle->p1),
|
||||
list_get(model->vertices, triangle->p2),
|
||||
};
|
||||
V3f normals[TRIANGLE_VERTICES] = {
|
||||
list_get(model->normals, triangle->n0),
|
||||
list_get(model->normals, triangle->n1),
|
||||
list_get(model->normals, triangle->n2),
|
||||
};
|
||||
V2f coordinates[TRIANGLE_VERTICES] = {
|
||||
list_get(model->texture_coordinates, triangle->tx0),
|
||||
list_get(model->texture_coordinates, triangle->tx1),
|
||||
list_get(model->texture_coordinates, triangle->tx2),
|
||||
};
|
||||
|
||||
VertexData vertices[TRIANGLE_VERTICES];
|
||||
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
|
||||
vertices[i] = run_vertex_shader(shader, &vertices[i]);
|
||||
// clang-format off
|
||||
vertices[i].position = list_get(model->vertices, triangle->positions[i]);
|
||||
vertices[i].normal = list_get(model->normals, triangle->normals[i]);
|
||||
vertices[i].uv = list_get(model->texture_coordinates, triangle->coordinates[i]);
|
||||
// clang-format on
|
||||
|
||||
vertices[i] = run_vertex_shader(shader, &vertices[i], i, model);
|
||||
}
|
||||
|
||||
if (render_type == RENDER_TYPE_WIREFRAME) {
|
||||
V3f v0, v1;
|
||||
u64 x0, y0, x1, y1;
|
||||
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
|
||||
v0 = vertices[i];
|
||||
v1 = vertices[(i + 1) % TRIANGLE_VERTICES];
|
||||
|
||||
draw_line(img, (u64)v0.x, (u64)v0.y, (u64)v1.x, (u64)v1.y, colour);
|
||||
}
|
||||
// V3f v0, v1;
|
||||
// u64 x0, y0, x1, y1;
|
||||
// for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
|
||||
// v0 = vertices[i];
|
||||
// v1 = vertices[(i + 1) % TRIANGLE_VERTICES];
|
||||
//
|
||||
// draw_line(img, (u64)v0.x, (u64)v0.y, (u64)v1.x, (u64)v1.y, colour);
|
||||
// }
|
||||
} else if (render_type == RENDER_TYPE_FILLED ||
|
||||
render_type == RENDER_TYPE_SHADED) {
|
||||
fill_triangle(render, shader, vertices, normals, coordinates, colour, model,
|
||||
render_type);
|
||||
fill_triangle(render, shader, vertices, colour, model, render_type);
|
||||
}
|
||||
}
|
||||
|
||||
internal void fill_triangle(Render *render, ShaderID shader,
|
||||
V3f vertices[TRIANGLE_VERTICES],
|
||||
V3f normals[TRIANGLE_VERTICES],
|
||||
V2f coordinates[TRIANGLE_VERTICES], Colour colour,
|
||||
const Model *model, RenderType type) {
|
||||
VertexData vertices[TRIANGLE_VERTICES],
|
||||
Colour colour, const Model *model,
|
||||
RenderType type) {
|
||||
Image *img = &(render->img);
|
||||
Depth *depth = &(render->depth);
|
||||
|
||||
V3f vp_verts[TRIANGLE_VERTICES] = {0};
|
||||
V2f vp_verts[TRIANGLE_VERTICES] = {0};
|
||||
for (u64 i = 0; i < TRIANGLE_VERTICES; ++i) {
|
||||
vp_verts[i] = get_viewport_vertex(&vertices[i], img);
|
||||
vp_verts[i] = get_viewport_vertex(&vertices[i].position, img);
|
||||
}
|
||||
|
||||
TriangleBBox bbox = get_triangle_bbox(img, vp_verts);
|
||||
|
||||
V3f vert0 = vp_verts[0];
|
||||
V3f vert1 = vp_verts[1];
|
||||
V3f vert2 = vp_verts[2];
|
||||
V3f point;
|
||||
|
||||
V2f point;
|
||||
V3f coords;
|
||||
f32 z;
|
||||
f32 zbuf;
|
||||
f32 px, py, pz;
|
||||
V3f position;
|
||||
f32 nx, ny, nz;
|
||||
V3f normal;
|
||||
f32 tx_u, tx_v;
|
||||
u64 tx_x, tx_y;
|
||||
V2f tex_coords;
|
||||
FragmentData data = {0};
|
||||
FragmentResult result;
|
||||
|
||||
f32 intensity = 1.0f;
|
||||
|
||||
for (u64 y = bbox.y0; y <= bbox.y1; ++y) {
|
||||
for (u64 x = bbox.x0; x <= bbox.x1; ++x) {
|
||||
point = (V3f){x, y, 1.0f};
|
||||
coords = get_barycentric_coords(vert0, vert1, vert2, point);
|
||||
point = (V2f){x, y};
|
||||
coords =
|
||||
get_barycentric_coords(vp_verts[0], vp_verts[1], vp_verts[2], point);
|
||||
if (coords.x < 0.0f || coords.y < 0.0f || coords.z < 0.0f) {
|
||||
continue;
|
||||
}
|
||||
|
||||
z = 0.0f;
|
||||
z += vert0.z * coords.x + vert1.z * coords.y + vert2.z * coords.z;
|
||||
z += vertices[0].position.z * coords.x +
|
||||
vertices[1].position.z * coords.y +
|
||||
vertices[2].position.z * coords.z;
|
||||
zbuf = get_pixel(f32, &(render->depth), x, y);
|
||||
|
||||
if (z <= zbuf) {
|
||||
continue;
|
||||
}
|
||||
|
||||
px = vp_verts[0].x * coords.x + vp_verts[1].x * coords.y +
|
||||
vp_verts[2].x * coords.z;
|
||||
py = vp_verts[0].y * coords.x + vp_verts[1].y * coords.y +
|
||||
vp_verts[2].y * coords.z;
|
||||
pz = vp_verts[0].z * coords.x + vp_verts[1].z * coords.y +
|
||||
vp_verts[2].z * coords.z;
|
||||
position = (V3f){px, py, pz};
|
||||
normalise_v3(position);
|
||||
data.position = position;
|
||||
|
||||
nx = normals[0].x * coords.x + normals[1].x * coords.y +
|
||||
normals[2].x * coords.z;
|
||||
ny = normals[0].y * coords.x + normals[1].y * coords.y +
|
||||
normals[2].y * coords.z;
|
||||
nz = normals[0].z * coords.x + normals[1].z * coords.y +
|
||||
normals[2].z * coords.z;
|
||||
normal = (V3f){nx, ny, nz};
|
||||
data.normal = normal;
|
||||
|
||||
tx_u = coordinates[0].u * coords.x + coordinates[1].u * coords.y +
|
||||
coordinates[2].u * coords.z;
|
||||
tx_v = coordinates[0].v * coords.x + coordinates[1].v * coords.y +
|
||||
coordinates[2].v * coords.z;
|
||||
tex_coords = (V2f){tx_u, tx_v};
|
||||
data.tex_coords = tex_coords;
|
||||
|
||||
result = run_fragment_shader(shader, &data, &colour, model);
|
||||
result = run_fragment_shader(shader, &coords, &colour, model);
|
||||
if (DISCARD_FRAGMENT(result)) {
|
||||
continue;
|
||||
}
|
||||
@@ -184,7 +139,7 @@ internal void fill_triangle(Render *render, ShaderID shader,
|
||||
}
|
||||
|
||||
internal TriangleBBox get_triangle_bbox(const Image *img,
|
||||
V3f vertices[TRIANGLE_VERTICES]) {
|
||||
V2f vertices[TRIANGLE_VERTICES]) {
|
||||
f32 x0 = min(vertices[0].x, min(vertices[1].x, vertices[2].x));
|
||||
f32 x1 = max(vertices[0].x, max(vertices[1].x, vertices[2].x));
|
||||
f32 y0 = min(vertices[0].y, min(vertices[1].y, vertices[2].y));
|
||||
@@ -198,7 +153,7 @@ internal TriangleBBox get_triangle_bbox(const Image *img,
|
||||
};
|
||||
}
|
||||
|
||||
internal V3f get_barycentric_coords(V3f a, V3f b, V3f c, V3f p) {
|
||||
internal V3f get_barycentric_coords(V2f a, V2f b, V2f c, V2f p) {
|
||||
V3f x_vec = V3(V3f, f32, c.x, b.x, a.x, a.x, a.x, p.x);
|
||||
V3f y_vec = V3(V3f, f32, c.y, b.y, a.y, a.y, a.y, p.y);
|
||||
|
||||
@@ -210,14 +165,13 @@ internal V3f get_barycentric_coords(V3f a, V3f b, V3f c, V3f p) {
|
||||
return (V3f){1.0f - (u.x + u.y) / u.z, u.y / u.z, u.x / u.z};
|
||||
}
|
||||
|
||||
internal V3f get_viewport_vertex(const V3f *vertex, const Image *img) {
|
||||
internal V2f get_viewport_vertex(const V3f *vertex, const Image *img) {
|
||||
V4f vh = {.x = vertex->x, .y = 0.0f - vertex->y, .z = vertex->z, .w = 1.0f};
|
||||
M4x4f vp = viewport(vh.x, vh.y, img->width, img->height);
|
||||
vh = mat4x4_mul_vec4(vp, vh);
|
||||
vh = mat4x4_mul_vec4(g_viewport, vh);
|
||||
|
||||
V3f output = project_vec4(vh);
|
||||
output.x = clamp(output.x, 0.0f, img->width);
|
||||
output.y = clamp(output.y, 0.0f, img->height);
|
||||
|
||||
return output;
|
||||
return (V2f){output.x, output.y};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user