aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-05-13 11:48:22 +0200
committerCharles <sircharlesaze@gmail.com>2020-05-13 11:48:22 +0200
commit5635d61927b2fc864d92f9f7b40cdb164eeab275 (patch)
tree5a9fa7c0cd9a4e662b0ad63d87143890880af173 /src
parent723c4602c6ec9b74e841501754e651ef359f6385 (diff)
downloadscop-5635d61927b2fc864d92f9f7b40cdb164eeab275.tar.gz
scop-5635d61927b2fc864d92f9f7b40cdb164eeab275.tar.bz2
scop-5635d61927b2fc864d92f9f7b40cdb164eeab275.zip
Added model center translation
Diffstat (limited to 'src')
-rw-r--r--src/center.c52
-rw-r--r--src/gl.c4
-rw-r--r--src/main.c86
-rw-r--r--src/parse.c55
-rw-r--r--src/shader.c4
5 files changed, 146 insertions, 55 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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(&center_trans, object.vertices, object.vertices_len);
+ /* debugmat(&center_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, &center_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, &center_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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));