aboutsummaryrefslogtreecommitdiff
path: root/vendor/libftm/src/mat4/ftm_mat4rotate.c
blob: 0d9f32b8b02d31e4238a7e01fccfa3ae75058e21 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ftm_mat4rotate.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/05/12 11:44:18 by charles           #+#    #+#             */
/*   Updated: 2020/05/12 19:52:55 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libftm_mat4.h"

/*
** Rotation axis/vector [x, y, z]
**
** 1st column:
** | cos(θ) + x^2 (1 − cos(θ))   |
** | y x (1 − cos(θ)) + z sin(θ) |
** | z x (1 − cos(θ)) − y sin(θ) |
** | 0                           |
**
** 2nd column:
** | x y (1 − cos(θ)) − z sin(θ) |
** | cos(θ) + y^2 (1 − cos(θ))   |
** | z y (1 − cos(θ)) + x sin(θ) |
** | 0                           |
**
** 3rd column:
** | x z (1 − cos(θ)) + y sin(θ) |
** | y z (1 − cos(θ)) − x sin(θ) |
** | cos(θ) + z^2 (1 − cos(θ))   |
** | 0                           |
*/

t_ftmmat4	*ftm_mat4rotate(t_ftmmat4 *mat4, float theta, t_ftmvec3 *axis)
{
	float		x;
	float		y;
	float		z;
	float		sin_t;
	float		cos_t;
	float		m_cos_t;
	t_ftmmat4	rot;

	x = axis->v[0];
	y = axis->v[1];
	z = axis->v[2];
	sin_t = sinf(theta);
	cos_t = cosf(theta);
	m_cos_t = 1.0 - cos_t;
	ftm_mat4init_eye(&rot, 1.0);
	ftm_mat4set(&rot, 0, 0, cos_t + x * x * m_cos_t);
	ftm_mat4set(&rot, 1, 0, y * x * m_cos_t + z * sin_t);
	ftm_mat4set(&rot, 2, 0, z * x * m_cos_t - y * sin_t);
	ftm_mat4set(&rot, 3, 0, 0.0);

	ftm_mat4set(&rot, 0, 1, x * y * m_cos_t - z * sin_t);
	ftm_mat4set(&rot, 1, 1, cos_t + y * y * m_cos_t);
	ftm_mat4set(&rot, 2, 1, z * y * m_cos_t + x * sin_t);
	ftm_mat4set(&rot, 3, 1, 0.0);

	ftm_mat4set(&rot, 0, 2, x * z * m_cos_t + y * sin_t);
	ftm_mat4set(&rot, 1, 2, y * z * m_cos_t - x * sin_t);
	ftm_mat4set(&rot, 2, 2, cos_t + z * z * m_cos_t);
	ftm_mat4set(&rot, 3, 2, 0.0);

	ftm_mat4mul(mat4, &rot);
	return (mat4);
}