trueno/src/trile.jai

113 lines
3.0 KiB
Plaintext

#load "meshgen.jai";
#scope_file
trile_gfx_table : Table(string, Trile_GFX);
trile_table : Table(string, Trile);
#scope_export
Trile_GFX :: struct {
trixel_colors : sg_image;
vertex_buffer : sg_buffer;
normal_buffer : sg_buffer;
centre_buffer : sg_buffer;
vertices : []float;
vertex_count : s64;
};
// @Note: Creates the gfx things if they are not yet created.
// Could be a bad idea to do this implicitly. Think about it
// once it's more clear how this whole trile storage thing
// will pan out. -ktjst
get_trile_gfx :: (name: string) -> (Trile_GFX, success: bool) {
success, value := table_find_new(*trile_gfx_table, name);
if !success {
// Check if such a trile exists.
trile, success_with_trile := get_trile(name);
if success_with_trile {
// Okay, so we have the trile, let's generate the GFX stuff for it.
gfx := generate_trile_gfx_matias(trile);
set_trile_gfx(name, gfx, true);
return gfx, true;
} else {
return .{}, false;
}
}
return value, success;
}
set_trile_gfx :: (name: string, gfx: Trile_GFX, skip_preexist_check: bool = false) {
if !skip_preexist_check {
old_gfx, success := get_trile_gfx(name);
if success {
sg_destroy_buffer(old_gfx.vertex_buffer);
sg_destroy_buffer(old_gfx.normal_buffer);
sg_destroy_image(old_gfx.trixel_colors);
array_reset(*old_gfx.vertices);
print("Destroyed old GFX buffers for trile: %\n", name);
}
}
table_set(*trile_gfx_table, name, gfx);
}
set_trile :: (name: string, trile: Trile) {
table_set(*trile_table, name, trile);
}
get_trile :: (name: string) -> (*Trile, success: bool) {
trileptr := table_find_pointer(*trile_table, name);
if !trileptr {
print("Failed to get trile with name: %\n", name);
return null, false;
}
return trileptr, true;
}
list_trile :: () -> string {
count := 0;
for v : trile_table {
console_add_output_line(sprint("%", v.name));
count += 1;
}
return tprint("% triles", count);
} @Command
Material :: struct {
addRoughness : u8 = 0;
roughness : u8 = 4;
metallic : u8 = 0;
emittance : u8 = 0;
color : Vector3 = .{1.0, 0.0, 1.0};
}
Trixel :: struct {
material : Material;
empty : bool = false;
};
Trile :: struct {
name : string = "test";
trixels : [16][16][16] Trixel;
};
material_encode_to_char :: (mat: Material) -> u8 {
return (mat.addRoughness & 0x1) | ((mat.emittance & 0x3) << 1) |
((mat.metallic & 0x3) << 3) | ((mat.roughness & 0x7) << 5);
}
material_encode_to_float :: (mat: Material) -> float {
return cast(float)(material_encode_to_char(mat)) / 255.0;
}
material_to_rgba :: (mat: Material) -> (r: u8, g: u8, b: u8, a: u8) {
r : u8 = cast(u8) (mat.color.x * 255.0);
g : u8 = cast(u8) (mat.color.y * 255.0);
b : u8 = cast(u8) (mat.color.z * 255.0);
a : u8 = material_encode_to_char(mat);
return r,g,b,a;
}