diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/gl.c | 27 | ||||
| -rw-r--r-- | src/glfw.c | 5 | ||||
| -rw-r--r-- | src/main.c | 142 | ||||
| -rw-r--r-- | src/parse.c | 15 |
4 files changed, 128 insertions, 61 deletions
@@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 01:31:10 by charles #+# #+# */ -/* Updated: 2020/05/12 14:07:04 by charles ### ########.fr */ +/* Updated: 2020/05/12 18:46:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,14 @@ 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) + GL_CALL(state->view_location = glGetUniformLocation(state->shader, "u_view")); + if (state->view_location == -1) + return (-1); + GL_CALL(state->model_location = glGetUniformLocation(state->shader, "u_model")); + if (state->model_location == -1) + return (-1); + GL_CALL(state->proj_location = glGetUniformLocation(state->shader, "u_proj")); + if (state->proj_location == -1) return (-1); GL_CALL(glGenVertexArrays(1, &state->vertex_array)); @@ -49,7 +55,18 @@ void gl_state_quit(t_gl_state *state, t_object *object) free(object->indices); } -void gl_state_set_mvp(t_gl_state *state, t_ftmmat4 *mvp) +void gl_state_set_mvp(t_gl_state *state, t_ftmmat4 *model, t_ftmmat4 *view, t_ftmmat4 *proj) { - GL_CALL(glUniformMatrix4fv(state->mvp_location, 1, GL_TRUE, mvp->m)); + if (model != NULL) + { + GL_CALL(glUniformMatrix4fv(state->model_location, 1, GL_TRUE, model->m)); + } + if (view != NULL) + { + GL_CALL(glUniformMatrix4fv(state->view_location, 1, GL_TRUE, view->m)); + } + if (proj != NULL) + { + GL_CALL(glUniformMatrix4fv(state->proj_location, 1, GL_TRUE, proj->m)); + } } @@ -6,15 +6,18 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 01:58:00 by charles #+# #+# */ -/* Updated: 2020/05/12 13:58:51 by charles ### ########.fr */ +/* Updated: 2020/05/12 19:05:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "scop.h" +bool g_window_resized = false; + void st_resize_callback(GLFWwindow *window, int width, int height) { (void)window; + g_window_resized = true; glViewport(0, 0, width, height); } @@ -6,73 +6,97 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 10:20:09 by charles #+# #+# */ -/* Updated: 2020/05/12 14:10:09 by charles ### ########.fr */ +/* Updated: 2020/05/12 19:08:00 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "scop.h" +void debugmat(t_ftmmat4 *mat) +{ + for (int i = 0; i < 4; i++) + { + for (int j = 0; j < 4; j++) + { + printf("%f, ", mat->m[i * 4 + j]); + } + printf("\n"); + } + printf("\n"); +} + +extern bool g_window_resized; + int main(int argc, char **argv) { GLFWwindow *window; 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); */ - /* } */ - - 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; + 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, ", object.indices[i++]); + printf("%u, ", object.indices[i++]); + printf("%u\n", object.indices[i]); + } + printf("yo %lu\n", object.indices_len); + for (size_t i = 0; i < object.vertices_len; i++) + { + printf("%f, ", object.vertices[i++]); + printf("%f, ", object.vertices[i++]); + printf("%f\n", object.vertices[i]); + } + printf("yo %lu\n", object.vertices_len); + + t_ftmmat4 model; + t_ftmmat4 view; + t_ftmmat4 proj; t_ftmvec3 vec; - ftm_mat4init_eye(&trans, 1.0); - /* ftm_mat4translate(&trans, 0.5, 0.5, 0.0); */ + ftm_mat4init_eye(&model, 1.0); + ftm_mat4translate(&model, 0.0, 0.0, -5.0); + /* ftm_mat4rotate(&model, ftm_radian(45.0), ftm_vec3init(&vec, 0.0, 1.0, 0.0)); */ + /* ftm_mat4scale(&model, 1.1, 1.1, 1.1); */ - ftm_mat4rotate(&trans, ftm_radian(45.0), ftm_vec3init(&vec, 0.0, 0.0, 1.0)); + ftm_mat4init_eye(&view, 1.0); - /* ftm_mat4scale(&trans, 0.5, 0.5, 0.5); */ + ftm_mat4init_perspective(&proj, M_PI_2 / 2.0, 1.0, 0.1, 100.0); + + /* debugmat(&model); */ + /* debugmat(&view); */ + debugmat(&proj); - 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) @@ -84,7 +108,7 @@ int main(int argc, char **argv) GL_CALL(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)); GL_CALL(glUseProgram(state.shader)); - gl_state_set_mvp(&state, &trans); + gl_state_set_mvp(&state, &model, &view, &proj); while (!glfwWindowShouldClose(window)) { @@ -92,8 +116,18 @@ int main(int argc, char **argv) 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); + ftm_mat4rotate(&model, ftm_radian(0.5), ftm_vec3init(&vec, 1.0, 0.0, 0.0)); + + if (g_window_resized) + { + int w, h; + glfwGetFramebufferSize(window, &w, &h); + printf("%d %d\n", w, h); + ftm_mat4init_perspective(&proj, M_PI_2 / 2.0, (float)w / (float)h, 0.1, 100.0); + g_window_resized = false; + } + + gl_state_set_mvp(&state, &model, &view, &proj); GL_CALL(glBindVertexArray(state.vertex_array)); GL_CALL(glDrawElements(GL_TRIANGLES, object.indices_len, GL_UNSIGNED_INT, (void*)0)); diff --git a/src/parse.c b/src/parse.c index 6290b1a..6f632d6 100644 --- a/src/parse.c +++ b/src/parse.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 11:02:00 by charles #+# #+# */ -/* Updated: 2020/05/11 15:55:44 by charles ### ########.fr */ +/* Updated: 2020/05/12 18:38:56 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,15 @@ #define SCOP_VEC_DEFAULT_SIZE 64 +static void *st_iter_func_decrement_uint(void *void_uint) +{ + unsigned int uint; + + uint = *(unsigned int*)&void_uint; + uint--; + return (*(void**)&uint); +} + static int st_parse_face(char **indexes_strs, t_ftvec *indices) { size_t i; @@ -79,11 +88,14 @@ static int st_parse_file(int fd, t_ftvec *vertices, t_ftvec *indices) while ((ret = ft_getline(fd, &line)) == FT_LINE) { + printf("[%s]\n", line); ret = st_parse_line(line, vertices, indices); free(line); if (ret == -1) break ; } + printf(">>%d\n", ret); + printf("[%s]\n", line); if (ret == FT_ERROR) return (-1); if (*line != '\0') @@ -109,6 +121,7 @@ int parse(char *filepath, t_object *object) if ((indices = ft_vecnew(SCOP_VEC_DEFAULT_SIZE)) == NULL) return (-1); st_parse_file(fd, vertices, indices); + ft_veciter_ret(indices, st_iter_func_decrement_uint); object->vertices = (float*)ft_vectobuf32(vertices); object->indices = (unsigned int*)ft_vectobuf32(indices); object->vertices_len = vertices->size; |
