Render multiple objects

This commit is contained in:
Riyyi
2026-10-31 18:23:23 +01:00
parent af063209ff
commit 148b511009
5 changed files with 514 additions and 91 deletions
+80
View File
@@ -0,0 +1,80 @@
package core
import "base:runtime"
import "core:fmt"
import "sindri:base"
// -----------------------------------------
// Types
Model :: struct {
vertices: []base.Vertex,
indices: []u16,
// Some file formats embed their texture
// texture: ^Texture2D,
}
Texture :: struct {
width: u32,
height: u32,
id: u32,
internal_format: u32,
data_format: u32,
data_type: u32,
}
Assets :: struct {
allocator: runtime.Allocator,
model_list: map[string]Model,
texture_list: map[string]Texture,
}
// -----------------------------------------
// Constructor/destructor
assets_init :: proc(allocator := context.allocator) -> Assets {
return {
allocator = allocator,
model_list = make(map[string]Model, allocator),
texture_list = make(map[string]Texture, allocator),
}
}
assets_destroy :: proc(a: ^Assets) {
for _, &model in a.model_list {
delete(model.vertices, a.allocator)
delete(model.indices, a.allocator)
}
for _, &texture in a.texture_list {
}
delete(a.model_list)
delete(a.texture_list)
}
// -----------------------------------------
// Public functions
load_model :: proc(a: ^Assets, path: string) -> ^Model {
if path in a.model_list do return &a.model_list[path]
vertices, indices, ok := load_obj_model(path, a.allocator)
fmt.assertf(ok, "[assets] failed to read model: %s", path)
a.model_list[path] = {
vertices = vertices,
indices = indices,
}
return &a.model_list[path]
}
// Batching:
// max_vertices 60k
// max_elements 60k
// max_texture_slots 32, query gpu or can wgpu do a larger buffer?
// Scene load: loop through all models, add all model sizes together,
// then create a vertex_buffer using the max size, storing the index somewhere.
+5 -5
View File
@@ -7,21 +7,21 @@ import "core:strconv"
import "core:strings"
import "sindri:base"
import "sindri:platform"
// Minimal OBJ loader: reads v, vt, vn and triangulates convex polygon faces.
// Produces one GPU vertex per face corner, then centers and normalizes the
// model so it fits roughly inside a [-1, 1] cube at the origin
load_obj_model :: proc(
path: string,
allocator := context.allocator,
) -> (
vertices: []base.Vertex,
indices: []u16,
ok: bool,
) {
data, err := os.read_entire_file_from_path(path, context.allocator)
data, err := os.read_entire_file_from_path(path, allocator)
if err != os.ERROR_NONE {
fmt.eprintfln("[core] failed to read model: %s", path)
fmt.eprintfln("[obj] failed to read model: %s", path)
return nil, nil, false
}
defer delete(data)
@@ -141,8 +141,8 @@ load_obj_model :: proc(
v.pos = (v.pos - center) * scale
}
vertices = make([]base.Vertex, len(temp_vertices))
indices = make([]u16, len(temp_indices))
vertices = make([]base.Vertex, len(temp_vertices), allocator)
indices = make([]u16, len(temp_indices), allocator)
copy(vertices, temp_vertices[:])
copy(indices, temp_indices[:])
return vertices, indices, true