Skip to content

Commit

Permalink
Added cglft_float32 when 32 bit float is needed, cglft_float may be f…
Browse files Browse the repository at this point in the history
…loat or double.

CGLTF_DECIMAL_DIG is increased to DBL_DECIMAL_DIG or 17 when CGLTF_DOUBLE_PRECICION flag is set
Fixed memcpy issue in cgltf_accessor_unpack_floats when CGLTF_DOUBLE_PRECISION is set
  • Loading branch information
wheresjames committed Oct 12, 2023
1 parent fb9c02e commit 0a88cbf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
42 changes: 25 additions & 17 deletions cgltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
*
* `cgltf_num_components` is a tiny utility that tells you the dimensionality of
* a certain accessor type. This can be used before `cgltf_accessor_unpack_floats` to help allocate
* the necessary amount of memory. `cgltf_component_size` and `cgltf_calc_size` exist for
* the necessary amount of memory. `cgltf_component_size` and `cgltf_calc_size` exist for
* similar purposes.
*
* `cgltf_accessor_read_float` reads a certain element from a non-sparse accessor and converts it to
Expand All @@ -75,7 +75,7 @@
*
* `cgltf_accessor_read_uint` is similar to its floating-point counterpart, but limited to reading
* vector types and does not support matrix types. The passed-in element size is the number of uints
* in the output buffer, which should be in the range [1, 4]. Returns false if the passed-in
* in the output buffer, which should be in the range [1, 4]. Returns false if the passed-in
* element_size is too small, or if the accessor is sparse.
*
* `cgltf_accessor_read_index` is similar to its floating-point counterpart, but it returns size_t
Expand All @@ -98,11 +98,12 @@ extern "C" {

typedef size_t cgltf_size;
typedef long long int cgltf_ssize;
#ifdef GLTF_DOUBLE_PRECISION
#ifdef CGLTF_DOUBLE_PRECISION
typedef double cgltf_float;
#else
typedef float cgltf_float;
#endif
typedef float cgltf_float32;
typedef int cgltf_int;
typedef unsigned int cgltf_uint;
typedef int cgltf_bool;
Expand Down Expand Up @@ -1049,7 +1050,7 @@ static cgltf_result cgltf_default_file_read(const struct cgltf_memory_options* m
fclose(file);
return cgltf_result_out_of_memory;
}

cgltf_size read_size = fread(file_data, 1, file_size, file);

fclose(file);
Expand Down Expand Up @@ -1981,7 +1982,7 @@ void cgltf_free(cgltf_data* data)

data->memory.free_func(data->memory.user_data, data->materials);

for (cgltf_size i = 0; i < data->images_count; ++i)
for (cgltf_size i = 0; i < data->images_count; ++i)
{
data->memory.free_func(data->memory.user_data, data->images[i].name);
data->memory.free_func(data->memory.user_data, data->images[i].uri);
Expand Down Expand Up @@ -2230,7 +2231,7 @@ static cgltf_ssize cgltf_component_read_integer(const void* in, cgltf_component_
case cgltf_component_type_r_32u:
return *((const uint32_t*) in);
case cgltf_component_type_r_32f:
return (cgltf_ssize)*((const float*) in);
return (cgltf_ssize)*((const cgltf_float32*) in);
case cgltf_component_type_r_8:
return *((const int8_t*) in);
case cgltf_component_type_r_8u:
Expand All @@ -2249,7 +2250,7 @@ static cgltf_size cgltf_component_read_index(const void* in, cgltf_component_typ
case cgltf_component_type_r_32u:
return *((const uint32_t*) in);
case cgltf_component_type_r_32f:
return (cgltf_size)((cgltf_ssize)*((const float*) in));
return (cgltf_size)((cgltf_ssize)*((const cgltf_float32*) in));
case cgltf_component_type_r_8u:
return *((const uint8_t*) in);
default:
Expand All @@ -2261,7 +2262,7 @@ static cgltf_float cgltf_component_read_float(const void* in, cgltf_component_ty
{
if (component_type == cgltf_component_type_r_32f)
{
return *((const float*) in);
return *((const cgltf_float32*) in);
}

if (normalized)
Expand Down Expand Up @@ -2400,9 +2401,16 @@ cgltf_size cgltf_accessor_unpack_floats(const cgltf_accessor* accessor, cgltf_fl
}
element += accessor->offset;

if (accessor->component_type == cgltf_component_type_r_32f && accessor->stride == floats_per_element * sizeof(cgltf_float))
if (accessor->component_type == cgltf_component_type_r_32f && accessor->stride == floats_per_element * sizeof(cgltf_float32))
{
memcpy(out, element, element_count * floats_per_element * sizeof(cgltf_float));
#ifdef CGLTF_DOUBLE_PRECISION
for (cgltf_size i = 0; i < element_count * floats_per_element; ++i)
{
out[i] = ((const cgltf_float32*)element)[i];
}
#else
memcpy(out, element, element_count * floats_per_element * sizeof(cgltf_float32));
#endif
}
else
{
Expand Down Expand Up @@ -3805,7 +3813,7 @@ static int cgltf_parse_json_texture_view(cgltf_options* options, jsmntok_t const
out_texture_view->texcoord = cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
else if (cgltf_json_strcmp(tokens + i, json_chunk, "scale") == 0)
{
++i;
out_texture_view->scale = cgltf_json_to_float(tokens + i, json_chunk);
Expand Down Expand Up @@ -3890,11 +3898,11 @@ static int cgltf_parse_json_pbr_metallic_roughness(cgltf_options* options, jsmnt
if (cgltf_json_strcmp(tokens+i, json_chunk, "metallicFactor") == 0)
{
++i;
out_pbr->metallic_factor =
out_pbr->metallic_factor =
cgltf_json_to_float(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens+i, json_chunk, "roughnessFactor") == 0)
else if (cgltf_json_strcmp(tokens+i, json_chunk, "roughnessFactor") == 0)
{
++i;
out_pbr->roughness_factor =
Expand Down Expand Up @@ -4364,11 +4372,11 @@ static int cgltf_parse_json_image(cgltf_options* options, jsmntok_t const* token
int size = tokens[i].size;
++i;

for (int j = 0; j < size; ++j)
for (int j = 0; j < size; ++j)
{
CGLTF_CHECK_KEY(tokens[i]);

if (cgltf_json_strcmp(tokens + i, json_chunk, "uri") == 0)
if (cgltf_json_strcmp(tokens + i, json_chunk, "uri") == 0)
{
i = cgltf_parse_json_string(options, tokens, i + 1, json_chunk, &out_image->uri);
}
Expand Down Expand Up @@ -4448,7 +4456,7 @@ static int cgltf_parse_json_sampler(cgltf_options* options, jsmntok_t const* tok
= cgltf_json_to_int(tokens + i, json_chunk);
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapT") == 0)
else if (cgltf_json_strcmp(tokens + i, json_chunk, "wrapT") == 0)
{
++i;
out_sampler->wrap_t
Expand Down Expand Up @@ -4498,7 +4506,7 @@ static int cgltf_parse_json_texture(cgltf_options* options, jsmntok_t const* tok
out_texture->sampler = CGLTF_PTRINDEX(cgltf_sampler, cgltf_json_to_int(tokens + i, json_chunk));
++i;
}
else if (cgltf_json_strcmp(tokens + i, json_chunk, "source") == 0)
else 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));
Expand Down
19 changes: 16 additions & 3 deletions cgltf_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,25 @@ typedef struct {

#define CGLTF_MIN(a, b) (a < b ? a : b)

#ifdef CGLTF_DOUBLE_PRECISION
#ifdef DBL_DECIMAL_DIG
// DBL_DECIMAL_DIG is C11
#define CGLTF_DECIMAL_DIG (DBL_DECIMAL_DIG)
#else
#define CGLTF_DECIMAL_DIG 17
#endif
#else
#ifdef FLT_DECIMAL_DIG
// FLT_DECIMAL_DIG is C11
#define CGLTF_DECIMAL_DIG (FLT_DECIMAL_DIG)
#else
#define CGLTF_DECIMAL_DIG 9
#endif
#endif

Added cglft_float32 when 32 bit float is needed, cglft_float may be float or double.
CGLTF_DECIMAL_DIG is increased to DBL_DECIMAL_DIG or 17 when CGLTF_DOUBLE_PRECISION flag is set
Fixed memcpy issue in cgltf_accessor_unpack_floats when CGLTF_DOUBLE_PRECISION isset

#define CGLTF_SPRINTF(...) { \
assert(context->cursor || (!context->cursor && context->remaining == 0)); \
Expand Down Expand Up @@ -287,7 +300,7 @@ static void cgltf_write_sizeprop(cgltf_write_context* context, const char* label
}
}

static void cgltf_write_floatprop(cgltf_write_context* context, const char* label, float val, float def)
static void cgltf_write_floatprop(cgltf_write_context* context, const char* label, cgltf_float val, cgltf_float def)
{
if (val != def)
{
Expand Down Expand Up @@ -493,7 +506,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 @@ -714,7 +727,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

0 comments on commit 0a88cbf

Please sign in to comment.