From 0267b512527b85af6cd815bb9215bd659b75931e Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 13 May 2020 13:05:33 +0200 Subject: Added basic color --- src/center.c | 4 ++-- src/color.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gl.c | 10 ++++++---- src/glfw.c | 5 +++-- src/main.c | 33 +++++++++++++++++++----------- src/parse.c | 6 +++--- 6 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 src/color.c (limited to 'src') diff --git a/src/center.c b/src/center.c index b15811d..7974896 100644 --- a/src/center.c +++ b/src/center.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/13 10:48:15 by charles #+# #+# */ -/* Updated: 2020/05/13 11:32:45 by charles ### ########.fr */ +/* Updated: 2020/05/13 12:52:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ void st_find_boundary(float *vertices, size_t vertices_len, t_ftmvec3 *min, t_ft ftm_vec3init(min, INFINITY, INFINITY, INFINITY); ftm_vec3init(max, -INFINITY, -INFINITY, -INFINITY); i = 0; - while (i < vertices_len) + while (i < vertices_len * 4) { if (vertices[i] > max->x) max->x = vertices[i]; diff --git a/src/color.c b/src/color.c new file mode 100644 index 0000000..45a00fa --- /dev/null +++ b/src/color.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/13 11:53:53 by charles #+# #+# */ +/* Updated: 2020/05/13 13:00:48 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "scop.h" + +#include +float *color_new(size_t n) +{ + size_t i; + float step; + float *colors; + union u_color c; + + if ((colors = malloc(sizeof(float) * (n * 4))) == NULL) + return (NULL); + step = 1.0 / (float)n; + i = 0; + c.r = 0.0; + c.g = 0.0; + c.b = 0.0; + c.a = 1.0; + while (i < n) + { + c.r += step; + c.g += step; + c.b += step; + ft_memcpy(&colors[i * 4], c.data, 4 * sizeof(float)); + i++; + } + return (colors); +} + +bool color_merge_vertices(t_object *object) +{ + size_t i; + float *colors; + float *new_vertices; + + if ((colors = color_new(object->vertices_len)) == NULL) + return (false); + if ((new_vertices = malloc(sizeof(float) * (object->vertices_len * 8))) == NULL) + { + free(colors); + return (false); + } + i = 0; + while (i < object->vertices_len) + { + ft_memcpy(&new_vertices[i * 8], &object->vertices[i * 4], 4 * sizeof(float)); + ft_memcpy(&new_vertices[i * 8 + 4], &colors[i * 4], 4 * sizeof(float)); + i++; + } + free(object->vertices); + object->vertices = new_vertices; + return (true); +} diff --git a/src/gl.c b/src/gl.c index a479147..e923e9b 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/13 09:24:08 by charles ### ########.fr */ +/* Updated: 2020/05/13 12:48:04 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,15 +33,17 @@ int gl_state_init(t_gl_state *state, t_object *object) 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, + GL_CALL(glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 8 * object->vertices_len, object->vertices, GL_STATIC_DRAW)); GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->index_buf)); - GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * object->indices_len, + GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 3 * object->indices_len, object->indices, GL_STATIC_DRAW)); - GL_CALL(glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*)0)); GL_CALL(glEnableVertexAttribArray(0)); + GL_CALL(glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (void*)0)); + GL_CALL(glEnableVertexAttribArray(1)); + GL_CALL(glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 8, (void*)(4 * sizeof(float)))); return (0); } diff --git a/src/glfw.c b/src/glfw.c index 585f24f..69699b5 100644 --- a/src/glfw.c +++ b/src/glfw.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 01:58:00 by charles #+# #+# */ -/* Updated: 2020/05/12 19:05:43 by charles ### ########.fr */ +/* Updated: 2020/05/13 12:54:59 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,6 +45,7 @@ GLFWwindow *glfw_init(int width, int height) return (NULL); } glfwSwapInterval(1); - /* glViewport(0, 0, width, height); */ + GL_CALL(glViewport(0, 0, width, height)); + GL_CALL(glEnable(GL_DEPTH_TEST)); return (window); } diff --git a/src/main.c b/src/main.c index 375be0a..37dcd67 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/13 11:45:32 by charles ### ########.fr */ +/* Updated: 2020/05/13 13:04:45 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ ** - texture ** - parse vt ** - parse coord index of f -** - center object ** - parse mtl file ** - color ** - transition with texture @@ -62,18 +61,31 @@ int main(int argc, char **argv) center_mat4_init_translate(¢er_trans, object.vertices, object.vertices_len); /* debugmat(¢er_trans); */ - /* for (size_t i = 0; i < object.indices_len; i++) */ + if (!color_merge_vertices(&object)) + { + ft_putstr("Error: couldn't create colors"); + return (1); + } + + + /* for (size_t i = 0; i < object.indices_len * 3; 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++) */ + + /* for (size_t i = 0; i < object.vertices_len * 8; i++) */ /* { */ - /* printf("%f, ", object.vertices[i++]); */ - /* printf("%f, ", object.vertices[i++]); */ - /* printf("%f\n", object.vertices[i]); */ + /* printf("% f, ", object.vertices[i++]); */ + /* printf("% f, ", object.vertices[i++]); */ + /* printf("% f, ", object.vertices[i++]); */ + /* printf("% f\tcolor: ", object.vertices[i++]); */ + /* printf("% f, ", object.vertices[i++]); */ + /* printf("% f, ", object.vertices[i++]); */ + /* printf("% f, ", object.vertices[i++]); */ + /* printf("% f\n", object.vertices[i]); */ /* } */ /* printf("yo %lu\n", object.vertices_len); */ @@ -93,13 +105,10 @@ int main(int argc, char **argv) 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); */ - if ((window = glfw_init(400, 400)) == NULL || gl_state_init(&state, &object) == -1) return (1); @@ -116,7 +125,7 @@ int main(int argc, char **argv) while (!glfwWindowShouldClose(window)) { GL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f)); - GL_CALL(glClear(GL_COLOR_BUFFER_BIT)); + GL_CALL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); GL_CALL(glUseProgram(state.shader)); @@ -138,7 +147,7 @@ int main(int argc, char **argv) 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)); + GL_CALL(glDrawElements(GL_TRIANGLES, object.indices_len * sizeof(unsigned int), GL_UNSIGNED_INT, (void*)0)); glfwSwapBuffers(window); glfwPollEvents(); diff --git a/src/parse.c b/src/parse.c index ca3747c..c0952c0 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/13 10:15:43 by charles ### ########.fr */ +/* Updated: 2020/05/13 12:25:46 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -155,8 +155,8 @@ int parse(char *filepath, t_object *object) 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; - object->indices_len = indices->size; + object->vertices_len = vertices->size / 4; + object->indices_len = indices->size / 3; ft_vecdestroy(vertices, NULL); ft_vecdestroy(indices, NULL); return (0); -- cgit