96 cgltf_options options = {0};
97 cgltf_data *data = NULL;
98 cgltf_result result = cgltf_parse_file(&options, path, &data);
99 if (result != cgltf_result_success) {
100 printf(
"gmd_gltf_print: Failed to parse GLTF file: %s\n", path);
104 printf(
"--- GLTF Inspector: %s ---\n", path);
105 printf(
"Scenes: %zu\n", data->scenes_count);
107 printf(
"Default Scene: '%s' (%zu root nodes)\n",
108 data->scene->name ? data->scene->name :
"unnamed",
109 data->scene->nodes_count);
110 for (
size_t i = 0; i < data->scene->nodes_count; ++i) {
111 gmd_gltf_print_node(data->scene->nodes[i], 1);
114 printf(
"Meshes: %zu\n", data->meshes_count);
115 printf(
"Materials: %zu\n", data->materials_count);
116 printf(
"Textures: %zu\n", data->textures_count);
117 printf(
"Images: %zu\n", data->images_count);
118 printf(
"---------------------------------\n");
166 memset(mesh, 0,
sizeof(
gm3Mesh));
168 cgltf_options options = {0};
169 cgltf_data *data = NULL;
170 if (cgltf_parse_file(&options, path, &data) != cgltf_result_success)
172 if (cgltf_load_buffers(&options, data, path) != cgltf_result_success) {
178 size_t vertices_cap = 0, normals_cap = 0, texs_cap = 0, faces_cap = 0;
184 strcpy(mtllib->
name,
"gltf_materials");
188 if (data->images_count > 0) {
190 for (
size_t i = 0; i < data->images_count; ++i) {
191 cgltf_image *gimg = &data->images[i];
193 if (gimg->buffer_view) {
194 snprintf(dtex->
path,
sizeof(dtex->
path),
"gltf_embedded_image_%zu", i);
197 (
unsigned char *)gimg->buffer_view->buffer->
data +
198 gimg->buffer_view->offset,
199 gimg->buffer_view->size);
200 }
else if (gimg->uri) {
202 const char *slash = strrchr(path,
'/');
204 size_t dir_len = slash - path;
206 if (dir_len <
sizeof(dir)) {
207 memcpy(dir, path, dir_len);
211 strncpy(dir, path,
sizeof(dir) - 1);
212 dir[
sizeof(dir) - 1] =
'\0';
215 snprintf(dtex->
path,
sizeof(dtex->
path),
"%s/%s", dir, gimg->uri);
223 if (data->materials_count > 0) {
225 for (
size_t i = 0; i < data->materials_count; ++i) {
226 cgltf_material *gmat = &data->materials[i];
232 snprintf(dmat->
name,
sizeof(dmat->
name),
"%s",
233 gmat->name ? gmat->name :
"default");
235 if (gmat->has_pbr_metallic_roughness) {
236 cgltf_pbr_metallic_roughness *pbr = &gmat->pbr_metallic_roughness;
237 dmat->
alpha = pbr->base_color_factor[3];
239 if (pbr->base_color_texture.texture &&
240 pbr->base_color_texture.texture->image) {
242 pbr->base_color_texture.texture->image - data->images;
250 gm_rgb(pbr->base_color_factor[0] * 255,
251 pbr->base_color_factor[1] * 255,
252 pbr->base_color_factor[2] * 255);
259 for (
size_t i = 0; i < data->nodes_count; ++i) {
260 cgltf_node *node = &data->nodes[i];
264 cgltf_float matrix[16];
265 cgltf_node_transform_world(node, matrix);
267 for (
size_t j = 0; j < node->mesh->primitives_count; ++j) {
268 cgltf_primitive *prim = &node->mesh->primitives[j];
269 if (prim->type != cgltf_primitive_type_triangles || !prim->indices)
273 size_t t_base_idx = mesh->
n_texs;
275 cgltf_accessor *pos_acc = NULL, *nrm_acc = NULL, *uv_acc = NULL;
276 for (
size_t k = 0; k < prim->attributes_count; k++) {
277 if (prim->attributes[k].type == cgltf_attribute_type_position)
278 pos_acc = prim->attributes[k].data;
279 else if (prim->attributes[k].type == cgltf_attribute_type_normal)
280 nrm_acc = prim->attributes[k].data;
281 else if (prim->attributes[k].type == cgltf_attribute_type_texcoord)
282 uv_acc = prim->attributes[k].data;
287 for (
size_t k = 0; k < pos_acc->count; k++) {
288 float p_float[3], n_float[3], t_float[2];
292 cgltf_accessor_read_float(pos_acc, k, p_float, 3);
296 transform_pos(&p, &p, matrix);
300 cgltf_accessor_read_float(nrm_acc, k, n_float, 3);
304 transform_normal(&n, &n, matrix);
308 cgltf_accessor_read_float(uv_acc, k, t_float, 2);
315 for (
size_t k = 0; k < prim->indices->count; k += 3) {
318 v_base_idx + cgltf_accessor_read_index(prim->indices, k + 0);
320 v_base_idx + cgltf_accessor_read_index(prim->indices, k + 1);
322 v_base_idx + cgltf_accessor_read_index(prim->indices, k + 2);
325 t_base_idx + cgltf_accessor_read_index(prim->indices, k + 0);
327 t_base_idx + cgltf_accessor_read_index(prim->indices, k + 1);
329 t_base_idx + cgltf_accessor_read_index(prim->indices, k + 2);
332 prim->material ? (int)(prim->material - data->materials) : -1;
339 gm3_pos_substract(&e1, &p0);
341 gm3_pos_substract(&e2, &p0);
343 gm3_pos_normalize(&face.
normal);
Represents a 3D texture, including its raw image data and file path.
Definition mtl.h:43