jumptrigger

Untitled

Nov 8th, 2025 (edited)
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.48 KB | Source Code | 0 0
  1. package engine
  2.  
  3. import "core:mem"
  4. import "core:encoding/json"
  5. import "core:fmt"
  6. import "core:os"
  7.  
  8. load_gltf_file :: proc(path: string) {
  9.     gltfData, bin := read_gltf_file(path)
  10.     byteData := bin[0:len(bin)]
  11.  
  12.     positions: [dynamic]Vec3
  13.     normals: [dynamic]Vec3
  14.     uvs: [dynamic]Vec2
  15.     indices: [dynamic]u16
  16.  
  17.     get_vec3("POSITION", &gltfData, &byteData, &positions)
  18.     get_vec3("NORMAL", &gltfData, &byteData, &normals)
  19.     get_vec2("TEXCOORD_0", &gltfData, &byteData, &uvs)
  20.     get_indices(&gltfData, &byteData, &indices)
  21. }
  22.  
  23. read_gltf_file :: proc(path: string) -> (json.Object, []byte) {
  24.     data, ok := os.read_entire_file_from_filename(path)
  25.     if !ok {
  26.         fmt.eprintln("Failed to load the file!")
  27.         return {}, {}
  28.     }
  29.  
  30.     jsonData, err := json.parse(data)
  31.     if err != .None {
  32.         fmt.eprintln("Failed to parse the json file.")
  33.         fmt.eprintln("Error:", err)
  34.         return {}, {}
  35.     }
  36.  
  37.     bin, ok2 := os.read_entire_file_from_filename("res/models/cube.bin")
  38.     if !ok2 {
  39.         fmt.eprintln("Failed to load the file!")
  40.         return {}, {}
  41.     }
  42.  
  43.     return jsonData.(json.Object), bin
  44. }
  45.  
  46. get_vec3 :: proc(attributeName: string, gltfData: ^json.Object, byteData: ^[]byte, dst: ^[dynamic]Vec3) {
  47.     accessorIndex := int(gltfData["meshes"].(json.Array)[0].(json.Object)["primitives"].(json.Array)[0].(json.Object)["attributes"].(json.Object)[attributeName].(json.Float))
  48.    
  49.     accessor := gltfData["accessors"].(json.Array)[accessorIndex].(json.Object)
  50.     bufferViewIndex := int(accessor["bufferView"].(json.Float))
  51.     count := int(accessor["count"].(json.Float))
  52.  
  53.     bufferViews := gltfData["bufferViews"].(json.Array)
  54.     byteLength := int(bufferViews[bufferViewIndex].(json.Object)["byteLength"].(json.Float))
  55.     byteOffset := int(bufferViews[bufferViewIndex].(json.Object)["byteOffset"].(json.Float))
  56.  
  57.     resize(&dst^, count)
  58.     mem.copy(&dst^[0], &byteData^[byteOffset], byteLength)
  59. }
  60.  
  61. get_vec2 :: proc(attributeName: string, gltfData: ^json.Object, byteData: ^[]byte, dst: ^[dynamic]Vec2) {
  62.     accessorIndex := int(gltfData["meshes"].(json.Array)[0].(json.Object)["primitives"].(json.Array)[0].(json.Object)["attributes"].(json.Object)[attributeName].(json.Float))
  63.    
  64.     accessor := gltfData["accessors"].(json.Array)[accessorIndex].(json.Object)
  65.     bufferViewIndex := int(accessor["bufferView"].(json.Float))
  66.     count := int(accessor["count"].(json.Float))
  67.  
  68.     bufferViews := gltfData["bufferViews"].(json.Array)
  69.     byteLength := int(bufferViews[bufferViewIndex].(json.Object)["byteLength"].(json.Float))
  70.     byteOffset := int(bufferViews[bufferViewIndex].(json.Object)["byteOffset"].(json.Float))
  71.  
  72.     resize(&dst^, count)
  73.     mem.copy(&dst^[0], &byteData^[byteOffset], byteLength)
  74. }
  75.  
  76. get_indices :: proc(gltfData: ^json.Object, byteData: ^[]byte, dst: ^[dynamic]u16) {
  77.     accessorIndex := int(gltfData["meshes"].(json.Array)[0].(json.Object)["primitives"].(json.Array)[0].(json.Object)["indices"].(json.Float))
  78.    
  79.     accessor := gltfData["accessors"].(json.Array)[accessorIndex].(json.Object)
  80.     bufferViewIndex := int(accessor["bufferView"].(json.Float))
  81.     count := int(accessor["count"].(json.Float))
  82.  
  83.     bufferViews := gltfData["bufferViews"].(json.Array)
  84.     byteLength := int(bufferViews[bufferViewIndex].(json.Object)["byteLength"].(json.Float))
  85.     byteOffset := int(bufferViews[bufferViewIndex].(json.Object)["byteOffset"].(json.Float))
  86.  
  87.     resize(&dst^, count)
  88.     mem.copy(&dst^[0], &byteData^[byteOffset], byteLength)
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment