Untitled
Loading...
Searching...
No Matches
gltf.c File Reference
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include "core/engine_types.h"
#include "utilities/mathf.h"
#include "utilities/engine_utilities.h"
#include "utilities/types.h"
#include "load/load.h"
#include "load/init.h"
#include "nuklear/nuklear.h"
#include "nuklear/nuklear_sdl_gl3.h"
#include "cJSON/cJSON.h"
#include "utilities/utilities.h"

Go to the source code of this file.

Functions

int comp_count (const char *type)
int comp_size (int componentType)
char * load_text_file (const char *path)
unsigned char * load_bin_file (const char *path, int expectedLen)
int str_eq (const char *a, const char *b)
int json_get_int (cJSON *obj, const char *key, int defv)
const char * json_get_str (cJSON *obj, const char *key)
void json_get_vec3 (cJSON *obj, const char *key, vec3 *out, const float def[3])
void json_get_vec4 (cJSON *obj, const char *key, vec4 *out, const float def[3])
void json_get_float_array (cJSON *parent, const char *name, float *out, int count, const float *defaults)
int json_get_tex_index (cJSON *parent, const char *name)
void parse_gltf_materials (cJSON *root, GLTFDoc *doc)
int parse_gltf_json (const char *gltfPath, const char *prefix, GLTFDoc *doc)
unsigned char * accessor_base_ptr (GLTFDoc *doc, const GLTFAccessor *acc)
const unsigned char * accessor_elem_ptr (const GLTFDoc *doc, const GLTFAccessor *acc, int index)
void fetch_vec3f (const GLTFDoc *doc, const GLTFAccessor *acc, int idx, float out[3])
void fetch_vec4 (const GLTFDoc *doc, const GLTFAccessor *acc, int idx, vec4 *out)
void fetch_vec2f (GLTFDoc *doc, const GLTFAccessor *acc, int idx, float out[2])
void fetch_joints4_from_accessor (const GLTFDoc *doc, const GLTFAccessor *acc, int vertexIndex, int out_joints[4])
void fetch_weights4_from_accessor (const GLTFDoc *doc, const GLTFAccessor *acc, int vertexIndex, float out_weights[4])
unsigned int fetch_index (GLTFDoc *doc, const GLTFAccessor *acc, int idx)
mat4 gltf_to_engine_basis ()
void compute_node_globals_recursive (GLTFDoc *doc, int idx, const mat4 *parent_global)
void compute_all_node_globals (GLTFDoc *doc)
void bbox_of_points (vec3 *pts, int n, vec3 *minp, vec3 *maxp)
float extent (vec3 a, vec3 b)
vec3 apply_mapping_to_point (const vec3 p, const int perm[3], const int sign[3])
int detect_best_axis_mapping (GLTFDoc *doc, GLTFAccessor *posAcc, MeshNode *node, int out_perm[3], int out_sign[3])
float * expand_accessor (const GLTFAccessor *acc, int *out_elem_count, GLTFDoc *doc)
AnimationSampler expand_sampler (const GLTFAccessor *input_acc, const GLTFAccessor *output_acc, int interpolation, GLTFDoc *doc)
Animation build_animation (const char *name, GLTFAnimation *animation, GLTFDoc *doc)
void remap_joints_to_skin (const GLTFSkin *skin, const int raw[4], int out[4])
vec3 map_x_z_neg_y (vec3 p)
vec3 map_x_negz_y (vec3 p)
int load_gltf_texture (GLTFTexture *texture, GLTFSampler *samplers, GLTFImage *gltf_images, bool normal)
void apply_static_mesh_to_children (MeshNode *mns, Mesh *meshes, MeshNode *mn, bool static_mesh)
AABB determine_aabb_of_children (MeshNode *mns, Mesh *meshes, MeshNode *mn)
MeshNode * find_root_node_armature (MeshNode *mns, MeshNode *mn)
void load_gltf (const char *gltf_path)
 Loads a GLTF.

Variables

const int perms [6][3]

Function Documentation

◆ comp_count()

int comp_count ( const char * type)

◆ comp_size()

int comp_size ( int componentType)

◆ load_text_file()

char * load_text_file ( const char * path)

Definition at line 44 of file gltf.c.

References load_text_file().

Referenced by load_text_file(), and parse_gltf_json().

◆ load_bin_file()

unsigned char * load_bin_file ( const char * path,
int expectedLen )

Definition at line 56 of file gltf.c.

References load_bin_file().

Referenced by load_bin_file(), and parse_gltf_json().

◆ str_eq()

int str_eq ( const char * a,
const char * b )

Definition at line 70 of file gltf.c.

References str_eq().

Referenced by str_eq().

◆ json_get_int()

int json_get_int ( cJSON * obj,
const char * key,
int defv )

Definition at line 72 of file gltf.c.

References json_get_int().

Referenced by json_get_int(), and parse_gltf_json().

◆ json_get_str()

const char * json_get_str ( cJSON * obj,
const char * key )

Definition at line 77 of file gltf.c.

References json_get_str().

Referenced by json_get_str(), and parse_gltf_json().

◆ json_get_vec3()

void json_get_vec3 ( cJSON * obj,
const char * key,
vec3 * out,
const float def[3] )

Definition at line 81 of file gltf.c.

References json_get_vec3(), vec3::x, vec3::y, and vec3::z.

Referenced by json_get_vec3(), and parse_gltf_json().

◆ json_get_vec4()

void json_get_vec4 ( cJSON * obj,
const char * key,
vec4 * out,
const float def[3] )

Definition at line 94 of file gltf.c.

References json_get_vec4(), vec4::w, vec4::x, vec4::y, and vec4::z.

Referenced by json_get_vec4(), and parse_gltf_json().

◆ json_get_float_array()

void json_get_float_array ( cJSON * parent,
const char * name,
float * out,
int count,
const float * defaults )

Definition at line 111 of file gltf.c.

References json_get_float_array().

Referenced by json_get_float_array(), and parse_gltf_materials().

◆ json_get_tex_index()

int json_get_tex_index ( cJSON * parent,
const char * name )

Definition at line 123 of file gltf.c.

References json_get_tex_index().

Referenced by json_get_tex_index(), and parse_gltf_materials().

◆ parse_gltf_materials()

◆ parse_gltf_json()

int parse_gltf_json ( const char * gltfPath,
const char * prefix,
GLTFDoc * doc )

Definition at line 313 of file gltf.c.

References GLTFDoc::accessorCount, GLTFDoc::accessors, GLTFDoc::animation_count, GLTFDoc::animations, GLTFBufferView::buffer, GLTFImage::buffer_view, GLTFDoc::bufferCount, GLTFDoc::buffers, GLTFAccessor::bufferView, GLTFBuffer::byteLength, GLTFBufferView::byteLength, GLTFAccessor::byteOffset, GLTFBufferView::byteOffset, GLTFBufferView::byteStride, GLTFAnimation::channel_count, GLTFAnimation::channels, MeshNode::child_count, MeshNode::children, COL_HULL, COL_TRIANGLE, MeshNode::collision_type, GLTFAccessor::componentType, GLTFAccessor::count, GLTFBuffer::data, GLTFSkin::ibm_accessor, GLTFDoc::image_count, GLTFDoc::images, GLTFPrimitive::indices, GLTFAnimationSampler::input, GLTFAnimationSampler::interpolation, GLTFSkin::joint_count, GLTFSkin::joints, GLTFPrimitive::joints0, json_get_int(), json_get_str(), json_get_vec3(), json_get_vec4(), load_bin_file(), load_text_file(), GLTFSampler::mag_filter, GLTFPrimitive::material, MeshNode::mesh, GLTFDoc::meshCount, GLTFDoc::meshes, GLTFImage::mime_type, GLTFSampler::min_filter, n, GLTFAnimation::name, GLTFImage::name, GLTFSampler::name, GLTFSkin::name, MeshNode::name, GLTFDoc::node_count, GLTFDoc::nodes, GLTFPrimitive::normal, GLTFAnimationSampler::output, MeshNode::parent, parse_gltf_json(), parse_gltf_materials(), GLTFAnimationChannel::path, MeshNode::physics_mode, GLTFPrimitive::position, GLTFMesh::primCount, GLTFMesh::prims, MeshNode::rotation, GLTFAnimationChannel::sampler, GLTFTexture::sampler, GLTFAnimation::sampler_count, GLTFDoc::sampler_count, GLTFAnimation::samplers, GLTFDoc::samplers, MeshNode::scale, MeshNode::skin, GLTFDoc::skin_count, GLTFDoc::skins, GLTFTexture::source, MeshNode::static_mesh, GLTFPrimitive::tangent, GLTFAnimationChannel::target_node, TARGET_ROTATION, TARGET_SCALE, TARGET_TRANSLATION, TARGET_WEIGHTS, GLTFPrimitive::texcoord0, GLTFDoc::texture_count, GLTFDoc::textures, MeshNode::translation, GLTFAccessor::type, GLTFBuffer::uri, GLTFImage::uri, GLTFDoc::viewCount, GLTFDoc::views, GLTFPrimitive::weights0, GLTFSampler::wrap_s, and GLTFSampler::wrap_t.

Referenced by load_gltf(), and parse_gltf_json().

◆ accessor_base_ptr()

◆ accessor_elem_ptr()

◆ fetch_vec3f()

void fetch_vec3f ( const GLTFDoc * doc,
const GLTFAccessor * acc,
int idx,
float out[3] )

◆ fetch_vec4()

void fetch_vec4 ( const GLTFDoc * doc,
const GLTFAccessor * acc,
int idx,
vec4 * out )

◆ fetch_vec2f()

void fetch_vec2f ( GLTFDoc * doc,
const GLTFAccessor * acc,
int idx,
float out[2] )

Definition at line 844 of file gltf.c.

References accessor_elem_ptr(), GLTFAccessor::componentType, and fetch_vec2f().

Referenced by fetch_vec2f(), and load_gltf().

◆ fetch_joints4_from_accessor()

void fetch_joints4_from_accessor ( const GLTFDoc * doc,
const GLTFAccessor * acc,
int vertexIndex,
int out_joints[4] )

◆ fetch_weights4_from_accessor()

void fetch_weights4_from_accessor ( const GLTFDoc * doc,
const GLTFAccessor * acc,
int vertexIndex,
float out_weights[4] )

◆ fetch_index()

unsigned int fetch_index ( GLTFDoc * doc,
const GLTFAccessor * acc,
int idx )

Definition at line 915 of file gltf.c.

References accessor_elem_ptr(), GLTFAccessor::componentType, and fetch_index().

Referenced by fetch_index(), and load_gltf().

◆ gltf_to_engine_basis()

mat4 gltf_to_engine_basis ( )

Definition at line 926 of file gltf.c.

References gltf_to_engine_basis().

Referenced by compute_node_globals_recursive(), and gltf_to_engine_basis().

◆ compute_node_globals_recursive()

◆ compute_all_node_globals()

void compute_all_node_globals ( GLTFDoc * doc)

◆ bbox_of_points()

void bbox_of_points ( vec3 * pts,
int n,
vec3 * minp,
vec3 * maxp )

Definition at line 959 of file gltf.c.

References bbox_of_points(), n, vec3::x, vec3::y, and vec3::z.

Referenced by bbox_of_points().

◆ extent()

float extent ( vec3 a,
vec3 b )

Definition at line 968 of file gltf.c.

References extent(), and vec3::x.

Referenced by extent().

◆ apply_mapping_to_point()

vec3 apply_mapping_to_point ( const vec3 p,
const int perm[3],
const int sign[3] )

Definition at line 970 of file gltf.c.

References apply_mapping_to_point(), perm(), vec3::x, vec3::y, and vec3::z.

Referenced by apply_mapping_to_point(), and detect_best_axis_mapping().

◆ detect_best_axis_mapping()

int detect_best_axis_mapping ( GLTFDoc * doc,
GLTFAccessor * posAcc,
MeshNode * node,
int out_perm[3],
int out_sign[3] )

◆ expand_accessor()

float * expand_accessor ( const GLTFAccessor * acc,
int * out_elem_count,
GLTFDoc * doc )

◆ expand_sampler()

◆ build_animation()

◆ remap_joints_to_skin()

void remap_joints_to_skin ( const GLTFSkin * skin,
const int raw[4],
int out[4] )

Definition at line 1097 of file gltf.c.

References GLTFSkin::joint_count, GLTFSkin::joints, and remap_joints_to_skin().

Referenced by load_gltf(), and remap_joints_to_skin().

◆ map_x_z_neg_y()

vec3 map_x_z_neg_y ( vec3 p)

Definition at line 1107 of file gltf.c.

References map_x_z_neg_y(), vec3::x, vec3::y, and vec3::z.

Referenced by map_x_z_neg_y().

◆ map_x_negz_y()

vec3 map_x_negz_y ( vec3 p)

Definition at line 1108 of file gltf.c.

References map_x_negz_y(), vec3::x, vec3::y, and vec3::z.

Referenced by map_x_negz_y().

◆ load_gltf_texture()

◆ apply_static_mesh_to_children()

void apply_static_mesh_to_children ( MeshNode * mns,
Mesh * meshes,
MeshNode * mn,
bool static_mesh )

◆ determine_aabb_of_children()

◆ find_root_node_armature()

MeshNode * find_root_node_armature ( MeshNode * mns,
MeshNode * mn )

Definition at line 1228 of file gltf.c.

References find_root_node_armature(), MeshNode::name, and MeshNode::parent.

Referenced by find_root_node_armature().

◆ load_gltf()

void load_gltf ( const char * gltf_path)

Loads a GLTF.

Parameters
Thepath and file name of a GLTF.

