Browse Source

Updated cgltf.

Бранимир Караџић 1 year ago
parent
commit
8fc9516dcd
2 changed files with 46 additions and 3 deletions
  1. 31 0
      3rdparty/cgltf/cgltf.h
  2. 15 3
      3rdparty/cgltf/cgltf_write.h

+ 31 - 0
3rdparty/cgltf/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;
@@ -4551,6 +4553,34 @@ 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;
+					++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->webp_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++]));
@@ -6561,6 +6591,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);
 	}
 

+ 15 - 3
3rdparty/cgltf/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);
@@ -1280,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");
 	}