Fix barycentric coordinates calculation #2

Merged
abdelrahman merged 8 commits from debug-barycentric into main 2024-08-26 17:12:45 +00:00
6 changed files with 53 additions and 55 deletions
Showing only changes of commit 36b113c036 - Show all commits

Binary file not shown.

Before

Width:  |  Height:  |  Size: 568 KiB

After

Width:  |  Height:  |  Size: 494 KiB

View File

@ -32,23 +32,23 @@ int main(void) {
return TINY_EXIT_RENDER_INIT_FAILED; return TINY_EXIT_RENDER_INIT_FAILED;
} }
// Model obj = load_obj_file(arena, RESOURCE("head.obj"), Model obj = load_obj_file(arena, RESOURCE("head.obj"), RESOURCE("head.pnm"),
// RESOURCE("head.pnm"), RESOURCE("head_nm.pnm"));
// RESOURCE("head_nm.pnm")); /* Model obj = */
Model obj = /* load_obj_file(arena, RESOURCE("polygon.obj"), RESOURCE("grid.pnm"),
load_obj_file(arena, RESOURCE("polygon.obj"), RESOURCE("grid.pnm"), NULL); * NULL); */
if (IS_INVALID_MODEL(obj)) { if (IS_INVALID_MODEL(obj)) {
return TINY_EXIT_MODEL_LOAD_FAILED; return TINY_EXIT_MODEL_LOAD_FAILED;
} }
PhongMaterial material = { PhongMaterial material = {
// .ambient = 0.3f, .ambient = 0.3f,
// .diffuse = 1.5f, .diffuse = 1.5f,
// .specular = 0.5f, .specular = 2.0f,
.ambient = 2.0f, /* .ambient = 0.0f, */
.diffuse = 0.0f, /* .diffuse = 0.0f, */
.specular = 0.0f, /* .specular = 2.0f, */
.shininess = 0.1f, .shininess = 1.5f,
}; };
obj.material = material; obj.material = material;

View File

