Skip to content

Commit

Permalink
Material.default
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Sep 24, 2024
1 parent 2630246 commit 5d6d509
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 42 deletions.
6 changes: 4 additions & 2 deletions deps/sokol_samples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ const Sample = struct {
root_source_file: []const u8,
};

// .sokol_shader = "minimal/gltf.glsl",

pub const samples = [_]Sample{
.{
.name = "minimal",
Expand All @@ -157,4 +155,8 @@ pub const samples = [_]Sample{
.name = "CesiumMilkTruck",
.root_source_file = "CesiumMilkTruck/main.zig",
},
.{
.name = "draco_bunny",
.root_source_file = "draco_bunny//main.zig",
},
};
8 changes: 8 additions & 0 deletions deps/sokol_samples/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
.url = "git+https://github.com/nothings/stb.git#f75e8d1cad7d90d72ef7a4661f1b994ef78b4e31",
.hash = "1220c4fe5a4c4ebec402f5cdef08bc264b56fb07f259107d2b01ba8d416d88624b50",
},
.vrm = .{
.url = "git+https://github.com/vrm-c/vrm-specification.git#f0da3145ddc13cfcc0877980815f0a249272ef79",
.hash = "1220ac6dab2e9b8f1c6e3bc18f531f5576fa32ce5af1459bd3fbf5ec3ecd20555ea1",
},
.draco = .{
.url = "git+https://github.com/google/draco.git#f4e08970937303d3016b8d841fe9e6141a9b66a0",
.hash = "12203bbaae27dde1346550405b678c29bbc6bae0db6e59fb5be1a20d5d51f454dc04",
},
},
.paths = .{
"",
Expand Down
214 changes: 214 additions & 0 deletions deps/sokol_samples/draco_bunny/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
const std = @import("std");
const sokol = @import("sokol");
const sg = sokol.gfx;

const zigltf = @import("zigltf");
const rowmath = @import("rowmath");
const framework = @import("framework");
// const utils = @import("utils");
const Scene = framework.Scene;
// const gltf_fetcher = @import("gltf_fetcher.zig");

// https://github.khronos.org/glTF-Tutorials/gltfTutorial/gltfTutorial_003_MinimalGltfFile.html
const minimal_gltf =
\\{
\\ "scene": 0,
\\ "scenes" : [
\\ {
\\ "nodes" : [ 0 ]
\\ }
\\ ],
\\
\\ "nodes" : [
\\ {
\\ "mesh" : 0
\\ }
\\ ],
\\
\\ "meshes" : [
\\ {
\\ "primitives" : [ {
\\ "attributes" : {
\\ "POSITION" : 1
\\ },
\\ "indices" : 0
\\ } ]
\\ }
\\ ],
\\
\\ "buffers" : [
\\ {
\\ "uri" : "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAA=",
\\ "byteLength" : 44
\\ }
\\ ],
\\ "bufferViews" : [
\\ {
\\ "buffer" : 0,
\\ "byteOffset" : 0,
\\ "byteLength" : 6,
\\ "target" : 34963
\\ },
\\ {
\\ "buffer" : 0,
\\ "byteOffset" : 8,
\\ "byteLength" : 36,
\\ "target" : 34962
\\ }
\\ ],
\\ "accessors" : [
\\ {
\\ "bufferView" : 0,
\\ "byteOffset" : 0,
\\ "componentType" : 5123,
\\ "count" : 3,
\\ "type" : "SCALAR",
\\ "max" : [ 2 ],
\\ "min" : [ 0 ]
\\ },
\\ {
\\ "bufferView" : 1,
\\ "byteOffset" : 0,
\\ "componentType" : 5126,
\\ "count" : 3,
\\ "type" : "VEC3",
\\ "max" : [ 1.0, 1.0, 0.0 ],
\\ "min" : [ 0.0, 0.0, 0.0 ]
\\ }
\\ ],
\\
\\ "asset" : {
\\ "version" : "2.0"
\\ }
\\}
;

const state = struct {
var pass_action = sg.PassAction{};
var input = rowmath.InputState{};
var orbit = rowmath.OrbitCamera{};
var gltf: ?std.json.Parsed(zigltf.Gltf) = null;
var scene = Scene{};
};

export fn init() void {
sg.setup(.{
.environment = sokol.glue.environment(),
.logger = .{ .func = sokol.log.func },
});
sokol.gl.setup(.{
.logger = .{ .func = sokol.log.func },
});

var debugtext_desc = sokol.debugtext.Desc{
.logger = .{ .func = sokol.log.func },
};
debugtext_desc.fonts[0] = sokol.debugtext.fontOric();
sokol.debugtext.setup(debugtext_desc);

state.pass_action.colors[0] = .{
.load_action = .CLEAR,
.clear_value = .{ .r = 0.1, .g = 0.1, .b = 0.1, .a = 1.0 },
};

state.scene.init(std.heap.c_allocator);

// parse gltf
const allocator = std.heap.c_allocator;
const parsed = std.json.parseFromSlice(
zigltf.Gltf,
allocator,
minimal_gltf,
.{
.ignore_unknown_fields = true,
},
) catch |e| {
std.debug.print("{s}\n", .{@errorName(e)});
@panic("parseFromSlice");
};

// build
state.scene.load(parsed, &.{}) catch |e| {
std.debug.print("{s}\n", .{@errorName(e)});
@panic("Scene.load");
};
}

export fn frame() void {
state.input.screen_width = sokol.app.widthf();
state.input.screen_height = sokol.app.heightf();
state.orbit.frame(state.input);
state.input.mouse_wheel = 0;

sokol.debugtext.canvas(sokol.app.widthf() * 0.5, sokol.app.heightf() * 0.5);
sokol.debugtext.pos(0.5, 0.5);
sokol.debugtext.puts("minimal_gltf");

sg.beginPass(.{
.action = state.pass_action,
.swapchain = sokol.glue.swapchain(),
});
state.scene.draw(state.orbit.camera);
sokol.debugtext.draw();
sg.endPass();
sg.commit();
}

export fn event(e: [*c]const sokol.app.Event) void {
switch (e.*.type) {
.MOUSE_DOWN => {
switch (e.*.mouse_button) {
.LEFT => {
state.input.mouse_left = true;
},
.RIGHT => {
state.input.mouse_right = true;
},
.MIDDLE => {
state.input.mouse_middle = true;
},
.INVALID => {},
}
},
.MOUSE_UP => {
switch (e.*.mouse_button) {
.LEFT => {
state.input.mouse_left = false;
},
.RIGHT => {
state.input.mouse_right = false;
},
.MIDDLE => {
state.input.mouse_middle = false;
},
.INVALID => {},
}
},
.MOUSE_MOVE => {
state.input.mouse_x = e.*.mouse_x;
state.input.mouse_y = e.*.mouse_y;
},
.MOUSE_SCROLL => {
state.input.mouse_wheel = e.*.scroll_y;
},
else => {},
}
}

export fn cleanup() void {
sg.shutdown();
}

pub fn main() void {
sokol.app.run(.{
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = event,
.width = 800,
.height = 600,
.window_title = "rowmath: examples/sokol/camera_simple",
.icon = .{ .sokol_default = true },
.logger = .{ .func = sokol.log.func },
});
}
56 changes: 28 additions & 28 deletions deps/sokol_samples/sample_framework/Scene.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Mesh = @import("Mesh.zig");
pub const Scene = @This();

const light_pos = [3]f32{ -10, -10, -10 };
const light_color = [3]f32{ 1,1,1 };
const light_color = [3]f32{ 1, 1, 1 };
const ambient = [3]f32{ 0.2, 0.2, 0.2 };

allocator: std.mem.Allocator = undefined,
Expand Down Expand Up @@ -139,39 +139,39 @@ pub fn load(
}

const index_accessor = gltf.accessors[indices_accessor_index];
if (primitive.material) |material_index| {
const material = gltf.materials[material_index];
var color: [4]f32 = .{ 1, 1, 1, 1 };
if (material.pbrMetallicRoughness) |pbr| {
if (pbr.baseColorFactor) |base_color| {
color = base_color;
}
const material = if (primitive.material) |material_index|
gltf.materials[material_index]
else
zigltf.Material.default;

var color: [4]f32 = .{ 1, 1, 1, 1 };
if (material.pbrMetallicRoughness) |pbr| {
if (pbr.baseColorFactor) |base_color| {
color = base_color;
}
}

var color_texture = self.white_texture;
if (material.pbrMetallicRoughness) |pbr| {
if (pbr.baseColorTexture) |base| {
const texture = gltf.textures[base.index];
if (texture.source) |source| {
const image_bytes = try gltf_buffer.getImageBytes(source);
if (Mesh.Image.init(image_bytes)) |image| {
defer image.deinit();
color_texture = Mesh.Texture.init(image);
}
var color_texture = self.white_texture;
if (material.pbrMetallicRoughness) |pbr| {
if (pbr.baseColorTexture) |base| {
const texture = gltf.textures[base.index];
if (texture.source) |source| {
const image_bytes = try gltf_buffer.getImageBytes(source);
if (Mesh.Image.init(image_bytes)) |image| {
defer image.deinit();
color_texture = Mesh.Texture.init(image);
}
}
}

try submeshes.append(.{
.draw_count = index_accessor.count,
.submesh_params = .{
.material_rgba = color,
},
.color_texture = color_texture,
});
} else {
@panic("no material");
}

try submeshes.append(.{
.draw_count = index_accessor.count,
.submesh_params = .{
.material_rgba = color,
},
.color_texture = color_texture,
});
index_count += index_accessor.count;
} else {
unreachable;
Expand Down
22 changes: 11 additions & 11 deletions deps/zigltf/src/types/Gltf.zig
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const std = @import("std");
pub const Asset = @import("Asset.zig");
pub const Buffer = @import("Buffer.zig");
pub const BufferView = @import("BufferView.zig");
pub const Accessor = @import("Accessor.zig");
pub const Image = @import("Image.zig");
pub const Texture = @import("Texture.zig");
pub const Material = @import("Material.zig");
pub const Mesh = @import("Mesh.zig");
pub const Node = @import("Node.zig");
pub const Skin = @import("Skin.zig");
pub const Scene = @import("Scene.zig");
const Asset = @import("Asset.zig");
const Buffer = @import("Buffer.zig");
const BufferView = @import("BufferView.zig");
const Accessor = @import("Accessor.zig");
const Image = @import("Image.zig");
const Texture = @import("Texture.zig");
const Material = @import("Material.zig");
const Mesh = @import("Mesh.zig");
const Node = @import("Node.zig");
const Skin = @import("Skin.zig");
const Scene = @import("Scene.zig");
pub const Gltf = @This();

asset: Asset,
Expand Down
6 changes: 5 additions & 1 deletion deps/zigltf/src/types/Material.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const format_helper = @import("format_helper.zig");
pub const Material = @This();

pub const PbrMetallicRoughness = struct {
baseColorFactor: ?struct { f32, f32, f32, f32 } = null,
Expand Down Expand Up @@ -35,8 +36,11 @@ pub const PbrMetallicRoughness = struct {
}
};

name: ?[]const u8 = null,
pub const default = Material{
.name = "gltf_default",
};

name: ?[]const u8 = null,
pbrMetallicRoughness: ?PbrMetallicRoughness = null,

pub fn format(
Expand Down
11 changes: 11 additions & 0 deletions deps/zigltf/src/types/types.zig
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
pub usingnamespace @import("Gltf.zig");
pub usingnamespace @import("Asset.zig");
pub usingnamespace @import("Buffer.zig");
pub usingnamespace @import("BufferView.zig");
pub usingnamespace @import("Accessor.zig");
pub usingnamespace @import("Image.zig");
pub usingnamespace @import("Texture.zig");
pub usingnamespace @import("Material.zig");
pub usingnamespace @import("Mesh.zig");
pub usingnamespace @import("Node.zig");
pub usingnamespace @import("Skin.zig");
pub usingnamespace @import("Scene.zig");

0 comments on commit 5d6d509

Please sign in to comment.