|
|
@ -24,8 +24,8 @@ |
|
|
|
#ifndef TINOBJ_LOADER_C_H_ |
|
|
|
#define TINOBJ_LOADER_C_H_ |
|
|
|
|
|
|
|
/* @todo { Remove stddef dependency. unsigned int? } ---> RAY: DONE. */ |
|
|
|
o">//#include <stddef.h> |
|
|
|
/* @todo { Remove stddef dependency. size_t? } */ |
|
|
|
cp">#include <stddef.h> |
|
|
|
|
|
|
|
typedef struct { |
|
|
|
char *name; |
|
|
@ -98,7 +98,7 @@ typedef struct { |
|
|
|
* @param[out] buf Content of loaded file |
|
|
|
* @param[out] len Size of content(file) |
|
|
|
*/ |
|
|
|
typedef void (*file_reader_callback)(const char *filename, int is_mtl, const char *obj_filename, char **buf, kt">unsigned char *len); |
|
|
|
typedef void (*file_reader_callback)(const char *filename, int is_mtl, const char *obj_filename, char **buf, n">size_t *len); |
|
|
|
|
|
|
|
/* Parse wavefront .obj |
|
|
|
* @param[out] attrib Attibutes |
|
|
@ -114,8 +114,8 @@ typedef void (*file_reader_callback)(const char *filename, int is_mtl, const cha |
|
|
|
* Returns TINYOBJ_ERR_*** when there is an error. |
|
|
|
*/ |
|
|
|
extern int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes, |
|
|
|
kt">unsigned int *num_shapes, tinyobj_material_t **materials, |
|
|
|
kt">unsigned int *num_materials, const char *file_name, file_reader_callback file_reader, |
|
|
|
n">size_t *num_shapes, tinyobj_material_t **materials, |
|
|
|
n">size_t *num_materials, const char *file_name, file_reader_callback file_reader, |
|
|
|
unsigned int flags); |
|
|
|
|
|
|
|
/* Parse wavefront .mtl |
|
|
@ -130,14 +130,14 @@ extern int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes, |
|
|
|
* Returns TINYOBJ_ERR_*** when there is an error. |
|
|
|
*/ |
|
|
|
extern int tinyobj_parse_mtl_file(tinyobj_material_t **materials_out, |
|
|
|
kt">unsigned int *num_materials_out, |
|
|
|
n">size_t *num_materials_out, |
|
|
|
const char *filename, const char *obj_filename, file_reader_callback file_reader); |
|
|
|
|
|
|
|
extern void tinyobj_attrib_init(tinyobj_attrib_t *attrib); |
|
|
|
extern void tinyobj_attrib_free(tinyobj_attrib_t *attrib); |
|
|
|
extern void tinyobj_shapes_free(tinyobj_shape_t *shapes, kt">unsigned char num_shapes); |
|
|
|
extern void tinyobj_shapes_free(tinyobj_shape_t *shapes, n">size_t num_shapes); |
|
|
|
extern void tinyobj_materials_free(tinyobj_material_t *materials, |
|
|
|
kt">unsigned char num_materials); |
|
|
|
n">size_t num_materials); |
|
|
|
|
|
|
|
#ifdef TINYOBJ_LOADER_C_IMPLEMENTATION |
|
|
|
#include <stdio.h> |
|
|
@ -189,8 +189,8 @@ static int until_space(const char *token) { |
|
|
|
return (int)(p - token); |
|
|
|
} |
|
|
|
|
|
|
|
static kt">unsigned int length_until_newline(const char *token, kt">unsigned int n) { |
|
|
|
kt">unsigned int len = 0; |
|
|
|
static n">size_t length_until_newline(const char *token, n">size_t n) { |
|
|
|
n">size_t len = 0; |
|
|
|
|
|
|
|
/* Assume token[n-1] = '\0' */ |
|
|
|
for (len = 0; len < n - 1; len++) { |
|
|
@ -205,8 +205,8 @@ static unsigned int length_until_newline(const char *token, unsigned int n) { |
|
|
|
return len; |
|
|
|
} |
|
|
|
|
|
|
|
static kt">unsigned int length_until_line_feed(const char *token, kt">unsigned int n) { |
|
|
|
kt">unsigned int len = 0; |
|
|
|
static n">size_t length_until_line_feed(const char *token, n">size_t n) { |
|
|
|
n">size_t len = 0; |
|
|
|
|
|
|
|
/* Assume token[n-1] = '\0' */ |
|
|
|
for (len = 0; len < n; len++) { |
|
|
@ -236,7 +236,7 @@ static int my_atoi(const char *c) { |
|
|
|
} |
|
|
|
|
|
|
|
/* Make index zero-base, and also support relative index. */ |
|
|
|
static int fixIndex(int idx, kt">unsigned int n) { |
|
|
|
static int fixIndex(int idx, n">size_t n) { |
|
|
|
if (idx > 0) return idx - 1; |
|
|
|
if (idx == 0) return 0; |
|
|
|
return (int)n + idx; /* negative value = relative */ |
|
|
@ -487,14 +487,14 @@ static void parseFloat3(float *x, float *y, float *z, const char **token) { |
|
|
|
(*z) = parseFloat(token); |
|
|
|
} |
|
|
|
|
|
|
|
static kt">unsigned int my_strnlen(const char *s, kt">unsigned int n) { |
|
|
|
static n">size_t my_strnlen(const char *s, n">size_t n) { |
|
|
|
const char *p = memchr(s, 0, n); |
|
|
|
return p ? (kt">unsigned int)(p - s) : n; |
|
|
|
return p ? (n">size_t)(p - s) : n; |
|
|
|
} |
|
|
|
|
|
|
|
static char *my_strdup(const char *s, kt">unsigned int max_length) { |
|
|
|
static char *my_strdup(const char *s, n">size_t max_length) { |
|
|
|
char *d; |
|
|
|
kt">unsigned int len; |
|
|
|
n">size_t len; |
|
|
|
|
|
|
|
if (s == NULL) return NULL; |
|
|
|
|
|
|
@ -504,15 +504,15 @@ static char *my_strdup(const char *s, unsigned int max_length) { |
|
|
|
|
|
|
|
/* trim line ending and append '\0' */ |
|
|
|
d = (char *)TINYOBJ_MALLOC(len + 1); /* + '\0' */ |
|
|
|
memcpy(d, s, (kt">unsigned int)(len)); |
|
|
|
memcpy(d, s, (n">size_t)(len)); |
|
|
|
d[len] = '\0'; |
|
|
|
|
|
|
|
return d; |
|
|
|
} |
|
|
|
|
|
|
|
static char *my_strndup(const char *s, kt">unsigned int len) { |
|
|
|
static char *my_strndup(const char *s, n">size_t len) { |
|
|
|
char *d; |
|
|
|
kt">unsigned int slen; |
|
|
|
n">size_t slen; |
|
|
|
|
|
|
|
if (s == NULL) return NULL; |
|
|
|
if (len == 0) return NULL; |
|
|
@ -528,11 +528,11 @@ static char *my_strndup(const char *s, unsigned int len) { |
|
|
|
return d; |
|
|
|
} |
|
|
|
|
|
|
|
static char *my_joinpath(const char *s, const char *t, const char delim, kt">unsigned char max_len) { |
|
|
|
static char *my_joinpath(const char *s, const char *t, const char delim, n">size_t max_len) { |
|
|
|
char *d; |
|
|
|
kt">unsigned char slen; |
|
|
|
kt">unsigned char tlen; |
|
|
|
kt">unsigned char len; |
|
|
|
n">size_t slen; |
|
|
|
n">size_t tlen; |
|
|
|
n">size_t len; |
|
|
|
|
|
|
|
if ((s == NULL) && (t == NULL)) return NULL; |
|
|
|
if (max_len == 0) return NULL; |
|
|
@ -554,10 +554,10 @@ static char *my_joinpath(const char *s, const char *t, const char delim, unsigne |
|
|
|
return d; |
|
|
|
} |
|
|
|
|
|
|
|
char *dynamic_fgets(char **buf, kt">unsigned char *size, FILE *file) { |
|
|
|
char *dynamic_fgets(char **buf, n">size_t *size, FILE *file) { |
|
|
|
char *offset; |
|
|
|
char *ret; |
|
|
|
kt">unsigned int old_size; |
|
|
|
n">size_t old_size; |
|
|
|
|
|
|
|
if (!(ret = fgets(*buf, (int)*size, file))) { |
|
|
|
return ret; |
|
|
@ -623,8 +623,8 @@ typedef struct |
|
|
|
{ |
|
|
|
unsigned long* hashes; |
|
|
|
hash_table_entry_t* entries; |
|
|
|
kt">unsigned int capacity; |
|
|
|
kt">unsigned int n; |
|
|
|
n">size_t capacity; |
|
|
|
n">size_t n; |
|
|
|
} hash_table_t; |
|
|
|
|
|
|
|
static unsigned long hash_djb2(const unsigned char* str) |
|
|
@ -639,7 +639,7 @@ static unsigned long hash_djb2(const unsigned char* str) |
|
|
|
return hash; |
|
|
|
} |
|
|
|
|
|
|
|
static void create_hash_table(kt">unsigned int start_capacity, hash_table_t* hash_table) |
|
|
|
static void create_hash_table(n">size_t start_capacity, hash_table_t* hash_table) |
|
|
|
{ |
|
|
|
if (start_capacity < 1) |
|
|
|
start_capacity = HASH_TABLE_DEFAULT_SIZE; |
|
|
@ -659,10 +659,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) |
|
|
|
{ |
|
|
|
/* Insert value */ |
|
|
|
kt">unsigned int start_index = hash % hash_table->capacity; |
|
|
|
kt">unsigned int index = start_index; |
|
|
|
n">size_t start_index = hash % hash_table->capacity; |
|
|
|
n">size_t index = start_index; |
|
|
|
hash_table_entry_t* start_entry = hash_table->entries + start_index; |
|
|
|
kt">unsigned int i; |
|
|
|
n">size_t i; |
|
|
|
hash_table_entry_t* entry; |
|
|
|
|
|
|
|
for (i = 1; hash_table->entries[index].filled; i++) |
|
|
@ -711,11 +711,11 @@ static hash_table_entry_t* hash_table_find(unsigned long hash, hash_table_t* has |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
static void hash_table_maybe_grow(kt">unsigned int new_n, hash_table_t* hash_table) |
|
|
|
static void hash_table_maybe_grow(n">size_t new_n, hash_table_t* hash_table) |
|
|
|
{ |
|
|
|
kt">unsigned int new_capacity; |
|
|
|
n">size_t new_capacity; |
|
|
|
hash_table_t new_hash_table; |
|
|
|
kt">unsigned char i; |
|
|
|
n">size_t i; |
|
|
|
|
|
|
|
if (new_n <= hash_table->capacity) { |
|
|
|
return; |
|
|
@ -743,7 +743,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; |
|
|
|
} |
|
|
|
|
|
|
|
static void hash_table_set(const char* name, kt">unsigned int val, hash_table_t* hash_table) |
|
|
|
static void hash_table_set(const char* name, n">size_t val, hash_table_t* hash_table) |
|
|
|
{ |
|
|
|
/* Hash name */ |
|
|
|
unsigned long hash = hash_djb2((const unsigned char *)name); |
|
|
@ -772,7 +772,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, |
|
|
|
kt">unsigned int num_materials, |
|
|
|
n">size_t num_materials, |
|
|
|
tinyobj_material_t *new_mat) { |
|
|
|
tinyobj_material_t *dst; |
|
|
|
dst = (tinyobj_material_t *)TINYOBJ_REALLOC( |
|
|
@ -782,7 +782,7 @@ static tinyobj_material_t *tinyobj_material_add(tinyobj_material_t *prev, |
|
|
|
return dst; |
|
|
|
} |
|
|
|
|
|
|
|
static int is_line_ending(const char *p, kt">unsigned char i, unsigned char end_i) { |
|
|
|
static int is_line_ending(const char *p, n">size_t i, size_t end_i) { |
|
|
|
if (p[i] == '\0') return 1; |
|
|
|
if (p[i] == '\n') return 1; /* this includes \r\n */ |
|
|
|
if (p[i] == '\r') { |
|
|
@ -794,18 +794,18 @@ static int is_line_ending(const char *p, unsigned char i, unsigned char end_i) { |
|
|
|
} |
|
|
|
|
|
|
|
typedef struct { |
|
|
|
kt">unsigned char pos; |
|
|
|
kt">unsigned char len; |
|
|
|
n">size_t pos; |
|
|
|
n">size_t len; |
|
|
|
} LineInfo; |
|
|
|
|
|
|
|
/* Find '\n' and create line data. */ |
|
|
|
static int get_line_infos(const char *buf, kt">unsigned char buf_len, LineInfo **line_infos, kt">unsigned int *num_lines) |
|
|
|
static int get_line_infos(const char *buf, n">size_t buf_len, LineInfo **line_infos, n">size_t *num_lines) |
|
|
|
{ |
|
|
|
kt">unsigned char i = 0; |
|
|
|
kt">unsigned char end_idx = buf_len; |
|
|
|
kt">unsigned char prev_pos = 0; |
|
|
|
kt">unsigned char line_no = 0; |
|
|
|
kt">unsigned char last_line_ending = 0; |
|
|
|
n">size_t i = 0; |
|
|
|
n">size_t end_idx = buf_len; |
|
|
|
n">size_t prev_pos = 0; |
|
|
|
n">size_t line_no = 0; |
|
|
|
n">size_t last_line_ending = 0; |
|
|
|
|
|
|
|
/* Count # of lines. */ |
|
|
|
for (i = 0; i < end_idx; i++) { |
|
|
@ -844,19 +844,19 @@ static int get_line_infos(const char *buf, unsigned char buf_len, LineInfo **lin |
|
|
|
} |
|
|
|
|
|
|
|
static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out, |
|
|
|
kt">unsigned int *num_materials_out, |
|
|
|
n">size_t *num_materials_out, |
|
|
|
const char *mtl_filename, const char *obj_filename, file_reader_callback file_reader, |
|
|
|
hash_table_t* material_table) { |
|
|
|
tinyobj_material_t material; |
|
|
|
kt">unsigned int num_materials = 0; |
|
|
|
n">size_t num_materials = 0; |
|
|
|
tinyobj_material_t *materials = NULL; |
|
|
|
int has_previous_material = 0; |
|
|
|
const char *line_end = NULL; |
|
|
|
kt">unsigned int num_lines = 0; |
|
|
|
n">size_t num_lines = 0; |
|
|
|
LineInfo *line_infos = NULL; |
|
|
|
kt">unsigned char i = 0; |
|
|
|
n">size_t i = 0; |
|
|
|
char *buf = NULL; |
|
|
|
kt">unsigned char len = 0; |
|
|
|
n">size_t len = 0; |
|
|
|
|
|
|
|
if (materials_out == NULL) { |
|
|
|
return TINYOBJ_ERROR_INVALID_PARAMETER; |
|
|
@ -882,7 +882,7 @@ static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out, |
|
|
|
|
|
|
|
for (i = 0; i < num_lines; i++) { |
|
|
|
const char *p = &buf[line_infos[i].pos]; |
|
|
|
kt">unsigned char p_len = line_infos[i].len; |
|
|
|
n">size_t p_len = line_infos[i].len; |
|
|
|
|
|
|
|
char linebuf[4096]; |
|
|
|
const char *token; |
|
|
@ -924,7 +924,7 @@ static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out, |
|
|
|
#else |
|
|
|
sscanf(token, "%s", namebuf); |
|
|
|
#endif |
|
|
|
material.name = my_strdup(namebuf, (kt">unsigned int) (line_end - token)); |
|
|
|
material.name = my_strdup(namebuf, (n">size_t) (line_end - token)); |
|
|
|
|
|
|
|
/* Add material to material table */ |
|
|
|
if (material_table) |
|
|
@ -1025,56 +1025,56 @@ static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out, |
|
|
|
/* ambient texture */ |
|
|
|
if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) { |
|
|
|
token += 7; |
|
|
|
material.ambient_texname = my_strdup(token, (kt">unsigned int) (line_end - token)); |
|
|
|
material.ambient_texname = my_strdup(token, (n">size_t) (line_end - token)); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
/* diffuse texture */ |
|
|
|
if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) { |
|
|
|
token += 7; |
|
|
|
material.diffuse_texname = my_strdup(token, (kt">unsigned int) (line_end - token)); |
|
|
|
material.diffuse_texname = my_strdup(token, (n">size_t) (line_end - token)); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
/* specular texture */ |
|
|
|
if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) { |
|
|
|
token += 7; |
|
|
|
material.specular_texname = my_strdup(token, (kt">unsigned int) (line_end - token)); |
|
|
|
material.specular_texname = my_strdup(token, (n">size_t) (line_end - token)); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
/* specular highlight texture */ |
|
|
|
if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) { |
|
|
|
token += 7; |
|
|
|
material.specular_highlight_texname = my_strdup(token, (kt">unsigned int) (line_end - token)); |
|
|
|
material.specular_highlight_texname = my_strdup(token, (n">size_t) (line_end - token)); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
/* bump texture */ |
|
|
|
if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) { |
|
|
|
token += 9; |
|
|
|
material.bump_texname = my_strdup(token, (kt">unsigned int) (line_end - token)); |
|
|
|
material.bump_texname = my_strdup(token, (n">size_t) (line_end - token)); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
/* alpha texture */ |
|
|
|
if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) { |
|
|
|
token += 6; |
|
|
|
material.alpha_texname = my_strdup(token, (kt">unsigned int) (line_end - token)); |
|
|
|
material.alpha_texname = my_strdup(token, (n">size_t) (line_end - token)); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
/* bump texture */ |
|
|
|
if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) { |
|
|
|
token += 5; |
|
|
|
material.bump_texname = my_strdup(token, (kt">unsigned int) (line_end - token)); |
|
|
|
material.bump_texname = my_strdup(token, (n">size_t) (line_end - token)); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
/* displacement texture */ |
|
|
|
if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) { |
|
|
|
token += 5; |
|
|
|
material.displacement_texname = my_strdup(token, (kt">unsigned int) (line_end - token)); |
|
|
|
material.displacement_texname = my_strdup(token, (n">size_t) (line_end - token)); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
@ -1094,7 +1094,7 @@ static int tinyobj_parse_and_index_mtl_file(tinyobj_material_t **materials_out, |
|
|
|
} |
|
|
|
|
|
|
|
int tinyobj_parse_mtl_file(tinyobj_material_t **materials_out, |
|
|
|
kt">unsigned int *num_materials_out, |
|
|
|
n">size_t *num_materials_out, |
|
|
|
const char *mtl_filename, const char *obj_filename, file_reader_callback file_reader) { |
|
|
|
return tinyobj_parse_and_index_mtl_file(materials_out, num_materials_out, mtl_filename, obj_filename, file_reader, NULL); |
|
|
|
} |
|
|
@ -1120,10 +1120,10 @@ typedef struct { |
|
|
|
|
|
|
|
/* @todo { Use dynamic array } */ |
|
|
|
tinyobj_vertex_index_t f[TINYOBJ_MAX_FACES_PER_F_LINE]; |
|
|
|
kt">unsigned int num_f; |
|
|
|
n">size_t num_f; |
|
|
|
|
|
|
|
int f_num_verts[TINYOBJ_MAX_FACES_PER_F_LINE]; |
|
|
|
kt">unsigned int num_f_num_verts; |
|
|
|
n">size_t num_f_num_verts; |
|
|
|
|
|
|
|
const char *group_name; |
|
|
|
unsigned int group_name_len; |
|
|
@ -1143,7 +1143,7 @@ typedef struct { |
|
|
|
CommandType type; |
|
|
|
} Command; |
|
|
|
|
|
|
|
static int parseLine(Command *command, const char *p, kt">unsigned int p_len, |
|
|
|
static int parseLine(Command *command, const char *p, n">size_t p_len, |
|
|
|
int triangulate) { |
|
|
|
char linebuf[4096]; |
|
|
|
const char *token; |
|
|
@ -1205,7 +1205,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len, |
|
|
|
|
|
|
|
/* face */ |
|
|
|
if (token[0] == 'f' && IS_SPACE((token[1]))) { |
|
|
|
kt">unsigned int num_f = 0; |
|
|
|
n">size_t num_f = 0; |
|
|
|
|
|
|
|
tinyobj_vertex_index_t f[TINYOBJ_MAX_FACES_PER_F_LINE]; |
|
|
|
token += 2; |
|
|
@ -1222,8 +1222,8 @@ static int parseLine(Command *command, const char *p, unsigned int p_len, |
|
|
|
command->type = COMMAND_F; |
|
|
|
|
|
|
|
if (triangulate) { |
|
|
|
kt">unsigned int k; |
|
|
|
kt">unsigned int n = 0; |
|
|
|
n">size_t k; |
|
|
|
n">size_t n = 0; |
|
|
|
|
|
|
|
tinyobj_vertex_index_t i0 = f[0]; |
|
|
|
tinyobj_vertex_index_t i1; |
|
|
@ -1245,7 +1245,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len, |
|
|
|
command->num_f_num_verts = n; |
|
|
|
|
|
|
|
} else { |
|
|
|
kt">unsigned int k = 0; |
|
|
|
n">size_t k = 0; |
|
|
|
assert(num_f < TINYOBJ_MAX_FACES_PER_F_LINE); |
|
|
|
for (k = 0; k < num_f; k++) { |
|
|
|
command->f[k] = f[k]; |
|
|
@ -1266,7 +1266,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len, |
|
|
|
skip_space(&token); |
|
|
|
command->material_name = p + (token - linebuf); |
|
|
|
command->material_name_len = (unsigned int)length_until_newline( |
|
|
|
token, (p_len - (kt">unsigned int)(token - linebuf)) + 1); |
|
|
|
token, (p_len - (n">size_t)(token - linebuf)) + 1); |
|
|
|
command->type = COMMAND_USEMTL; |
|
|
|
|
|
|
|
return 1; |
|
|
@ -1280,7 +1280,8 @@ static int parseLine(Command *command, const char *p, unsigned int p_len, |
|
|
|
skip_space(&token); |
|
|
|
command->mtllib_name = p + (token - linebuf); |
|
|
|
command->mtllib_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_MTLLIB; |
|
|
|
|
|
|
|
return 1; |
|
|
@ -1293,7 +1294,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len, |
|
|
|
|
|
|
|
command->group_name = p + (token - linebuf); |
|
|
|
command->group_name_len = (unsigned int)length_until_newline( |
|
|
|
token, p_len - (kt">unsigned int)(token - linebuf)) + |
|
|
|
token, p_len - (n">size_t)(token - linebuf)) + |
|
|
|
1; |
|
|
|
command->type = COMMAND_G; |
|
|
|
|
|
|
@ -1307,7 +1308,7 @@ static int parseLine(Command *command, const char *p, unsigned int p_len, |
|
|
|
|
|
|
|
command->object_name = p + (token - linebuf); |
|
|
|
command->object_name_len = (unsigned int)length_until_newline( |
|
|
|
token, p_len - (kt">unsigned int)(token - linebuf)) + |
|
|
|
token, p_len - (n">size_t)(token - linebuf)) + |
|
|
|
1; |
|
|
|
command->type = COMMAND_O; |
|
|
|
|
|
|
@ -1348,28 +1349,28 @@ static char *get_dirname(char *path) |
|
|
|
|
|
|
|
|
|
|
|
int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes, |
|
|
|
kt">unsigned int *num_shapes, tinyobj_material_t **materials_out, |
|
|
|
kt">unsigned int *num_materials_out, const char *obj_filename, file_reader_callback file_reader, |
|
|
|
n">size_t *num_shapes, tinyobj_material_t **materials_out, |
|
|
|
n">size_t *num_materials_out, const char *obj_filename, file_reader_callback file_reader, |
|
|
|
unsigned int flags) { |
|
|
|
LineInfo *line_infos = NULL; |
|
|
|
Command *commands = NULL; |
|
|
|
kt">unsigned int num_lines = 0; |
|
|
|
n">size_t num_lines = 0; |
|
|
|
|
|
|
|
kt">unsigned int num_v = 0; |
|
|
|
kt">unsigned int num_vn = 0; |
|
|
|
kt">unsigned int num_vt = 0; |
|
|
|
kt">unsigned int num_f = 0; |
|
|
|
kt">unsigned int num_faces = 0; |
|
|
|
n">size_t num_v = 0; |
|
|
|
n">size_t num_vn = 0; |
|
|
|
n">size_t num_vt = 0; |
|
|
|
n">size_t num_f = 0; |
|
|
|
n">size_t num_faces = 0; |
|
|
|
|
|
|
|
int mtllib_line_index = -1; |
|
|
|
|
|
|
|
tinyobj_material_t *materials = NULL; |
|
|
|
kt">unsigned int num_materials = 0; |
|
|
|
n">size_t num_materials = 0; |
|
|
|
|
|
|
|
hash_table_t material_table; |
|
|
|
|
|
|
|
char *buf = NULL; |
|
|
|
kt">unsigned char len = 0; |
|
|
|
n">size_t len = 0; |
|
|
|
file_reader(obj_filename, /* is_mtl */0, obj_filename, &buf, &len); |
|
|
|
|
|
|
|
if (len < 1) return TINYOBJ_ERROR_INVALID_PARAMETER; |
|
|
@ -1393,7 +1394,7 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes, |
|
|
|
|
|
|
|
/* 2. parse each line */ |
|
|
|
{ |
|
|
|
kt">unsigned char i = 0; |
|
|
|
n">size_t i = 0; |
|
|
|
for (i = 0; i < num_lines; i++) { |
|
|
|
int ret = parseLine(&commands[i], &buf[line_infos[i].pos], |
|
|
|
line_infos[i].len, flags & TINYOBJ_FLAG_TRIANGULATE); |
|
|
@ -1462,13 +1463,13 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes, |
|
|
|
/* Construct attributes */ |
|
|
|
|
|
|
|
{ |
|
|
|
kt">unsigned char v_count = 0; |
|
|
|
kt">unsigned char n_count = 0; |
|
|
|
kt">unsigned char t_count = 0; |
|
|
|
kt">unsigned char f_count = 0; |
|
|
|
kt">unsigned char face_count = 0; |
|
|
|
n">size_t v_count = 0; |
|
|
|
n">size_t n_count = 0; |
|
|
|
n">size_t t_count = 0; |
|
|
|
n">size_t f_count = 0; |
|
|
|
n">size_t face_count = 0; |
|
|
|
int material_id = -1; /* -1 = default unknown material. */ |
|
|
|
kt">unsigned char i = 0; |
|
|
|
n">size_t i = 0; |
|
|
|
|
|
|
|
attrib->vertices = (float *)TINYOBJ_MALLOC(sizeof(float) * num_v * 3); |
|
|
|
attrib->num_vertices = (unsigned int)num_v; |
|
|
@ -1531,7 +1532,7 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes, |
|
|
|
attrib->texcoords[2 * t_count + 1] = commands[i].ty; |
|
|
|
t_count++; |
|
|
|
} else if (commands[i].type == COMMAND_F) { |
|
|
|
kt">unsigned char k = 0; |
|
|
|
n">size_t k = 0; |
|
|
|
for (k = 0; k < commands[i].num_f; k++) { |
|
|
|
tinyobj_vertex_index_t vi = commands[i].f[k]; |
|
|
|
int v_idx = fixIndex(vi.v_idx, v_count); |
|
|
@ -1556,9 +1557,9 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes, |
|
|
|
/* 5. Construct shape information. */ |
|
|
|
{ |
|
|
|
unsigned int face_count = 0; |
|
|
|
kt">unsigned char i = 0; |
|
|
|
kt">unsigned char n = 0; |
|
|
|
kt">unsigned char shape_idx = 0; |
|
|
|
n">size_t i = 0; |
|
|
|
n">size_t n = 0; |
|
|
|
n">size_t shape_idx = 0; |
|
|
|
|
|
|
|
const char *shape_name = NULL; |
|
|
|
unsigned int shape_name_len = 0; |
|
|
@ -1630,7 +1631,7 @@ int tinyobj_parse_obj(tinyobj_attrib_t *attrib, tinyobj_shape_t **shapes, |
|
|
|
} |
|
|
|
|
|
|
|
if ((face_count - prev_face_offset) > 0) { |
|
|
|
kt">unsigned char length = face_count - prev_shape_face_offset; |
|
|
|
n">size_t length = face_count - prev_shape_face_offset; |
|
|
|
if (length > 0) { |
|
|
|
(*shapes)[shape_idx].name = |
|
|
|
my_strndup(prev_shape_name, prev_shape_name_len); |
|
|
@ -1681,8 +1682,8 @@ void tinyobj_attrib_free(tinyobj_attrib_t *attrib) { |
|
|
|
if (attrib->material_ids) TINYOBJ_FREE(attrib->material_ids); |
|
|
|
} |
|
|
|
|
|
|
|
void tinyobj_shapes_free(tinyobj_shape_t *shapes, kt">unsigned char num_shapes) { |
|
|
|
kt">unsigned char i; |
|
|
|
void tinyobj_shapes_free(tinyobj_shape_t *shapes, n">size_t num_shapes) { |
|
|
|
n">size_t i; |
|
|
|
if (shapes == NULL) return; |
|
|
|
|
|
|
|
for (i = 0; i < num_shapes; i++) { |
|
|
@ -1693,8 +1694,8 @@ void tinyobj_shapes_free(tinyobj_shape_t *shapes, unsigned char num_shapes) { |
|
|
|
} |
|
|
|
|
|
|
|
void tinyobj_materials_free(tinyobj_material_t *materials, |
|
|
|
kt">unsigned char num_materials) { |
|
|
|
kt">unsigned char i; |
|
|
|
n">size_t num_materials) { |
|
|
|
n">size_t i; |
|
|
|
if (materials == NULL) return; |
|
|
|
|
|
|
|
for (i = 0; i < num_materials; i++) { |
|
|
|