static int parse_polygon_components( const char **tokens, t_polygon *new_polygon) { int i; int nt; char **ctokens; i = 0; if (!tokens_are_enough(tokens, 3)) parser_die("A face must declare at least three vertices."); while (tokens[i]) { nt = 0; if (strstr(tokens[i], "//")) nt = 1; ctokens = ft_split(tokens[i], "/\n"); if (!add_vertex((const char **)ctokens, nt, new_polygon->vertices)) { ft_free_tab(ctokens); return (0); } ++i; ft_free_tab(ctokens); } return (1); }
static void fill_vertex_normal(const char **tokens, t_vertex *vertx, int nt) { int i; t_lst *normals; t_vec3 *normal; normals = g_current_data->normals; i = token_to_int(tokens, 2); if (nt) i = token_to_int(tokens, 1); normal = lst_data_at(normals, i); if (normal) { vertx->normal.x = normal->x; vertx->normal.y = normal->y; vertx->normal.z = normal->z; } else if (i == DEFAULT_CODE || (i >= 0 && lst_get_size(normals) > (size_t)i)) { vertx->normal.x = FLT_MAX; vertx->normal.y = FLT_MAX; vertx->normal.z = FLT_MAX; } else parser_die("Invalid index for normal."); }
int parse_normal(const char **tokens) { t_vec3 *new_normal; if (!(new_normal = malloc(sizeof(t_vec3)))) return (0); else if (!parse_vec3(tokens, new_normal)) { free(new_normal); parser_die("A normal needs three arguments."); } lst_push_back(g_current_data->normals, new_normal); return (1); }
int parse_color(const char **tokens) { t_vec2 *new_color; new_color = NULL; if (!tokens_are_enough(tokens, 2)) parser_die("A texture needs two arguments."); else if (!(new_color = malloc(sizeof(t_vec2)))) return (0); new_color->x = atof(tokens[0]); new_color->y = atof(tokens[1]); lst_push_back(g_current_data->uvs, new_color); return (1); }
int add_vertex(const char **tokens, int no_texture, t_lst *vertices) { t_vertex *new_vertex; new_vertex = NULL; if (!tokens[0]) parser_die("A face component can't be empty."); else if (!(new_vertex = malloc(sizeof(t_vertex)))) return (0); fill_vertex_position(tokens, new_vertex); fill_vertex_color(tokens, new_vertex, no_texture); fill_vertex_normal(tokens, new_vertex, no_texture); lst_push_back(vertices, new_vertex); return (1); }
int token_to_int(const char **tokens, int index) { int j; int ret; j = 0; while (j <= index) { if (!tokens[j++]) return (DEFAULT_CODE); } ret = atoi(tokens[index]) - 1; if (ret < 0) parser_die("A face index can't be negative."); return (ret); }
int parse_line(char *line) { char **tokens; int (*parse_function)(const char **); if (!(tokens = ft_split(line, " \t\n"))) return (0); parse_function = get_parse_func(tokens[0]); if (!parse_function) { puts(tokens[0]); ft_free_tab(tokens); parser_die("Unknown data type."); } (*parse_function)((const char **)(tokens + 1)); ft_free_tab(tokens); return (1); }
static void fill_vertex_color(const char **tokens, t_vertex *vertex, int nt) { int i; t_lst *colors; t_vec2 *color; colors = g_current_data->uvs; i = DEFAULT_CODE; if (!nt) i = token_to_int(tokens, 1); color = lst_data_at(colors, i); if (color) { vertex->uv.x = color->x; vertex->uv.y = color->y; } else if (i == DEFAULT_CODE || (i >= 0 && lst_get_size(colors) > (size_t)i)) { vertex->uv.x = FLT_MAX; vertex->uv.y = FLT_MAX; } else parser_die("Invalid index for color."); }
static void fill_vertex_position(const char **tokens, t_vertex *vertex) { int index; t_lst *positions; t_vec3 *pos; index = token_to_int(tokens, 0); positions = g_current_data->positions; pos = lst_data_at(positions, index); if (pos) { vertex->position.x = pos->x; vertex->position.y = pos->y; vertex->position.z = pos->z; } else if (index >= 0 && lst_get_size(positions) > (size_t)index) { vertex->position.x = FLT_MAX; vertex->position.y = FLT_MAX; vertex->position.z = FLT_MAX; } else parser_die("Invalid index for a position."); }