Update tinyobj_loader_c.h
This commit is contained in:
parent
402ce4e53b
commit
53b32f1c20
1 changed files with 102 additions and 100 deletions
202
src/external/tinyobj_loader_c.h
vendored
202
src/external/tinyobj_loader_c.h
vendored
|
@ -24,8 +24,8 @@
|
||||||
#ifndef TINOBJ_LOADER_C_H_
|
#ifndef TINOBJ_LOADER_C_H_
|
||||||
#define TINOBJ_LOADER_C_H_
|
#define TINOBJ_LOADER_C_H_
|
||||||
|
|
||||||
/* @todo { Remove stddef dependency. unsigned int? } ---> RAY: DONE. */
|
/* @todo { Remove stddef dependency. size_t? } */
|
||||||
//#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *name;
|
char *name;
|
||||||
|
@ -93,18 +93,18 @@ typedef struct {
|
||||||
* Returns TINYOBJ_ERR_*** when there is an error.
|
* Returns TINYOBJ_ERR_*** when there is an error.
|
||||||
*/
|
*/
|
||||||
extern int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
extern int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
||||||
unsigned int *num_shapes, tinyobj_material_t **materials,
|
size_t *num_shapes, tinyobj_material_t **materials,
|
||||||
unsigned int *num_materials, const char *buf, unsigned int len,
|
size_t *num_materials, const char *buf, size_t len,
|
||||||
unsigned int flags);
|
unsigned int flags);
|
||||||
extern int tinyobj_parse_mtl_file(tinyobj_material_t **materials_out,
|
extern int tinyobj_parse_mtl_file(tinyobj_material_t **materials_out,
|
||||||
unsigned int *num_materials_out,
|
size_t *num_materials_out,
|
||||||
const char *filename);
|
const char *filename);
|
||||||
|
|
||||||
extern void tinyobj_attrib_init(tinyobj_attrib_t *attrib);
|
extern void tinyobj_attrib_init(tinyobj_attrib_t *attrib);
|
||||||
extern void tinyobj_attrib_free(tinyobj_attrib_t *attrib);
|
extern void tinyobj_attrib_free(tinyobj_attrib_t *attrib);
|
||||||
extern void tinyobj_shapes_free(tinyobj_shape_t *shapes, unsigned int num_shapes);
|
extern void tinyobj_shapes_free(tinyobj_shape_t *shapes, size_t num_shapes);
|
||||||
extern void tinyobj_materials_free(tinyobj_material_t *materials,
|
extern void tinyobj_materials_free(tinyobj_material_t *materials,
|
||||||
unsigned int num_materials);
|
size_t num_materials);
|
||||||
|
|
||||||
#ifdef TINYOBJ_LOADER_C_IMPLEMENTATION
|
#ifdef TINYOBJ_LOADER_C_IMPLEMENTATION
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -155,8 +155,8 @@ static int until_space(const char *token) {
|
||||||
return (int)(p - token);
|
return (int)(p - token);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int length_until_newline(const char *token, unsigned int n) {
|
static size_t length_until_newline(const char *token, size_t n) {
|
||||||
unsigned int len = 0;
|
size_t len = 0;
|
||||||
|
|
||||||
/* Assume token[n-1] = '\0' */
|
/* Assume token[n-1] = '\0' */
|
||||||
for (len = 0; len < n - 1; len++) {
|
for (len = 0; len < n - 1; len++) {
|
||||||
|
@ -171,8 +171,8 @@ static unsigned int length_until_newline(const char *token, unsigned int n) {
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int length_until_line_feed(const char *token, unsigned int n) {
|
static size_t length_until_line_feed(const char *token, size_t n) {
|
||||||
unsigned int len = 0;
|
size_t len = 0;
|
||||||
|
|
||||||
/* Assume token[n-1] = '\0' */
|
/* Assume token[n-1] = '\0' */
|
||||||
for (len = 0; len < n; len++) {
|
for (len = 0; len < n; len++) {
|
||||||
|
@ -202,7 +202,7 @@ static int my_atoi(const char *c) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make index zero-base, and also support relative index. */
|
/* Make index zero-base, and also support relative index. */
|
||||||
static int fixIndex(int idx, unsigned int n) {
|
static int fixIndex(int idx, size_t n) {
|
||||||
if (idx > 0) return idx - 1;
|
if (idx > 0) return idx - 1;
|
||||||
if (idx == 0) return 0;
|
if (idx == 0) return 0;
|
||||||
return (int)n + idx; /* negative value = relative */
|
return (int)n + idx; /* negative value = relative */
|
||||||
|
@ -453,9 +453,14 @@ static void parseFloat3(float *x, float *y, float *z, const char **token) {
|
||||||
(*z) = parseFloat(token);
|
(*z) = parseFloat(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *my_strdup(const char *s, unsigned int max_length) {
|
static size_t my_strnlen(const char *s, size_t n) {
|
||||||
|
const char *p = memchr(s, 0, n);
|
||||||
|
return p ? (size_t)(p - s) : n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *my_strdup(const char *s, size_t max_length) {
|
||||||
char *d;
|
char *d;
|
||||||
unsigned int len;
|
size_t len;
|
||||||
|
|
||||||
if (s == NULL) return NULL;
|
if (s == NULL) return NULL;
|
||||||
|
|
||||||
|
@ -465,36 +470,34 @@ static char *my_strdup(const char *s, unsigned int max_length) {
|
||||||
|
|
||||||
/* trim line ending and append '\0' */
|
/* trim line ending and append '\0' */
|
||||||
d = (char *)TINYOBJ_MALLOC(len + 1); /* + '\0' */
|
d = (char *)TINYOBJ_MALLOC(len + 1); /* + '\0' */
|
||||||
memcpy(d, s, (unsigned int)(len));
|
memcpy(d, s, (size_t)(len));
|
||||||
d[len] = '\0';
|
d[len] = '\0';
|
||||||
|
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *my_strndup(const char *s, unsigned int len) {
|
static char *my_strndup(const char *s, size_t len) {
|
||||||
char *d;
|
char *d;
|
||||||
unsigned int slen;
|
size_t slen;
|
||||||
|
|
||||||
if (s == NULL) return NULL;
|
if (s == NULL) return NULL;
|
||||||
if (len == 0) return NULL;
|
if (len == 0) return NULL;
|
||||||
|
|
||||||
d = (char *)TINYOBJ_MALLOC(len + 1); /* + '\0' */
|
slen = my_strnlen(s, len);
|
||||||
slen = strlen(s);
|
d = (char *)TINYOBJ_MALLOC(slen + 1); /* + '\0' */
|
||||||
if (slen < len) {
|
if (!d) {
|
||||||
memcpy(d, s, slen);
|
return NULL;
|
||||||
d[slen] = '\0';
|
|
||||||
} else {
|
|
||||||
memcpy(d, s, len);
|
|
||||||
d[len] = '\0';
|
|
||||||
}
|
}
|
||||||
|
memcpy(d, s, slen);
|
||||||
|
d[slen] = '\0';
|
||||||
|
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *dynamic_fgets(char **buf, unsigned int *size, FILE *file) {
|
char *dynamic_fgets(char **buf, size_t *size, FILE *file) {
|
||||||
char *offset;
|
char *offset;
|
||||||
char *ret;
|
char *ret;
|
||||||
unsigned int old_size;
|
size_t old_size;
|
||||||
|
|
||||||
if (!(ret = fgets(*buf, (int)*size, file))) {
|
if (!(ret = fgets(*buf, (int)*size, file))) {
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -560,8 +563,8 @@ typedef struct
|
||||||
{
|
{
|
||||||
unsigned long* hashes;
|
unsigned long* hashes;
|
||||||
hash_table_entry_t* entries;
|
hash_table_entry_t* entries;
|
||||||
unsigned int capacity;
|
size_t capacity;
|
||||||
unsigned int n;
|
size_t n;
|
||||||
} hash_table_t;
|
} hash_table_t;
|
||||||
|
|
||||||
static unsigned long hash_djb2(const unsigned char* str)
|
static unsigned long hash_djb2(const unsigned char* str)
|
||||||
|
@ -576,7 +579,7 @@ static unsigned long hash_djb2(const unsigned char* str)
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void create_hash_table(unsigned int start_capacity, hash_table_t* hash_table)
|
static void create_hash_table(size_t start_capacity, hash_table_t* hash_table)
|
||||||
{
|
{
|
||||||
if (start_capacity < 1)
|
if (start_capacity < 1)
|
||||||
start_capacity = HASH_TABLE_DEFAULT_SIZE;
|
start_capacity = HASH_TABLE_DEFAULT_SIZE;
|
||||||
|
@ -596,10 +599,10 @@ static void destroy_hash_table(hash_table_t* hash_table)
|
||||||
static int hash_table_insert_value(unsigned long hash, long value, hash_table_t* hash_table)
|
static int hash_table_insert_value(unsigned long hash, long value, hash_table_t* hash_table)
|
||||||
{
|
{
|
||||||
/* Insert value */
|
/* Insert value */
|
||||||
unsigned int start_index = hash % hash_table->capacity;
|
size_t start_index = hash % hash_table->capacity;
|
||||||
unsigned int index = start_index;
|
size_t index = start_index;
|
||||||
hash_table_entry_t* start_entry = hash_table->entries + start_index;
|
hash_table_entry_t* start_entry = hash_table->entries + start_index;
|
||||||
unsigned int i;
|
size_t i;
|
||||||
hash_table_entry_t* entry;
|
hash_table_entry_t* entry;
|
||||||
|
|
||||||
for (i = 1; hash_table->entries[index].filled; i++)
|
for (i = 1; hash_table->entries[index].filled; i++)
|
||||||
|
@ -648,11 +651,11 @@ static hash_table_entry_t* hash_table_find(unsigned long hash, hash_table_t* has
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hash_table_maybe_grow(unsigned int new_n, hash_table_t* hash_table)
|
static void hash_table_maybe_grow(size_t new_n, hash_table_t* hash_table)
|
||||||
{
|
{
|
||||||
unsigned int new_capacity;
|
size_t new_capacity;
|
||||||
hash_table_t new_hash_table;
|
hash_table_t new_hash_table;
|
||||||
unsigned int i;
|
size_t i;
|
||||||
|
|
||||||
if (new_n <= hash_table->capacity) {
|
if (new_n <= hash_table->capacity) {
|
||||||
return;
|
return;
|
||||||
|
@ -680,7 +683,7 @@ static int hash_table_exists(const char* name, hash_table_t* hash_table)
|
||||||
return hash_table_find(hash_djb2((const unsigned char*)name), hash_table) != NULL;
|
return hash_table_find(hash_djb2((const unsigned char*)name), hash_table) != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hash_table_set(const char* name, unsigned int val, hash_table_t* hash_table)
|
static void hash_table_set(const char* name, size_t val, hash_table_t* hash_table)
|
||||||
{
|
{
|
||||||
/* Hash name */
|
/* Hash name */
|
||||||
unsigned long hash = hash_djb2((const unsigned char *)name);
|
unsigned long hash = hash_djb2((const unsigned char *)name);
|
||||||
|
@ -709,7 +712,7 @@ static long hash_table_get(const char* name, hash_table_t* hash_table)
|
||||||
}
|
}
|
||||||
|
|
||||||
static tinyobj_material_t *tinyobj_material_add(tinyobj_material_t *prev,
|
static tinyobj_material_t *tinyobj_material_add(tinyobj_material_t *prev,
|
||||||
unsigned int num_materials,
|
size_t num_materials,
|
||||||
tinyobj_material_t *new_mat) {
|
tinyobj_material_t *new_mat) {
|
||||||
tinyobj_material_t *dst;
|
tinyobj_material_t *dst;
|
||||||
dst = (tinyobj_material_t *)TINYOBJ_REALLOC(
|
dst = (tinyobj_material_t *)TINYOBJ_REALLOC(
|
||||||
|
@ -720,14 +723,14 @@ static tinyobj_material_t *tinyobj_material_add(tinyobj_material_t *prev,
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out,
|
static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out,
|
||||||
unsigned int *num_materials_out,
|
size_t *num_materials_out,
|
||||||
const char *filename,
|
const char *filename,
|
||||||
hash_table_t* material_table) {
|
hash_table_t* material_table) {
|
||||||
tinyobj_material_t material;
|
tinyobj_material_t material;
|
||||||
unsigned int buffer_size = 128;
|
size_t buffer_size = 128;
|
||||||
char *linebuf;
|
char *linebuf;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
unsigned int num_materials = 0;
|
size_t num_materials = 0;
|
||||||
tinyobj_material_t *materials = NULL;
|
tinyobj_material_t *materials = NULL;
|
||||||
int has_previous_material = 0;
|
int has_previous_material = 0;
|
||||||
const char *line_end = NULL;
|
const char *line_end = NULL;
|
||||||
|
@ -788,7 +791,7 @@ static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out,
|
||||||
#else
|
#else
|
||||||
sscanf(token, "%s", namebuf);
|
sscanf(token, "%s", namebuf);
|
||||||
#endif
|
#endif
|
||||||
material.name = my_strdup(namebuf, (unsigned int) (line_end - token));
|
material.name = my_strdup(namebuf, (size_t) (line_end - token));
|
||||||
|
|
||||||
/* Add material to material table */
|
/* Add material to material table */
|
||||||
if (material_table)
|
if (material_table)
|
||||||
|
@ -889,56 +892,56 @@ static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out,
|
||||||
/* ambient texture */
|
/* ambient texture */
|
||||||
if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) {
|
if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) {
|
||||||
token += 7;
|
token += 7;
|
||||||
material.ambient_texname = my_strdup(token, (unsigned int) (line_end - token));
|
material.ambient_texname = my_strdup(token, (size_t) (line_end - token));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* diffuse texture */
|
/* diffuse texture */
|
||||||
if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) {
|
if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) {
|
||||||
token += 7;
|
token += 7;
|
||||||
material.diffuse_texname = my_strdup(token, (unsigned int) (line_end - token));
|
material.diffuse_texname = my_strdup(token, (size_t) (line_end - token));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* specular texture */
|
/* specular texture */
|
||||||
if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) {
|
if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) {
|
||||||
token += 7;
|
token += 7;
|
||||||
material.specular_texname = my_strdup(token, (unsigned int) (line_end - token));
|
material.specular_texname = my_strdup(token, (size_t) (line_end - token));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* specular highlight texture */
|
/* specular highlight texture */
|
||||||
if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) {
|
if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) {
|
||||||
token += 7;
|
token += 7;
|
||||||
material.specular_highlight_texname = my_strdup(token, (unsigned int) (line_end - token));
|
material.specular_highlight_texname = my_strdup(token, (size_t) (line_end - token));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bump texture */
|
/* bump texture */
|
||||||
if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) {
|
if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) {
|
||||||
token += 9;
|
token += 9;
|
||||||
material.bump_texname = my_strdup(token, (unsigned int) (line_end - token));
|
material.bump_texname = my_strdup(token, (size_t) (line_end - token));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* alpha texture */
|
/* alpha texture */
|
||||||
if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) {
|
if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) {
|
||||||
token += 6;
|
token += 6;
|
||||||
material.alpha_texname = my_strdup(token, (unsigned int) (line_end - token));
|
material.alpha_texname = my_strdup(token, (size_t) (line_end - token));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bump texture */
|
/* bump texture */
|
||||||
if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) {
|
if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) {
|
||||||
token += 5;
|
token += 5;
|
||||||
material.bump_texname = my_strdup(token, (unsigned int) (line_end - token));
|
material.bump_texname = my_strdup(token, (size_t) (line_end - token));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* displacement texture */
|
/* displacement texture */
|
||||||
if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) {
|
if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) {
|
||||||
token += 5;
|
token += 5;
|
||||||
material.displacement_texname = my_strdup(token, (unsigned int) (line_end - token));
|
material.displacement_texname = my_strdup(token, (size_t) (line_end - token));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -962,7 +965,7 @@ static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out,
|
||||||
}
|
}
|
||||||
|
|
||||||
int tinyobj_parse_mtl_file(tinyobj_material_t **materials_out,
|
int tinyobj_parse_mtl_file(tinyobj_material_t **materials_out,
|
||||||
unsigned int *num_materials_out,
|
size_t *num_materials_out,
|
||||||
const char *filename) {
|
const char *filename) {
|
||||||
return tinyobj_parse_and_index_mtl_file(materials_out, num_materials_out, filename, NULL);
|
return tinyobj_parse_and_index_mtl_file(materials_out, num_materials_out, filename, NULL);
|
||||||
}
|
}
|
||||||
|
@ -988,10 +991,10 @@ typedef struct {
|
||||||
|
|
||||||
/* @todo { Use dynamic array } */
|
/* @todo { Use dynamic array } */
|
||||||
tinyobj_vertex_index_t f[TINYOBJ_MAX_FACES_PER_F_LINE];
|
tinyobj_vertex_index_t f[TINYOBJ_MAX_FACES_PER_F_LINE];
|
||||||
unsigned int num_f;
|
size_t num_f;
|
||||||
|
|
||||||
int f_num_verts[TINYOBJ_MAX_FACES_PER_F_LINE];
|
int f_num_verts[TINYOBJ_MAX_FACES_PER_F_LINE];
|
||||||
unsigned int num_f_num_verts;
|
size_t num_f_num_verts;
|
||||||
|
|
||||||
const char *group_name;
|
const char *group_name;
|
||||||
unsigned int group_name_len;
|
unsigned int group_name_len;
|
||||||
|
@ -1011,7 +1014,7 @@ typedef struct {
|
||||||
CommandType type;
|
CommandType type;
|
||||||
} Command;
|
} Command;
|
||||||
|
|
||||||
static int parseLine(Command *command, const char *p, unsigned int p_len,
|
static int parseLine(Command *command, const char *p, size_t p_len,
|
||||||
int triangulate) {
|
int triangulate) {
|
||||||
char linebuf[4096];
|
char linebuf[4096];
|
||||||
const char *token;
|
const char *token;
|
||||||
|
@ -1073,7 +1076,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len,
|
||||||
|
|
||||||
/* face */
|
/* face */
|
||||||
if (token[0] == 'f' && IS_SPACE((token[1]))) {
|
if (token[0] == 'f' && IS_SPACE((token[1]))) {
|
||||||
unsigned int num_f = 0;
|
size_t num_f = 0;
|
||||||
|
|
||||||
tinyobj_vertex_index_t f[TINYOBJ_MAX_FACES_PER_F_LINE];
|
tinyobj_vertex_index_t f[TINYOBJ_MAX_FACES_PER_F_LINE];
|
||||||
token += 2;
|
token += 2;
|
||||||
|
@ -1090,8 +1093,8 @@ static int parseLine(Command *command, const char *p, unsigned int p_len,
|
||||||
command->type = COMMAND_F;
|
command->type = COMMAND_F;
|
||||||
|
|
||||||
if (triangulate) {
|
if (triangulate) {
|
||||||
unsigned int k;
|
size_t k;
|
||||||
unsigned int n = 0;
|
size_t n = 0;
|
||||||
|
|
||||||
tinyobj_vertex_index_t i0 = f[0];
|
tinyobj_vertex_index_t i0 = f[0];
|
||||||
tinyobj_vertex_index_t i1;
|
tinyobj_vertex_index_t i1;
|
||||||
|
@ -1113,7 +1116,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len,
|
||||||
command->num_f_num_verts = n;
|
command->num_f_num_verts = n;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
unsigned int k = 0;
|
size_t k = 0;
|
||||||
assert(num_f < TINYOBJ_MAX_FACES_PER_F_LINE);
|
assert(num_f < TINYOBJ_MAX_FACES_PER_F_LINE);
|
||||||
for (k = 0; k < num_f; k++) {
|
for (k = 0; k < num_f; k++) {
|
||||||
command->f[k] = f[k];
|
command->f[k] = f[k];
|
||||||
|
@ -1134,7 +1137,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len,
|
||||||
skip_space(&token);
|
skip_space(&token);
|
||||||
command->material_name = p + (token - linebuf);
|
command->material_name = p + (token - linebuf);
|
||||||
command->material_name_len = (unsigned int)length_until_newline(
|
command->material_name_len = (unsigned int)length_until_newline(
|
||||||
token, (p_len - (unsigned int)(token - linebuf)) + 1);
|
token, (p_len - (size_t)(token - linebuf)) + 1);
|
||||||
command->type = COMMAND_USEMTL;
|
command->type = COMMAND_USEMTL;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1148,7 +1151,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len,
|
||||||
skip_space(&token);
|
skip_space(&token);
|
||||||
command->mtllib_name = p + (token - linebuf);
|
command->mtllib_name = p + (token - linebuf);
|
||||||
command->mtllib_name_len = (unsigned int)length_until_newline(
|
command->mtllib_name_len = (unsigned int)length_until_newline(
|
||||||
token, p_len - (unsigned int)(token - linebuf)) +
|
token, p_len - (size_t)(token - linebuf)) +
|
||||||
1;
|
1;
|
||||||
command->type = COMMAND_MTLLIB;
|
command->type = COMMAND_MTLLIB;
|
||||||
|
|
||||||
|
@ -1162,7 +1165,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len,
|
||||||
|
|
||||||
command->group_name = p + (token - linebuf);
|
command->group_name = p + (token - linebuf);
|
||||||
command->group_name_len = (unsigned int)length_until_newline(
|
command->group_name_len = (unsigned int)length_until_newline(
|
||||||
token, p_len - (unsigned int)(token - linebuf)) +
|
token, p_len - (size_t)(token - linebuf)) +
|
||||||
1;
|
1;
|
||||||
command->type = COMMAND_G;
|
command->type = COMMAND_G;
|
||||||
|
|
||||||
|
@ -1176,7 +1179,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len,
|
||||||
|
|
||||||
command->object_name = p + (token - linebuf);
|
command->object_name = p + (token - linebuf);
|
||||||
command->object_name_len = (unsigned int)length_until_newline(
|
command->object_name_len = (unsigned int)length_until_newline(
|
||||||
token, p_len - (unsigned int)(token - linebuf)) +
|
token, p_len - (size_t)(token - linebuf)) +
|
||||||
1;
|
1;
|
||||||
command->type = COMMAND_O;
|
command->type = COMMAND_O;
|
||||||
|
|
||||||
|
@ -1187,11 +1190,11 @@ static int parseLine(Command *command, const char *p, unsigned int p_len,
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned int pos;
|
size_t pos;
|
||||||
unsigned int len;
|
size_t len;
|
||||||
} LineInfo;
|
} LineInfo;
|
||||||
|
|
||||||
static int is_line_ending(const char *p, unsigned int i, unsigned int end_i) {
|
static int is_line_ending(const char *p, size_t i, size_t end_i) {
|
||||||
if (p[i] == '\0') return 1;
|
if (p[i] == '\0') return 1;
|
||||||
if (p[i] == '\n') return 1; /* this includes \r\n */
|
if (p[i] == '\n') return 1; /* this includes \r\n */
|
||||||
if (p[i] == '\r') {
|
if (p[i] == '\r') {
|
||||||
|
@ -1203,23 +1206,23 @@ static int is_line_ending(const char *p, unsigned int i, unsigned int end_i) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
||||||
unsigned int *num_shapes, tinyobj_material_t **materials_out,
|
size_t *num_shapes, tinyobj_material_t **materials_out,
|
||||||
unsigned int *num_materials_out, const char *buf, unsigned int len,
|
size_t *num_materials_out, const char *buf, size_t len,
|
||||||
unsigned int flags) {
|
unsigned int flags) {
|
||||||
LineInfo *line_infos = NULL;
|
LineInfo *line_infos = NULL;
|
||||||
Command *commands = NULL;
|
Command *commands = NULL;
|
||||||
unsigned int num_lines = 0;
|
size_t num_lines = 0;
|
||||||
|
|
||||||
unsigned int num_v = 0;
|
size_t num_v = 0;
|
||||||
unsigned int num_vn = 0;
|
size_t num_vn = 0;
|
||||||
unsigned int num_vt = 0;
|
size_t num_vt = 0;
|
||||||
unsigned int num_f = 0;
|
size_t num_f = 0;
|
||||||
unsigned int num_faces = 0;
|
size_t num_faces = 0;
|
||||||
|
|
||||||
int mtllib_line_index = -1;
|
int mtllib_line_index = -1;
|
||||||
|
|
||||||
tinyobj_material_t *materials = NULL;
|
tinyobj_material_t *materials = NULL;
|
||||||
unsigned int num_materials = 0;
|
size_t num_materials = 0;
|
||||||
|
|
||||||
hash_table_t material_table;
|
hash_table_t material_table;
|
||||||
|
|
||||||
|
@ -1234,11 +1237,11 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
||||||
tinyobj_attrib_init(attrib);
|
tinyobj_attrib_init(attrib);
|
||||||
/* 1. Find '\n' and create line data. */
|
/* 1. Find '\n' and create line data. */
|
||||||
{
|
{
|
||||||
unsigned int i;
|
size_t i;
|
||||||
unsigned int end_idx = len;
|
size_t end_idx = len;
|
||||||
unsigned int prev_pos = 0;
|
size_t prev_pos = 0;
|
||||||
unsigned int line_no = 0;
|
size_t line_no = 0;
|
||||||
unsigned int last_line_ending = 0;
|
size_t last_line_ending = 0;
|
||||||
|
|
||||||
/* Count # of lines. */
|
/* Count # of lines. */
|
||||||
for (i = 0; i < end_idx; i++) {
|
for (i = 0; i < end_idx; i++) {
|
||||||
|
@ -1280,7 +1283,7 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
||||||
|
|
||||||
/* 2. parse each line */
|
/* 2. parse each line */
|
||||||
{
|
{
|
||||||
unsigned int i = 0;
|
size_t i = 0;
|
||||||
for (i = 0; i < num_lines; i++) {
|
for (i = 0; i < num_lines; i++) {
|
||||||
int ret = parseLine(&commands[i], &buf[line_infos[i].pos],
|
int ret = parseLine(&commands[i], &buf[line_infos[i].pos],
|
||||||
line_infos[i].len, flags & TINYOBJ_FLAG_TRIANGULATE);
|
line_infos[i].len, flags & TINYOBJ_FLAG_TRIANGULATE);
|
||||||
|
@ -1328,13 +1331,13 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
||||||
/* Construct attributes */
|
/* Construct attributes */
|
||||||
|
|
||||||
{
|
{
|
||||||
unsigned int v_count = 0;
|
size_t v_count = 0;
|
||||||
unsigned int n_count = 0;
|
size_t n_count = 0;
|
||||||
unsigned int t_count = 0;
|
size_t t_count = 0;
|
||||||
unsigned int f_count = 0;
|
size_t f_count = 0;
|
||||||
unsigned int face_count = 0;
|
size_t face_count = 0;
|
||||||
int material_id = -1; /* -1 = default unknown material. */
|
int material_id = -1; /* -1 = default unknown material. */
|
||||||
unsigned int i = 0;
|
size_t i = 0;
|
||||||
|
|
||||||
attrib->vertices = (float *)TINYOBJ_MALLOC(sizeof(float) * num_v * 3);
|
attrib->vertices = (float *)TINYOBJ_MALLOC(sizeof(float) * num_v * 3);
|
||||||
attrib->num_vertices = (unsigned int)num_v;
|
attrib->num_vertices = (unsigned int)num_v;
|
||||||
|
@ -1342,13 +1345,12 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
||||||
attrib->num_normals = (unsigned int)num_vn;
|
attrib->num_normals = (unsigned int)num_vn;
|
||||||
attrib->texcoords = (float *)TINYOBJ_MALLOC(sizeof(float) * num_vt * 2);
|
attrib->texcoords = (float *)TINYOBJ_MALLOC(sizeof(float) * num_vt * 2);
|
||||||
attrib->num_texcoords = (unsigned int)num_vt;
|
attrib->num_texcoords = (unsigned int)num_vt;
|
||||||
attrib->faces = (tinyobj_vertex_index_t *)TINYOBJ_MALLOC(sizeof(tinyobj_vertex_index_t) * num_f);
|
attrib->faces = (tinyobj_vertex_index_t *)TINYOBJ_MALLOC(
|
||||||
|
sizeof(tinyobj_vertex_index_t) * num_f);
|
||||||
|
attrib->num_faces = (unsigned int)num_f;
|
||||||
attrib->face_num_verts = (int *)TINYOBJ_MALLOC(sizeof(int) * num_faces);
|
attrib->face_num_verts = (int *)TINYOBJ_MALLOC(sizeof(int) * num_faces);
|
||||||
|
|
||||||
attrib->num_faces = (unsigned int)num_faces;
|
|
||||||
attrib->num_face_num_verts = (unsigned int)num_f;
|
|
||||||
|
|
||||||
attrib->material_ids = (int *)TINYOBJ_MALLOC(sizeof(int) * num_faces);
|
attrib->material_ids = (int *)TINYOBJ_MALLOC(sizeof(int) * num_faces);
|
||||||
|
attrib->num_face_num_verts = (unsigned int)num_faces;
|
||||||
|
|
||||||
for (i = 0; i < num_lines; i++) {
|
for (i = 0; i < num_lines; i++) {
|
||||||
if (commands[i].type == COMMAND_EMPTY) {
|
if (commands[i].type == COMMAND_EMPTY) {
|
||||||
|
@ -1398,7 +1400,7 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
||||||
attrib->texcoords[2 * t_count + 1] = commands[i].ty;
|
attrib->texcoords[2 * t_count + 1] = commands[i].ty;
|
||||||
t_count++;
|
t_count++;
|
||||||
} else if (commands[i].type == COMMAND_F) {
|
} else if (commands[i].type == COMMAND_F) {
|
||||||
unsigned int k = 0;
|
size_t k = 0;
|
||||||
for (k = 0; k < commands[i].num_f; k++) {
|
for (k = 0; k < commands[i].num_f; k++) {
|
||||||
tinyobj_vertex_index_t vi = commands[i].f[k];
|
tinyobj_vertex_index_t vi = commands[i].f[k];
|
||||||
int v_idx = fixIndex(vi.v_idx, v_count);
|
int v_idx = fixIndex(vi.v_idx, v_count);
|
||||||
|
@ -1423,9 +1425,9 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
||||||
/* 5. Construct shape information. */
|
/* 5. Construct shape information. */
|
||||||
{
|
{
|
||||||
unsigned int face_count = 0;
|
unsigned int face_count = 0;
|
||||||
unsigned int i = 0;
|
size_t i = 0;
|
||||||
unsigned int n = 0;
|
size_t n = 0;
|
||||||
unsigned int shape_idx = 0;
|
size_t shape_idx = 0;
|
||||||
|
|
||||||
const char *shape_name = NULL;
|
const char *shape_name = NULL;
|
||||||
unsigned int shape_name_len = 0;
|
unsigned int shape_name_len = 0;
|
||||||
|
@ -1497,7 +1499,7 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes,
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((face_count - prev_face_offset) > 0) {
|
if ((face_count - prev_face_offset) > 0) {
|
||||||
unsigned int length = face_count - prev_shape_face_offset;
|
size_t length = face_count - prev_shape_face_offset;
|
||||||
if (length > 0) {
|
if (length > 0) {
|
||||||
(*shapes)[shape_idx].name =
|
(*shapes)[shape_idx].name =
|
||||||
my_strndup(prev_shape_name, prev_shape_name_len);
|
my_strndup(prev_shape_name, prev_shape_name_len);
|
||||||
|
@ -1548,8 +1550,8 @@ void tinyobj_attrib_free(tinyobj_attrib_t *attrib) {
|
||||||
if (attrib->material_ids) TINYOBJ_FREE(attrib->material_ids);
|
if (attrib->material_ids) TINYOBJ_FREE(attrib->material_ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tinyobj_shapes_free(tinyobj_shape_t *shapes, unsigned int num_shapes) {
|
void tinyobj_shapes_free(tinyobj_shape_t *shapes, size_t num_shapes) {
|
||||||
unsigned int i;
|
size_t i;
|
||||||
if (shapes == NULL) return;
|
if (shapes == NULL) return;
|
||||||
|
|
||||||
for (i = 0; i < num_shapes; i++) {
|
for (i = 0; i < num_shapes; i++) {
|
||||||
|
@ -1560,8 +1562,8 @@ void tinyobj_shapes_free(tinyobj_shape_t *shapes, unsigned int num_shapes) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void tinyobj_materials_free(tinyobj_material_t *materials,
|
void tinyobj_materials_free(tinyobj_material_t *materials,
|
||||||
unsigned int num_materials) {
|
size_t num_materials) {
|
||||||
unsigned int i;
|
size_t i;
|
||||||
if (materials == NULL) return;
|
if (materials == NULL) return;
|
||||||
|
|
||||||
for (i = 0; i < num_materials; i++) {
|
for (i = 0; i < num_materials; i++) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue