Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* data = read_text_file(path, &frame_arena, null);
- s_json* json = parse_json(data)->object;
- s_json* foo = json_get(json, "buffers", e_json_array)->array;
- char* url = read_json_str(foo, "uri");
- u8* mesh_data = (u8*)read_binary_file(format_text("assets/%s", url), &frame_arena, null, null);
- s_json* accessors = json_get(json, "accessors", e_json_array)->array;
- s_json* buffer_views = json_get(json, "bufferViews", e_json_array)->array;
- s_json* nodes = json_get(json, "nodes", e_json_array)->array;
- s_json* skins = json_get(json, "skins", e_json_array);
- if(skins) { skins = skins->array; }
- int vertex_count = read_json_int(accessors, "count");
- u8* out_vertex_data = (u8*)malloc(10 * c_mb);
- u8* out_vertex_data_cursor = (u8*)out_vertex_data;
- buffer_write<int>(&out_vertex_data_cursor, vertex_count);
- constexpr int c_max_attributes = 7;
- s_carray<int, c_max_attributes> target_arr;
- {
- s_json* j = json_get(json, "meshes", e_json_array)->array;
- j = json_get(j, "primitives", e_json_array)->array;
- target_arr[3] = read_json_int(j, "indices");
- j = json_get(j, "attributes", e_json_object)->object;
- target_arr[0] = read_json_int(j, "POSITION");
- target_arr[1] = read_json_int(j, "TEXCOORD_0");
- target_arr[2] = read_json_int(j, "NORMAL");
- if(skins) {
- target_arr[4] = read_json_int(j, "JOINTS_0");
- target_arr[5] = read_json_int(j, "WEIGHTS_0");
- target_arr[6] = read_json_int(skins, "inverseBindMatrices");
- }
- }
- s_sarray<s_json*, 1024> accessor_arr;
- s_sarray<s_json*, 1024> buffer_view_arr;
- s_sarray<s_json*, 1024> node_arr;
- for(s_json* accessor = accessors; accessor; accessor = accessor->next) {
- accessor_arr.add(accessor);
- }
- for(s_json* buffer_view = buffer_views; buffer_view; buffer_view = buffer_view->next) {
- buffer_view_arr.add(buffer_view);
- }
- int root_bone_index = -1;
- if(skins) {
- int i = 0;
- for_json(node, nodes) {
- node_arr.add(node);
- char* name = read_json_str(node, "name");
- if(strcmp(name, "Bone") == 0) {
- root_bone_index = i;
- }
- i += 1;
- }
- assert(root_bone_index >= 0);
- }
- s_carray<s_json*, c_max_attributes> temp_accessor_arr;
- s_carray<s_json*, c_max_attributes> temp_buffer_view_arr;
- s_carray<u8*, c_max_attributes> ptr_arr;
- for(int j = 0; j < c_max_attributes; j += 1) {
- temp_accessor_arr[j] = accessor_arr[target_arr[j]];
- int buffer_view_index = read_json_int(temp_accessor_arr[j], "bufferView");
- temp_buffer_view_arr[j] = buffer_view_arr[buffer_view_index];
- ptr_arr[j] = mesh_data + read_json_int(temp_buffer_view_arr[j], "byteOffset");
- }
- for(int i = 0; i < vertex_count; i += 1) {
- buffer_write<s_v3>(&out_vertex_data_cursor, *(s_v3*)ptr_arr[0]);
- buffer_write<s_v2>(&out_vertex_data_cursor, *(s_v2*)ptr_arr[1]);
- buffer_write<s_v3>(&out_vertex_data_cursor, *(s_v3*)ptr_arr[2]);
- if(skins) {
- buffer_write<s_v4u8>(&out_vertex_data_cursor, *(s_v4u8*)ptr_arr[4]);
- buffer_write<s_v4>(&out_vertex_data_cursor, *(s_v4*)ptr_arr[5]);
- }
- ptr_arr[0] += sizeof(s_v3);
- ptr_arr[1] += sizeof(s_v2);
- ptr_arr[2] += sizeof(s_v3);
- ptr_arr[4] += sizeof(s_v4u8);
- ptr_arr[5] += sizeof(s_v4);
- }
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv indices start vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- {
- s_json* index_accessor = accessor_arr[target_arr[3]];
- int index_count = read_json_int(index_accessor, "count");
- int component_type = read_json_int(index_accessor, "componentType");
- buffer_write<int>(&out_vertex_data_cursor, index_count);
- if(component_type == GL_UNSIGNED_SHORT) {
- for(int i = 0; i < index_count; i += 1) {
- u16 index = buffer_read<u16>(&ptr_arr[3]);
- buffer_write<u32>(&out_vertex_data_cursor, index);
- }
- }
- else if(component_type == GL_UNSIGNED_INT) {
- for(int i = 0; i < index_count; i += 1) {
- u32 index = buffer_read<u32>(&ptr_arr[3]);
- buffer_write<u32>(&out_vertex_data_cursor, index);
- }
- }
- invalid_else;
- }
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ indices end ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv mesh bounds start vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- {
- s_json* jmin = json_get(accessor_arr[0], "min", e_json_array)->array;
- s_json* jmax = json_get(accessor_arr[0], "max", e_json_array)->array;
- s_v3 v_min = v3(
- jmin->type == e_json_integer ? (float)jmin->integer: jmin->float_val,
- jmin->next->type == e_json_integer ? (float)jmin->next->integer: jmin->next->float_val,
- jmin->next->next->type == e_json_integer ? (float)jmin->next->next->integer: jmin->next->next->float_val
- );
- s_v3 v_max = v3(
- jmax->type == e_json_integer ? (float)jmax->integer: jmax->float_val,
- jmax->next->type == e_json_integer ? (float)jmax->next->integer: jmax->next->float_val,
- jmax->next->next->type == e_json_integer ? (float)jmax->next->next->integer: jmax->next->next->float_val
- );
- buffer_write<s_v3>(&out_vertex_data_cursor, v_min);
- buffer_write<s_v3>(&out_vertex_data_cursor, v_max);
- }
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mesh bounds end ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv bones start vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- if(skins) {
- s_sarray<s_gltf_node, 1024> gltf_node_arr;
- foreach_val(node_i, node, node_arr) {
- s_gltf_node gltf_node = zero;
- gltf_node.transform = v3(0);
- gltf_node.scale = v3(1);
- gltf_node.rotation = make_quaternion();
- json_maybe_read_v3_into(node, &gltf_node.transform, "translation");
- json_maybe_read_v3_into(node, &gltf_node.scale, "scale");
- json_maybe_read_quaternion_into(node, &gltf_node.rotation, "rotation");
- gltf_node_arr.add(gltf_node);
- }
- setup_bones(root_bone_index, &gltf_node_arr, &node_arr);
- buffer_write<int>(&out_vertex_data_cursor, node_arr.count);
- foreach_val(gltf_node_i, gltf_node, gltf_node_arr) {
- buffer_write<s_gltf_node>(&out_vertex_data_cursor, gltf_node);
- }
- }
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bones end ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv inverse bind matrices start vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- if(skins) {
- s_json* inv_accessor = accessor_arr[target_arr[6]];
- int inv_count = read_json_int(inv_accessor, "count");
- int component_type = read_json_int(inv_accessor, "componentType");
- assert(component_type == GL_FLOAT);
- buffer_write<int>(&out_vertex_data_cursor, inv_count);
- s_json* j = json_get(skins, "joints", e_json_array)->array;
- int temp = 0;
- for_node(j2, j) {
- buffer_write<int>(&out_vertex_data_cursor, j2->integer);
- temp += 1;
- }
- assert(temp == inv_count);
- for(int i = 0; i < inv_count; i += 1) {
- s_m4 m4 = buffer_read<s_m4>(&ptr_arr[6]);
- buffer_write<s_m4>(&out_vertex_data_cursor, m4);
- }
- }
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ inverse bind matrices end ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv animation start vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
- if(skins) {
- int* animation_count_ptr = (int*)out_vertex_data_cursor;
- *animation_count_ptr = 0;
- out_vertex_data_cursor += sizeof(int);
- s_json* animations = json_get(json, "animations", e_json_array)->array;
- for_json(animation, animations) {
- *animation_count_ptr += 1;
- s_json* channels = json_get(animation, "channels", e_json_array)->array;
- s_json* samplers = json_get(animation, "samplers", e_json_array)->array;
- s_sarray<s_json*, 1024> sampler_arr;
- for_json(j, samplers) {
- sampler_arr.add(j);
- }
- s_carray2<s_keyframe, c_max_bones, c_max_keyframes> keyframe_arr;
- s_carray<float, c_max_keyframes> keyframe_time_arr = zero;
- b8 first_parse = true;
- for_json(j, channels) {
- s_json* target = json_get(j, "target", e_json_object)->object;
- int sampler_index = read_json_int(j, "sampler");
- int node_index = read_json_int(target, "node");
- char* target_property = read_json_str(target, "path");
- s_json* sampler = sampler_arr[sampler_index];
- int input_index = read_json_int(sampler, "input");
- // @TODO(tkap, 19/09/2024): use interpolation
- // char* interpolation = read_json_string(sampler);
- int output_index = read_json_int(sampler, "output");
- s_json* input_accessor = accessor_arr[input_index]; // @TODO(tkap, 19/09/2024): use it
- s_json* output_accessor = accessor_arr[output_index];
- int output_type = read_json_int(output_accessor, "componentType");
- assert(output_type == GL_FLOAT);
- int input_buffer_view_index = read_json_int(input_accessor, "bufferView");
- int output_buffer_view_index = read_json_int(output_accessor, "bufferView");
- int input_count = read_json_int(input_accessor, "count");
- int output_count = read_json_int(output_accessor, "count");
- s_json* input_buffer_view = buffer_view_arr[input_buffer_view_index];
- s_json* output_buffer_view = buffer_view_arr[output_buffer_view_index];
- int input_byte_offset = read_json_int(input_buffer_view, "byteOffset");
- int output_byte_offset = read_json_int(output_buffer_view, "byteOffset");
- u8* input_ptr = mesh_data + input_byte_offset;
- u8* output_ptr = mesh_data + output_byte_offset;
- assert(input_count == output_count);
- if(first_parse) {
- first_parse = false;
- for(int i = 0; i < input_count; i += 1) {
- keyframe_time_arr[i] = buffer_read<float>(&input_ptr);
- }
- buffer_write<int>(&out_vertex_data_cursor, input_count);
- }
- if(strcmp(target_property, "translation") == 0) {
- assert(strcmp(read_json_str(output_accessor, "type"), "VEC3") == 0);
- for(int i = 0; i < output_count; i += 1) {
- s_v3 v = buffer_read<s_v3>(&output_ptr);
- keyframe_arr[node_index][i].pos = v;
- }
- }
- else if(strcmp(target_property, "rotation") == 0) {
- assert(strcmp(read_json_str(output_accessor, "type"), "VEC4") == 0);
- for(int i = 0; i < output_count; i += 1) {
- keyframe_arr[node_index][i].rotation = buffer_read<s_quaternion>(&output_ptr);
- }
- }
- else if(strcmp(target_property, "scale") == 0) {
- assert(strcmp(read_json_str(output_accessor, "type"), "VEC3") == 0);
- for(int i = 0; i < output_count; i += 1) {
- s_v3 v = buffer_read<s_v3>(&output_ptr);
- keyframe_arr[node_index][i].scale = v;
- }
- }
- }
- memcpy(out_vertex_data_cursor, &keyframe_time_arr, sizeof(keyframe_time_arr));
- out_vertex_data_cursor += sizeof(keyframe_time_arr);
- memcpy(out_vertex_data_cursor, &keyframe_arr, sizeof(keyframe_arr));
- out_vertex_data_cursor += sizeof(keyframe_arr);
- }
- }
- // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ animation end ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- 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