aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-05-13 13:05:33 +0200
committerCharles <sircharlesaze@gmail.com>2020-05-13 13:05:33 +0200
commit0267b512527b85af6cd815bb9215bd659b75931e (patch)
tree3f798f9e4b89f84d5e11ae820329e15f3c1e351d /src
parent5635d61927b2fc864d92f9f7b40cdb164eeab275 (diff)
downloadscop-0267b512527b85af6cd815bb9215bd659b75931e.tar.gz
scop-0267b512527b85af6cd815bb9215bd659b75931e.tar.bz2
scop-0267b512527b85af6cd815bb9215bd659b75931e.zip
Added basic color
Diffstat (limited to 'src')
-rw-r--r--src/center.c4
-rw-r--r--src/color.c65
-rw-r--r--src/gl.c10
-rw-r--r--src/glfw.c5
-rw-r--r--src/main.c33
-rw-r--r--src/parse.c6
6 files changed, 100 insertions, 23 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/05/13 11:53:53 by charles #+# #+# */
+/* Updated: 2020/05/13 13:00:48 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "scop.h"
+
+#include <string.h>
+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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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(&center_trans, object.vertices, object.vertices_len);
/* debugmat(&center_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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);