Fix barycentric coordinates calculation #2
BIN
output.png
BIN
output.png
Binary file not shown.
Before Width: | Height: | Size: 568 KiB After Width: | Height: | Size: 494 KiB |
24
src/main.c
24
src/main.c
@ -32,23 +32,23 @@ int main(void) {
|
||||
return TINY_EXIT_RENDER_INIT_FAILED;
|
||||
}
|
||||
|
||||
// Model obj = load_obj_file(arena, RESOURCE("head.obj"),
|
||||
// RESOURCE("head.pnm"),
|
||||
// RESOURCE("head_nm.pnm"));
|
||||
Model obj =
|
||||
load_obj_file(arena, RESOURCE("polygon.obj"), RESOURCE("grid.pnm"), NULL);
|
||||
Model obj = load_obj_file(arena, RESOURCE("head.obj"), RESOURCE("head.pnm"),
|
||||
RESOURCE("head_nm.pnm"));
|
||||
/* Model obj = */
|
||||
/* load_obj_file(arena, RESOURCE("polygon.obj"), RESOURCE("grid.pnm"),
|
||||
* NULL); */
|
||||
if (IS_INVALID_MODEL(obj)) {
|
||||
return TINY_EXIT_MODEL_LOAD_FAILED;
|
||||
}
|
||||
|
||||
PhongMaterial material = {
|
||||
// .ambient = 0.3f,
|
||||
// .diffuse = 1.5f,
|
||||
// .specular = 0.5f,
|
||||
.ambient = 2.0f,
|
||||
.diffuse = 0.0f,
|
||||
.specular = 0.0f,
|
||||
.shininess = 0.1f,
|
||||
.ambient = 0.3f,
|
||||
.diffuse = 1.5f,
|
||||
.specular = 2.0f,
|
||||
/* .ambient = 0.0f, */
|
||||
/* .diffuse = 0.0f, */
|
||||
/* .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,21 @@ 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 s[2];
|
||||
|
||||
for (int i = 2; i--;) {
|
||||
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));
|
||||
f32 d21 = dot_v2((*v2), (*v1));
|
||||
V3f u = cross_product(s[0], s[1]);
|
||||
if (fabsf(u.z) < 1e-2) {
|
||||
return (V3f){-1.0f, 1.0f, 1.0f};
|
||||
}
|
||||
|
||||
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.f - (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) {
|
||||
|
@ -46,14 +46,14 @@ internal M4x4f get_projection_matrix(ProjectionType projection_type);
|
||||
internal f32 get_intensity(const V3f *normal);
|
||||
|
||||
void load_shaders(void) {
|
||||
// M4x4f model_view = lookat(g_eye, g_target, g_up);
|
||||
// M4x4f orthographic_projection =
|
||||
// get_projection_matrix(PROJECTION_TYPE_ORTHOGRAPHIC);
|
||||
// M4x4f perspective_projection =
|
||||
// get_projection_matrix(PROJECTION_TYPE_PERSPECTIVE);
|
||||
M4x4f model_view = mat4x4_identity;
|
||||
M4x4f orthographic_projection = mat4x4_identity;
|
||||
M4x4f perspective_projection = mat4x4_identity;
|
||||
M4x4f model_view = lookat(g_eye, g_target, g_up);
|
||||
M4x4f orthographic_projection =
|
||||
get_projection_matrix(PROJECTION_TYPE_ORTHOGRAPHIC);
|
||||
M4x4f perspective_projection =
|
||||
get_projection_matrix(PROJECTION_TYPE_PERSPECTIVE);
|
||||
/* M4x4f model_view = mat4x4_identity; */
|
||||
/* M4x4f orthographic_projection = mat4x4_identity; */
|
||||
/* M4x4f perspective_projection = mat4x4_identity; */
|
||||
|
||||
perspective.model_view = orthographic.model_view = model_view;
|
||||
perspective.projection = perspective_projection;
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "vec.h"
|
||||
#include <assert.h>
|
||||
|
||||
#define DEPTH_MAX 255
|
||||
|
||||
|
@ -31,6 +31,9 @@ struct f32x2 {
|
||||
|
||||
typedef struct f32x3 V3f;
|
||||
struct f32x3 {
|
||||
union {
|
||||
f32 elements[3];
|
||||
struct {
|
||||
union {
|
||||
f32 x;
|
||||
f32 r;
|
||||
@ -43,6 +46,8 @@ struct f32x3 {
|
||||
f32 z;
|
||||
f32 b;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
typedef struct f32x4 V4f;
|
||||
|
Loading…
Reference in New Issue
Block a user