Skip to content

Commit

Permalink
fix parsing/writing of cgltf_size byte props (jkuhlmann#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeljetstream committed Aug 23, 2021
1 parent f9a8804 commit 55e9d32
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
36 changes: 25 additions & 11 deletions cgltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ cgltf_result cgltf_copy_extras_json(const cgltf_data* data, const cgltf_extras*
#include <limits.h> /* For UINT_MAX etc */
#include <float.h> /* For FLT_MAX */

#if !defined(CGLTF_MALLOC) || !defined(CGLTF_FREE) || !defined(CGLTF_ATOI) || !defined(CGLTF_ATOF)
#if !defined(CGLTF_MALLOC) || !defined(CGLTF_FREE) || !defined(CGLTF_ATOI) || !defined(CGLTF_ATOF) || !defined(CGLTF_ATOLL)
#include <stdlib.h> /* For malloc, free, atoi, atof */
#endif

Expand Down Expand Up @@ -886,6 +886,9 @@ static const uint32_t GlbMagicBinChunk = 0x004E4942;
#ifndef CGLTF_ATOF
#define CGLTF_ATOF(str) atof(str)
#endif
#ifndef CGLTF_ATOLL
#define CGLTF_ATOLL(str) atoll(str)
#endif
#ifndef CGLTF_VALIDATE_ENABLE_ASSERTS
#define CGLTF_VALIDATE_ENABLE_ASSERTS 0
#endif
Expand Down Expand Up @@ -2262,6 +2265,7 @@ cgltf_size cgltf_accessor_read_index(const cgltf_accessor* accessor, cgltf_size
#define CGLTF_ERROR_LEGACY -3

#define CGLTF_CHECK_TOKTYPE(tok_, type_) if ((tok_).type != (type_)) { return CGLTF_ERROR_JSON; }
#define CGLTF_CHECK_TOKTYPE_RETTYPE(tok_, type_, ret_) if ((tok_).type != (type_)) { return (ret_)CGLTF_ERROR_JSON; }
#define CGLTF_CHECK_KEY(tok_) if ((tok_).type != JSMN_STRING || (tok_).size == 0) { return CGLTF_ERROR_JSON; } /* checking size for 0 verifies that a value follows the key */

#define CGLTF_PTRINDEX(type, idx) (type*)((cgltf_size)idx + 1)
Expand All @@ -2286,6 +2290,16 @@ static int cgltf_json_to_int(jsmntok_t const* tok, const uint8_t* json_chunk)
return CGLTF_ATOI(tmp);
}

static cgltf_size cgltf_json_to_size(jsmntok_t const* tok, const uint8_t* json_chunk)
{
CGLTF_CHECK_TOKTYPE_RETTYPE(*tok, JSMN_PRIMITIVE, cgltf_size);
char tmp[128];
int size = (cgltf_size)(tok->end - tok->start) < sizeof(tmp) ? tok->end - tok->start : (int)(sizeof(tmp) - 1);
strncpy(tmp, (const char*)json_chunk + tok->start, size);
tmp[size] = 0;
return (cgltf_size)CGLTF_ATOLL(tmp);
}

static cgltf_float cgltf_json_to_float(jsmntok_t const* tok, const uint8_t* json_chunk)
{
CGLTF_CHECK_TOKTYPE(*tok, JSMN_PRIMITIVE);
Expand Down Expand Up @@ -3027,7 +3041,7 @@ static int cgltf_parse_json_accessor_sparse(cgltf_options* options, jsmntok_t co
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
{
++i;
out_sparse->indices_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
out_sparse->indices_byte_offset = cgltf_json_to_size(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
Expand Down Expand Up @@ -3076,7 +3090,7 @@ static int cgltf_parse_json_accessor_sparse(cgltf_options* options, jsmntok_t co
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
{
++i;
out_sparse->values_byte_offset = cgltf_json_to_int(tokens + i, json_chunk);
out_sparse->values_byte_offset = cgltf_json_to_size(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "extras") == 0)
Expand Down Expand Up @@ -3145,7 +3159,7 @@ static int cgltf_parse_json_accessor(cgltf_options* options, jsmntok_t const* to
{
++i;
out_accessor->offset =
cgltf_json_to_int(tokens+i, json_chunk);
cgltf_json_to_size(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "componentType") == 0)
Expand Down Expand Up @@ -4250,19 +4264,19 @@ static int cgltf_parse_json_meshopt_compression(cgltf_options* options, jsmntok_
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteOffset") == 0)
{
++i;
out_meshopt_compression->offset = cgltf_json_to_int(tokens+i, json_chunk);
out_meshopt_compression->offset = cgltf_json_to_size(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
{
++i;
out_meshopt_compression->size = cgltf_json_to_int(tokens+i, json_chunk);
out_meshopt_compression->size = cgltf_json_to_size(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
{
++i;
out_meshopt_compression->stride = cgltf_json_to_int(tokens+i, json_chunk);
out_meshopt_compression->stride = cgltf_json_to_size(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "count") == 0)
Expand Down Expand Up @@ -4348,21 +4362,21 @@ static int cgltf_parse_json_buffer_view(cgltf_options* options, jsmntok_t const*
{
++i;
out_buffer_view->offset =
cgltf_json_to_int(tokens+i, json_chunk);
cgltf_json_to_size(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteLength") == 0)
{
++i;
out_buffer_view->size =
cgltf_json_to_int(tokens+i, json_chunk);
cgltf_json_to_size(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "byteStride") == 0)
{
++i;
out_buffer_view->stride =
cgltf_json_to_int(tokens+i, json_chunk);
cgltf_json_to_size(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "target") == 0)
Expand Down Expand Up @@ -4480,7 +4494,7 @@ static int cgltf_parse_json_buffer(cgltf_options* options, jsmntok_t const* toke
{
++i;
out_buffer->size =
cgltf_json_to_int(tokens+i, json_chunk);
cgltf_json_to_size(tokens+i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "uri") == 0)
Expand Down
24 changes: 17 additions & 7 deletions cgltf_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ static void cgltf_write_intprop(cgltf_write_context* context, const char* label,
}
}

static void cgltf_write_sizeprop(cgltf_write_context* context, const char* label, cgltf_size val, cgltf_size def)
{
if (val != def)
{
cgltf_write_indent(context);
CGLTF_SPRINTF("\"%s\": %zu", label, val);
context->needs_comma = 1;
}
}

static void cgltf_write_floatprop(cgltf_write_context* context, const char* label, float val, float def)
{
if (val != def)
Expand Down Expand Up @@ -501,9 +511,9 @@ static void cgltf_write_buffer_view(cgltf_write_context* context, const cgltf_bu
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", view->name);
CGLTF_WRITE_IDXPROP("buffer", view->buffer, context->data->buffers);
cgltf_write_intprop(context, "byteLength", (int)view->size, -1);
cgltf_write_intprop(context, "byteOffset", (int)view->offset, 0);
cgltf_write_intprop(context, "byteStride", (int)view->stride, 0);
cgltf_write_sizeprop(context, "byteLength", view->size, (cgltf_size)-1);
cgltf_write_sizeprop(context, "byteOffset", view->offset, 0);
cgltf_write_sizeprop(context, "byteStride", view->stride, 0);
// NOTE: We skip writing "target" because the spec says its usage can be inferred.
cgltf_write_extras(context, &view->extras);
cgltf_write_line(context, "}");
Expand All @@ -515,7 +525,7 @@ static void cgltf_write_buffer(cgltf_write_context* context, const cgltf_buffer*
cgltf_write_line(context, "{");
cgltf_write_strprop(context, "name", buffer->name);
cgltf_write_strprop(context, "uri", buffer->uri);
cgltf_write_intprop(context, "byteLength", (int)buffer->size, -1);
cgltf_write_sizeprop(context, "byteLength", buffer->size, (cgltf_size)-1);
cgltf_write_extras(context, &buffer->extras);
cgltf_write_line(context, "}");
}
Expand Down Expand Up @@ -907,7 +917,7 @@ static void cgltf_write_accessor(cgltf_write_context* context, const cgltf_acces
cgltf_write_strprop(context, "type", cgltf_str_from_type(accessor->type));
cgltf_size dim = cgltf_dim_from_type(accessor->type);
cgltf_write_boolprop_optional(context, "normalized", accessor->normalized, false);
cgltf_write_intprop(context, "byteOffset", (int)accessor->offset, 0);
cgltf_write_sizeprop(context, "byteOffset", (int)accessor->offset, 0);
cgltf_write_intprop(context, "count", (int)accessor->count, -1);
if (accessor->has_min)
{
Expand All @@ -922,13 +932,13 @@ static void cgltf_write_accessor(cgltf_write_context* context, const cgltf_acces
cgltf_write_line(context, "\"sparse\": {");
cgltf_write_intprop(context, "count", (int)accessor->sparse.count, 0);
cgltf_write_line(context, "\"indices\": {");
cgltf_write_intprop(context, "byteOffset", (int)accessor->sparse.indices_byte_offset, 0);
cgltf_write_sizeprop(context, "byteOffset", (int)accessor->sparse.indices_byte_offset, 0);
CGLTF_WRITE_IDXPROP("bufferView", accessor->sparse.indices_buffer_view, context->data->buffer_views);
cgltf_write_intprop(context, "componentType", cgltf_int_from_component_type(accessor->sparse.indices_component_type), 0);
cgltf_write_extras(context, &accessor->sparse.indices_extras);
cgltf_write_line(context, "}");
cgltf_write_line(context, "\"values\": {");
cgltf_write_intprop(context, "byteOffset", (int)accessor->sparse.values_byte_offset, 0);
cgltf_write_sizeprop(context, "byteOffset", (int)accessor->sparse.values_byte_offset, 0);
CGLTF_WRITE_IDXPROP("bufferView", accessor->sparse.values_buffer_view, context->data->buffer_views);
cgltf_write_extras(context, &accessor->sparse.values_extras);
cgltf_write_line(context, "}");
Expand Down

0 comments on commit 55e9d32

Please sign in to comment.