Clean main.c
This commit is contained in:
parent
7f2c4cdd0a
commit
7a17d129a1
117
src/main.c
117
src/main.c
@ -1,115 +1,6 @@
|
||||
#include "img.h"
|
||||
#include "mem_arena.h"
|
||||
#include "misc_utils.h"
|
||||
#include "obj.h"
|
||||
#include "render.h"
|
||||
#include "shaders.h"
|
||||
#include "str.h"
|
||||
#include "vec.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include "aliases.h"
|
||||
#include "tiny.h"
|
||||
|
||||
#define IMAGE_DIMENSION 1200
|
||||
|
||||
enum {
|
||||
TINY_EXIT_SUCCESS,
|
||||
TINY_EXIT_MISSING_ARGS,
|
||||
TINY_EXIT_OBJ_NOT_EXIST,
|
||||
TINY_EXIT_ARENA_INIT_FAILED,
|
||||
TINY_EXIT_RENDER_INIT_FAILED,
|
||||
TINY_EXIT_MODEL_LOAD_FAILED,
|
||||
};
|
||||
|
||||
typedef struct tiny_args TinyArgs;
|
||||
struct tiny_args {
|
||||
Str8 obj;
|
||||
Str8 diffuse;
|
||||
Str8 tangent;
|
||||
};
|
||||
|
||||
internal TinyArgs parse_args(Arena *arena, int argc, char *argv[]);
|
||||
internal i32 tinyrenderer(Arena *arena, TinyArgs args);
|
||||
internal bool file_exists(const Str8 *path);
|
||||
|
||||
i32 main(int argc, char *argv[]) {
|
||||
Arena *arena = NULL;
|
||||
if (!wapp_mem_arena_init(&arena, GB(10))) {
|
||||
return TINY_EXIT_ARENA_INIT_FAILED;
|
||||
}
|
||||
|
||||
TinyArgs args = parse_args(arena, argc, argv);
|
||||
i32 output = tinyrenderer(arena, args);
|
||||
|
||||
wapp_mem_arena_destroy(&arena);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
internal TinyArgs parse_args(Arena *arena, int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
exit(TINY_EXIT_MISSING_ARGS);
|
||||
}
|
||||
|
||||
TinyArgs args = {
|
||||
.obj = str8_lit(argv[1]),
|
||||
};
|
||||
|
||||
if (!file_exists(&args.obj)) {
|
||||
exit(TINY_EXIT_OBJ_NOT_EXIST);
|
||||
}
|
||||
|
||||
u64 substr_end = args.obj.length - 4;
|
||||
|
||||
args.diffuse = str8_substr(arena, &args.obj, 0, substr_end);
|
||||
str8_concat(arena, &args.diffuse, "_diffuse.pnm");
|
||||
if (!file_exists(&args.diffuse)) {
|
||||
args.diffuse = (Str8){0};
|
||||
}
|
||||
|
||||
args.tangent = str8_substr(arena, &args.obj, 0, substr_end);
|
||||
str8_concat(arena, &args.tangent, "_tangent.pnm");
|
||||
if (!file_exists(&args.tangent)) {
|
||||
args.tangent = (Str8){0};
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
internal i32 tinyrenderer(Arena *arena, TinyArgs args) {
|
||||
Colour bg = {.r = 42, .g = 45, .b = 52, .a = 255};
|
||||
Colour main_colour = {.r = 14, .g = 156, .b = 208, .a = 255};
|
||||
|
||||
Render shadowbuffer, framebuffer;
|
||||
if (!init_render(arena, &shadowbuffer, IMAGE_DIMENSION, IMAGE_DIMENSION) ||
|
||||
!init_render(arena, &framebuffer, IMAGE_DIMENSION, IMAGE_DIMENSION)) {
|
||||
return TINY_EXIT_RENDER_INIT_FAILED;
|
||||
}
|
||||
|
||||
const char *diffuse = args.diffuse.length > 0 ? args.diffuse.str : NULL;
|
||||
const char *tangent = args.tangent.length > 0 ? args.tangent.str : NULL;
|
||||
|
||||
Model obj = load_obj_file(arena, args.obj.str, diffuse, tangent);
|
||||
if (IS_INVALID_MODEL(obj)) {
|
||||
return TINY_EXIT_MODEL_LOAD_FAILED;
|
||||
}
|
||||
|
||||
load_shaders(viewport(0, 0, IMAGE_DIMENSION, IMAGE_DIMENSION));
|
||||
|
||||
clear_buffer(&(framebuffer.img), &bg);
|
||||
|
||||
render_model(&obj, &shadowbuffer, depth, RENDER_TYPE_SHADED, main_colour);
|
||||
render_model(&obj, &framebuffer, perspective_diffuse, RENDER_TYPE_SHADED, main_colour);
|
||||
|
||||
save_image(&(framebuffer.img), "result.pam");
|
||||
|
||||
return TINY_EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
internal bool file_exists(const Str8 *path) {
|
||||
struct stat st;
|
||||
return stat(path->str, &st) == 0;
|
||||
i32 main(i32 argc, char *argv[]) {
|
||||
return tiny_main(argc, argv);
|
||||
}
|
||||
|
115
src/tiny/tiny.c
Normal file
115
src/tiny/tiny.c
Normal file
@ -0,0 +1,115 @@
|
||||
#include "img.h"
|
||||
#include "mem_arena.h"
|
||||
#include "misc_utils.h"
|
||||
#include "obj.h"
|
||||
#include "render.h"
|
||||
#include "shaders.h"
|
||||
#include "str.h"
|
||||
#include "vec.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
|
||||
#define IMAGE_DIMENSION 1200
|
||||
|
||||
enum {
|
||||
TINY_EXIT_SUCCESS,
|
||||
TINY_EXIT_MISSING_ARGS,
|
||||
TINY_EXIT_OBJ_NOT_EXIST,
|
||||
TINY_EXIT_ARENA_INIT_FAILED,
|
||||
TINY_EXIT_RENDER_INIT_FAILED,
|
||||
TINY_EXIT_MODEL_LOAD_FAILED,
|
||||
};
|
||||
|
||||
typedef struct tiny_args TinyArgs;
|
||||
struct tiny_args {
|
||||
Str8 obj;
|
||||
Str8 diffuse;
|
||||
Str8 tangent;
|
||||
};
|
||||
|
||||
internal TinyArgs parse_args(Arena *arena, int argc, char *argv[]);
|
||||
internal i32 tinyrenderer(Arena *arena, TinyArgs args);
|
||||
internal bool file_exists(const Str8 *path);
|
||||
|
||||
i32 tiny_main(i32 argc, char *argv[]) {
|
||||
Arena *arena = NULL;
|
||||
if (!wapp_mem_arena_init(&arena, GB(10))) {
|
||||
return TINY_EXIT_ARENA_INIT_FAILED;
|
||||
}
|
||||
|
||||
TinyArgs args = parse_args(arena, argc, argv);
|
||||
i32 output = tinyrenderer(arena, args);
|
||||
|
||||
wapp_mem_arena_destroy(&arena);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
internal TinyArgs parse_args(Arena *arena, int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
exit(TINY_EXIT_MISSING_ARGS);
|
||||
}
|
||||
|
||||
TinyArgs args = {
|
||||
.obj = str8_lit(argv[1]),
|
||||
};
|
||||
|
||||
if (!file_exists(&args.obj)) {
|
||||
exit(TINY_EXIT_OBJ_NOT_EXIST);
|
||||
}
|
||||
|
||||
u64 substr_end = args.obj.length - 4;
|
||||
|
||||
args.diffuse = str8_substr(arena, &args.obj, 0, substr_end);
|
||||
str8_concat(arena, &args.diffuse, "_diffuse.pnm");
|
||||
if (!file_exists(&args.diffuse)) {
|
||||
args.diffuse = (Str8){0};
|
||||
}
|
||||
|
||||
args.tangent = str8_substr(arena, &args.obj, 0, substr_end);
|
||||
str8_concat(arena, &args.tangent, "_tangent.pnm");
|
||||
if (!file_exists(&args.tangent)) {
|
||||
args.tangent = (Str8){0};
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
internal i32 tinyrenderer(Arena *arena, TinyArgs args) {
|
||||
Colour bg = {.r = 42, .g = 45, .b = 52, .a = 255};
|
||||
Colour main_colour = {.r = 14, .g = 156, .b = 208, .a = 255};
|
||||
|
||||
Render shadowbuffer, framebuffer;
|
||||
if (!init_render(arena, &shadowbuffer, IMAGE_DIMENSION, IMAGE_DIMENSION) ||
|
||||
!init_render(arena, &framebuffer, IMAGE_DIMENSION, IMAGE_DIMENSION)) {
|
||||
return TINY_EXIT_RENDER_INIT_FAILED;
|
||||
}
|
||||
|
||||
const char *diffuse = args.diffuse.length > 0 ? args.diffuse.str : NULL;
|
||||
const char *tangent = args.tangent.length > 0 ? args.tangent.str : NULL;
|
||||
|
||||
Model obj = load_obj_file(arena, args.obj.str, diffuse, tangent);
|
||||
if (IS_INVALID_MODEL(obj)) {
|
||||
return TINY_EXIT_MODEL_LOAD_FAILED;
|
||||
}
|
||||
|
||||
load_shaders(viewport(0, 0, IMAGE_DIMENSION, IMAGE_DIMENSION));
|
||||
|
||||
clear_buffer(&(framebuffer.img), &bg);
|
||||
|
||||
render_model(&obj, &shadowbuffer, depth, RENDER_TYPE_SHADED, main_colour);
|
||||
render_model(&obj, &framebuffer, perspective_diffuse, RENDER_TYPE_SHADED, main_colour);
|
||||
|
||||
save_image(&(framebuffer.img), "result.pam");
|
||||
|
||||
return TINY_EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
internal bool file_exists(const Str8 *path) {
|
||||
struct stat st;
|
||||
return stat(path->str, &st) == 0;
|
||||
}
|
8
src/tiny/tiny.h
Normal file
8
src/tiny/tiny.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef TINY_H
|
||||
#define TINY_H
|
||||
|
||||
#include "aliases.h"
|
||||
|
||||
i32 tiny_main(i32 argc, char *argv[]);
|
||||
|
||||
#endif // !TINY_H
|
Loading…
Reference in New Issue
Block a user