Compare commits
6 Commits
main
...
c66b4e4870
Author | SHA1 | Date | |
---|---|---|---|
c66b4e4870 | |||
0e6235da9a | |||
e905fed7a5 | |||
d0b293ec9a | |||
36b113c036 | |||
c39efad8fb |
BIN
output.png
BIN
output.png
Binary file not shown.
Before Width: | Height: | Size: 568 KiB After Width: | Height: | Size: 494 KiB |
10
src/main.c
10
src/main.c
@@ -34,6 +34,9 @@ int main(void) {
|
|||||||
|
|
||||||
Model obj = load_obj_file(arena, RESOURCE("head.obj"), RESOURCE("head.pnm"),
|
Model obj = load_obj_file(arena, RESOURCE("head.obj"), RESOURCE("head.pnm"),
|
||||||
RESOURCE("head_nm.pnm"));
|
RESOURCE("head_nm.pnm"));
|
||||||
|
// Model obj =
|
||||||
|
// load_obj_file(arena, RESOURCE("polygon.obj"), RESOURCE("grid.pnm"),
|
||||||
|
// NULL);
|
||||||
if (IS_INVALID_MODEL(obj)) {
|
if (IS_INVALID_MODEL(obj)) {
|
||||||
return TINY_EXIT_MODEL_LOAD_FAILED;
|
return TINY_EXIT_MODEL_LOAD_FAILED;
|
||||||
}
|
}
|
||||||
@@ -41,8 +44,11 @@ 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,
|
// .ambient = 1.0f,
|
||||||
|
// .diffuse = 0.0f,
|
||||||
|
// .specular = 0.0f,
|
||||||
|
.shininess = 1.5f,
|
||||||
};
|
};
|
||||||
obj.material = material;
|
obj.material = material;
|
||||||
|
|
||||||
|
@@ -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 (u64 i = 0; 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.0f - (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) {
|
||||||
|
@@ -51,6 +51,9 @@ void load_shaders(void) {
|
|||||||
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 orthographic_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;
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
#include "vec.h"
|
#include "vec.h"
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#define DEPTH_MAX 255
|
#define DEPTH_MAX 255
|
||||||
|
|
||||||
|
@@ -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 {
|
||||||
|
Reference in New Issue
Block a user