Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into anisotropy
Browse files Browse the repository at this point in the history
  • Loading branch information
abwood committed May 17, 2023
2 parents b5f7808 + 9f77acc commit 1e1470e
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 215 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ cgltf supports core glTF 2.0:
- extras data

cgltf also supports some glTF extensions:
- EXT_mesh_gpu_instancing
- EXT_meshopt_compression
- KHR_draco_mesh_compression (requires a library like [Google's Draco](https://github.com/google/draco) for decompression though)
- KHR_lights_punctual
- KHR_materials_clearcoat
- KHR_materials_emissive_strength
- KHR_materials_ior
- KHR_materials_iridescence
- KHR_materials_pbrSpecularGlossiness
- KHR_materials_sheen
- KHR_materials_specular
Expand Down Expand Up @@ -134,14 +136,15 @@ Everyone is welcome to contribute to the library. If you find any problems, you
## Dependencies
None.

C headers being used by implementation:
C headers being used by the implementation:
```
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <assert.h> // If asserts are enabled.
```

Note, this library has a copy of the [JSMN JSON parser](https://github.com/zserge/jsmn) embedded in its source.
Expand Down
478 changes: 288 additions & 190 deletions cgltf.h

Large diffs are not rendered by default.

124 changes: 105 additions & 19 deletions cgltf_write.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* cgltf_write - a single-file glTF 2.0 writer written in C99.
*
* Version: 1.12
* Version: 1.13
*
* Website: https://github.com/jkuhlmann/cgltf
*
Expand All @@ -15,19 +15,17 @@
*
* Reference:
* `cgltf_result cgltf_write_file(const cgltf_options* options, const char*
* path, const cgltf_data* data)` writes JSON to the given file path. Buffer
* files and external images are not written out. `data` is not deallocated.
* path, const cgltf_data* data)` writes a glTF data to the given file path.
* If `options->type` is `cgltf_file_type_glb`, both JSON content and binary
* buffer of the given glTF data will be written in a GLB format.
* Otherwise, only the JSON part will be written.
* External buffers and images are not written out. `data` is not deallocated.
*
* `cgltf_size cgltf_write(const cgltf_options* options, char* buffer,
* cgltf_size size, const cgltf_data* data)` writes JSON into the given memory
* buffer. Returns the number of bytes written to `buffer`, including a null
* terminator. If buffer is null, returns the number of bytes that would have
* been written. `data` is not deallocated.
*
* To write custom JSON into the `extras` field, aggregate all the custom JSON
* into a single buffer, then set `file_data` to this buffer. By supplying
* start_offset and end_offset values for various objects, you can select a
* range of characters within the aggregated buffer.
*/
#ifndef CGLTF_WRITE_H_INCLUDED__
#define CGLTF_WRITE_H_INCLUDED__
Expand Down Expand Up @@ -148,6 +146,17 @@ typedef struct {
context->needs_comma = 1; }

#define CGLTF_WRITE_TEXTURE_INFO(label, info) if (info.texture) { \
cgltf_write_line(context, "\"" label "\": {"); \
CGLTF_WRITE_IDXPROP("index", info.texture, context->data->textures); \
cgltf_write_intprop(context, "texCoord", info.texcoord, 0); \
if (info.has_transform) { \
context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM; \
cgltf_write_texture_transform(context, &info.transform); \
} \
cgltf_write_extras(context, &info.extras); \
cgltf_write_line(context, "}"); }

#define CGLTF_WRITE_NORMAL_TEXTURE_INFO(label, info) if (info.texture) { \
cgltf_write_line(context, "\"" label "\": {"); \
CGLTF_WRITE_IDXPROP("index", info.texture, context->data->textures); \
cgltf_write_intprop(context, "texCoord", info.texcoord, 0); \
Expand All @@ -159,6 +168,28 @@ typedef struct {
cgltf_write_extras(context, &info.extras); \
cgltf_write_line(context, "}"); }

#define CGLTF_WRITE_OCCLUSION_TEXTURE_INFO(label, info) if (info.texture) { \
cgltf_write_line(context, "\"" label "\": {"); \
CGLTF_WRITE_IDXPROP("index", info.texture, context->data->textures); \
cgltf_write_intprop(context, "texCoord", info.texcoord, 0); \
cgltf_write_floatprop(context, "strength", info.scale, 1.0f); \
if (info.has_transform) { \
context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM; \
cgltf_write_texture_transform(context, &info.transform); \
} \
cgltf_write_extras(context, &info.extras); \
cgltf_write_line(context, "}"); }

#ifndef CGLTF_CONSTS
static const cgltf_size GlbHeaderSize = 12;
static const cgltf_size GlbChunkHeaderSize = 8;
static const uint32_t GlbVersion = 2;
static const uint32_t GlbMagic = 0x46546C67;
static const uint32_t GlbMagicJsonChunk = 0x4E4F534A;
static const uint32_t GlbMagicBinChunk = 0x004E4942;
#define CGLTF_CONSTS
#endif

static void cgltf_write_indent(cgltf_write_context* context)
{
if (context->needs_comma)
Expand Down Expand Up @@ -209,15 +240,24 @@ static void cgltf_write_strprop(cgltf_write_context* context, const char* label,

static void cgltf_write_extras(cgltf_write_context* context, const cgltf_extras* extras)
{
cgltf_size length = extras->end_offset - extras->start_offset;
if (length > 0 && context->data->file_data)
if (extras->data)
{
char* json_string = ((char*) context->data->file_data) + extras->start_offset;
cgltf_write_indent(context);
CGLTF_SPRINTF("%s", "\"extras\": ");
CGLTF_SNPRINTF(length, "%.*s", (int)(extras->end_offset - extras->start_offset), json_string);
CGLTF_SPRINTF("\"extras\": %s", extras->data);
context->needs_comma = 1;
}
else
{
cgltf_size length = extras->end_offset - extras->start_offset;
if (length > 0 && context->data->json)
{
char* json_string = ((char*) context->data->json) + extras->start_offset;
cgltf_write_indent(context);
CGLTF_SPRINTF("%s", "\"extras\": ");
CGLTF_SNPRINTF(length, "%.*s", (int)(extras->end_offset - extras->start_offset), json_string);
context->needs_comma = 1;
}
}
}

static void cgltf_write_stritem(cgltf_write_context* context, const char* item)
Expand Down Expand Up @@ -619,7 +659,6 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater
{
cgltf_write_floatarrayprop(context, "baseColorFactor", params->base_color_factor, 4);
}
cgltf_write_extras(context, &params->extras);
cgltf_write_line(context, "}");
}

Expand All @@ -632,7 +671,7 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater
cgltf_write_line(context, "\"KHR_materials_clearcoat\": {");
CGLTF_WRITE_TEXTURE_INFO("clearcoatTexture", params->clearcoat_texture);
CGLTF_WRITE_TEXTURE_INFO("clearcoatRoughnessTexture", params->clearcoat_roughness_texture);
CGLTF_WRITE_TEXTURE_INFO("clearcoatNormalTexture", params->clearcoat_normal_texture);
CGLTF_WRITE_NORMAL_TEXTURE_INFO("clearcoatNormalTexture", params->clearcoat_normal_texture);
cgltf_write_floatprop(context, "clearcoatFactor", params->clearcoat_factor, 0.0f);
cgltf_write_floatprop(context, "clearcoatRoughnessFactor", params->clearcoat_roughness_factor, 0.0f);
cgltf_write_line(context, "}");
Expand Down Expand Up @@ -746,8 +785,8 @@ static void cgltf_write_material(cgltf_write_context* context, const cgltf_mater
cgltf_write_line(context, "}");
}

CGLTF_WRITE_TEXTURE_INFO("normalTexture", material->normal_texture);
CGLTF_WRITE_TEXTURE_INFO("occlusionTexture", material->occlusion_texture);
CGLTF_WRITE_NORMAL_TEXTURE_INFO("normalTexture", material->normal_texture);
CGLTF_WRITE_OCCLUSION_TEXTURE_INFO("occlusionTexture", material->occlusion_texture);
CGLTF_WRITE_TEXTURE_INFO("emissiveTexture", material->emissive_texture);
if (cgltf_check_floatarray(material->emissive_factor, 3, 0.0f))
{
Expand Down Expand Up @@ -1099,6 +1138,7 @@ static void cgltf_write_light(cgltf_write_context* context, const cgltf_light* l
cgltf_write_floatprop(context, "outerConeAngle", light->spot_outer_cone_angle, 3.14159265358979323846f/4.0f);
cgltf_write_line(context, "}");
}
cgltf_write_extras( context, &light->extras );
cgltf_write_line(context, "}");
}

Expand All @@ -1112,6 +1152,47 @@ static void cgltf_write_variant(cgltf_write_context* context, const cgltf_materi
cgltf_write_line(context, "}");
}

static void cgltf_write_glb(FILE* file, const void* json_buf, const cgltf_size json_size, const void* bin_buf, const cgltf_size bin_size)
{
char header[GlbHeaderSize];
char chunk_header[GlbChunkHeaderSize];
char json_pad[3] = { 0x20, 0x20, 0x20 };
char bin_pad[3] = { 0, 0, 0 };

cgltf_size json_padsize = (json_size % 4 != 0) ? 4 - json_size % 4 : 0;
cgltf_size bin_padsize = (bin_size % 4 != 0) ? 4 - bin_size % 4 : 0;
cgltf_size total_size = GlbHeaderSize + GlbChunkHeaderSize + json_size + json_padsize;
if (bin_buf != NULL && bin_size > 0) {
total_size += GlbChunkHeaderSize + bin_size + bin_padsize;
}

// Write a GLB header
memcpy(header, &GlbMagic, 4);
memcpy(header + 4, &GlbVersion, 4);
memcpy(header + 8, &total_size, 4);
fwrite(header, 1, GlbHeaderSize, file);

// Write a JSON chunk (header & data)
uint32_t json_chunk_size = (uint32_t)(json_size + json_padsize);
memcpy(chunk_header, &json_chunk_size, 4);
memcpy(chunk_header + 4, &GlbMagicJsonChunk, 4);
fwrite(chunk_header, 1, GlbChunkHeaderSize, file);

fwrite(json_buf, 1, json_size, file);
fwrite(json_pad, 1, json_padsize, file);

if (bin_buf != NULL && bin_size > 0) {
// Write a binary chunk (header & data)
uint32_t bin_chunk_size = (uint32_t)(bin_size + bin_padsize);
memcpy(chunk_header, &bin_chunk_size, 4);
memcpy(chunk_header + 4, &GlbMagicBinChunk, 4);
fwrite(chunk_header, 1, GlbChunkHeaderSize, file);

fwrite(bin_buf, 1, bin_size, file);
fwrite(bin_pad, 1, bin_padsize, file);
}
}

cgltf_result cgltf_write_file(const cgltf_options* options, const char* path, const cgltf_data* data)
{
cgltf_size expected = cgltf_write(options, NULL, 0, data);
Expand All @@ -1120,13 +1201,18 @@ cgltf_result cgltf_write_file(const cgltf_options* options, const char* path, co
if (expected != actual) {
fprintf(stderr, "Error: expected %zu bytes but wrote %zu bytes.\n", expected, actual);
}
FILE* file = fopen(path, "wt");
FILE* file = fopen(path, "wb");
if (!file)
{
return cgltf_result_file_not_found;
}
// Note that cgltf_write() includes a null terminator, which we omit from the file content.
fwrite(buffer, actual - 1, 1, file);
if (options->type == cgltf_file_type_glb) {
cgltf_write_glb(file, buffer, actual - 1, data->bin, data->bin_size);
} else {
// Write a plain JSON file.
fwrite(buffer, actual - 1, 1, file);
}
fclose(file);
free(buffer);
return cgltf_result_success;
Expand Down
13 changes: 13 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ else()
endif()
install( TARGETS ${EXE_NAME} RUNTIME DESTINATION bin )

set( EXE_NAME test_write_glb )
add_executable( ${EXE_NAME} test_write_glb.cpp )
set_property( TARGET ${EXE_NAME} PROPERTY CXX_STANDARD 11 )
if(MSVC)
target_compile_options(${EXE_NAME} PRIVATE /W4 /WX)
add_definitions( -D_CRT_SECURE_NO_WARNINGS)
else()
target_compile_options(${EXE_NAME} PRIVATE -Wall -Wextra -pedantic -Werror)
target_compile_options(${EXE_NAME} PUBLIC -fsanitize=address)
target_link_options(${EXE_NAME} PUBLIC -fsanitize=address)
endif()
install( TARGETS ${EXE_NAME} RUNTIME DESTINATION bin )

set( EXE_NAME test_math )
add_executable( ${EXE_NAME} test_math.cpp )
set_property( TARGET ${EXE_NAME} PROPERTY CXX_STANDARD 11 )
Expand Down
1 change: 1 addition & 0 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def collect_files(path, type, name):
collect_files("glTF-Sample-Models/2.0/", ".glb", "test_conversion")
collect_files("glTF-Sample-Models/2.0/", ".gltf", "test_conversion")
collect_files("glTF-Sample-Models/2.0/", ".gltf", "test_write")
collect_files("glTF-Sample-Models/2.0/", ".glb", "test_write_glb")

result = os.system(get_executable_path("test_math"))
if result != 0:
Expand Down
12 changes: 7 additions & 5 deletions test/test_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ int main(int argc, char** argv)
if (result == cgltf_result_success)
result = cgltf_load_buffers(&options, data, argv[1]);

if (strstr(argv[1], "Draco"))
{
cgltf_free(data);
return 0;
}
// Skip files that use mesh compression since they require extra code to decompress accessor data
for (size_t i = 0; i < data->extensions_used_count; ++i)
if (strcmp(data->extensions_used[i], "KHR_draco_mesh_compression") == 0 || strcmp(data->extensions_used[i], "EXT_meshopt_compression") == 0)
{
cgltf_free(data);
return 0;
}

if (result != cgltf_result_success)
return result;
Expand Down
58 changes: 58 additions & 0 deletions test/test_write_glb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#define CGLTF_IMPLEMENTATION
#define CGLTF_WRITE_IMPLEMENTATION
#include "../cgltf_write.h"

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <limits>
#include <string.h>

int main(int argc, char** argv)
{
if (argc < 2)
{
printf("err\n");
return -1;
}

cgltf_options options = {};
cgltf_data* data0 = NULL;
cgltf_result result = cgltf_parse_file(&options, argv[1], &data0);

// Silently skip over files that are unreadable since this is a writing test.
if (result != cgltf_result_success)
{
return cgltf_result_success;
}

options.type = cgltf_file_type_glb; // Write back in a GLB format
result = cgltf_write_file(&options, "out.glb", data0);
if (result != cgltf_result_success)
{
return result;
}

cgltf_data* data1 = NULL;
result = cgltf_parse_file(&options, "out.glb", &data1);
if (result != cgltf_result_success)
{
return result;
}

if (data0->meshes_count != data1->meshes_count) {
return -1;
}

// Compare binary buffers
if (data0->bin_size != data1->bin_size) {
return -1;
}
if (memcmp(data0->bin, data1->bin, data0->bin_size) != 0) {
return -1;
}

cgltf_free(data1);
cgltf_free(data0);
return cgltf_result_success;
}

0 comments on commit 1e1470e

Please sign in to comment.