scop/src/parse_line.c

99 lines
2.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/22 18:41:28 by gbrochar #+# #+# */
/* Updated: 2020/12/23 21:15:54 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "scop.h"
t_gl_buf_type parse_gl_buf_type(char *token)
{
if (strcmp(token, "v") == SUCCESS)
return (VERTEX);
if (strcmp(token, "vt") == SUCCESS)
return (UV);
if (strcmp(token, "vn") == SUCCESS)
return (NORMAL);
if (strcmp(token, "f") == SUCCESS)
return (INDEX);
return (OTHER);
}
void parse_append_data_tmp(
t_buf_ui *indices, int vertex_count, unsigned int *data_tmp)
{
int i;
i = 0;
while (i < 3 * (vertex_count - 2))
{
if (indices->ptr == indices->len)
{
indices->data = (unsigned int *)realloc(
indices->data, sizeof(unsigned int) * (indices->len + BUFFER_SIZE));
indices->len += BUFFER_SIZE;
}
indices->data[indices->ptr++] = data_tmp[i++];
}
}
void parse_triangulate(t_buf_ui *indices, int vertex_count)
{
unsigned int *data_tmp;
int i;
i = 0;
data_tmp = (unsigned int *)malloc(
sizeof(unsigned int) * 3 * (vertex_count - 2));
indices->ptr -= vertex_count;
while (i < 3 * (vertex_count - 2))
{
if (i % 3 == 0)
data_tmp[i] = indices->data[indices->ptr];
else if (i % 3 == 1)
data_tmp[i] = indices->data[indices->ptr + (i - 1) / 3 + 1];
else if (i % 3 == 2)
data_tmp[i] = indices->data[indices->ptr + (i - 2) / 3 + 2];
i++;
}
parse_append_data_tmp(indices, vertex_count, data_tmp);
free(data_tmp);
}
void parse_triangulate_dispatcher(t_env *e, int vertex_count) {
parse_triangulate(&(e->object.vertices_indices), vertex_count);
parse_triangulate(&(e->object.uvs_indices), vertex_count);
parse_triangulate(&(e->object.normals_indices), vertex_count);
}
int parse_line(t_env *e, char *line)
{
char *token;
t_gl_buf_type gl_buf_type;
int token_count;
int ret;
token_count = 0;
ret = SUCCESS;
while ((token = strtok_r(line, " \t", &line)))
{
if (token_count == 0)
gl_buf_type = parse_gl_buf_type(token);
else
ret = parse_token(e, token, gl_buf_type);
if (ret == FAILURE)
return (FAILURE);
token_count++;
}
if (gl_buf_type == INDEX && token_count > 4)
parse_triangulate_dispatcher(e, token_count - 1);
return (SUCCESS);
}