Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package engine
- import "core:mem"
- import "core:encoding/json"
- import "core:fmt"
- import "core:os"
- load_gltf_file :: proc(path: string) {
- gltfData, bin := read_gltf_file(path)
- byteData := bin[0:len(bin)]
- positions: [dynamic]Vec3
- normals: [dynamic]Vec3
- uvs: [dynamic]Vec2
- indices: [dynamic]u16
- get_vec3("POSITION", &gltfData, &byteData, &positions)
- get_vec3("NORMAL", &gltfData, &byteData, &normals)
- get_vec2("TEXCOORD_0", &gltfData, &byteData, &uvs)
- get_indices(&gltfData, &byteData, &indices)
- }
- read_gltf_file :: proc(path: string) -> (json.Object, []byte) {
- data, ok := os.read_entire_file_from_filename(path)
- if !ok {
- fmt.eprintln("Failed to load the file!")
- return {}, {}
- }
- jsonData, err := json.parse(data)
- if err != .None {
- fmt.eprintln("Failed to parse the json file.")
- fmt.eprintln("Error:", err)
- return {}, {}
- }
- bin, ok2 := os.read_entire_file_from_filename("res/models/cube.bin")
- if !ok2 {
- fmt.eprintln("Failed to load the file!")
- return {}, {}
- }
- return jsonData.(json.Object), bin
- }
- get_vec3 :: proc(attributeName: string, gltfData: ^json.Object, byteData: ^[]byte, dst: ^[dynamic]Vec3) {
- accessorIndex := int(gltfData["meshes"].(json.Array)[0].(json.Object)["primitives"].(json.Array)[0].(json.Object)["attributes"].(json.Object)[attributeName].(json.Float))
- accessor := gltfData["accessors"].(json.Array)[accessorIndex].(json.Object)
- bufferViewIndex := int(accessor["bufferView"].(json.Float))
- count := int(accessor["count"].(json.Float))
- bufferViews := gltfData["bufferViews"].(json.Array)
- byteLength := int(bufferViews[bufferViewIndex].(json.Object)["byteLength"].(json.Float))
- byteOffset := int(bufferViews[bufferViewIndex].(json.Object)["byteOffset"].(json.Float))
- resize(&dst^, count)
- mem.copy(&dst^[0], &byteData^[byteOffset], byteLength)
- }
- get_vec2 :: proc(attributeName: string, gltfData: ^json.Object, byteData: ^[]byte, dst: ^[dynamic]Vec2) {
- accessorIndex := int(gltfData["meshes"].(json.Array)[0].(json.Object)["primitives"].(json.Array)[0].(json.Object)["attributes"].(json.Object)[attributeName].(json.Float))
- accessor := gltfData["accessors"].(json.Array)[accessorIndex].(json.Object)
- bufferViewIndex := int(accessor["bufferView"].(json.Float))
- count := int(accessor["count"].(json.Float))
- bufferViews := gltfData["bufferViews"].(json.Array)
- byteLength := int(bufferViews[bufferViewIndex].(json.Object)["byteLength"].(json.Float))
- byteOffset := int(bufferViews[bufferViewIndex].(json.Object)["byteOffset"].(json.Float))
- resize(&dst^, count)
- mem.copy(&dst^[0], &byteData^[byteOffset], byteLength)
- }
- get_indices :: proc(gltfData: ^json.Object, byteData: ^[]byte, dst: ^[dynamic]u16) {
- accessorIndex := int(gltfData["meshes"].(json.Array)[0].(json.Object)["primitives"].(json.Array)[0].(json.Object)["indices"].(json.Float))
- accessor := gltfData["accessors"].(json.Array)[accessorIndex].(json.Object)
- bufferViewIndex := int(accessor["bufferView"].(json.Float))
- count := int(accessor["count"].(json.Float))
- bufferViews := gltfData["bufferViews"].(json.Array)
- byteLength := int(bufferViews[bufferViewIndex].(json.Object)["byteLength"].(json.Float))
- byteOffset := int(bufferViews[bufferViewIndex].(json.Object)["byteOffset"].(json.Float))
- resize(&dst^, count)
- mem.copy(&dst^[0], &byteData^[byteOffset], byteLength)
- }
Advertisement
Add Comment
Please, Sign In to add comment