From 5635d61927b2fc864d92f9f7b40cdb164eeab275 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 13 May 2020 11:48:22 +0200 Subject: Added model center translation --- src/center.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/center.c (limited to 'src/center.c') 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 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} -- cgit