Fancy bunny texture, use color if texture is not bound

This commit is contained in:
sergeypdev 2024-02-15 16:56:50 +04:00
parent 406c40280c
commit 9c441c067a
7 changed files with 16 additions and 5 deletions

2
.gitattributes vendored
View File

@ -6,6 +6,8 @@
*.wav filter=lfs diff=lfs merge=lfs -text *.wav filter=lfs diff=lfs merge=lfs -text
*.mp3 filter=lfs diff=lfs merge=lfs -text *.mp3 filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text *.png filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.jpeg filter=lfs diff=lfs merge=lfs -text
*.tiff filter=lfs diff=lfs merge=lfs -text *.tiff filter=lfs diff=lfs merge=lfs -text
*.bmp filter=lfs diff=lfs merge=lfs -text *.bmp filter=lfs diff=lfs merge=lfs -text
*.ttf filter=lfs diff=lfs merge=lfs -text *.ttf filter=lfs diff=lfs merge=lfs -text

BIN
assets/bunny_tex1.jpg (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -52,7 +52,7 @@ void main() {
out vec4 FragColor; out vec4 FragColor;
void main() { void main() {
vec3 diffuseColor = texture(diffuse, VertexOut.uv).rgb; vec3 diffuseColor = textureSize(diffuse, 0) == ivec2(0) ? color : texture(diffuse, VertexOut.uv).rgb;
vec3 finalColor = vec3(0); vec3 finalColor = vec3(0);
for (int i = 0; i < lights_count; i++) { for (int i = 0; i < lights_count; i++) {

View File

@ -273,7 +273,7 @@ fn buildAssets(b: *std.Build, step: *Step, assetc: *Step.Compile, path: []const
} }
} }
if (std.mem.endsWith(u8, entry.basename, ".png")) { if (std.mem.endsWith(u8, entry.basename, ".png") or std.mem.endsWith(u8, entry.basename, ".jpg")) {
const run_assetc = b.addRunArtifact(assetc); const run_assetc = b.addRunArtifact(assetc);
run_assetc.addFileArg(.{ .path = b.pathJoin(&.{ path, entry.path }) }); run_assetc.addFileArg(.{ .path = b.pathJoin(&.{ path, entry.path }) });
const out_name = try std.mem.concat( const out_name = try std.mem.concat(

View File

@ -104,6 +104,7 @@ pub fn resolveMesh(self: *AssetManager, handle: Handle.Mesh) *const LoadedMesh {
} }
pub fn resolveTexture(self: *AssetManager, handle: Handle.Texture) *const LoadedTexture { pub fn resolveTexture(self: *AssetManager, handle: Handle.Texture) *const LoadedTexture {
if (handle.id == 0) return &NullTexture;
if (self.loaded_assets.getPtr(handle.id)) |asset| { if (self.loaded_assets.getPtr(handle.id)) |asset| {
switch (asset.*) { switch (asset.*) {
.texture => |*texture| { .texture => |*texture| {

View File

@ -65,6 +65,10 @@ pub const InitMemory = struct {
syswm_info: c.SDL_SysWMinfo = .{}, syswm_info: c.SDL_SysWMinfo = .{},
}; };
pub const Material = struct {
diffuse: AssetManager.Handle.Texture = .{},
};
pub const Entity = struct { pub const Entity = struct {
pub const Flags = packed struct { pub const Flags = packed struct {
active: bool = false, active: bool = false,
@ -87,6 +91,7 @@ pub const Entity = struct {
pub const Mesh = struct { pub const Mesh = struct {
handle: AssetManager.Handle.Mesh = .{}, handle: AssetManager.Handle.Mesh = .{},
color: Vec3 = Vec3.one(), color: Vec3 = Vec3.one(),
material: Material = .{},
}; };
pub const PointLight = struct { pub const PointLight = struct {
radius: f32 = std.math.floatEps(f32), // should never be 0 or bad things happen radius: f32 = std.math.floatEps(f32), // should never be 0 or bad things happen
@ -469,7 +474,7 @@ export fn game_init(global_allocator: *std.mem.Allocator) void {
.transform = .{ .pos = Vec3.new(@as(f32, @floatFromInt(i)) * 0.3, 0, 0) }, .transform = .{ .pos = Vec3.new(@as(f32, @floatFromInt(i)) * 0.3, 0, 0) },
.flags = .{ .mesh = true }, .flags = .{ .mesh = true },
.mesh = .{ .handle = a.Meshes.bunny }, .mesh = .{ .handle = a.Meshes.bunny, .material = .{ .diffuse = a.Textures.bunny_tex1 } },
}); });
} }
} }
@ -738,7 +743,7 @@ export fn game_update() bool {
gl.uniform3fv(2, 1, @ptrCast(&color.data)); gl.uniform3fv(2, 1, @ptrCast(&color.data));
gl.GL_ARB_bindless_texture.uniformHandleui64ARB( gl.GL_ARB_bindless_texture.uniformHandleui64ARB(
3, 3,
g_assetman.resolveTexture(a.Textures.@"test").handle, g_assetman.resolveTexture(ent.mesh.material.diffuse).handle,
); );
const mesh_handle = if (ent.flags.mesh) ent.mesh.handle else a.Meshes.sphere; const mesh_handle = if (ent.flags.mesh) ent.mesh.handle else a.Meshes.sphere;

View File

@ -32,7 +32,7 @@ pub fn resolveAssetTypeByExtension(path: []const u8) ?AssetType {
if (std.mem.endsWith(u8, path, ".glsl")) { if (std.mem.endsWith(u8, path, ".glsl")) {
return .Shader; return .Shader;
} }
if (std.mem.endsWith(u8, path, ".png")) { if (std.mem.endsWith(u8, path, ".png") or std.mem.endsWith(u8, path, ".jpg")) {
return .Texture; return .Texture;
} }
return null; return null;