mirror of
https://github.com/thisistherk/fast_obj.git
synced 2025-04-04 21:15:04 +00:00
Move material texture maps into fastObjMesh
This commit is contained in:
parent
85778da5fc
commit
6f4e843882
1 changed files with 62 additions and 43 deletions
105
fast_obj.h
105
fast_obj.h
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* fast_obj
|
* fast_obj
|
||||||
*
|
*
|
||||||
* Version 1.2
|
* Version 1.3
|
||||||
*
|
*
|
||||||
* MIT License
|
* MIT License
|
||||||
*
|
*
|
||||||
|
@ -65,16 +65,16 @@ typedef struct
|
||||||
float d; /* Disolve (alpha) */
|
float d; /* Disolve (alpha) */
|
||||||
int illum; /* Illumination model */
|
int illum; /* Illumination model */
|
||||||
|
|
||||||
/* Texture maps */
|
/* Texture map indices in fastObjMesh textures array */
|
||||||
fastObjTexture map_Ka;
|
unsigned int map_Ka;
|
||||||
fastObjTexture map_Kd;
|
unsigned int map_Kd;
|
||||||
fastObjTexture map_Ks;
|
unsigned int map_Ks;
|
||||||
fastObjTexture map_Ke;
|
unsigned int map_Ke;
|
||||||
fastObjTexture map_Kt;
|
unsigned int map_Kt;
|
||||||
fastObjTexture map_Ns;
|
unsigned int map_Ns;
|
||||||
fastObjTexture map_Ni;
|
unsigned int map_Ni;
|
||||||
fastObjTexture map_d;
|
unsigned int map_d;
|
||||||
fastObjTexture map_bump;
|
unsigned int map_bump;
|
||||||
|
|
||||||
} fastObjMaterial;
|
} fastObjMaterial;
|
||||||
|
|
||||||
|
@ -111,6 +111,10 @@ typedef struct
|
||||||
} fastObjGroup;
|
} fastObjGroup;
|
||||||
|
|
||||||
|
|
||||||
|
/* Note: a dummy zero-initialized value is added to the first index
|
||||||
|
of the positions, texcoords, normals and textures arrays. Hence,
|
||||||
|
valid indices into these arrays start from 1, with an index of 0
|
||||||
|
indicating that the attribute is not present. */
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
/* Vertex data */
|
/* Vertex data */
|
||||||
|
@ -136,6 +140,10 @@ typedef struct
|
||||||
unsigned int material_count;
|
unsigned int material_count;
|
||||||
fastObjMaterial* materials;
|
fastObjMaterial* materials;
|
||||||
|
|
||||||
|
/* Texture maps */
|
||||||
|
unsigned int texture_count;
|
||||||
|
fastObjTexture* textures;
|
||||||
|
|
||||||
/* Mesh objects ('o' tag in .obj file) */
|
/* Mesh objects ('o' tag in .obj file) */
|
||||||
unsigned int object_count;
|
unsigned int object_count;
|
||||||
fastObjGroup* objects;
|
fastObjGroup* objects;
|
||||||
|
@ -848,15 +856,15 @@ fastObjMaterial mtl_default(void)
|
||||||
mtl.d = 1.0;
|
mtl.d = 1.0;
|
||||||
mtl.illum = 1;
|
mtl.illum = 1;
|
||||||
|
|
||||||
mtl.map_Ka = map_default();
|
mtl.map_Ka = 0;
|
||||||
mtl.map_Kd = map_default();
|
mtl.map_Kd = 0;
|
||||||
mtl.map_Ks = map_default();
|
mtl.map_Ks = 0;
|
||||||
mtl.map_Ke = map_default();
|
mtl.map_Ke = 0;
|
||||||
mtl.map_Kt = map_default();
|
mtl.map_Kt = 0;
|
||||||
mtl.map_Ns = map_default();
|
mtl.map_Ns = 0;
|
||||||
mtl.map_Ni = map_default();
|
mtl.map_Ni = 0;
|
||||||
mtl.map_d = map_default();
|
mtl.map_d = 0;
|
||||||
mtl.map_bump = map_default();
|
mtl.map_bump = 0;
|
||||||
|
|
||||||
return mtl;
|
return mtl;
|
||||||
}
|
}
|
||||||
|
@ -891,8 +899,8 @@ const char* parse_usemtl(fastObjData* data, const char* ptr)
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If doesn't exists, create a default one with this name
|
/* If doesn't exist, create a default one with this name
|
||||||
Note: this case happens when OBJ doesn't have its MTL */
|
Note: this case happens when OBJ doesn't have its MTL */
|
||||||
if (idx == array_size(data->mesh->materials))
|
if (idx == array_size(data->mesh->materials))
|
||||||
{
|
{
|
||||||
fastObjMaterial new_mtl = mtl_default();
|
fastObjMaterial new_mtl = mtl_default();
|
||||||
|
@ -917,16 +925,6 @@ void map_clean(fastObjTexture* map)
|
||||||
static
|
static
|
||||||
void mtl_clean(fastObjMaterial* mtl)
|
void mtl_clean(fastObjMaterial* mtl)
|
||||||
{
|
{
|
||||||
map_clean(&mtl->map_Ka);
|
|
||||||
map_clean(&mtl->map_Kd);
|
|
||||||
map_clean(&mtl->map_Ks);
|
|
||||||
map_clean(&mtl->map_Ke);
|
|
||||||
map_clean(&mtl->map_Kt);
|
|
||||||
map_clean(&mtl->map_Ns);
|
|
||||||
map_clean(&mtl->map_Ni);
|
|
||||||
map_clean(&mtl->map_d);
|
|
||||||
map_clean(&mtl->map_bump);
|
|
||||||
|
|
||||||
memory_dealloc(mtl->name);
|
memory_dealloc(mtl->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -957,12 +955,12 @@ const char* read_mtl_triple(const char* p, float v[3])
|
||||||
|
|
||||||
|
|
||||||
static
|
static
|
||||||
const char* read_map(fastObjData* data, const char* ptr, fastObjTexture* map)
|
const char* read_map(fastObjData* data, const char* ptr, unsigned int* idx)
|
||||||
{
|
{
|
||||||
const char* s;
|
const char* s;
|
||||||
const char* e;
|
const char* e;
|
||||||
char* name;
|
fastObjTexture* map;
|
||||||
char* path;
|
|
||||||
|
|
||||||
ptr = skip_whitespace(ptr);
|
ptr = skip_whitespace(ptr);
|
||||||
|
|
||||||
|
@ -978,13 +976,26 @@ const char* read_map(fastObjData* data, const char* ptr, fastObjTexture* map)
|
||||||
|
|
||||||
e = ptr;
|
e = ptr;
|
||||||
|
|
||||||
name = string_copy(s, e);
|
/* Try to find an existing texture map with the same name */
|
||||||
|
*idx = 1; /* skip dummy at index 0 */
|
||||||
|
while (*idx < array_size(data->mesh->textures))
|
||||||
|
{
|
||||||
|
map = &data->mesh->textures[*idx];
|
||||||
|
if (map->name && string_equal(map->name, s, e))
|
||||||
|
break;
|
||||||
|
|
||||||
path = string_concat(data->base, s, e);
|
(*idx)++;
|
||||||
string_fix_separators(path);
|
}
|
||||||
|
|
||||||
map->name = name;
|
/* Add it to the texture array if it didn't already exist */
|
||||||
map->path = path;
|
if (*idx == array_size(data->mesh->textures))
|
||||||
|
{
|
||||||
|
fastObjTexture new_map = map_default();
|
||||||
|
new_map.name = string_copy(s, e);
|
||||||
|
new_map.path = string_concat(data->base, s, e);
|
||||||
|
string_fix_separators(new_map.path);
|
||||||
|
array_push(data->mesh->textures, new_map);
|
||||||
|
}
|
||||||
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
@ -1344,6 +1355,9 @@ void fast_obj_destroy(fastObjMesh* m)
|
||||||
for (ii = 0; ii < array_size(m->materials); ii++)
|
for (ii = 0; ii < array_size(m->materials); ii++)
|
||||||
mtl_clean(&m->materials[ii]);
|
mtl_clean(&m->materials[ii]);
|
||||||
|
|
||||||
|
for (ii = 0; ii < array_size(m->textures); ii++)
|
||||||
|
map_clean(&m->textures[ii]);
|
||||||
|
|
||||||
array_clean(m->positions);
|
array_clean(m->positions);
|
||||||
array_clean(m->texcoords);
|
array_clean(m->texcoords);
|
||||||
array_clean(m->normals);
|
array_clean(m->normals);
|
||||||
|
@ -1353,6 +1367,7 @@ void fast_obj_destroy(fastObjMesh* m)
|
||||||
array_clean(m->objects);
|
array_clean(m->objects);
|
||||||
array_clean(m->groups);
|
array_clean(m->groups);
|
||||||
array_clean(m->materials);
|
array_clean(m->materials);
|
||||||
|
array_clean(m->textures);
|
||||||
|
|
||||||
memory_dealloc(m);
|
memory_dealloc(m);
|
||||||
}
|
}
|
||||||
|
@ -1405,11 +1420,12 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac
|
||||||
m->face_materials = 0;
|
m->face_materials = 0;
|
||||||
m->indices = 0;
|
m->indices = 0;
|
||||||
m->materials = 0;
|
m->materials = 0;
|
||||||
|
m->textures = 0;
|
||||||
m->objects = 0;
|
m->objects = 0;
|
||||||
m->groups = 0;
|
m->groups = 0;
|
||||||
|
|
||||||
|
|
||||||
/* Add dummy position/texcoord/normal */
|
/* Add dummy position/texcoord/normal/texture */
|
||||||
array_push(m->positions, 0.0f);
|
array_push(m->positions, 0.0f);
|
||||||
array_push(m->positions, 0.0f);
|
array_push(m->positions, 0.0f);
|
||||||
array_push(m->positions, 0.0f);
|
array_push(m->positions, 0.0f);
|
||||||
|
@ -1421,6 +1437,8 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac
|
||||||
array_push(m->normals, 0.0f);
|
array_push(m->normals, 0.0f);
|
||||||
array_push(m->normals, 1.0f);
|
array_push(m->normals, 1.0f);
|
||||||
|
|
||||||
|
array_push(m->textures, map_default());
|
||||||
|
|
||||||
|
|
||||||
/* Data needed during parsing */
|
/* Data needed during parsing */
|
||||||
data.mesh = m;
|
data.mesh = m;
|
||||||
|
@ -1511,6 +1529,7 @@ fastObjMesh* fast_obj_read_with_callbacks(const char* path, const fastObjCallbac
|
||||||
m->face_count = array_size(m->face_vertices);
|
m->face_count = array_size(m->face_vertices);
|
||||||
m->index_count = array_size(m->indices);
|
m->index_count = array_size(m->indices);
|
||||||
m->material_count = array_size(m->materials);
|
m->material_count = array_size(m->materials);
|
||||||
|
m->texture_count = array_size(m->textures);
|
||||||
m->object_count = array_size(m->objects);
|
m->object_count = array_size(m->objects);
|
||||||
m->group_count = array_size(m->groups);
|
m->group_count = array_size(m->groups);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue