From bc8fbd10857a5639b598e7575a7e37362cad4bc7 Mon Sep 17 00:00:00 2001 From: Edgar Simo-Serra Date: Mon, 3 Jun 2024 00:13:07 +0900 Subject: [PATCH 1/4] Implement support for KHR_texture_webp Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Vendor/EXT_texture_webp --- cgltf.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/cgltf.h b/cgltf.h index 17dc0ca..d5751ea 100644 --- a/cgltf.h +++ b/cgltf.h @@ -4538,6 +4538,33 @@ static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tok } } } + else if (cgltf_json_strcmp(tokens + i, json_chunk, "EXT_texture_webp") == 0) + { + ++i; + CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT); + int num_properties = tokens[i].size; + ++i; + + for (int t = 0; t < num_properties; ++t) + { + CGLTF_CHECK_KEY(tokens[i]); + + if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0) + { + ++i; + out_texture->image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk)); + ++i; + } + else + { + i = cgltf_skip_json(tokens, i + 1); + } + if (i < 0) + { + return i; + } + } + } else { i = cgltf_parse_json_unprocessed_extension(options, tokens, i, json_chunk, &(out_texture->extensions[out_texture->extensions_count++])); From 9c131d82119d9731d4a7bd2c00cbfcfdc0569098 Mon Sep 17 00:00:00 2001 From: Edgar Simo-Serra Date: Tue, 18 Jun 2024 14:38:00 +0900 Subject: [PATCH 2/4] Have EXT_texture_webp not overwrite the image sampler, and added write support for EXT_texture_webp. --- README.md | 1 + cgltf.h | 5 ++++- cgltf_write.h | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 02e611c..ac40fa4 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ cgltf supports core glTF 2.0: cgltf also supports some glTF extensions: - EXT_mesh_gpu_instancing - EXT_meshopt_compression +- EXT_texture_webp - KHR_draco_mesh_compression (requires a library like [Google's Draco](https://github.com/google/draco) for decompression though) - KHR_lights_punctual - KHR_materials_anisotropy diff --git a/cgltf.h b/cgltf.h index d5751ea..1d6c648 100644 --- a/cgltf.h +++ b/cgltf.h @@ -395,6 +395,8 @@ typedef struct cgltf_texture cgltf_sampler* sampler; cgltf_bool has_basisu; cgltf_image* basisu_image; + cgltf_bool has_webp; + cgltf_image* webp_image; cgltf_extras extras; cgltf_size extensions_count; cgltf_extension* extensions; @@ -4540,6 +4542,7 @@ static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tok } else if (cgltf_json_strcmp(tokens + i, json_chunk, "EXT_texture_webp") == 0) { + out_texture->has_webp = 1; ++i; CGLTF_CHECK_TOKTYPE(tokens[i], JSMN_OBJECT); int num_properties = tokens[i].size; @@ -4552,7 +4555,7 @@ static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tok if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0) { ++i; - out_texture->image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk)); + out_texture->webp_image = CGLTF_PTRINDEX(cgltf_image, cgltf_json_to_int(tokens + i, json_chunk)); ++i; } else diff --git a/cgltf_write.h b/cgltf_write.h index aa648a4..3799488 100644 --- a/cgltf_write.h +++ b/cgltf_write.h @@ -87,6 +87,7 @@ cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size si #define CGLTF_EXTENSION_FLAG_MATERIALS_IRIDESCENCE (1 << 15) #define CGLTF_EXTENSION_FLAG_MATERIALS_ANISOTROPY (1 << 16) #define CGLTF_EXTENSION_FLAG_MATERIALS_DISPERSION (1 << 17) +#define CGLTF_EXTENSION_FLAG_TEXTURE_WEBP (1 << 18) typedef struct { char* buffer; @@ -506,7 +507,7 @@ static void cgltf_write_primitive(cgltf_write_context* context, const cgltf_prim context->extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION; if (prim->attributes_count == 0 || prim->indices == 0) { - context->required_extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION; + context->required_extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION; } cgltf_write_line(context, "\"KHR_draco_mesh_compression\": {"); @@ -732,7 +733,7 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater { cgltf_write_floatarrayprop(context, "attenuationColor", params->attenuation_color, 3); } - if (params->attenuation_distance < FLT_MAX) + if (params->attenuation_distance < FLT_MAX) { cgltf_write_floatprop(context, "attenuationDistance", params->attenuation_distance, FLT_MAX); } @@ -840,15 +841,23 @@ static void cgltf_write_texture(cgltf_write_context* context, const cgltf_textur CGLTF_WRITE_IDXPROP("source", texture->image, context->data->images); CGLTF_WRITE_IDXPROP("sampler", texture->sampler, context->data->samplers); - if (texture->has_basisu) + if (texture->has_basisu || texture->has_webp) { cgltf_write_line(context, "\"extensions\": {"); + if (texture->has_basisu) { context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_BASISU; cgltf_write_line(context, "\"KHR_texture_basisu\": {"); CGLTF_WRITE_IDXPROP("source", texture->basisu_image, context->data->images); cgltf_write_line(context, "}"); } + if (texture->has_webp) + { + context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_WEBP; + cgltf_write_line(context, "\"EXT_texture_webp\": {"); + CGLTF_WRITE_IDXPROP("source", texture->webp_image, context->data->images); + cgltf_write_line(context, "}"); + } cgltf_write_line(context, "}"); } cgltf_write_extras(context, &texture->extras); From ed28704fc219c620eab3fa7a3184ba5e2b5f8fd7 Mon Sep 17 00:00:00 2001 From: Edgar Simo-Serra Date: Fri, 21 Jun 2024 22:22:01 +0900 Subject: [PATCH 3/4] Call CGLTF_PTRFIXUP on webp_image. --- cgltf.h | 1 + 1 file changed, 1 insertion(+) diff --git a/cgltf.h b/cgltf.h index 1d6c648..02e47e2 100644 --- a/cgltf.h +++ b/cgltf.h @@ -6578,6 +6578,7 @@ static int cgltf_fixup_pointers(cgltf_data* data) { CGLTF_PTRFIXUP(data->textures[i].image, data->images, data->images_count); CGLTF_PTRFIXUP(data->textures[i].basisu_image, data->images, data->images_count); + CGLTF_PTRFIXUP(data->textures[i].webp_image, data->images, data->images_count); CGLTF_PTRFIXUP(data->textures[i].sampler, data->samplers, data->samplers_count); } From 1efa7fc7ba0c207d481aa0995416d9c5f8b2f6fb Mon Sep 17 00:00:00 2001 From: Edgar Simo-Serra Date: Sat, 22 Jun 2024 10:35:49 +0900 Subject: [PATCH 4/4] Write EXT_texture_webp as used extension flag when necessary. --- cgltf_write.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cgltf_write.h b/cgltf_write.h index 3799488..8677a9a 100644 --- a/cgltf_write.h +++ b/cgltf_write.h @@ -1289,6 +1289,9 @@ static void cgltf_write_extensions(cgltf_write_context* context, uint32_t extens if (extension_flags & CGLTF_EXTENSION_FLAG_TEXTURE_BASISU) { cgltf_write_stritem(context, "KHR_texture_basisu"); } + if (extension_flags & CGLTF_EXTENSION_FLAG_TEXTURE_WEBP) { + cgltf_write_stritem(context, "EXT_texture_webp"); + } if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_EMISSIVE_STRENGTH) { cgltf_write_stritem(context, "KHR_materials_emissive_strength"); }