center vertices

This commit is contained in:
gbrochar 2020-12-26 06:41:27 +01:00
parent 397b67fa1a
commit 304ec993c8
6 changed files with 54 additions and 8 deletions

View File

@ -6,7 +6,7 @@
# By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/12/17 19:47:03 by gbrochar #+# #+# #
# Updated: 2020/12/25 14:54:54 by gbrochar ### ########.fr #
# Updated: 2020/12/26 06:11:43 by gbrochar ### ########.fr #
# #
# **************************************************************************** #
@ -16,6 +16,7 @@ SRC_FILE = main.c \
parse_file.c \
parse_line.c \
parse_token.c \
center_vertices.c \
env.c \
run.c \
mat4_transpose.c \

View File

@ -6,7 +6,7 @@
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/22 12:08:22 by gbrochar #+# #+# */
/* Updated: 2020/12/25 15:06:43 by gbrochar ### ########.fr */
/* Updated: 2020/12/26 06:32:21 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
@ -114,6 +114,9 @@ int parse_token(
int parse_append_data_d(t_buf_d *buffer, char *token);
int parse_append_data_ui(t_buf_ui *buffer, char *token);
void center_vertices(t_buf_d *vertices);
void free_env(t_env *e);
void init_window(t_env *e);
void init_camera(t_env *e);

41
src/center_vertices.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* center_vertices.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/26 06:11:21 by gbrochar #+# #+# */
/* Updated: 2020/12/26 06:25:59 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "scop.h"
#include "vec3.h"
void center_vertices(t_buf_d *vertices)
{
t_vec3 average;
for (size_t i = 0; i < vertices->ptr; i++)
{
if (i % 3 == 0)
average.x += vertices->data[i];
else if (i % 3 == 1)
average.y += vertices->data[i];
else if (i % 3 == 2)
average.z += vertices->data[i];
}
average.x /= (vertices->ptr / 3);
average.y /= (vertices->ptr / 3);
average.z /= (vertices->ptr / 3);
for (size_t i = 0; i < vertices->ptr; i++)
{
if (i % 3 == 0)
vertices->data[i] -= average.x;
else if (i % 3 == 1)
vertices->data[i] -= average.y;
else if (i % 3 == 2)
vertices->data[i] -= average.z;
}
}

View File

@ -6,7 +6,7 @@
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/22 12:02:14 by gbrochar #+# #+# */
/* Updated: 2020/12/25 14:58:03 by gbrochar ### ########.fr */
/* Updated: 2020/12/26 06:41:15 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@ int main(int argc, char **argv)
init_env(&e);
if (parse(&e, argc, argv) == SUCCESS)
{
center_vertices(&(e.object.vertices));
if (run(&e) == FAILURE)
printf("Error with glfw initialization\n");
}

View File

@ -6,7 +6,7 @@
/* By: gbrochar <gbrochar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/23 19:38:05 by gbrochar #+# #+# */
/* Updated: 2020/12/25 15:22:27 by gbrochar ### ########.fr */
/* Updated: 2020/12/26 06:26:36 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
@ -52,8 +52,8 @@ static void RenderSceneCB()
t_mat4 proj = mat4_perspective(45.0 * M_PI / 180.0, 800.0 / 600.0, 0.001, 1000);
t_mat4 view = mat4_inverse(mat4_lookat(eye, up, target));
t_mat4 model = mat4_rotatey(mat4_rotatex(mat4_identity(), Scale), Scale / 1.3);
// glUniform1f(gScaleLocation, sinf(Scale));
// t_mat4 model = mat4_rotatey(mat4_rotatex(mat4_identity(), Scale), Scale / 1.3);
t_mat4 model = mat4_rotatey(mat4_identity(), Scale);
glUniform1f(gScaleIntLocation, Scale);
glUniformMatrix4fv(gProjLocation, 1, GL_FALSE, (GLfloat *)proj.data);
glUniformMatrix4fv(gViewLocation, 1, GL_FALSE, (GLfloat *)view.data);
@ -211,8 +211,8 @@ int run(t_env *e)
glfwMakeContextCurrent(window);
index_count = e->object.indices.ptr;
printf("%ld\n", e->object.indices.ptr);
printf("%ld\n", e->object.vertices.ptr);
printf("%ld\n", e->object.indices.ptr);
GLenum res = glewInit();
if (res != GLEW_OK)
{

View File

@ -12,6 +12,6 @@ float rand(float n)
void main()
{
vec3 color = vec3(rand(float(vID + int(time))), rand(float(vID + 1 + int(time))), rand(float(vID + 2 + int(time))));
vec3 color = vec3(rand(float(vID + int(time))), rand(float(vID + int(time))), rand(float(vID + int(time))));
FragColor = vec4(color, 1.0);
}