91 lines
2.6 KiB
C
91 lines
2.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* parse_line.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2020/12/22 18:41:28 by gbrochar #+# #+# */
|
|
/* Updated: 2020/12/23 20:31:41 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_i *indices, int vertex_count, int *data_tmp)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (i < 3 * (vertex_count - 2))
|
|
{
|
|
if (indices->ptr == indices->len)
|
|
{
|
|
indices->data = (int *)realloc(
|
|
indices->data, sizeof(int) * (indices->len + BUFFER_SIZE));
|
|
indices->len += BUFFER_SIZE;
|
|
}
|
|
indices->data[indices->ptr++] = data_tmp[i++];
|
|
}
|
|
}
|
|
|
|
void parse_triangulate(t_buf_i *indices, int vertex_count)
|
|
{
|
|
int *data_tmp;
|
|
int i;
|
|
|
|
i = 0;
|
|
data_tmp = (int *)malloc(sizeof(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);
|
|
}
|
|
|
|
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(&(e->object.indices), token_count - 1);
|
|
return (SUCCESS);
|
|
}
|