Fix barycentric coordinates calculation (#2)

Reviewed-on: #2

	* Set up for debugging
	* Replace get_barycentric_coords with different calculation
	* Update commented lines
	* Add V3_ELEM_COUNT macro
	* Change loop in barycentric coordinates calculation function
	* Update debug values
	* Simplify barycentric coordinates function
	* Remove debug lines
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
This commit is contained in:
Abdelrahman Said 2024-08-26 17:12:44 +00:00 committed by Abdelrahman Said
parent 57dcf6457c
commit 319bad9659
4 changed files with 32 additions and 38 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 568 KiB

After

Width:  |  Height:  |  Size: 494 KiB

View File

@ -41,8 +41,8 @@ int main(void) {
PhongMaterial material = { PhongMaterial material = {
.ambient = 0.3f, .ambient = 0.3f,
.diffuse = 1.5f, .diffuse = 1.5f,
.specular = 0.5f, .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,16 @@ 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 x_vec = V3(V3f, f32, c.x, b.x, a.x, a.x, a.x, p.x);
const V2f *v2) { V3f y_vec = V3(V3f, f32, c.y, b.y, a.y, a.y, a.y, p.y);
if (denom == 0.0f) {
return (V3f){-INFINITY, -INFINITY, -INFINITY}; V3f u = cross_product(x_vec, y_vec);
if (fabsf(u.z) < 1e-2) {
return (V3f){-1.0f, 1.0f, 1.0f};
} }
f32 d20 = dot_v2((*v2), (*v0)); return (V3f){1.0f - (u.x + u.y) / u.z, u.y / u.z, u.x / u.z};
f32 d21 = dot_v2((*v2), (*v1));
f32 v = (d11 * d20 - d01 * d21) / denom;
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

@ -5,6 +5,8 @@
#include "typed_list.h" #include "typed_list.h"
#include <math.h> #include <math.h>
#define V3_ELEM_COUNT 3
typedef struct i64x2 V2i; typedef struct i64x2 V2i;
struct i64x2 { struct i64x2 {
i64 x; i64 x;
@ -31,6 +33,9 @@ struct f32x2 {
typedef struct f32x3 V3f; typedef struct f32x3 V3f;
struct f32x3 { struct f32x3 {
union {
f32 elements[V3_ELEM_COUNT];
struct {
union { union {
f32 x; f32 x;
f32 r; f32 r;
@ -44,6 +49,8 @@ struct f32x3 {
f32 b; f32 b;
}; };
}; };
};
};
typedef struct f32x4 V4f; typedef struct f32x4 V4f;
struct f32x4 { struct f32x4 {