Compare commits
2 Commits
f024d51d85
...
1ab8c964bf
Author | SHA1 | Date | |
---|---|---|---|
1ab8c964bf | |||
677627d8c9 |
@ -6,7 +6,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define BUF_TYPE(TYPE, NAME) \
|
#define BUF_TYPE(TYPE, NAME) \
|
||||||
typedef struct { \
|
typedef struct NAME { \
|
||||||
u64 width; \
|
u64 width; \
|
||||||
u64 height; \
|
u64 height; \
|
||||||
TYPE *buf; \
|
TYPE *buf; \
|
||||||
|
@ -31,7 +31,8 @@ int main(void) {
|
|||||||
return TINY_EXIT_RENDER_INIT_FAILED;
|
return TINY_EXIT_RENDER_INIT_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
Model model = load_obj_file(arena, "resources/head.obj");
|
Model model =
|
||||||
|
load_obj_file(arena, "resources/head_new.obj", "resources/head.pnm");
|
||||||
if (IS_NULL_MODEL(model)) {
|
if (IS_NULL_MODEL(model)) {
|
||||||
return TINY_EXIT_MODEL_LOAD_FAILED;
|
return TINY_EXIT_MODEL_LOAD_FAILED;
|
||||||
}
|
}
|
||||||
|
100
src/obj.c
100
src/obj.c
@ -2,6 +2,7 @@
|
|||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "img.h"
|
#include "img.h"
|
||||||
#include "mem_arena.h"
|
#include "mem_arena.h"
|
||||||
|
#include "pam.h"
|
||||||
#include "typed_list.h"
|
#include "typed_list.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
@ -72,19 +73,20 @@ struct f32_3x3 {
|
|||||||
internal void render_triangle(const Triangle *triangle, const Model *model,
|
internal void render_triangle(const Triangle *triangle, const Model *model,
|
||||||
Render *render, Colour colour, RenderType type);
|
Render *render, Colour colour, RenderType type);
|
||||||
internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES],
|
internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES],
|
||||||
Colour colour);
|
TexCoord coordinates[TRIANGLE_VERTICES],
|
||||||
|
Colour colour, f32 intensity, Image *texture);
|
||||||
internal TriangleBBox get_triangle_bbox(const Image *img,
|
internal TriangleBBox get_triangle_bbox(const Image *img,
|
||||||
Vertex vertices[TRIANGLE_VERTICES]);
|
Vertex 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 V2i *ab, const V2i *ac,
|
||||||
const V2i *ap);
|
const V2i *ap);
|
||||||
internal void get_image_coordinates(const Vertex *vertex, const Image *img,
|
internal void get_image_coordinates(f32 norm_x, f32 norm_y, const Image *img,
|
||||||
u64 *x, u64 *y);
|
u64 *x, u64 *y);
|
||||||
internal u64 ndc_to_image_coordinate(f32 value, u64 max);
|
internal u64 ndc_to_image_coordinate(f32 value, u64 max);
|
||||||
|
|
||||||
V3f g_light_dir = {0.0f, 0.0f, -1.0f};
|
V3f g_light_dir = {0.0f, 0.0f, -1.0f};
|
||||||
|
|
||||||
Model load_obj_file(Arena *arena, const char *filename) {
|
Model load_obj_file(Arena *arena, const char *filename, const char *texture) {
|
||||||
if (!arena) {
|
if (!arena) {
|
||||||
return NULL_MODEL;
|
return NULL_MODEL;
|
||||||
}
|
}
|
||||||
@ -96,21 +98,25 @@ Model load_obj_file(Arena *arena, const char *filename) {
|
|||||||
|
|
||||||
Model model = (Model){
|
Model model = (Model){
|
||||||
.vertices = list_create(Vertex, arena),
|
.vertices = list_create(Vertex, arena),
|
||||||
|
.texture_coordinates = list_create(TexCoord, arena),
|
||||||
.triangles = list_create(Triangle, arena),
|
.triangles = list_create(Triangle, arena),
|
||||||
};
|
};
|
||||||
if (!(model.vertices) || !(model.triangles)) {
|
if (!(model.vertices) || !(model.texture_coordinates) || !(model.triangles)) {
|
||||||
return NULL_MODEL;
|
return NULL_MODEL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char line[8192];
|
char line[8192];
|
||||||
char identifier[8];
|
char identifier[8];
|
||||||
Vertex vertex;
|
Vertex vertex;
|
||||||
|
TexCoord coord;
|
||||||
Triangle triangle;
|
Triangle triangle;
|
||||||
f32 vx, vy, vz;
|
f32 vx, vy, vz;
|
||||||
|
f32 u, v;
|
||||||
u64 fp0, fp1, fp2;
|
u64 fp0, fp1, fp2;
|
||||||
u64 ign_0_1, ign_0_2;
|
u64 tx0, tx1, tx2;
|
||||||
u64 ign_1_1, ign_1_2;
|
u64 ign_0_2;
|
||||||
u64 ign_2_1, ign_2_2;
|
u64 ign_1_2;
|
||||||
|
u64 ign_2_2;
|
||||||
while (fgets(line, 8191, fp) != NULL) {
|
while (fgets(line, 8191, fp) != NULL) {
|
||||||
sscanf(line, "%s", identifier);
|
sscanf(line, "%s", identifier);
|
||||||
if (strncmp(identifier, "v", 8) == 0) {
|
if (strncmp(identifier, "v", 8) == 0) {
|
||||||
@ -119,17 +125,29 @@ Model load_obj_file(Arena *arena, const char *filename) {
|
|||||||
vertex.y = vy;
|
vertex.y = vy;
|
||||||
vertex.z = vz;
|
vertex.z = vz;
|
||||||
list_append(Vertex, arena, model.vertices, vertex);
|
list_append(Vertex, arena, model.vertices, vertex);
|
||||||
|
} else if (strncmp(identifier, "vt", 8) == 0) {
|
||||||
|
sscanf(line + 2, "%f %f", &u, &v);
|
||||||
|
coord.u = u;
|
||||||
|
coord.v = v;
|
||||||
|
list_append(TexCoord, arena, model.texture_coordinates, coord);
|
||||||
} else if (strncmp(identifier, "f", 8) == 0) {
|
} else if (strncmp(identifier, "f", 8) == 0) {
|
||||||
sscanf(line + 2, "%lu/%lu/%lu %lu/%lu/%lu %lu/%lu/%lu", &fp0, &ign_0_1,
|
sscanf(line + 2, "%lu/%lu/%lu %lu/%lu/%lu %lu/%lu/%lu", &fp0, &tx0,
|
||||||
&ign_0_2, &fp1, &ign_1_1, &ign_1_2, &fp2, &ign_2_1, &ign_2_2);
|
&ign_0_2, &fp1, &tx1, &ign_1_2, &fp2, &tx2, &ign_2_2);
|
||||||
// OBJ indices start from 1
|
// OBJ indices start from 1
|
||||||
triangle.p0 = fp0 - 1;
|
triangle.p0 = fp0 - 1;
|
||||||
triangle.p1 = fp1 - 1;
|
triangle.p1 = fp1 - 1;
|
||||||
triangle.p2 = fp2 - 1;
|
triangle.p2 = fp2 - 1;
|
||||||
|
triangle.tx0 = tx0 - 1;
|
||||||
|
triangle.tx1 = tx1 - 1;
|
||||||
|
triangle.tx2 = tx2 - 1;
|
||||||
list_append(Triangle, arena, model.triangles, triangle);
|
list_append(Triangle, arena, model.triangles, triangle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (texture) {
|
||||||
|
model.texture = load_p6_image(arena, texture);
|
||||||
|
}
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,6 +192,11 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
|
|||||||
list_get(model->vertices, triangle->p1),
|
list_get(model->vertices, triangle->p1),
|
||||||
list_get(model->vertices, triangle->p2),
|
list_get(model->vertices, triangle->p2),
|
||||||
};
|
};
|
||||||
|
TexCoord coordinates[TRIANGLE_VERTICES] = {
|
||||||
|
list_get(model->texture_coordinates, triangle->tx0),
|
||||||
|
list_get(model->texture_coordinates, triangle->tx1),
|
||||||
|
list_get(model->texture_coordinates, triangle->tx2),
|
||||||
|
};
|
||||||
|
|
||||||
if (type == RENDER_TYPE_WIREFRAME) {
|
if (type == RENDER_TYPE_WIREFRAME) {
|
||||||
Vertex v0, v1;
|
Vertex v0, v1;
|
||||||
@ -182,8 +205,8 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
|
|||||||
v0 = vertices[i];
|
v0 = vertices[i];
|
||||||
v1 = vertices[(i + 1) % TRIANGLE_VERTICES];
|
v1 = vertices[(i + 1) % TRIANGLE_VERTICES];
|
||||||
|
|
||||||
get_image_coordinates(&v0, img, &x0, &y0);
|
get_image_coordinates(v0.x, v0.y, img, &x0, &y0);
|
||||||
get_image_coordinates(&v1, img, &x1, &y1);
|
get_image_coordinates(v1.x, v1.y, img, &x1, &y1);
|
||||||
|
|
||||||
draw_line(img, x0, y0, x1, y1, colour);
|
draw_line(img, x0, y0, x1, y1, colour);
|
||||||
}
|
}
|
||||||
@ -200,28 +223,26 @@ internal void render_triangle(const Triangle *triangle, const Model *model,
|
|||||||
normalise_v3(normal);
|
normalise_v3(normal);
|
||||||
|
|
||||||
intensity = dot_v3(normal, g_light_dir);
|
intensity = dot_v3(normal, g_light_dir);
|
||||||
|
|
||||||
colour.r *= intensity;
|
|
||||||
colour.g *= intensity;
|
|
||||||
colour.b *= intensity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intensity > 0.0f) {
|
if (intensity > 0.0f) {
|
||||||
fill_triangle(render, vertices, colour);
|
fill_triangle(render, vertices, coordinates, colour, intensity,
|
||||||
|
model->texture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES],
|
internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES],
|
||||||
Colour colour) {
|
TexCoord coordinates[TRIANGLE_VERTICES],
|
||||||
|
Colour colour, f32 intensity, Image *texture) {
|
||||||
Image *img = &(render->img);
|
Image *img = &(render->img);
|
||||||
Depth *depth = &(render->depth);
|
Depth *depth = &(render->depth);
|
||||||
TriangleBBox bbox = get_triangle_bbox(img, vertices);
|
TriangleBBox bbox = get_triangle_bbox(img, vertices);
|
||||||
|
|
||||||
u64 v0x, v0y, v1x, v1y, v2x, v2y;
|
u64 v0x, v0y, v1x, v1y, v2x, v2y;
|
||||||
get_image_coordinates(&(vertices[0]), img, &v0x, &v0y);
|
get_image_coordinates(vertices[0].x, vertices[0].y, img, &v0x, &v0y);
|
||||||
get_image_coordinates(&(vertices[1]), img, &v1x, &v1y);
|
get_image_coordinates(vertices[1].x, vertices[1].y, img, &v1x, &v1y);
|
||||||
get_image_coordinates(&(vertices[2]), img, &v2x, &v2y);
|
get_image_coordinates(vertices[2].x, vertices[2].y, img, &v2x, &v2y);
|
||||||
|
|
||||||
V2i ab = V2(V2i, i64, v0x, v0y, v1x, v1y);
|
V2i ab = V2(V2i, i64, v0x, v0y, v1x, v1y);
|
||||||
V2i ac = V2(V2i, i64, v0x, v0y, v2x, v2y);
|
V2i ac = V2(V2i, i64, v0x, v0y, v2x, v2y);
|
||||||
@ -233,6 +254,14 @@ internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES],
|
|||||||
V3f coords;
|
V3f coords;
|
||||||
f32 z;
|
f32 z;
|
||||||
f32 zbuf;
|
f32 zbuf;
|
||||||
|
f32 tx_u, tx_v;
|
||||||
|
u64 tx_x, tx_y;
|
||||||
|
|
||||||
|
if (!texture) {
|
||||||
|
colour.r *= intensity;
|
||||||
|
colour.g *= intensity;
|
||||||
|
colour.b *= intensity;
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
||||||
@ -248,6 +277,20 @@ internal void fill_triangle(Render *render, Vertex vertices[TRIANGLE_VERTICES],
|
|||||||
zbuf = get_pixel(f32, &(render->depth), x, y);
|
zbuf = get_pixel(f32, &(render->depth), x, y);
|
||||||
|
|
||||||
if (z > zbuf) {
|
if (z > zbuf) {
|
||||||
|
if (texture) {
|
||||||
|
tx_u = coordinates[0].u * coords.x + coordinates[1].u * coords.y +
|
||||||
|
coordinates[2].u * coords.z;
|
||||||
|
tx_v = coordinates[0].v * coords.x + coordinates[1].v * coords.y +
|
||||||
|
coordinates[2].v * coords.z;
|
||||||
|
tx_x = tx_u * texture->width;
|
||||||
|
tx_y = (1.0f - tx_v) * texture->height;
|
||||||
|
|
||||||
|
colour = get_pixel(Colour, texture, tx_x, tx_y);
|
||||||
|
colour.r *= intensity;
|
||||||
|
colour.g *= intensity;
|
||||||
|
colour.b *= intensity;
|
||||||
|
}
|
||||||
|
|
||||||
set_pixel(depth, x, y, &z);
|
set_pixel(depth, x, y, &z);
|
||||||
set_pixel(img, x, y, &colour);
|
set_pixel(img, x, y, &colour);
|
||||||
}
|
}
|
||||||
@ -264,12 +307,11 @@ internal TriangleBBox get_triangle_bbox(const Image *img,
|
|||||||
f32 y0 = max(vertices[0].y, max(vertices[1].y, vertices[2].y));
|
f32 y0 = max(vertices[0].y, max(vertices[1].y, vertices[2].y));
|
||||||
f32 y1 = min(vertices[0].y, min(vertices[1].y, vertices[2].y));
|
f32 y1 = min(vertices[0].y, min(vertices[1].y, vertices[2].y));
|
||||||
|
|
||||||
return (TriangleBBox){
|
TriangleBBox bbox = {0};
|
||||||
.x0 = ndc_to_image_coordinate(x0, img->width),
|
get_image_coordinates(x0, y0, img, &(bbox.x0), &(bbox.y0));
|
||||||
.y0 = ndc_to_image_coordinate(0.0f - y0, img->height),
|
get_image_coordinates(x1, y1, img, &(bbox.x1), &(bbox.y1));
|
||||||
.x1 = ndc_to_image_coordinate(x1, img->width),
|
|
||||||
.y1 = ndc_to_image_coordinate(0.0f - y1, img->height),
|
return bbox;
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
||||||
@ -285,10 +327,10 @@ internal V3f get_barycentric_coords(f32 d00, f32 d01, f32 d11, f32 denom,
|
|||||||
return (V3f){v, w, u};
|
return (V3f){v, w, u};
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void get_image_coordinates(const Vertex *vertex, const Image *img,
|
internal void get_image_coordinates(f32 norm_x, f32 norm_y, const Image *img,
|
||||||
u64 *x, u64 *y) {
|
u64 *x, u64 *y) {
|
||||||
*x = ndc_to_image_coordinate(vertex->x, img->width);
|
*x = ndc_to_image_coordinate(norm_x, img->width);
|
||||||
*y = ndc_to_image_coordinate(0.0f - vertex->y, img->height);
|
*y = ndc_to_image_coordinate(0.0f - norm_y, img->height);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal u64 ndc_to_image_coordinate(f32 value, u64 max) {
|
internal u64 ndc_to_image_coordinate(f32 value, u64 max) {
|
||||||
|
14
src/obj.h
14
src/obj.h
@ -16,11 +16,20 @@ struct vertex {
|
|||||||
f32 z;
|
f32 z;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef struct texcoord TexCoord;
|
||||||
|
struct texcoord {
|
||||||
|
f32 u;
|
||||||
|
f32 v;
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct triangle Triangle;
|
typedef struct triangle Triangle;
|
||||||
struct triangle {
|
struct triangle {
|
||||||
u64 p0;
|
u64 p0;
|
||||||
u64 p1;
|
u64 p1;
|
||||||
u64 p2;
|
u64 p2;
|
||||||
|
u64 tx0;
|
||||||
|
u64 tx1;
|
||||||
|
u64 tx2;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@ -39,12 +48,15 @@ typedef enum {
|
|||||||
} ColourType;
|
} ColourType;
|
||||||
|
|
||||||
MAKE_LIST_TYPE(Vertex);
|
MAKE_LIST_TYPE(Vertex);
|
||||||
|
MAKE_LIST_TYPE(TexCoord);
|
||||||
MAKE_LIST_TYPE(Triangle);
|
MAKE_LIST_TYPE(Triangle);
|
||||||
|
|
||||||
typedef struct model Model;
|
typedef struct model Model;
|
||||||
struct model {
|
struct model {
|
||||||
LIST_TYPE(Vertex) * vertices;
|
LIST_TYPE(Vertex) * vertices;
|
||||||
|
LIST_TYPE(TexCoord) * texture_coordinates;
|
||||||
LIST_TYPE(Triangle) * triangles;
|
LIST_TYPE(Triangle) * triangles;
|
||||||
|
Image *texture;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct render Render;
|
typedef struct render Render;
|
||||||
@ -53,7 +65,7 @@ struct render {
|
|||||||
Depth depth;
|
Depth depth;
|
||||||
};
|
};
|
||||||
|
|
||||||
Model load_obj_file(Arena *arena, const char *filename);
|
Model load_obj_file(Arena *arena, const char *filename, const char *texture);
|
||||||
bool init_render(Arena *arena, Render *render, u64 width, u64 height);
|
bool init_render(Arena *arena, Render *render, u64 width, u64 height);
|
||||||
void render_model(const Model *model, Render *render, Colour colour,
|
void render_model(const Model *model, Render *render, Colour colour,
|
||||||
RenderType type, ColourType colour_type);
|
RenderType type, ColourType colour_type);
|
||||||
|
58
src/pam.c
58
src/pam.c
@ -1,4 +1,8 @@
|
|||||||
#include "pam.h"
|
#include "pam.h"
|
||||||
|
#include "img.h"
|
||||||
|
#include "mem_arena.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -28,3 +32,57 @@ void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile) {
|
|||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Image *load_p6_image(Arena *arena, const char *filename) {
|
||||||
|
Image *output = NULL;
|
||||||
|
|
||||||
|
if (!arena || !filename) {
|
||||||
|
goto RETURN_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *fp = fopen(filename, "rb");
|
||||||
|
if (!fp) {
|
||||||
|
goto RETURN_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
char type[4] = {0};
|
||||||
|
fread(type, 3, 1, fp);
|
||||||
|
if (strncmp(type, "P6\n", 4) != 0) {
|
||||||
|
goto CLOSE_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 w, h;
|
||||||
|
fscanf(fp, "%lu %lu\n", &w, &h);
|
||||||
|
|
||||||
|
u64 max;
|
||||||
|
fscanf(fp, "%lu\n", &max);
|
||||||
|
if (max > UINT8_MAX) {
|
||||||
|
goto CLOSE_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
output = wapp_mem_arena_alloc(arena, sizeof(Image));
|
||||||
|
if (!output) {
|
||||||
|
goto CLOSE_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
output->width = w;
|
||||||
|
output->height = h;
|
||||||
|
if (!init_buffer(arena, output)) {
|
||||||
|
goto CLOSE_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
Colour colour = {0};
|
||||||
|
for (u64 y = 0; y < h; ++y) {
|
||||||
|
for (u64 x = 0; x < w; ++x) {
|
||||||
|
fread(&colour, 3, 1, fp);
|
||||||
|
colour.a = 255;
|
||||||
|
set_pixel(output, x, y, &colour);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CLOSE_FILE:
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
RETURN_VALUE:
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
#define PAM_H
|
#define PAM_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "img.h"
|
||||||
|
|
||||||
void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile);
|
void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile);
|
||||||
|
Image *load_p6_image(Arena *arena, const char *filename);
|
||||||
|
|
||||||
#endif // PAM_H
|
#endif // PAM_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user