From d26100734623f37063206b9b144c2a29fd71d414 Mon Sep 17 00:00:00 2001 From: quou Date: Wed, 1 Jan 2025 18:43:31 +1100 Subject: material system --- convmaterial.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 convmaterial.c (limited to 'convmaterial.c') diff --git a/convmaterial.c b/convmaterial.c new file mode 100644 index 0000000..fde7223 --- /dev/null +++ b/convmaterial.c @@ -0,0 +1,83 @@ +#include +#include + +#include "cfg/cfgparse.h" +#include "material.h" +#include "memory.h" +#include "plat.h" +#include "str.h" + +void parse(cfg_Object* cfg, FILE* f) { + Material_File r; + zero(&r, sizeof r); + fwrite(&r, sizeof r, 1, f); + r.magic[0] = 'M'; + r.magic[1] = 'T'; + r.magic[2] = 'R'; + r.magic[3] = 'L'; + const char* albedo_tex = ""; + const char* ao_tex = ""; + const char* metal_tex = ""; + const char* rough_tex = ""; + const char* normal_tex = ""; + for (; cfg; cfg = cfg->next) { + if (string_equal(cfg->name, "params")) { + r.metalness = find_float_default(cfg, "metalness", 0.0f); + r.roughness = find_float_default(cfg, "roughness", 0.0f); + r.ao = find_float_default(cfg, "ao", 1.0f); + r.albedo = find_colour_default(cfg, "albedo", "ffffff"); + } else if (string_equal(cfg->name, "textures")) { + albedo_tex = find_string_default(cfg, "albedo", ""); + ao_tex = find_string_default(cfg, "ao", ""); + metal_tex = find_string_default(cfg, "metal", ""); + rough_tex = find_string_default(cfg, "rough", ""); + normal_tex = find_string_default(cfg, "normal", ""); + } + } + r.albedo_tex_len = string_len(albedo_tex); + r.ao_tex_len = string_len(ao_tex); + r.metal_tex_len = string_len(metal_tex); + r.rough_tex_len = string_len(rough_tex); + r.normal_tex_len = string_len(normal_tex); + fseek(f, 0, SEEK_SET); + fwrite(&r, sizeof r, 1, f); + fwrite(albedo_tex, 1, r.albedo_tex_len, f); + fwrite(ao_tex, 1, r.ao_tex_len, f); + fwrite(metal_tex, 1, r.metal_tex_len, f); + fwrite(rough_tex, 1, r.rough_tex_len, f); + fwrite(normal_tex, 1, r.albedo_tex_len, f); +} + +int main(int argc, const char** argv) { + int mem_size = 1024 * 1024; + void* mem = malloc(mem_size); + Arena arena; + cfg_Object* cfg; + FILE* infile, * outfile; + char* src; + int size; + init_arena(&arena, mem, mem_size); + if (argc < 3) { + print_err("Usage: %s infile outfile format.\n", argv[0]); + return 1; + } + infile = fopen(argv[1], "r"); + if (!infile) { + print_err("Failed to open %s.\n", argv[1]); + return 2; + } + fseek(infile, 0, SEEK_END); + size = ftell(infile); + rewind(infile); + src = malloc(size + 1); + src[fread(src, 1, size, infile)] = 0; + cfg = cfg_parse(src, &arena); + outfile = fopen(argv[2], "wb"); + if (!outfile) { + print_err("Failed to open %s.\n", argv[1]); + return 3; + } + parse(cfg, outfile); + fclose(outfile); + return 0; +} -- cgit v1.2.3-54-g00ecf