Loading materials and textures

This commit is contained in:
2025-11-22 00:41:56 +01:00
parent 63a8eee18c
commit bf0224ccd8
16 changed files with 563 additions and 626 deletions

View File

@@ -34,7 +34,7 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine) !Textures {
var array: Array = try .initCapacity(allocator, 4);
errdefer {
for (array.items) |texture| {
for (array.items) |*texture| {
texture.deinit(engine);
}
array.deinit(allocator);
@@ -56,12 +56,13 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine) !Textures {
const empty_occlusuion_roughness_metallic_texture: Texture = try .init(engine, 1, 1, .occlusion_roughness_metallic);
array.appendAssumeCapacity(empty_occlusuion_roughness_metallic_texture);
empty_base_color_texture.write([3]u8, &.{.{ 255, 255, 255 }});
empty_emissive_texture.write([3]f32, &.{.{ 1.0, 1.0, 1.0 }});
empty_normal_texture.write([3]u8, &.{.{ 128, 128, 255 }});
empty_occlusuion_roughness_metallic_texture.write([3]u8, &.{.{ 255, 255, 255 }});
try empty_base_color_texture.write([4]u8, engine, &.{.{ 255, 255, 255, 255 }});
try empty_emissive_texture.write([4]f16, engine, &.{.{ 1.0, 1.0, 1.0, 1.0 }});
try empty_normal_texture.write([4]u8, engine, &.{.{ 128, 128, 255, 255 }});
try empty_occlusuion_roughness_metallic_texture.write([4]u8, engine, &.{.{ 255, 255, 255, 255 }});
return .{
.allocator = allocator,
.map = map,
.array = array,
@@ -72,13 +73,13 @@ pub fn init(allocator: std.mem.Allocator, engine: *Engine) !Textures {
};
}
pub fn deinit(self: *Textures, allocator: std.mem.Allocator, engine: *Engine) void {
for (self.array.items) |texture| {
pub fn deinit(self: *Textures, engine: *Engine) void {
for (self.array.items) |*texture| {
texture.deinit(engine);
}
self.array.deinit(allocator);
self.array.deinit(self.allocator);
self.map.deinit(allocator);
self.map.deinit(self.allocator);
}
pub fn getTexture(self: *const Textures, id: Id) ?*Texture {
@@ -90,15 +91,15 @@ pub fn getId(self: *const Textures, key: Key) ?Id {
return self.map.get(key);
}
pub fn getOrLoadId(self: *Textures, engine: *Engine, key: Key) !Id {
pub fn getOrLoadId(self: *Textures, engine: *Engine, atoms: *Atoms, key: Key) !Id {
const entry = try self.map.getOrPut(self.allocator, key);
if (entry.found_existing) {
return entry.value_ptr.*;
} else {
errdefer self.map.remove(key);
errdefer _ = self.map.remove(key);
try self.array.ensureUnusedCapacity(self.allocator, 1);
const texture = try self.loadTexture(engine, key);
const texture = try self.loadTexture(engine, atoms, key);
const id = self.nextId();
@@ -109,13 +110,14 @@ pub fn getOrLoadId(self: *Textures, engine: *Engine, key: Key) !Id {
}
}
fn loadTexture(self: *Textures, engine: *Engine, key: Key) !Texture {
fn loadTexture(self: *Textures, engine: *Engine, atoms: *Atoms, key: Key) !Texture {
const filename = atoms.getString(key.atom).?;
const cwd = std.fs.cwd();
var dir = try cwd.openDir("assets/textures", .{});
defer dir.close();
const file = try dir.openFile(key.name, .{});
const file = try dir.openFile(filename, .{});
defer file.close();
const file_buf = try file.readToEndAlloc(self.allocator, std.math.maxInt(usize));
@@ -128,7 +130,7 @@ fn loadTexture(self: *Textures, engine: *Engine, key: Key) !Texture {
var texture: Texture = try .init(engine, img.width, img.height, key.usage);
errdefer texture.deinit(engine);
texture.write(u8, img.data);
try texture.write(u8, engine, img.data);
return texture;
}