aboutsummaryrefslogtreecommitdiff
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
parent5635d61927b2fc864d92f9f7b40cdb164eeab275 (diff)
downloadscop-0267b512527b85af6cd815bb9215bd659b75931e.tar.gz
scop-0267b512527b85af6cd815bb9215bd659b75931e.tar.bz2
scop-0267b512527b85af6cd815bb9215bd659b75931e.zip
Added basic color
-rw-r--r--inc/scop.h22
-rw-r--r--res/cube.obj14
-rw-r--r--shader/fragment.glsl4
-rw-r--r--shader/vertex.glsl8
-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
10 files changed, 137 insertions, 34 deletions
diff --git a/inc/scop.h b/inc/scop.h
index 1032f1d..15e7eb1 100644
--- a/inc/scop.h
+++ b/inc/scop.h
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/09 10:41:44 by charles #+# #+# */
-/* Updated: 2020/05/13 11:28:22 by charles ### ########.fr */
+/* Updated: 2020/05/13 12:15:23 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,6 +26,7 @@
# include "libftm_mat4.h"
# include "libftm_vec3.h"
+
typedef struct
{
float *vertices;
@@ -45,6 +46,18 @@ typedef struct
int proj_location;
} t_gl_state;
+union u_color
+{
+ float data[4];
+ struct
+ {
+ float r;
+ float g;
+ float b;
+ float a;
+ };
+};
+
/*
** parse.c
*/
@@ -98,4 +111,11 @@ bool has_extension(char *filepath, char *extension);
void center_mat4_init_translate(t_ftmmat4 *dst, float *vertices, size_t vertices_len);
+/*
+** color.c
+*/
+
+bool color_merge_vertices(t_object *object);
+
+
#endif
diff --git a/res/cube.obj b/res/cube.obj
index 7908719..6b87906 100644
--- a/res/cube.obj
+++ b/res/cube.obj
@@ -2,14 +2,14 @@
# www.blender.org
mtllib cube.mtl
o Cube
-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
-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
+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
-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
diff --git a/shader/fragment.glsl b/shader/fragment.glsl
index 7f5d052..bf1c286 100644
--- a/shader/fragment.glsl
+++ b/shader/fragment.glsl
@@ -1,8 +1,10 @@
#version 400 core
+in vec4 v_color;
+
out vec4 out_color;
void main()
{
- out_color = vec4(1.0, 0.5, 0.2, 1.0);
+ out_color = v_color;
}
diff --git a/shader/vertex.glsl b/shader/vertex.glsl
index f2b0634..d46ecd5 100644
--- a/shader/vertex.glsl
+++ b/shader/vertex.glsl
@@ -1,12 +1,16 @@
#version 400 core
-layout (location = 0) in vec4 v_position;
+layout (location = 0) in vec4 in_position;
+layout (location = 1) in vec4 in_color;
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_proj;
+out vec4 v_color;
+
void main()
{
- gl_Position = u_proj * u_view * u_model * v_position;
+ v_color = in_color;
+ gl_Position = u_proj * u_view * u_model * in_position;
}
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);