Pass float vectors when calculating barycentric coordinates
This commit is contained in:
parent
185f44252f
commit
5dac82ffb7
@ -25,8 +25,8 @@ internal void fill_triangle(Render *render, ShaderID shader,
|
|||||||
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(f32 d00, f32 d01, f32 d11, f32 denom,
|
||||||
const V2i *ab, const V2i *ac,
|
const V2f *v0, const V2f *v1,
|
||||||
const V2i *ap);
|
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) {
|
||||||
@ -105,17 +105,17 @@ internal void fill_triangle(Render *render, ShaderID shader,
|
|||||||
Depth *depth = &(render->depth);
|
Depth *depth = &(render->depth);
|
||||||
TriangleBBox bbox = get_triangle_bbox(img, vertices);
|
TriangleBBox bbox = get_triangle_bbox(img, vertices);
|
||||||
|
|
||||||
V3f v0 = vertices[0];
|
V3f vert0 = vertices[0];
|
||||||
V3f v1 = vertices[1];
|
V3f vert1 = vertices[1];
|
||||||
V3f v2 = vertices[2];
|
V3f vert2 = vertices[2];
|
||||||
|
|
||||||
V2i ab = V2(V2i, i64, v0.x, v0.y, v1.x, v1.y);
|
V2f v0 = V2(V2f, f32, vert0.x, vert0.y, vert1.x, vert1.y);
|
||||||
V2i ac = V2(V2i, i64, v0.x, v0.y, v2.x, v2.y);
|
V2f v1 = V2(V2f, f32, vert0.x, vert0.y, vert2.x, vert2.y);
|
||||||
f32 d00 = dot_v2(ab, ab);
|
f32 d00 = dot_v2(v0, v0);
|
||||||
f32 d01 = dot_v2(ab, ac);
|
f32 d01 = dot_v2(v0, v1);
|
||||||
f32 d11 = dot_v2(ac, ac);
|
f32 d11 = dot_v2(v1, v1);
|
||||||
f32 denom = d00 * d11 - d01 * d01;
|
f32 denom = d00 * d11 - d01 * d01;
|
||||||
V2i ap;
|
V2f v2;
|
||||||
V3f coords;
|
V3f coords;
|
||||||
f32 z;
|
f32 z;
|
||||||
f32 zbuf;
|
f32 zbuf;
|
||||||
@ -133,14 +133,14 @@ 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) {
|
||||||
ap = V2(V2i, i64, v0.x, v0.y, x, y);
|
v2 = V2(V2f, f32, vert0.x, vert0.y, x, y);
|
||||||
coords = get_barycentric_coords(d00, d01, d11, denom, &ab, &ac, &ap);
|
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) {
|
if (coords.x < 0.0f || coords.y < 0.0f || coords.x + coords.y > 1.0f) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
z = 0.0f;
|
z = 0.0f;
|
||||||
z += v0.z * coords.x + v1.z * coords.y + v2.z * coords.z;
|
z += vert0.z * coords.x + vert1.z * coords.y + vert2.z * coords.z;
|
||||||
zbuf = get_pixel(f32, &(render->depth), x, y);
|
zbuf = get_pixel(f32, &(render->depth), x, y);
|
||||||
|
|
||||||
if (z <= zbuf) {
|
if (z <= zbuf) {
|
||||||
@ -200,14 +200,14 @@ 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(f32 d00, f32 d01, f32 d11, f32 denom,
|
||||||
const V2i *ab, const V2i *ac,
|
const V2f *v0, const V2f *v1,
|
||||||
const V2i *ap) {
|
const V2f *v2) {
|
||||||
if (denom == 0.0f) {
|
if (denom == 0.0f) {
|
||||||
return (V3f){-INFINITY, -INFINITY, -INFINITY};
|
return (V3f){-INFINITY, -INFINITY, -INFINITY};
|
||||||
}
|
}
|
||||||
|
|
||||||
f32 d20 = dot_v2((*ap), (*ab));
|
f32 d20 = dot_v2((*v2), (*v0));
|
||||||
f32 d21 = dot_v2((*ap), (*ac));
|
f32 d21 = dot_v2((*v2), (*v1));
|
||||||
|
|
||||||
f32 v = (d11 * d20 - d01 * d21) / denom;
|
f32 v = (d11 * d20 - d01 * d21) / denom;
|
||||||
f32 w = (d00 * d21 - d01 * d20) / denom;
|
f32 w = (d00 * d21 - d01 * d20) / denom;
|
||||||
|
Loading…
Reference in New Issue
Block a user