Merge pull request #1 from maxrigout/colors

Colors
This commit is contained in:
Max Rigout 2023-08-02 22:34:25 -04:00 committed by GitHub
commit 20a9db53a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -206,6 +206,9 @@ typedef struct
/* Final mesh */
fastObjMesh* mesh;
/* Position index goes to color index map */
unsigned int* vertex_color_index;
/* Current object/group */
fastObjGroup object;
fastObjGroup group;
@ -652,25 +655,35 @@ const char* parse_vertex(fastObjData* data, const char* ptr)
unsigned int ii;
float v;
for (ii = 0; ii < 3; ii++)
{
ptr = parse_float(ptr, &v);
array_push(data->mesh->positions, v);
}
// extract color data
for (ii = 0; ii < 3; ii++)
#ifdef FAST_OBJ_ENABLE_COLOURS
#ifndef FAST_OBJ_LAZY_FILL
/* Default the vertex color to the default color index */
array_push(data->vertex_color_index, 0);
#endif
ptr = skip_whitespace(ptr);
if (!is_newline(*ptr))
{
const char* tmp = parse_float(ptr, &v);
// if there's no data, we default to white
if (tmp == ptr)
#ifdef FAST_OBJ_LAZY_FILL
unsigned int oldSz = array_size(data->vertex_color_index);
_array_grow(data->vertex_color_index, array_size(data->mesh->positions) / 3);
_array_size(data->vertex_color_index) = array_size(data->mesh->positions) / 3;
memset(data->vertex_color_index + oldSz, 0, array_size(data->vertex_color_index) - oldSz);
#endif
/* We associate the current vertex position with the vertex color */
data->vertex_color_index[array_size(data->vertex_color_index) - 1] = array_size(data->mesh->colors) / 3;
for (ii = 0; ii < 3; ++ii)
{
v = 1.0f;
ptr = parse_float(ptr, &v);
array_push(data->mesh->colors, v);
}
ptr = tmp;
array_push(data->mesh->colors, v);
}
#endif
return ptr;
}
@ -762,7 +775,11 @@ const char* parse_face(fastObjData* data, const char* ptr)
else
vn.n = 0;
vn.c = vn.p;
#ifdef FAST_OBJ_ENABLE_COLOURS
vn.c = data->vertex_color_index[vn.p];
#else
vn.c = 0;
#endif
array_push(data->mesh->indices, vn);
count++;
@ -1345,6 +1362,15 @@ void parse_buffer(fastObjData* data, const char* ptr, const char* end, const fas
data->line++;
}
#ifdef FAST_OBJ_LAZY_FILL
if (array_size(data->vertex_color_index) < array_size(data->mesh->positions) / 3)
{
unsigned int oldSz = array_size(data->vertex_color_index);
_array_grow(data->vertex_color_index, array_size(data->mesh->positions) / 3);
_array_size(data->vertex_color_index) = array_size(data->mesh->positions) / 3;
memset(data->vertex_color_index + oldSz, 0, array_size(data->vertex_color_index) - oldSz);
}
#endif
}
@ -1365,7 +1391,9 @@ void fast_obj_destroy(fastObjMesh* m)
array_clean(m->positions);
array_clean(m->texcoords);
array_clean(m->normals);
#ifdef FAST_OBJ_ENABLE_COLOURS
array_clean(m->colors);
#endif
array_clean(m->face_vertices);
array_clean(m->face_materials);
array_clean(m->indices);
@ -1420,7 +1448,9 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac
m->positions = 0;
m->texcoords = 0;
m->normals = 0;
#ifdef FAST_OBJ_ENABLE_COLOURS
m->colors = 0;
#endif
m->face_vertices = 0;
m->face_materials = 0;
m->indices = 0;
@ -1441,17 +1471,24 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac
array_push(m->normals, 0.0f);
array_push(m->normals, 1.0f);
array_push(m->colors, 0.0f);
array_push(m->colors, 0.0f);
array_push(m->colors, 0.0f);
#ifdef FAST_OBJ_ENABLE_COLOURS
/* Default white color */
array_push(m->colors, 1.0f);
array_push(m->colors, 1.0f);
array_push(m->colors, 1.0f);
#endif
/* Data needed during parsing */
data.mesh = m;
data.object = object_default();
data.group = group_default();
data.material = 0;
data.line = 1;
data.base = 0;
data.mesh = m;
data.vertex_color_index = 0;
data.object = object_default();
data.group = group_default();
data.material = 0;
data.line = 1;
data.base = 0;
/* Dummy position is assigned to the dummy color */
array_push(data.vertex_color_index, 0);
/* Find base path for materials/textures */
@ -1531,7 +1568,9 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac
m->position_count = array_size(m->positions) / 3;
m->texcoord_count = array_size(m->texcoords) / 2;
m->normal_count = array_size(m->normals) / 3;
#ifdef FAST_OBJ_ENABLE_COLOURS
m->color_count = array_size(m->colors) / 3;
#endif
m->face_count = array_size(m->face_vertices);
m->index_count = array_size(m->indices);
m->material_count = array_size(m->materials);
@ -1542,6 +1581,7 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac
/* Clean up */
memory_dealloc(buffer);
memory_dealloc(data.base);
array_clean(data.vertex_color_index);
callbacks->file_close(file, user_data);