scop/src/parse_line.c

99 lines
2.9 KiB
C
Raw Normal View History

2020-12-22 18:15:25 +00:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 */
2020-12-22 18:15:25 +00:00
/* */
/* ************************************************************************** */
#include "scop.h"
t_gl_buf_type parse_gl_buf_type(char *token)
{
if (strcmp(token, "v") == SUCCESS)
2020-12-22 18:15:25 +00:00
return (VERTEX);
if (strcmp(token, "vt") == SUCCESS)
2020-12-22 18:15:25 +00:00
return (UV);
if (strcmp(token, "vn") == SUCCESS)
2020-12-22 18:15:25 +00:00
return (NORMAL);
if (strcmp(token, "f") == SUCCESS)
2020-12-22 18:15:25 +00:00
return (INDEX);
return (OTHER);
}
2020-12-23 16:58:47 +00:00
void parse_append_data_tmp(
t_buf_ui *indices, int vertex_count, unsigned int *data_tmp)
2020-12-22 18:15:25 +00:00
{
2020-12-23 16:58:47 +00:00
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));
2020-12-23 16:58:47 +00:00
indices->len += BUFFER_SIZE;
}
indices->data[indices->ptr++] = data_tmp[i++];
}
}
void parse_triangulate(t_buf_ui *indices, int vertex_count)
2020-12-23 16:58:47 +00:00
{
unsigned int *data_tmp;
2020-12-23 16:58:47 +00:00
int i;
2020-12-22 18:15:25 +00:00
2020-12-23 16:58:47 +00:00
i = 0;
data_tmp = (unsigned int *)malloc(
sizeof(unsigned int) * 3 * (vertex_count - 2));
2020-12-23 16:58:47 +00:00
indices->ptr -= vertex_count;
while (i < 3 * (vertex_count - 2))
{
2020-12-23 16:58:47 +00:00
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++;
}
2020-12-23 16:58:47 +00:00
parse_append_data_tmp(indices, vertex_count, data_tmp);
free(data_tmp);
2020-12-22 18:15:25 +00:00
}
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);
}
2020-12-22 18:15:25 +00:00
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;
2020-12-23 16:58:47 +00:00
while ((token = strtok_r(line, " \t", &line)))
2020-12-22 18:15:25 +00:00
{
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++;
}
2020-12-23 16:58:47 +00:00
if (gl_buf_type == INDEX && token_count > 4)
parse_triangulate_dispatcher(e, token_count - 1);
2020-12-22 18:15:25 +00:00
return (SUCCESS);
}