triangulate
This commit is contained in:
parent
be37bd8c3e
commit
7cc0265f3f
3
Makefile
3
Makefile
|
@ -6,7 +6,7 @@
|
||||||
# By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ #
|
# By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2020/12/17 19:47:03 by gbrochar #+# #+# #
|
# Created: 2020/12/17 19:47:03 by gbrochar #+# #+# #
|
||||||
# Updated: 2020/12/23 17:05:36 by gbrochar ### ########.fr #
|
# Updated: 2020/12/23 17:36:57 by gbrochar ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ NAME = scop
|
||||||
SRC_FILE = main.c \
|
SRC_FILE = main.c \
|
||||||
parse_file.c \
|
parse_file.c \
|
||||||
parse_line.c \
|
parse_line.c \
|
||||||
|
parse_token.c \
|
||||||
env.c \
|
env.c \
|
||||||
mat4_transpose.c \
|
mat4_transpose.c \
|
||||||
mat4_multiply.c \
|
mat4_multiply.c \
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
|
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
|
||||||
/* Updated: 2020/12/23 16:23:16 by gbrochar ### ########.fr */
|
/* Updated: 2020/12/23 17:57:54 by gbrochar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -96,6 +96,13 @@ int parse_file(t_env *e);
|
||||||
|
|
||||||
int parse_line(t_env *e, char *line);
|
int parse_line(t_env *e, char *line);
|
||||||
t_gl_buf_type parse_gl_buf_type(char *token);
|
t_gl_buf_type parse_gl_buf_type(char *token);
|
||||||
|
void parse_triangulate(
|
||||||
|
t_buf_i *indices, int token_count);
|
||||||
|
void parse_append_data_tmp(
|
||||||
|
t_buf_i *indices, int vertex_count, int *data_tmp);
|
||||||
|
|
||||||
|
int parse_token(
|
||||||
|
t_env *e, char *token, t_gl_buf_type gl_buf_type);
|
||||||
int parse_append_data_d(t_buf_d *buffer, char *token);
|
int parse_append_data_d(t_buf_d *buffer, char *token);
|
||||||
int parse_append_data_i(t_buf_i *buffer, char *token);
|
int parse_append_data_i(t_buf_i *buffer, char *token);
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2020/12/22 12:02:14 by gbrochar #+# #+# */
|
/* Created: 2020/12/22 12:02:14 by gbrochar #+# #+# */
|
||||||
/* Updated: 2020/12/22 20:48:00 by gbrochar ### ########.fr */
|
/* Updated: 2020/12/23 17:57:34 by gbrochar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
@ -6,38 +6,12 @@
|
||||||
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2020/12/22 18:41:28 by gbrochar #+# #+# */
|
/* Created: 2020/12/22 18:41:28 by gbrochar #+# #+# */
|
||||||
/* Updated: 2020/12/22 20:26:47 by gbrochar ### ########.fr */
|
/* Updated: 2020/12/23 17:57:40 by gbrochar ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "scop.h"
|
#include "scop.h"
|
||||||
|
|
||||||
int parse_append_data_d(t_buf_d *buffer, char *token)
|
|
||||||
{
|
|
||||||
if (buffer->ptr == buffer->len)
|
|
||||||
{
|
|
||||||
buffer->data = (double *)realloc(
|
|
||||||
buffer->data, sizeof(double) * (buffer->len + BUFFER_SIZE));
|
|
||||||
buffer->len += BUFFER_SIZE;
|
|
||||||
}
|
|
||||||
buffer->data[buffer->ptr] = atof(token);
|
|
||||||
buffer->ptr++;
|
|
||||||
return (SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
int parse_append_data_i(t_buf_i *buffer, char *token)
|
|
||||||
{
|
|
||||||
if (buffer->ptr == buffer->len)
|
|
||||||
{
|
|
||||||
buffer->data = (int *)realloc(
|
|
||||||
buffer->data, sizeof(int) * (buffer->len + BUFFER_SIZE));
|
|
||||||
buffer->len += BUFFER_SIZE;
|
|
||||||
}
|
|
||||||
buffer->data[buffer->ptr] = atoi(token);
|
|
||||||
buffer->ptr++;
|
|
||||||
return (SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_gl_buf_type parse_gl_buf_type(char *token)
|
t_gl_buf_type parse_gl_buf_type(char *token)
|
||||||
{
|
{
|
||||||
if (strcmp(token, "v") == SUCCESS)
|
if (strcmp(token, "v") == SUCCESS)
|
||||||
|
@ -51,27 +25,43 @@ t_gl_buf_type parse_gl_buf_type(char *token)
|
||||||
return (OTHER);
|
return (OTHER);
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_token(
|
void parse_append_data_tmp(
|
||||||
t_env *e,
|
t_buf_i *indices, int vertex_count, int *data_tmp)
|
||||||
char *token,
|
|
||||||
t_gl_buf_type gl_buf_type)
|
|
||||||
{
|
{
|
||||||
int ret;
|
int i;
|
||||||
|
|
||||||
if (gl_buf_type == OTHER)
|
i = 0;
|
||||||
ret = BREAK;
|
while (i < 3 * (vertex_count - 2))
|
||||||
if (gl_buf_type == VERTEX)
|
|
||||||
ret = parse_append_data_d(&(e->object.vertices), token);
|
|
||||||
if (gl_buf_type == UV)
|
|
||||||
{
|
{
|
||||||
printf("uv");
|
if (indices->ptr == indices->len)
|
||||||
ret = parse_append_data_d(&(e->object.uvs), token);
|
{
|
||||||
|
indices->data = (int *)realloc(
|
||||||
|
indices->data, sizeof(int) * (indices->len + BUFFER_SIZE));
|
||||||
|
indices->len += BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
indices->data[indices->ptr++] = data_tmp[i++];
|
||||||
}
|
}
|
||||||
if (gl_buf_type == NORMAL)
|
}
|
||||||
ret = parse_append_data_d(&(e->object.normals), token);
|
|
||||||
if (gl_buf_type == INDEX)
|
void parse_triangulate(t_buf_i *indices, int vertex_count)
|
||||||
ret = parse_append_data_i(&(e->object.indices), token);
|
{
|
||||||
return (ret);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_line(t_env *e, char *line)
|
int parse_line(t_env *e, char *line)
|
||||||
|
@ -83,7 +73,7 @@ int parse_line(t_env *e, char *line)
|
||||||
|
|
||||||
token_count = 0;
|
token_count = 0;
|
||||||
ret = SUCCESS;
|
ret = SUCCESS;
|
||||||
while ((token = strtok_r(line, " ", &line)))
|
while ((token = strtok_r(line, " \t", &line)))
|
||||||
{
|
{
|
||||||
if (token_count == 0)
|
if (token_count == 0)
|
||||||
gl_buf_type = parse_gl_buf_type(token);
|
gl_buf_type = parse_gl_buf_type(token);
|
||||||
|
@ -93,5 +83,7 @@ int parse_line(t_env *e, char *line)
|
||||||
return (FAILURE);
|
return (FAILURE);
|
||||||
token_count++;
|
token_count++;
|
||||||
}
|
}
|
||||||
|
if (gl_buf_type == INDEX && token_count > 4)
|
||||||
|
parse_triangulate(&(e->object.indices), token_count - 1);
|
||||||
return (SUCCESS);
|
return (SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parse_token.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2020/12/23 17:33:33 by gbrochar #+# #+# */
|
||||||
|
/* Updated: 2020/12/23 17:38:10 by gbrochar ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "scop.h"
|
||||||
|
|
||||||
|
int parse_append_data_d(t_buf_d *buffer, char *token)
|
||||||
|
{
|
||||||
|
if (buffer->ptr == buffer->len)
|
||||||
|
{
|
||||||
|
buffer->data = (double *)realloc(
|
||||||
|
buffer->data, sizeof(double) * (buffer->len + BUFFER_SIZE));
|
||||||
|
buffer->len += BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
buffer->data[buffer->ptr] = atof(token);
|
||||||
|
buffer->ptr++;
|
||||||
|
return (SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_append_data_i(t_buf_i *buffer, char *token)
|
||||||
|
{
|
||||||
|
if (buffer->ptr == buffer->len)
|
||||||
|
{
|
||||||
|
buffer->data = (int *)realloc(
|
||||||
|
buffer->data, sizeof(int) * (buffer->len + BUFFER_SIZE));
|
||||||
|
buffer->len += BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
buffer->data[buffer->ptr] = atoi(token);
|
||||||
|
buffer->ptr++;
|
||||||
|
return (SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_token(
|
||||||
|
t_env *e,
|
||||||
|
char *token,
|
||||||
|
t_gl_buf_type gl_buf_type)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (gl_buf_type == OTHER)
|
||||||
|
ret = BREAK;
|
||||||
|
if (gl_buf_type == VERTEX)
|
||||||
|
ret = parse_append_data_d(&(e->object.vertices), token);
|
||||||
|
if (gl_buf_type == UV)
|
||||||
|
ret = parse_append_data_d(&(e->object.uvs), token);
|
||||||
|
if (gl_buf_type == NORMAL)
|
||||||
|
ret = parse_append_data_d(&(e->object.normals), token);
|
||||||
|
if (gl_buf_type == INDEX)
|
||||||
|
ret = parse_append_data_i(&(e->object.indices), token);
|
||||||
|
return (ret);
|
||||||
|
}
|
Loading…
Reference in New Issue