From 7b214503608550dc2853b9e01526723f8c65baf3 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 12 May 2020 14:14:06 +0200 Subject: Added mat4 in libftm, can rotate, scale and translate vector --- src/gl.c | 23 ++++++++++++----- src/glfw.c | 11 ++++++-- src/helper.c | 21 +++++++++++++++ src/main.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++------------ src/parse.c | 13 +++++----- src/shader.c | 10 ++++---- src/texture.c | 44 ++++++++++++++++++++++++++++++++ 7 files changed, 169 insertions(+), 35 deletions(-) create mode 100644 src/helper.c create mode 100644 src/texture.c (limited to 'src') diff --git a/src/gl.c b/src/gl.c index 82e33a9..557cb63 100644 --- a/src/gl.c +++ b/src/gl.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 01:31:10 by charles #+# #+# */ -/* Updated: 2020/05/11 10:42:42 by charles ### ########.fr */ +/* Updated: 2020/05/12 14:07:04 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,20 +16,26 @@ int gl_state_init(t_gl_state *state, t_object *object) { if ((state->shader = shader_new()) == 0) return (-1); + GL_CALL(state->mvp_location = glGetUniformLocation(state->shader, "u_mvp")); + if (state->mvp_location == -1) + return (-1); + + GL_CALL(glGenVertexArrays(1, &state->vertex_array)); GL_CALL(glGenBuffers(1, &state->vertex_buf)); + GL_CALL(glGenBuffers(1, &state->index_buf)); + + GL_CALL(glBindVertexArray(state->vertex_array)); + 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, + GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * object->indices_len, object->indices, GL_STATIC_DRAW)); - GL_CALL(glGenVertexArrays(1, &state->vertex_array)); - GL_CALL(glBindVertexArray(state->vertex_array)); + GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0)); GL_CALL(glEnableVertexAttribArray(0)); - GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (const void*)0)); return (0); } @@ -42,3 +48,8 @@ void gl_state_quit(t_gl_state *state, t_object *object) free(object->vertices); free(object->indices); } + +void gl_state_set_mvp(t_gl_state *state, t_ftmmat4 *mvp) +{ + GL_CALL(glUniformMatrix4fv(state->mvp_location, 1, GL_TRUE, mvp->m)); +} diff --git a/src/glfw.c b/src/glfw.c index a492c08..1071ae0 100644 --- a/src/glfw.c +++ b/src/glfw.c @@ -6,12 +6,18 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 01:58:00 by charles #+# #+# */ -/* Updated: 2020/05/11 09:41:51 by charles ### ########.fr */ +/* Updated: 2020/05/12 13:58:51 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "scop.h" +void st_resize_callback(GLFWwindow *window, int width, int height) +{ + (void)window; + glViewport(0, 0, width, height); +} + GLFWwindow *glfw_init(int width, int height) { GLFWwindow *window; @@ -28,6 +34,7 @@ GLFWwindow *glfw_init(int width, int height) return (NULL); } glfwMakeContextCurrent(window); + glfwSetFramebufferSizeCallback(window, st_resize_callback); if (glewInit() != GLEW_OK) { glfwDestroyWindow(window); @@ -35,6 +42,6 @@ GLFWwindow *glfw_init(int width, int height) return (NULL); } glfwSwapInterval(1); - glViewport(0, 0, width, height); + /* glViewport(0, 0, width, height); */ return (window); } diff --git a/src/helper.c b/src/helper.c new file mode 100644 index 0000000..34fd560 --- /dev/null +++ b/src/helper.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 14:55:27 by charles #+# #+# */ +/* Updated: 2020/05/11 14:57:29 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "scop.h" + +bool has_extension(char *filepath, char *extension) +{ + char *match; + + match = ft_strstr(filepath, extension); + return (match != NULL && ft_strlen(match) == ft_strlen(extension)); +} diff --git a/src/main.c b/src/main.c index 1dcc36c..5685478 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 10:20:09 by charles #+# #+# */ -/* Updated: 2020/05/11 10:49:17 by charles ### ########.fr */ +/* Updated: 2020/05/12 14:10:09 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,36 +18,86 @@ int main(int argc, char **argv) t_object object; t_gl_state state; - if (argc != 2) - { - ft_putstr("Usage: "); - ft_putstr(argv[0]); - ft_putendl(" [obj file]"); - return (1); - } - if (parse(argv[1], &object) == -1) - { - ft_putstr("Error: couldn't parse "); - ft_putendl(argv[1]); - return (1); - } + /* if (argc != 2) */ + /* { */ + /* ft_putstr("Usage: "); */ + /* ft_putstr(argv[0]); */ + /* ft_putendl(" [obj file]"); */ + /* return (1); */ + /* } */ + /* if (parse(argv[1], &object) == -1) */ + /* { */ + /* ft_putstr("Error: couldn't parse "); */ + /* ft_putendl(argv[1]); */ + /* return (1); */ + /* } */ + float positions[] = { + 0.5f, 0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + -0.5f, -0.5f, 0.0f, + -0.5f, 0.5f, 0.0f + }; + unsigned int index[] = { + 0, 1, 3, + 1, 2, 3 + }; + + object.vertices = malloc(sizeof(positions)); + ft_memcpy(object.vertices, positions, sizeof(positions)); + object.indices = malloc(sizeof(index)); + ft_memcpy(object.indices, index, sizeof(index)); + object.vertices_len = 12; + object.indices_len = 6; /* 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); */ + t_ftmmat4 trans; + t_ftmvec3 vec; + + ftm_mat4init_eye(&trans, 1.0); + /* ftm_mat4translate(&trans, 0.5, 0.5, 0.0); */ + + ftm_mat4rotate(&trans, ftm_radian(45.0), ftm_vec3init(&vec, 0.0, 0.0, 1.0)); + + /* ftm_mat4scale(&trans, 0.5, 0.5, 0.5); */ + + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + printf("%f, ", trans.m[i * 4 + j]); + } + printf("\n"); + } + if ((window = glfw_init(400, 400)) == NULL || gl_state_init(&state, &object) == -1) return (1); + + GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, 0)); + GL_CALL(glBindVertexArray(0)); + + GL_CALL(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)); + + GL_CALL(glUseProgram(state.shader)); + gl_state_set_mvp(&state, &trans); + while (!glfwWindowShouldClose(window)) { + GL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f)); GL_CALL(glClear(GL_COLOR_BUFFER_BIT)); + GL_CALL(glUseProgram(state.shader)); + ftm_mat4rotate(&trans, ftm_radian(1.0), ftm_vec3init(&vec, 0.0, 0.0, 1.0)); + gl_state_set_mvp(&state, &trans); + GL_CALL(glBindVertexArray(state.vertex_array)); - GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state.index_buf)); - GL_CALL(glDrawElements(GL_TRIANGLES, object.vertices_len, GL_UNSIGNED_INT, (const void*)0)); + GL_CALL(glDrawElements(GL_TRIANGLES, object.indices_len, GL_UNSIGNED_INT, (void*)0)); + glfwSwapBuffers(window); glfwPollEvents(); } diff --git a/src/parse.c b/src/parse.c index b83d9e8..6290b1a 100644 --- a/src/parse.c +++ b/src/parse.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 11:02:00 by charles #+# #+# */ -/* Updated: 2020/05/11 01:29:13 by charles ### ########.fr */ +/* Updated: 2020/05/11 15:55:44 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ static int st_parse_face(char **indexes_strs, t_ftvec *indices) unsigned int first; unsigned int tmp; - len = ft_split_len(indexes_strs); + len = ft_strslen(indexes_strs); first = ft_atoi(indexes_strs[0]); /* if (ft_vecpush(indices, *(void**)&first) == NULL) */ /* return (-1); */ @@ -64,11 +64,11 @@ static int st_parse_line(char *line, t_ftvec *vertices, t_ftvec *indices) if ((split = ft_split(line + 1, ' ')) == NULL) return (-1); ret = -1; - if (*line == 'v' && ft_split_len(split) == 3) + if (*line == 'v' && ft_strslen(split) == 3) ret = st_parse_vertex(split, vertices); - if (*line == 'f' && ft_split_len(split) >= 3) + if (*line == 'f' && ft_strslen(split) >= 3) ret = st_parse_face(split, indices); - ft_split_destroy(split); + ft_strsdestroy(split); return (ret); } @@ -101,7 +101,8 @@ int parse(char *filepath, t_object *object) t_ftvec *vertices; t_ftvec *indices; - if ((fd = open(filepath, O_RDONLY)) == -1) + if (!has_extension(filepath, ".obj") + || (fd = open(filepath, O_RDONLY)) == -1) return (-1); if ((vertices = ft_vecnew(SCOP_VEC_DEFAULT_SIZE)) == NULL) return (-1); diff --git a/src/shader.c b/src/shader.c index 2e12a3e..dac76c8 100644 --- a/src/shader.c +++ b/src/shader.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 09:35:54 by charles #+# #+# */ -/* Updated: 2020/05/11 12:25:16 by charles ### ########.fr */ +/* Updated: 2020/05/11 21:30:29 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,17 @@ static unsigned int st_compile(char *filepath, unsigned int type) { - char *src; + t_ftmem file; unsigned int id; int result; int len; char *msg; - if ((src = ft_getfile(open(filepath, O_RDONLY))) == NULL) + if (ft_getfile(open(filepath, O_RDONLY), &file) == -1) return (0); GL_CALL(id = glCreateShader(type)); - GL_CALL(glShaderSource(id, 1, (const char**)&src, NULL)); - free(src); + GL_CALL(glShaderSource(id, 1, (const char**)&file.data, NULL)); + free(file.data); GL_CALL(glCompileShader(id)); GL_CALL(glGetShaderiv(id, GL_COMPILE_STATUS, &result)); if (result == GL_FALSE) diff --git a/src/texture.c b/src/texture.c new file mode 100644 index 0000000..e499af6 --- /dev/null +++ b/src/texture.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* texture.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 14:27:34 by charles #+# #+# */ +/* Updated: 2020/05/11 16:19:56 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "scop.h" + +uint8_t *st_read_bmp(char *filepath, size_t *width, size_t *height) +{ + t_ftmem file; + + if (ft_getfile(open(filepath, O_RDONLY), &file) == -1) + return (NULL); + *width = 0; + *height = 0; + return (file.data); +} + +unsigned int texture_new(char *filepath) +{ + uint8_t *data; + unsigned int texture; + size_t width; + size_t height; + + if ((data = st_read_bmp(filepath, &width, &height)) == NULL) + return (0); + GL_CALL(glGenTextures(1, &texture)); + GL_CALL(glBindTexture(GL_TEXTURE_2D, texture)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); + GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data)); + free(data); + return (texture); +} -- cgit