aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--inc/scop.h33
-rw-r--r--src/error.c42
-rw-r--r--src/gl.c27
-rw-r--r--src/glfw.c30
-rw-r--r--src/main.c40
-rw-r--r--src/parse.c18
7 files changed, 166 insertions, 30 deletions
diff --git a/Makefile b/Makefile
index d8d99a3..78b3453 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@
# By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/05/09 10:24:52 by charles #+# #+# #
-# Updated: 2020/05/10 21:06:25 by charles ### ########.fr #
+# Updated: 2020/05/11 01:55:58 by charles ### ########.fr #
# #
# **************************************************************************** #
@@ -22,7 +22,7 @@ OBJ_DIR = obj
CC = gcc
CCFLAGS = -I$(LIBFT_DIR)/include -I$(INC_DIR) \
- -Wall -Wextra -Werror
+ -Wall -Wextra #-Werror
LDFLAGS = -L$(LIBFT_DIR) -lft
ifeq ($(shell uname),Linux)
@@ -48,7 +48,7 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC)
cleanloc:
$(RM) $(OBJ)
-fcleanloc:
+fcleanloc: cleanloc
$(RM) $(NAME)
reloc: fcleanloc all
diff --git a/inc/scop.h b/inc/scop.h
index d6cf415..3d5c966 100644
--- a/inc/scop.h
+++ b/inc/scop.h
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/09 10:41:44 by charles #+# #+# */
-/* Updated: 2020/05/10 21:18:15 by charles ### ########.fr */
+/* Updated: 2020/05/11 02:10:09 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,6 +15,8 @@
# include <unistd.h>
# include <fcntl.h>
+# include <stdbool.h>
+# include <stdlib.h>
# include <GL/glew.h>
# include <GLFW/glfw3.h>
# include "libft.h"
@@ -28,10 +30,39 @@ typedef struct
size_t indices_len;
} t_object;
+typedef struct
+{
+ unsigned int vertex_buf;
+ unsigned int index_buf;
+} t_gl_state;
+
/*
** parse.c
*/
int parse(char *filepath, t_object *object);
+/*
+** gl.c
+*/
+
+int gl_state_init(t_gl_state *state, t_object *object);
+
+/*
+** error.c
+*/
+
+# define GL_CALL(x) error_clear(); \
+ x; \
+ error_check(#x, __FILE__, __LINE__)
+
+void error_clear(void);
+void error_check(char *code, char *filename, int line_num);
+
+/*
+** glfw.c
+*/
+
+GLFWwindow *glfw_init(int width, int height);
+
#endif
diff --git a/src/error.c b/src/error.c
new file mode 100644
index 0000000..17e2611
--- /dev/null
+++ b/src/error.c
@@ -0,0 +1,42 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* error.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/05/11 01:43:45 by charles #+# #+# */
+/* Updated: 2020/05/11 01:55:03 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "scop.h"
+
+void error_clear(void)
+{
+ while (glGetError() != GL_NO_ERROR)
+ ;
+}
+
+void error_check(char *code, char *filename, int line_num)
+{
+ GLenum err;
+ bool occured;
+
+ occured = false;
+ while ((err = glGetError()) != GL_NO_ERROR)
+ {
+ ft_putstr("[ERROR opengl] (");
+ ft_putnbr(err);
+ ft_putstr(") ");
+ ft_putstr(code);
+ ft_putstr(" at ");
+ ft_putstr(filename);
+ ft_putchar(':');
+ ft_putnbr(line_num);
+ ft_putchar('\n');
+ occured = true;
+ }
+ if (occured)
+ exit(EXIT_FAILURE);
+}
diff --git a/src/gl.c b/src/gl.c
new file mode 100644
index 0000000..c28bf9e
--- /dev/null
+++ b/src/gl.c
@@ -0,0 +1,27 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* gl.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/05/11 01:31:10 by charles #+# #+# */
+/* Updated: 2020/05/11 02:17:16 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "scop.h"
+
+int gl_state_init(t_gl_state *state, t_object *object)
+{
+ GL_CALL(glGenBuffers(1, &state->vertex_buf));
+ GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, state->vertex_buf));
+ GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(float) * object->vertices_len,
+ object->vertices, GL_STATIC_DRAW));
+
+ GL_CALL(glGenBuffers(1, &state->index_buf));
+ GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->index_buf));
+ GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(float) * object->indices_len,
+ object->indices, GL_STATIC_DRAW));
+ return (0);
+}
diff --git a/src/glfw.c b/src/glfw.c
new file mode 100644
index 0000000..8afdfe4
--- /dev/null
+++ b/src/glfw.c
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* glfw.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/05/11 01:58:00 by charles #+# #+# */
+/* Updated: 2020/05/11 02:16:58 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "scop.h"
+
+GLFWwindow *glfw_init(int width, int height)
+{
+ GLFWwindow *window;
+
+ if (!glfwInit())
+ return (NULL);
+ window = glfwCreateWindow(width, height, "scop", NULL, NULL);
+ if (window == NULL)
+ {
+ glfwTerminate();
+ return (NULL);
+ }
+ glfwMakeContextCurrent(window);
+ glewInit();
+ return (window);
+}
diff --git a/src/main.c b/src/main.c
index 3e8ab44..4ac263a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/09 10:20:09 by charles #+# #+# */
-/* Updated: 2020/05/10 22:02:36 by charles ### ########.fr */
+/* Updated: 2020/05/11 02:14:14 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,8 +14,9 @@
int main(int argc, char **argv)
{
- /* GLFWwindow *window; */
+ GLFWwindow *window;
t_object object;
+ t_gl_state state;
if (argc != 2)
{
@@ -31,28 +32,23 @@ int main(int argc, char **argv)
return (1);
}
- for (size_t i = 0; i < object.indices_len; i++)
- printf("%u\n", object.indices[i]);
- for (size_t i = 0; i < object.vertices_len; i++)
- printf("%f\n", object.vertices[i]);
+ /* for (size_t i = 0; i < object.indices_len; i++) */
+ /* printf("%u\n", object.indices[i]); */
+ /* for (size_t i = 0; i < object.vertices_len; i++) */
+ /* printf("%f\n", object.vertices[i]); */
+ /* printf("%lu\n", object.indices_len); */
+ if ((window = glfw_init(400, 400)) == NULL
+ || gl_state_init(&state, &object) == -1)
+ return (1);
+ while (!glfwWindowShouldClose(window))
+ {
+ glClear(GL_COLOR_BUFFER_BIT);
+ glfwSwapBuffers(window);
+ glfwPollEvents();
+ }
+ glfwTerminate();
free(object.vertices);
free(object.indices);
- /* if (!glfwInit()) */
- /* return 1; */
- /* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); */
- /* if (window == NULL) */
- /* { */
- /* glfwTerminate(); */
- /* return 1; */
- /* } */
- /* glfwMakeContextCurrent(window); */
- /* while (!glfwWindowShouldClose(window)) */
- /* { */
- /* glClear(GL_COLOR_BUFFER_BIT); */
- /* glfwSwapBuffers(window); */
- /* glfwPollEvents(); */
- /* } */
- /* glfwTerminate(); */
return 0;
}
diff --git a/src/parse.c b/src/parse.c
index fa9f2b5..b83d9e8 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -6,27 +6,37 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/09 11:02:00 by charles #+# #+# */
-/* Updated: 2020/05/10 22:04:05 by charles ### ########.fr */
+/* Updated: 2020/05/11 01:29:13 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "scop.h"
-#define SCOP_VEC_DEFAULT_SIZE 32
+#define SCOP_VEC_DEFAULT_SIZE 64
static int st_parse_face(char **indexes_strs, t_ftvec *indices)
{
size_t i;
size_t len;
+ unsigned int first;
unsigned int tmp;
len = ft_split_len(indexes_strs);
- i = -1;
- while (++i < len)
+ first = ft_atoi(indexes_strs[0]);
+ /* if (ft_vecpush(indices, *(void**)&first) == NULL) */
+ /* return (-1); */
+ i = 1;
+ while (i < len - 1)
{
+ if (ft_vecpush(indices, *(void**)&first) == NULL)
+ return (-1);
tmp = ft_atoi(indexes_strs[i]);
if (ft_vecpush(indices, *(void**)&tmp) == NULL)
return (-1);
+ tmp = ft_atoi(indexes_strs[i + 1]);
+ if (ft_vecpush(indices, *(void**)&tmp) == NULL)
+ return (-1);
+ i++;
}
return (0);
}