Created
April 4, 2025 18:23
-
-
Save mahulst/9115712b119273e14e041e81c0ff775b to your computer and use it in GitHub Desktop.
loading a model using cgltf in odin
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
| Vertex2 :: struct { | |
| pos: Vec3, | |
| color: Vec4, | |
| uv: Vec2, | |
| normal: Vec3, | |
| } | |
| data, result := cgltf.parse_file({}, "./assets/barrel.glb") | |
| b := cgltf.load_buffers({}, data, "./assets/barrel.glb") | |
| vertices: [dynamic]Vertex2 = make([dynamic]Vertex2) | |
| indices: [dynamic]u16 = make([dynamic]u16) | |
| for mesh in data.meshes { | |
| for prim in mesh.primitives { | |
| pos_accessor: ^cgltf.accessor | |
| norm_accessor: ^cgltf.accessor | |
| uv_accessor: ^cgltf.accessor | |
| for attr in prim.attributes { | |
| #partial switch attr.type { | |
| case cgltf.attribute_type.position: | |
| pos_accessor = attr.data | |
| case cgltf.attribute_type.normal: | |
| norm_accessor = attr.data | |
| case cgltf.attribute_type.texcoord: | |
| uv_accessor = attr.data | |
| } | |
| } | |
| if pos_accessor == nil { | |
| fmt.println("Primitive missing position attribute") | |
| continue | |
| } | |
| vertex_count := (pos_accessor.count) | |
| for mesh: uint = 0; mesh <= vertex_count; mesh += 1 { | |
| v: Vertex2 = { | |
| color = {1, 1, 1, 1}, | |
| } | |
| ok := cgltf.accessor_read_float( | |
| pos_accessor, | |
| mesh, | |
| &v.pos[0], | |
| 3, | |
| );assert(ok == true) | |
| if norm_accessor != nil { | |
| ok := cgltf.accessor_read_float( | |
| norm_accessor, | |
| mesh, | |
| &v.normal[0], | |
| 3, | |
| );assert(ok == true) | |
| } | |
| if uv_accessor != nil { | |
| ok := cgltf.accessor_read_float( | |
| uv_accessor, | |
| mesh, | |
| &v.uv[0], | |
| 2, | |
| );assert(ok == true) | |
| } | |
| append(&vertices, v) | |
| } | |
| if prim.indices != nil { | |
| for i: uint = 0; i < prim.indices.count; i += 1 { | |
| index := cgltf.accessor_read_index(prim.indices, i) | |
| append(&indices, u16(index)) | |
| } | |
| } | |
| // Here you have the vertices and indices | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment