Advertisement
Tkap1

Untitled

Dec 8th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.49 KB | None | 0 0
  1. char* data = read_text_file(path, &frame_arena, null);
  2. s_json* json = parse_json(data)->object;
  3. s_json* foo = json_get(json, "buffers", e_json_array)->array;
  4. char* url = read_json_str(foo, "uri");
  5. u8* mesh_data = (u8*)read_binary_file(format_text("assets/%s", url), &frame_arena, null, null);
  6. s_json* accessors = json_get(json, "accessors", e_json_array)->array;
  7. s_json* buffer_views = json_get(json, "bufferViews", e_json_array)->array;
  8. s_json* nodes = json_get(json, "nodes", e_json_array)->array;
  9. s_json* skins = json_get(json, "skins", e_json_array);
  10. if(skins) { skins = skins->array; }
  11.  
  12. int vertex_count = read_json_int(accessors, "count");
  13.  
  14. u8* out_vertex_data = (u8*)malloc(10 * c_mb);
  15. u8* out_vertex_data_cursor = (u8*)out_vertex_data;
  16. buffer_write<int>(&out_vertex_data_cursor, vertex_count);
  17.  
  18. constexpr int c_max_attributes = 7;
  19. s_carray<int, c_max_attributes> target_arr;
  20. {
  21.     s_json* j = json_get(json, "meshes", e_json_array)->array;
  22.     j = json_get(j, "primitives", e_json_array)->array;
  23.     target_arr[3] = read_json_int(j, "indices");
  24.     j = json_get(j, "attributes", e_json_object)->object;
  25.     target_arr[0] = read_json_int(j, "POSITION");
  26.     target_arr[1] = read_json_int(j, "TEXCOORD_0");
  27.     target_arr[2] = read_json_int(j, "NORMAL");
  28.     if(skins) {
  29.         target_arr[4] = read_json_int(j, "JOINTS_0");
  30.         target_arr[5] = read_json_int(j, "WEIGHTS_0");
  31.         target_arr[6] = read_json_int(skins, "inverseBindMatrices");
  32.     }
  33. }
  34. s_sarray<s_json*, 1024> accessor_arr;
  35. s_sarray<s_json*, 1024> buffer_view_arr;
  36. s_sarray<s_json*, 1024> node_arr;
  37.  
  38. for(s_json* accessor = accessors; accessor; accessor = accessor->next) {
  39.     accessor_arr.add(accessor);
  40. }
  41.  
  42. for(s_json* buffer_view = buffer_views; buffer_view; buffer_view = buffer_view->next) {
  43.     buffer_view_arr.add(buffer_view);
  44. }
  45.  
  46. int root_bone_index = -1;
  47. if(skins) {
  48.     int i = 0;
  49.     for_json(node, nodes) {
  50.         node_arr.add(node);
  51.         char* name = read_json_str(node, "name");
  52.         if(strcmp(name, "Bone") == 0) {
  53.             root_bone_index = i;
  54.         }
  55.         i += 1;
  56.     }
  57.     assert(root_bone_index >= 0);
  58. }
  59.  
  60. s_carray<s_json*, c_max_attributes> temp_accessor_arr;
  61. s_carray<s_json*, c_max_attributes> temp_buffer_view_arr;
  62. s_carray<u8*, c_max_attributes> ptr_arr;
  63.  
  64. for(int j = 0; j < c_max_attributes; j += 1) {
  65.     temp_accessor_arr[j] = accessor_arr[target_arr[j]];
  66.     int buffer_view_index = read_json_int(temp_accessor_arr[j], "bufferView");
  67.     temp_buffer_view_arr[j] = buffer_view_arr[buffer_view_index];
  68.     ptr_arr[j] = mesh_data + read_json_int(temp_buffer_view_arr[j], "byteOffset");
  69. }
  70.  
  71. for(int i = 0; i < vertex_count; i += 1) {
  72.  
  73.     buffer_write<s_v3>(&out_vertex_data_cursor, *(s_v3*)ptr_arr[0]);
  74.     buffer_write<s_v2>(&out_vertex_data_cursor, *(s_v2*)ptr_arr[1]);
  75.     buffer_write<s_v3>(&out_vertex_data_cursor, *(s_v3*)ptr_arr[2]);
  76.  
  77.     if(skins) {
  78.         buffer_write<s_v4u8>(&out_vertex_data_cursor, *(s_v4u8*)ptr_arr[4]);
  79.         buffer_write<s_v4>(&out_vertex_data_cursor, *(s_v4*)ptr_arr[5]);
  80.     }
  81.  
  82.     ptr_arr[0] += sizeof(s_v3);
  83.     ptr_arr[1] += sizeof(s_v2);
  84.     ptr_arr[2] += sizeof(s_v3);
  85.     ptr_arr[4] += sizeof(s_v4u8);
  86.     ptr_arr[5] += sizeof(s_v4);
  87. }
  88.  
  89. // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv     indices start       vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  90. {
  91.     s_json* index_accessor = accessor_arr[target_arr[3]];
  92.     int index_count = read_json_int(index_accessor, "count");
  93.     int component_type = read_json_int(index_accessor, "componentType");
  94.  
  95.     buffer_write<int>(&out_vertex_data_cursor, index_count);
  96.  
  97.     if(component_type == GL_UNSIGNED_SHORT) {
  98.         for(int i = 0; i < index_count; i += 1) {
  99.             u16 index = buffer_read<u16>(&ptr_arr[3]);
  100.             buffer_write<u32>(&out_vertex_data_cursor, index);
  101.         }
  102.     }
  103.  
  104.     else if(component_type == GL_UNSIGNED_INT) {
  105.         for(int i = 0; i < index_count; i += 1) {
  106.             u32 index = buffer_read<u32>(&ptr_arr[3]);
  107.             buffer_write<u32>(&out_vertex_data_cursor, index);
  108.         }
  109.     }
  110.     invalid_else;
  111.  
  112. }
  113. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     indices end     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  114.  
  115. // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv     mesh bounds start       vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  116. {
  117.     s_json* jmin = json_get(accessor_arr[0], "min", e_json_array)->array;
  118.     s_json* jmax = json_get(accessor_arr[0], "max", e_json_array)->array;
  119.  
  120.     s_v3 v_min = v3(
  121.         jmin->type == e_json_integer ? (float)jmin->integer: jmin->float_val,
  122.         jmin->next->type == e_json_integer ? (float)jmin->next->integer: jmin->next->float_val,
  123.         jmin->next->next->type == e_json_integer ? (float)jmin->next->next->integer: jmin->next->next->float_val
  124.     );
  125.  
  126.     s_v3 v_max = v3(
  127.         jmax->type == e_json_integer ? (float)jmax->integer: jmax->float_val,
  128.         jmax->next->type == e_json_integer ? (float)jmax->next->integer: jmax->next->float_val,
  129.         jmax->next->next->type == e_json_integer ? (float)jmax->next->next->integer: jmax->next->next->float_val
  130.     );
  131.  
  132.     buffer_write<s_v3>(&out_vertex_data_cursor, v_min);
  133.     buffer_write<s_v3>(&out_vertex_data_cursor, v_max);
  134. }
  135. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     mesh bounds end     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  136.  
  137. // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv     bones start     vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  138. if(skins) {
  139.     s_sarray<s_gltf_node, 1024> gltf_node_arr;
  140.     foreach_val(node_i, node, node_arr) {
  141.         s_gltf_node gltf_node = zero;
  142.         gltf_node.transform = v3(0);
  143.         gltf_node.scale = v3(1);
  144.         gltf_node.rotation = make_quaternion();
  145.         json_maybe_read_v3_into(node, &gltf_node.transform, "translation");
  146.         json_maybe_read_v3_into(node, &gltf_node.scale, "scale");
  147.         json_maybe_read_quaternion_into(node, &gltf_node.rotation, "rotation");
  148.         gltf_node_arr.add(gltf_node);
  149.     }
  150.     setup_bones(root_bone_index, &gltf_node_arr, &node_arr);
  151.     buffer_write<int>(&out_vertex_data_cursor, node_arr.count);
  152.     foreach_val(gltf_node_i, gltf_node, gltf_node_arr) {
  153.         buffer_write<s_gltf_node>(&out_vertex_data_cursor, gltf_node);
  154.     }
  155. }
  156. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     bones end       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  157.  
  158. // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv     inverse bind matrices start     vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  159. if(skins) {
  160.     s_json* inv_accessor = accessor_arr[target_arr[6]];
  161.     int inv_count = read_json_int(inv_accessor, "count");
  162.     int component_type = read_json_int(inv_accessor, "componentType");
  163.     assert(component_type == GL_FLOAT);
  164.  
  165.     buffer_write<int>(&out_vertex_data_cursor, inv_count);
  166.  
  167.     s_json* j = json_get(skins, "joints", e_json_array)->array;
  168.     int temp = 0;
  169.     for_node(j2, j) {
  170.         buffer_write<int>(&out_vertex_data_cursor, j2->integer);
  171.         temp += 1;
  172.     }
  173.     assert(temp == inv_count);
  174.  
  175.     for(int i = 0; i < inv_count; i += 1) {
  176.         s_m4 m4 = buffer_read<s_m4>(&ptr_arr[6]);
  177.         buffer_write<s_m4>(&out_vertex_data_cursor, m4);
  178.     }
  179. }
  180. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     inverse bind matrices end       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  181.  
  182. // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv     animation start     vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  183. if(skins) {
  184.     int* animation_count_ptr = (int*)out_vertex_data_cursor;
  185.     *animation_count_ptr = 0;
  186.     out_vertex_data_cursor += sizeof(int);
  187.     s_json* animations = json_get(json, "animations", e_json_array)->array;
  188.     for_json(animation, animations) {
  189.         *animation_count_ptr += 1;
  190.         s_json* channels = json_get(animation, "channels", e_json_array)->array;
  191.         s_json* samplers = json_get(animation, "samplers", e_json_array)->array;
  192.  
  193.         s_sarray<s_json*, 1024> sampler_arr;
  194.         for_json(j, samplers) {
  195.             sampler_arr.add(j);
  196.         }
  197.  
  198.         s_carray2<s_keyframe, c_max_bones, c_max_keyframes> keyframe_arr;
  199.         s_carray<float, c_max_keyframes> keyframe_time_arr = zero;
  200.  
  201.         b8 first_parse = true;
  202.         for_json(j, channels) {
  203.             s_json* target = json_get(j, "target", e_json_object)->object;
  204.             int sampler_index = read_json_int(j, "sampler");
  205.             int node_index = read_json_int(target, "node");
  206.             char* target_property = read_json_str(target, "path");
  207.  
  208.             s_json* sampler = sampler_arr[sampler_index];
  209.             int input_index = read_json_int(sampler, "input");
  210.             // @TODO(tkap, 19/09/2024): use interpolation
  211.             // char* interpolation = read_json_string(sampler);
  212.             int output_index = read_json_int(sampler, "output");
  213.             s_json* input_accessor = accessor_arr[input_index]; // @TODO(tkap, 19/09/2024): use it
  214.             s_json* output_accessor = accessor_arr[output_index];
  215.             int output_type = read_json_int(output_accessor, "componentType");
  216.             assert(output_type == GL_FLOAT);
  217.  
  218.             int input_buffer_view_index = read_json_int(input_accessor, "bufferView");
  219.             int output_buffer_view_index = read_json_int(output_accessor, "bufferView");
  220.             int input_count = read_json_int(input_accessor, "count");
  221.             int output_count = read_json_int(output_accessor, "count");
  222.             s_json* input_buffer_view = buffer_view_arr[input_buffer_view_index];
  223.             s_json* output_buffer_view = buffer_view_arr[output_buffer_view_index];
  224.             int input_byte_offset = read_json_int(input_buffer_view, "byteOffset");
  225.             int output_byte_offset = read_json_int(output_buffer_view, "byteOffset");
  226.             u8* input_ptr = mesh_data + input_byte_offset;
  227.             u8* output_ptr = mesh_data + output_byte_offset;
  228.             assert(input_count == output_count);
  229.  
  230.             if(first_parse) {
  231.                 first_parse = false;
  232.                 for(int i = 0; i < input_count; i += 1) {
  233.                     keyframe_time_arr[i] = buffer_read<float>(&input_ptr);
  234.                 }
  235.                 buffer_write<int>(&out_vertex_data_cursor, input_count);
  236.             }
  237.  
  238.             if(strcmp(target_property, "translation") == 0) {
  239.                 assert(strcmp(read_json_str(output_accessor, "type"), "VEC3") == 0);
  240.                 for(int i = 0; i < output_count; i += 1) {
  241.                     s_v3 v = buffer_read<s_v3>(&output_ptr);
  242.                     keyframe_arr[node_index][i].pos = v;
  243.                 }
  244.             }
  245.             else if(strcmp(target_property, "rotation") == 0) {
  246.                 assert(strcmp(read_json_str(output_accessor, "type"), "VEC4") == 0);
  247.                 for(int i = 0; i < output_count; i += 1) {
  248.                     keyframe_arr[node_index][i].rotation = buffer_read<s_quaternion>(&output_ptr);
  249.                 }
  250.             }
  251.             else if(strcmp(target_property, "scale") == 0) {
  252.                 assert(strcmp(read_json_str(output_accessor, "type"), "VEC3") == 0);
  253.                 for(int i = 0; i < output_count; i += 1) {
  254.                     s_v3 v = buffer_read<s_v3>(&output_ptr);
  255.                     keyframe_arr[node_index][i].scale = v;
  256.                 }
  257.             }
  258.         }
  259.  
  260.         memcpy(out_vertex_data_cursor, &keyframe_time_arr, sizeof(keyframe_time_arr));
  261.         out_vertex_data_cursor += sizeof(keyframe_time_arr);
  262.  
  263.         memcpy(out_vertex_data_cursor, &keyframe_arr, sizeof(keyframe_arr));
  264.         out_vertex_data_cursor += sizeof(keyframe_arr);
  265.     }
  266.  
  267. }
  268. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     animation end       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  269.  
  270. buffer_write_array_(&cursor, out_vertex_data, (u32)(out_vertex_data_cursor - out_vertex_data));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement