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:
parent
57dcf6457c
commit
319bad9659
BIN
output.png
BIN
output.png
Binary file not shown.
Before Width: | Height: | Size: 568 KiB After Width: | Height: | Size: 494 KiB |
@ -41,8 +41,8 @@ int main(void) {
|
||||
PhongMaterial material = {
|
||||
.ambient = 0.3f,
|
||||
.diffuse = 1.5f,
|
||||
.specular = 0.5f,
|
||||
.shininess = 0.1f,
|
||||
.specular = 2.0f,
|
||||
.shininess = 1.5f,
|
||||
};
|
||||
obj.material = material;
|
||||
|
||||
|
@ -25,9 +25,7 @@ internal void fill_triangle(Render *render, ShaderID shader,
|
||||
const Model *model, RenderType type);
|
||||
internal TriangleBBox get_triangle_bbox(const Image *img,
|
||||
V3f vertices[TRIANGLE_VERTICES]);
|
||||
internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom,
|
||||
const V2f *v0, const V2f *v1,
|
||||
const V2f *v2);
|
||||
internal V3f get_barycentric_coords(V3f a, V3f b, V3f c, V3f p);
|
||||
internal V3f get_viewport_vertex(const V3f *vertex, const Image *img);
|
||||
|
||||
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 vert1 = vertices[1];
|
||||
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;
|
||||
f32 z;
|
||||
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 x = bbox.x0; x <= bbox.x1; ++x) {
|
||||
v2 = V2(V2f, f32, vert0.x, vert0.y, x, y);
|
||||
coords = get_barycentric_coords(d00, d01, d11, denom, &v0, &v1, &v2);
|
||||
if (coords.x < 0.0f || coords.y < 0.0f || coords.x + coords.y > 1.0f) {
|
||||
point = (V3f){x, y, 1.0f};
|
||||
coords = get_barycentric_coords(vert0, vert1, vert2, point);
|
||||
if (coords.x < 0.0f || coords.y < 0.0f || coords.z < 0.0f) {
|
||||
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,
|
||||
const V2f *v0, const V2f *v1,
|
||||
const V2f *v2) {
|
||||
if (denom == 0.0f) {
|
||||
return (V3f){-INFINITY, -INFINITY, -INFINITY};
|
||||
internal V3f get_barycentric_coords(V3f a, V3f b, V3f c, V3f 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);
|
||||
|
||||
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));
|
||||
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};
|
||||
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) {
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include "typed_list.h"
|
||||
#include <math.h>
|
||||
|
||||
#define V3_ELEM_COUNT 3
|
||||
|
||||
typedef struct i64x2 V2i;
|
||||
struct i64x2 {
|
||||
i64 x;
|
||||
@ -32,16 +34,21 @@ struct f32x2 {
|
||||
typedef struct f32x3 V3f;
|
||||
struct f32x3 {
|
||||
union {
|
||||
f32 x;
|
||||
f32 r;
|
||||
};
|
||||
union {
|
||||
f32 y;
|
||||
f32 g;
|
||||
};
|
||||
union {
|
||||
f32 z;
|
||||
f32 b;
|
||||
f32 elements[V3_ELEM_COUNT];
|
||||
struct {
|
||||
union {
|
||||
f32 x;
|
||||
f32 r;
|
||||
};
|
||||
union {
|
||||
f32 y;
|
||||
f32 g;
|
||||
};
|
||||
union {
|
||||
f32 z;
|
||||
f32 b;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user