Skip to content

Commit

Permalink
Merge pull request #253 from bobbens/master
Browse files Browse the repository at this point in the history
Implement support for EXT_texture_webp
  • Loading branch information
jkuhlmann authored Jul 3, 2024
2 parents 3efa245 + 1efa7fc commit fa3b80f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions cgltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++]));
Expand Down Expand Up @@ -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);
}

Expand Down
18 changes: 15 additions & 3 deletions cgltf_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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\": {");
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit fa3b80f

Please sign in to comment.