-
Notifications
You must be signed in to change notification settings - Fork 8
Remove usingnamespace usage, build script fixups, and support linking memory allocation functions from a shared library #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kcbanner
wants to merge
4
commits into
zig-gamedev:main
Choose a base branch
from
kcbanner:0.15.x
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e29e07c
Use the new @extern field, .is_dll_import, to link the memory allocat…
kcbanner 21f5017
- Alignment fixup
kcbanner dfe5ef1
- Remove usingnamespace usage
kcbanner 04b6715
- Change `appendMeshPrimitive` to use unmanaged data structures
kcbanner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,11 @@ | ||
const std = @import("std"); | ||
const assert = std.debug.assert; | ||
|
||
const mem = @import("memory.zig"); | ||
|
||
/// Deprecated. Use `zmesh.io.zcgltf.parseAndLoadFile` instead. | ||
pub const parseAndLoadFile = zcgltf.parseAndLoadFile; | ||
/// Deprecated. Use `zmesh.io.zcgltf.freeData` instead. | ||
pub const freeData = zcgltf.freeData; | ||
/// Deprecated. Use `zmesh.io.zcgltf.appendMeshPrimitive` instead. | ||
pub const appendMeshPrimitive = zcgltf.appendMeshPrimitive; | ||
|
||
pub const zcgltf = struct { | ||
const bindings = @import("zcgltf.zig"); | ||
const Data = bindings.Data; | ||
|
||
pub usingnamespace bindings; | ||
|
||
pub fn parseAndLoadFile(pathname: [:0]const u8) bindings.Error!*Data { | ||
const options = bindings.Options{ | ||
.memory = .{ | ||
.alloc_func = mem.zmeshAllocUser, | ||
.free_func = mem.zmeshFreeUser, | ||
}, | ||
}; | ||
|
||
const data = try bindings.parseFile(options, pathname); | ||
errdefer bindings.free(data); | ||
|
||
try bindings.loadBuffers(options, data, pathname); | ||
|
||
return data; | ||
} | ||
|
||
pub fn freeData(data: *Data) void { | ||
bindings.free(data); | ||
} | ||
|
||
pub fn appendMeshPrimitive( | ||
data: *Data, | ||
mesh_index: u32, | ||
prim_index: u32, | ||
indices: *std.ArrayList(u32), | ||
positions: *std.ArrayList([3]f32), | ||
normals: ?*std.ArrayList([3]f32), | ||
texcoords0: ?*std.ArrayList([2]f32), | ||
tangents: ?*std.ArrayList([4]f32), | ||
) !void { | ||
assert(mesh_index < data.meshes_count); | ||
assert(prim_index < data.meshes.?[mesh_index].primitives_count); | ||
|
||
const mesh = &data.meshes.?[mesh_index]; | ||
const prim = &mesh.primitives[prim_index]; | ||
|
||
const num_vertices: u32 = @as(u32, @intCast(prim.attributes[0].data.count)); | ||
const num_indices: u32 = @as(u32, @intCast(prim.indices.?.count)); | ||
|
||
// Indices. | ||
{ | ||
try indices.ensureTotalCapacity(indices.items.len + num_indices); | ||
|
||
const accessor = prim.indices.?; | ||
const buffer_view = accessor.buffer_view.?; | ||
|
||
assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0); | ||
assert(buffer_view.buffer.data != null); | ||
|
||
const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) + | ||
accessor.offset + buffer_view.offset; | ||
|
||
if (accessor.stride == 1) { | ||
if (accessor.component_type != .r_8u) { | ||
return error.InvalidIndicesAccessorComponentType; | ||
} | ||
const src = @as([*]const u8, @ptrCast(data_addr)); | ||
var i: u32 = 0; | ||
while (i < num_indices) : (i += 1) { | ||
indices.appendAssumeCapacity(src[i]); | ||
} | ||
} else if (accessor.stride == 2) { | ||
if (accessor.component_type != .r_16u) { | ||
return error.InvalidIndicesAccessorComponentType; | ||
} | ||
const src = @as([*]const u16, @ptrCast(@alignCast(data_addr))); | ||
var i: u32 = 0; | ||
while (i < num_indices) : (i += 1) { | ||
indices.appendAssumeCapacity(src[i]); | ||
} | ||
} else if (accessor.stride == 4) { | ||
if (accessor.component_type != .r_32u) { | ||
return error.InvalidIndicesAccessorComponentType; | ||
} | ||
const src = @as([*]const u32, @ptrCast(@alignCast(data_addr))); | ||
var i: u32 = 0; | ||
while (i < num_indices) : (i += 1) { | ||
indices.appendAssumeCapacity(src[i]); | ||
} | ||
} else { | ||
return error.InvalidIndicesAccessorStride; | ||
} | ||
} | ||
|
||
// Attributes. | ||
{ | ||
const attributes = prim.attributes[0..prim.attributes_count]; | ||
for (attributes) |attrib| { | ||
const accessor = attrib.data; | ||
assert(accessor.component_type == .r_32f); | ||
|
||
const buffer_view = accessor.buffer_view.?; | ||
assert(buffer_view.buffer.data != null); | ||
|
||
assert(accessor.stride == buffer_view.stride or buffer_view.stride == 0); | ||
assert(accessor.stride * accessor.count == buffer_view.size); | ||
|
||
const data_addr = @as([*]const u8, @ptrCast(buffer_view.buffer.data)) + | ||
accessor.offset + buffer_view.offset; | ||
|
||
if (attrib.type == .position) { | ||
assert(accessor.type == .vec3); | ||
const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices]; | ||
try positions.appendSlice(slice); | ||
} else if (attrib.type == .normal) { | ||
if (normals) |n| { | ||
assert(accessor.type == .vec3); | ||
const slice = @as([*]const [3]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices]; | ||
try n.appendSlice(slice); | ||
} | ||
} else if (attrib.type == .texcoord) { | ||
if (texcoords0) |tc| { | ||
assert(accessor.type == .vec2); | ||
const slice = @as([*]const [2]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices]; | ||
try tc.appendSlice(slice); | ||
} | ||
} else if (attrib.type == .tangent) { | ||
if (tangents) |tan| { | ||
assert(accessor.type == .vec4); | ||
const slice = @as([*]const [4]f32, @ptrCast(@alignCast(data_addr)))[0..num_vertices]; | ||
try tan.appendSlice(slice); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
pub const zcgltf = @import("zcgltf.zig"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.