@ -25,9 +25,7 @@ internal void fill_triangle(Render *render, ShaderID shader,
const Model *model, RenderType type); const Model *model, RenderType type);
internal TriangleBBox get_triangle_bbox(const Image *img, internal TriangleBBox get_triangle_bbox(const Image *img,
V3f vertices[TRIANGLE_VERTICES]); V3f vertices[TRIANGLE_VERTICES]);
internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom, internal V3f get_barycentric_coords(V3f a, V3f b, V3f c, V3f p);
const V2f *v0, const V2f *v1,
const V2f *v2);
internal V3f get_viewport_vertex(const V3f *vertex, const Image *img); internal V3f get_viewport_vertex(const V3f *vertex, const Image *img);
bool init_render(Arena *arena, Render *render, u64 width, u64 height) { bool init_render(Arena *arena, Render *render, u64 width, u64 height) {
@ -109,14 +107,8 @@ internal void fill_triangle(Render *render, ShaderID shader,
V3f vert0 = vertices[0]; V3f vert0 = vertices[0];
V3f vert1 = vertices[1]; V3f vert1 = vertices[1];
V3f vert2 = vertices[2]; V3f vert2 = vertices[2];
V3f point;
V2f v0 = V2(V2f, f32, vert0.x, vert0.y, vert1.x, vert1.y);
V2f v1 = V2(V2f, f32, vert0.x, vert0.y, vert2.x, vert2.y);
f32 d00 = dot_v2(v0, v0);
f32 d01 = dot_v2(v0, v1);
f32 d11 = dot_v2(v1, v1);
f32 denom = d00 * d11 - d01 * d01;
V2f v2;
V3f coords; V3f coords;
f32 z; f32 z;
f32 zbuf; f32 zbuf;
@ -134,9 +126,9 @@ internal void fill_triangle(Render *render, ShaderID shader,
for (u64 y = bbox.y0; y <= bbox.y1; ++y) { for (u64 y = bbox.y0; y <= bbox.y1; ++y) {
for (u64 x = bbox.x0; x <= bbox.x1; ++x) { for (u64 x = bbox.x0; x <= bbox.x1; ++x) {
v2 = V2(V2f, f32, vert0.x, vert0.y, x, y); point = (V3f){x, y, 1.0f};
coords = get_barycentric_coords(d00, d01, d11, denom, &v0, &v1, &v2); coords = get_barycentric_coords(vert0, vert1, vert2, point);
if (coords.x < 0.0f || coords.y < 0.0f || coords.x + coords.y > 1.0f) { if (coords.x < 0.0f || coords.y < 0.0f || coords.z < 0.0f) {
continue; continue;
} }
@ -200,21 +192,21 @@ internal TriangleBBox get_triangle_bbox(const Image *img,
}; };
} }
internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom, internal V3f get_barycentric_coords(V3f a, V3f b, V3f c, V3f p) {
const V2f *v0, const V2f *v1, V3f s[2];
const V2f *v2) {
if (denom == 0.0f) { for (int i = 2; i--;) {
return (V3f){-INFINITY, -INFINITY, -INFINITY}; s[i].x = c.elements[i] - a.elements[i];
s[i].y = b.elements[i] - a.elements[i];
s[i].z = a.elements[i] - p.elements[i];
} }
f32 d20 = dot_v2((*v2), (*v0)); V3f u = cross_product(s[0], s[1]);
f32 d21 = dot_v2((*v2), (*v1)); if (fabsf(u.z) < 1e-2) {
return (V3f){-1.0f, 1.0f, 1.0f};
}
f32 v = (d11 * d20 - d01 * d21) / denom; return (V3f){1.f - (u.x + u.y) / u.z, u.y / u.z, u.x / u.z};
f32 w = (d00 * d21 - d01 * d20) / denom;
f32 u = 1.0f - v - w;
return (V3f){v, w, u};
} }
internal V3f get_viewport_vertex(const V3f *vertex, const Image *img) { internal V3f get_viewport_vertex(const V3f *vertex, const Image *img) {

View File

@ -46,14 +46,14 @@ internal M4x4f get_projection_matrix(ProjectionType projection_type);
internal f32 get_intensity(const V3f *normal); internal f32 get_intensity(const V3f *normal);
void load_shaders(void) { void load_shaders(void) {
// M4x4f model_view = lookat(g_eye, g_target, g_up); M4x4f model_view = lookat(g_eye, g_target, g_up);
// M4x4f orthographic_projection = M4x4f orthographic_projection =
// get_projection_matrix(PROJECTION_TYPE_ORTHOGRAPHIC); get_projection_matrix(PROJECTION_TYPE_ORTHOGRAPHIC);
// M4x4f perspective_projection = M4x4f perspective_projection =
// get_projection_matrix(PROJECTION_TYPE_PERSPECTIVE); get_projection_matrix(PROJECTION_TYPE_PERSPECTIVE);
M4x4f model_view = mat4x4_identity; /* M4x4f model_view = mat4x4_identity; */
M4x4f orthographic_projection = mat4x4_identity; /* M4x4f orthographic_projection = mat4x4_identity; */
M4x4f perspective_projection = mat4x4_identity; /* M4x4f perspective_projection = mat4x4_identity; */
perspective.model_view = orthographic.model_view = model_view; perspective.model_view = orthographic.model_view = model_view;
perspective.projection = perspective_projection; perspective.projection = perspective_projection;

View File

@ -1,4 +1,5 @@
#include "vec.h" #include "vec.h"
#include <assert.h>
#define DEPTH_MAX 255 #define DEPTH_MAX 255

View File

@ -32,16 +32,21 @@ struct f32x2 {
typedef struct f32x3 V3f; typedef struct f32x3 V3f;
struct f32x3 { struct f32x3 {
union { union {
f32 x; f32 elements[3];
f32 r; struct {
}; union {
union { f32 x;
f32 y; f32 r;
f32 g; };
}; union {
union { f32 y;
f32 z; f32 g;
f32 b; };
union {
f32 z;
f32 b;
};
};
}; };
}; };