From 5635d61927b2fc864d92f9f7b40cdb164eeab275 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 13 May 2020 11:48:22 +0200 Subject: Added model center translation --- .gitignore | 1 + Makefile | 5 +- inc/scop.h | 8 ++- res/42.obj | 1 + res/cube.obj | 3 ++ res/pyramid.obj | 7 --- res/teapot2.obj | 1 + shader/fragment.glsl | 2 +- shader/vertex.glsl | 4 +- src/center.c | 52 ++++++++++++++++++++ src/gl.c | 4 +- src/main.c | 86 ++++++++++++++++++--------------- src/parse.c | 55 ++++++++++++++++----- src/shader.c | 4 +- vendor/libft | 2 +- vendor/libftm/inc/libftm_vec3.h | 13 ++++- vendor/libftm/src/mat4/ftm_mat4rotate.c | 29 +++++------ 17 files changed, 192 insertions(+), 85 deletions(-) create mode 100644 src/center.c diff --git a/.gitignore b/.gitignore index 1bee866..61bd2a6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.ghc a.out scop +vgcore.* diff --git a/Makefile b/Makefile index 7daf4dc..42c8110 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: charles +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/05/09 10:24:52 by charles #+# #+# # -# Updated: 2020/05/12 16:07:23 by charles ### ########.fr # +# Updated: 2020/05/12 21:17:54 by charles ### ########.fr # # # # **************************************************************************** # @@ -22,8 +22,9 @@ INC_DIR = inc OBJ_DIR = obj CC = gcc +OFLAG ?= -O0 CCFLAGS = -I$(LIBFT_DIR)/include -I$(INC_DIR) -I$(LIBFTM_DIR)/inc \ - -Wall -Wextra #-Werror + $(OFLAG) -Wall -Wextra #-Werror LDFLAGS = -L$(LIBFT_DIR) -lft -L$(LIBFTM_DIR) -lftm -lm ifeq ($(shell uname),Linux) diff --git a/inc/scop.h b/inc/scop.h index 2d8583a..1032f1d 100644 --- a/inc/scop.h +++ b/inc/scop.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 10:41:44 by charles #+# #+# */ -/* Updated: 2020/05/12 16:28:57 by charles ### ########.fr */ +/* Updated: 2020/05/13 11:28:22 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -92,4 +92,10 @@ unsigned int shader_new(void); bool has_extension(char *filepath, char *extension); +/* +** center.c +*/ + +void center_mat4_init_translate(t_ftmmat4 *dst, float *vertices, size_t vertices_len); + #endif diff --git a/res/42.obj b/res/42.obj index 10962ef..5cebfc0 100644 --- a/res/42.obj +++ b/res/42.obj @@ -44,6 +44,7 @@ v 0.223704 0.634134 -0.037620 v 0.223704 -0.066768 0.398575 v 0.223704 -0.684649 0.389681 v 0.223704 -0.075523 -0.037620 + usemtl Material s off f 16 2 3 17 diff --git a/res/cube.obj b/res/cube.obj index fbcb334..7908719 100644 --- a/res/cube.obj +++ b/res/cube.obj @@ -10,6 +10,7 @@ v -1.000000 1.000000 -1.000000 v -1.000000 -1.000000 -1.000000 v -1.000000 1.000000 1.000000 v -1.000000 -1.000000 1.000000 + vt 0.625000 0.500000 vt 0.875000 0.500000 vt 0.875000 0.750000 @@ -24,12 +25,14 @@ vt 0.375000 0.250000 vt 0.125000 0.500000 vt 0.375000 0.500000 vt 0.125000 0.750000 + vn 0.0000 1.0000 0.0000 vn 0.0000 0.0000 1.0000 vn -1.0000 0.0000 0.0000 vn 0.0000 -1.0000 0.0000 vn 1.0000 0.0000 0.0000 vn 0.0000 0.0000 -1.0000 + usemtl Material s off f 1/1/1 5/2/1 7/3/1 3/4/1 diff --git a/res/pyramid.obj b/res/pyramid.obj index 16073df..9d4f7b7 100644 --- a/res/pyramid.obj +++ b/res/pyramid.obj @@ -1,15 +1,8 @@ -# top v 0.0 1.0 0.0 -# base v 0.0 0.0 -1.0 v -1.0 0.0 1.0 v 1.0 0.0 1.0 - -# base f 2 3 4 - -# front f 1 3 4 - f 1 2 3 f 1 3 4 diff --git a/res/teapot2.obj b/res/teapot2.obj index 3a1df6c..f2f9e3c 100644 --- a/res/teapot2.obj +++ b/res/teapot2.obj @@ -3646,6 +3646,7 @@ v 1.213920 -1.175611 -0.890880 v 1.334760 -1.175611 -0.694440 v 1.424640 -1.175611 -0.478560 v 1.480680 -1.175611 -0.246120 + usemtl None s off f 1 2 3 diff --git a/shader/fragment.glsl b/shader/fragment.glsl index 3fbeac5..7f5d052 100644 --- a/shader/fragment.glsl +++ b/shader/fragment.glsl @@ -4,5 +4,5 @@ out vec4 out_color; void main() { - out_color = vec4(1.0, 1.0, 1.0, 1.0); + out_color = vec4(1.0, 0.5, 0.2, 1.0); } diff --git a/shader/vertex.glsl b/shader/vertex.glsl index 02a1954..f2b0634 100644 --- a/shader/vertex.glsl +++ b/shader/vertex.glsl @@ -1,6 +1,6 @@ #version 400 core -layout (location = 0) in vec3 v_position; +layout (location = 0) in vec4 v_position; uniform mat4 u_model; uniform mat4 u_view; @@ -8,5 +8,5 @@ uniform mat4 u_proj; void main() { - gl_Position = u_proj * u_view * u_model * vec4(v_position, 1.0); + gl_Position = u_proj * u_view * u_model * v_position; } diff --git a/src/center.c b/src/center.c new file mode 100644 index 0000000..b15811d --- /dev/null +++ b/src/center.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* center.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/13 10:48:15 by charles #+# #+# */ +/* Updated: 2020/05/13 11:32:45 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "scop.h" + +void st_find_boundary(float *vertices, size_t vertices_len, t_ftmvec3 *min, t_ftmvec3 *max) +{ + size_t i; + + ftm_vec3init(min, INFINITY, INFINITY, INFINITY); + ftm_vec3init(max, -INFINITY, -INFINITY, -INFINITY); + i = 0; + while (i < vertices_len) + { + if (vertices[i] > max->x) + max->x = vertices[i]; + if (vertices[i] < min->x) + min->x = vertices[i]; + i++; + if (vertices[i] > max->y) + max->y = vertices[i]; + if (vertices[i] < min->y) + min->y = vertices[i]; + i++; + if (vertices[i] > max->z) + max->z = vertices[i]; + if (vertices[i] < min->z) + min->z = vertices[i]; + i += 2; + } +} + +void center_mat4_init_translate(t_ftmmat4 *dst, float *vertices, size_t vertices_len) +{ + t_ftmvec3 min; + t_ftmvec3 max; + + st_find_boundary(vertices, vertices_len, &min, &max); + ftm_mat4init_eye(dst, 1.0); + ftm_mat4translate(dst, -(max.x + min.x) / 2.0f, + -(max.y + min.y) / 2.0f, + -(max.z + min.z) / 2.0f); +} diff --git a/src/gl.c b/src/gl.c index 42308a1..a479147 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/12 18:46:49 by charles ### ########.fr */ +/* Updated: 2020/05/13 09:24:08 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ int gl_state_init(t_gl_state *state, t_object *object) GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * object->indices_len, object->indices, GL_STATIC_DRAW)); - GL_CALL(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0)); + GL_CALL(glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*)0)); GL_CALL(glEnableVertexAttribArray(0)); return (0); } diff --git a/src/main.c b/src/main.c index 10e6c2c..375be0a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,19 +6,30 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 10:20:09 by charles #+# #+# */ -/* Updated: 2020/05/12 19:08:00 by charles ### ########.fr */ +/* Updated: 2020/05/13 11:45:32 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "scop.h" +/* +** TODO +** - texture +** - parse vt +** - parse coord index of f +** - center object +** - parse mtl file +** - color +** - transition with texture +*/ + 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("%.2f, ", mat->m[i * 4 + j]); } printf("\n"); } @@ -47,37 +58,24 @@ int main(int argc, char **argv) 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 center_trans; + center_mat4_init_translate(¢er_trans, object.vertices, object.vertices_len); + /* debugmat(¢er_trans); */ + + /* 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; @@ -85,17 +83,21 @@ int main(int argc, char **argv) t_ftmvec3 vec; 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_mat4translate(&model, 0.0, 0.0, -10.0); */ + /* ftm_mat4rotate(&model, ftm_radian(45.0f), ftm_vec3init(&vec, 0.0, 1.0, 0.0)); */ + /* ftm_mat4mul(&model, ¢er_trans); */ + /* ftm_mat4rotate(&model, M_PI_4, ftm_vec3init(&vec, 0.0, 1.0, 0.0)); */ /* ftm_mat4scale(&model, 1.1, 1.1, 1.1); */ ftm_mat4init_eye(&view, 1.0); ftm_mat4init_perspective(&proj, M_PI_2 / 2.0, 1.0, 0.1, 100.0); + /* printf("asfd\n"); */ + /* printf("%f %f %f\n", vec.v[0], vec.v[1], vec.v[2]); */ /* debugmat(&model); */ /* debugmat(&view); */ - debugmat(&proj); + /* debugmat(&proj); */ if ((window = glfw_init(400, 400)) == NULL @@ -110,20 +112,26 @@ int main(int argc, char **argv) GL_CALL(glUseProgram(state.shader)); gl_state_set_mvp(&state, &model, &view, &proj); + float deg = 0.0; 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(&model, ftm_radian(0.5), ftm_vec3init(&vec, 1.0, 0.0, 0.0)); + + ftm_mat4init_eye(&model, 1.0); + ftm_mat4translate(&model, 0.0, 0.0, -10.0); + ftm_mat4rotate(&model, ftm_radian(deg), ftm_vec3init(&vec, 0.0, 1.0, 0.0)); + ftm_mat4mul(&model, ¢er_trans); + deg += 0.4; 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); + /* printf("%d %d\n", w, h); */ + ftm_mat4init_perspective(&proj, M_PI_4, (float)w / (float)h, 0.1, 100.0); g_window_resized = false; } diff --git a/src/parse.c b/src/parse.c index 6f632d6..ca3747c 100644 --- a/src/parse.c +++ b/src/parse.c @@ -6,13 +6,13 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/09 11:02:00 by charles #+# #+# */ -/* Updated: 2020/05/12 18:38:56 by charles ### ########.fr */ +/* Updated: 2020/05/13 10:15:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "scop.h" -#define SCOP_VEC_DEFAULT_SIZE 64 +#define SCOP_VEC_DEFAULT_SIZE 2048 static void *st_iter_func_decrement_uint(void *void_uint) { @@ -32,8 +32,6 @@ static int st_parse_face(char **indexes_strs, t_ftvec *indices) len = ft_strslen(indexes_strs); first = ft_atoi(indexes_strs[0]); - /* if (ft_vecpush(indices, *(void**)&first) == NULL) */ - /* return (-1); */ i = 1; while (i < len - 1) { @@ -60,6 +58,45 @@ static int st_parse_vertex(char **positions_strs, t_ftvec *vertices) ft_vecpush(vertices, *((void**)&tmp)); tmp = ft_atof(positions_strs[2]); ft_vecpush(vertices, *((void**)&tmp)); + if (positions_strs[3] != NULL) + { + tmp = ft_atof(positions_strs[3]); + ft_vecpush(vertices, *((void**)&tmp)); + } + else + { + tmp = 1.0f; + ft_vecpush(vertices, *((void**)&tmp)); + } + return (0); +} + +static int st_parse_texture_coord(char **coord_strs, t_ftvec *coords) +{ + float tmp; + + tmp = ft_atof(coord_strs[0]); + ft_vecpush(coords, *((void**)&tmp)); + if (coord_strs[1] != NULL) + { + tmp = ft_atof(coord_strs[1]); + ft_vecpush(coords, *((void**)&tmp)); + } + else + { + tmp = 0.0f; + ft_vecpush(coords, *((void**)&tmp)); + } + if (ft_strslen(coord_strs) == 3) + { + tmp = ft_atof(coord_strs[2]); + ft_vecpush(coords, *((void**)&tmp)); + } + else + { + tmp = 0.0f; + ft_vecpush(coords, *((void**)&tmp)); + } return (0); } @@ -68,7 +105,7 @@ static int st_parse_line(char *line, t_ftvec *vertices, t_ftvec *indices) int ret; char **split; - if (*line != 'v' && *line != 'f') + if (!ft_strnequ(line, "v ", 2) && !ft_strnequ(line, "f ", 2)) return (0); if ((split = ft_split(line + 1, ' ')) == NULL) return (-1); @@ -88,21 +125,15 @@ 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') - { - free(line); - return (-1); - } + ret = -1; free(line); return (0); } diff --git a/src/shader.c b/src/shader.c index dac76c8..3a31d4e 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 21:30:29 by charles ### ########.fr */ +/* Updated: 2020/05/12 21:23:06 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ static unsigned int st_compile(char *filepath, unsigned int type) if (ft_getfile(open(filepath, O_RDONLY), &file) == -1) return (0); GL_CALL(id = glCreateShader(type)); - GL_CALL(glShaderSource(id, 1, (const char**)&file.data, NULL)); + GL_CALL(glShaderSource(id, 1, (const char**)&file.data, (int*)&file.size)); free(file.data); GL_CALL(glCompileShader(id)); GL_CALL(glGetShaderiv(id, GL_COMPILE_STATUS, &result)); diff --git a/vendor/libft b/vendor/libft index 966eb29..3a3915e 160000 --- a/vendor/libft +++ b/vendor/libft @@ -1 +1 @@ -Subproject commit 966eb29634a84496e0851ef2b5a7d64f413d33ed +Subproject commit 3a3915eb488ebc8cf6c686dce85aaee1af64dd99 diff --git a/vendor/libftm/inc/libftm_vec3.h b/vendor/libftm/inc/libftm_vec3.h index e9df2c7..c40d6c8 100644 --- a/vendor/libftm/inc/libftm_vec3.h +++ b/vendor/libftm/inc/libftm_vec3.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/12 11:02:29 by charles #+# #+# */ -/* Updated: 2020/05/12 11:04:08 by charles ### ########.fr */ +/* Updated: 2020/05/13 11:13:12 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,16 @@ typedef struct { - float v[3]; + union + { + float v[3]; + struct + { + float x; + float y; + float z; + }; + }; } t_ftmvec3; t_ftmvec3 *ftm_vec3init(t_ftmvec3 *vec3, float x, float y, float z); diff --git a/vendor/libftm/src/mat4/ftm_mat4rotate.c b/vendor/libftm/src/mat4/ftm_mat4rotate.c index 6d0432e..0d9f32b 100644 --- a/vendor/libftm/src/mat4/ftm_mat4rotate.c +++ b/vendor/libftm/src/mat4/ftm_mat4rotate.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/12 11:44:18 by charles #+# #+# */ -/* Updated: 2020/05/12 17:07:23 by charles ### ########.fr */ +/* Updated: 2020/05/12 19:52:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,6 +41,7 @@ t_ftmmat4 *ftm_mat4rotate(t_ftmmat4 *mat4, float theta, t_ftmvec3 *axis) float z; float sin_t; float cos_t; + float m_cos_t; t_ftmmat4 rot; x = axis->v[0]; @@ -48,23 +49,23 @@ t_ftmmat4 *ftm_mat4rotate(t_ftmmat4 *mat4, float theta, t_ftmvec3 *axis) z = axis->v[2]; sin_t = sinf(theta); cos_t = cosf(theta); - ftm_mat4init_fill(&rot, 0.0); - ftm_mat4set(&rot, 0, 0, cos_t + x * x * (1 - cos_t)); - ftm_mat4set(&rot, 1, 0, y * x * (1 - cos_t) + z * sin_t); - ftm_mat4set(&rot, 2, 0, z * x * (1 - cos_t) + y * sin_t); + m_cos_t = 1.0 - cos_t; + ftm_mat4init_eye(&rot, 1.0); + ftm_mat4set(&rot, 0, 0, cos_t + x * x * m_cos_t); + ftm_mat4set(&rot, 1, 0, y * x * m_cos_t + z * sin_t); + ftm_mat4set(&rot, 2, 0, z * x * m_cos_t - y * sin_t); ftm_mat4set(&rot, 3, 0, 0.0); - ftm_mat4set(&rot, 0, 1, x * y * (1 - cos_t) - z * sin_t); - ftm_mat4set(&rot, 1, 1, cos_t + y * y * (1 - cos_t)); - ftm_mat4set(&rot, 2, 1, z * y * (1 - cos_t) + x * sin_t); - ftm_mat4set(&rot, 3, 1, 0); + ftm_mat4set(&rot, 0, 1, x * y * m_cos_t - z * sin_t); + ftm_mat4set(&rot, 1, 1, cos_t + y * y * m_cos_t); + ftm_mat4set(&rot, 2, 1, z * y * m_cos_t + x * sin_t); + ftm_mat4set(&rot, 3, 1, 0.0); - ftm_mat4set(&rot, 0, 2, x * z * (1 - cos_t) + y * sin_t); - ftm_mat4set(&rot, 1, 2, y * z * (1 - cos_t) - x * sin_t); - ftm_mat4set(&rot, 2, 2, cos_t + z * z * (1 - cos_t)); - ftm_mat4set(&rot, 3, 2, 0); + ftm_mat4set(&rot, 0, 2, x * z * m_cos_t + y * sin_t); + ftm_mat4set(&rot, 1, 2, y * z * m_cos_t - x * sin_t); + ftm_mat4set(&rot, 2, 2, cos_t + z * z * m_cos_t); + ftm_mat4set(&rot, 3, 2, 0.0); - ftm_mat4set(&rot, 3, 3, 1.0); ftm_mat4mul(mat4, &rot); return (mat4); } -- cgit