aboutsummaryrefslogtreecommitdiff
path: root/src/center.c
blob: a3a18b470a4e8d0022484527b73d290d59af3b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   center.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/05/13 10:48:15 by charles           #+#    #+#             */
/*   Updated: 2020/05/13 16:43:38 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 * 4)
	{
		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);
}

float	*texture_coord_create(float *vertices, size_t vertices_len)
{
	t_ftmvec3	min;
	t_ftmvec3	max;
	float		*coords;
	size_t		i;

	if ((coords = malloc(sizeof(float) * (vertices_len * 2))) == NULL)
		return (NULL);
	st_find_boundary(vertices, vertices_len, &min, &max);
	i = 0;
	while (i < vertices_len)
	{
		coords[i * 2 + 0] = (vertices[i * 4 + 0] - min.x) * (1.0 / (max.x - min.x));
		coords[i * 2 + 1] = (vertices[i * 4 + 1] - min.y) * (1.0 / (max.y - min.y));
		i++;
	}
	return (coords);
}