Definition at line 1250 of file gltf.c.

References Asset::aabb, Mesh::aabb, MeshNode::aabb, Material::absorption_coefficients, GLTFDoc::accessors, Material::alpha_cutoff, Material::alpha_mode, Asset::animation_count, GLTFDoc::animation_count, GLTFSkin::animation_indices, GLTFSkin::animation_indices_count, Asset::animations, GLTFDoc::animations, Material::ao_strength, Material::ao_tex, apply_static_mesh_to_children(), Material::arm_tex, Animation::asset_id, Asset::asset_id, Mesh::asset_id, Globals::assets, AUDIO_NUM_BANDS, Vector::buffer, GLTFDoc::bufferCount, GLTFDoc::buffers, GLTFAccessor::bufferView, build_animation(), GLTFAccessor::byteOffset, calculate_aabb_extents(), CAST_VEC, Animation::channel_count, Animation::channels, GLTFAnimation::channels, MeshNode::child_count, MeshNode::children, compute_all_node_globals(), compute_tri_normal(), GLTFAccessor::count, Vector::count, GLTFBuffer::data, DEFAULT_ARM, determine_aabb_of_children(), Material::diffuse_tex, Material::double_sided, SubMesh::draw_offset, Material::emissive_strength, Material::emissive_tex, MeshNode::ent, fetch_index(), fetch_joints4_from_accessor(), fetch_vec2f(), fetch_vec3f(), fetch_vec4(), fetch_weights4_from_accessor(), GLTFSkin::final_mat, SubMesh::first_index, SubMesh::first_vertex, GLTFMaterial::fwd_shader, Material::fwd_shader, GLTFMaterial::gbuffer_shader, Material::gbuffer_shader, MeshNode::global_matrix, GLOBALS, GLTFSkin::ibm_accessor, Mesh::id, MeshNode::id, GLTFDoc::images, SubMesh::index_count, GLTFPrimitive::indices, MeshFace::indices, init_mesh(), init_vector(), GLTFSkin::inverse_bind, GLTFSkin::joint_count, GLTFSkin::joints, Point::joints, GLTFPrimitive::joints0, Material::ka, Material::kd, Material::ke, Material::ks, SubMesh::lm_offset, load_gltf(), load_gltf_texture(), m4_inverse(), GLTFPrimitive::material, material, SubMesh::material, GLTFDoc::material_count, MeshFace::material_id, Globals::materials, GLTFDoc::materials, MAX_ANIMATION_INDICES, AABB::max_x, AABB::max_y, AABB::max_z, MeshNode::mesh, Asset::mesh_indices, Globals::mesh_nodes, GLTFDoc::meshCount, Globals::meshes, GLTFDoc::meshes, Material::metallic, AABB::min_x, AABB::min_y, AABB::min_z, Material::mr_tex, GLTFAnimation::name, GLTFSkin::name, Material::name, Mesh::name, MeshNode::name, Asset::node_count, GLTFDoc::node_count, Asset::nodes, GLTFDoc::nodes, GLTFPrimitive::normal, MeshFace::normal, Point::normal, Material::normal_scale, Material::normal_tex, Material::override_arm, Material::override_transparent, MeshNode::parent, parse_gltf_json(), Asset::path, MeshNode::pc, Point::pos, GLTFPrimitive::position, GLTFMesh::primCount, GLTFMesh::prims, remap_joints_to_skin(), Material::roughness, Animation::samplers, GLTFAnimation::samplers, GLTFDoc::samplers, Material::scattering_coefficients, MeshNode::skin, Asset::skin_count, GLTFDoc::skin_count, Asset::skins, GLTFDoc::skins, Mesh::static_mesh, MeshNode::static_mesh, Mesh::submeshes, GLTFPrimitive::tangent, Point::tangent, GLTFAnimationChannel::target_node, GLTFPrimitive::texcoord0, GLTFDoc::textures, Mesh::tris, GLTFBuffer::uri, UV, Point::uv, MeshNode::uv_map, vec3_normalize(), vec3_to_vec4(), vec4_to_vec3(), vector_expand(), SubMesh::vertex_count, Mesh::vertices, view, GLTFDoc::views, vec4::w, Point::weights, GLTFPrimitive::weights0, vec3::x, vec3::y, and vec3::z.

Referenced by load_gltf(), main(), and render_main_menu().

Variable Documentation

◆ perms

const int perms[6][3]
Initial value:
= {
{0,1,2}, {0,2,1}, {1,0,2}, {1,2,0}, {2,0,1}, {2,1,0}
}

Definition at line 979 of file gltf.c.

Referenced by detect_best_axis_mapping().