-
Notifications
You must be signed in to change notification settings - Fork 3
/
GeometryUtil.vala
135 lines (116 loc) · 4.1 KB
/
GeometryUtil.vala
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
GeometryUtil.vala
Copyright (C) 2013, 2021 Maia Everett <maia@everett.one>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using GL;
namespace ValaGL.Core {
/**
* Static helper class for stock geometry calculations and matrix transformations.
*/
public class GeometryUtil : Object {
private GeometryUtil () { }
/**
* Converts the given angle from degrees to radians.
*
* @param deg Angle in radians
* @return Angle in degrees
*/
public static GLfloat deg_to_rad (GLfloat deg) {
return (GLfloat) (deg * Math.PI / 180f);
}
/**
* Converts the given angle from radians to degrees.
*
* @param rad Angle in degrees
* @return Angle in radians
*/
public static GLfloat rad_to_deg (GLfloat rad) {
return (GLfloat) (rad / Math.PI * 180f);
}
/**
* Multiples the given matrix by a matrix that specifies a translation
* by the given vector. The matrix is modified in-place.
*
* For more information, refer to the ``glTranslatef`` legacy OpenGL function.
*
* @param matrix The matrix to transform
* @param translation The translation vector
*/
public static void translate (ref Mat4 matrix, ref Vec3 translation) {
var tmp = Mat4.from_data (
1, 0, 0, translation.x,
0, 1, 0, translation.y,
0, 0, 1, translation.z,
0, 0, 0, 1
);
matrix.mul_mat (ref tmp);
}
/**
* Multiples the given matrix by a matrix that specifies a scale operation
* by the given factors in the x, y and z directions.
* The matrix is modified in-place.
*
* For more information, refer to the ``glScalef`` legacy OpenGL function.
*
* @param matrix The matrix to transform
* @param scale_factors The scale factor vector
*/
public static void scale (ref Mat4 matrix, ref Vec3 scale_factors) {
var tmp = Mat4.from_data (
scale_factors.x, 0, 0, 0,
0, scale_factors.y, 0, 0,
0, 0, scale_factors.z, 0,
0, 0, 0, 1
);
matrix.mul_mat (ref tmp);
}
/**
* Multiples the given matrix by a matrix that specifies a rotation around
* the given axis by the given angle. The matrix is modified in-place.
*
* Be careful with these rotations. Unlike quaternion-based transformations,
* they may incur gimbal lock.
*
* For more information, refer to the ``glRotatef`` legacy OpenGL function.
*
* @param matrix The matrix to transform
* @param angle_deg The rotation angle in degrees
* @param axis The rotation axis
*/
public static void rotate (ref Mat4 matrix, GLfloat angle_deg, ref Vec3 axis) {
Vec3 axis_normalized = axis;
axis_normalized.normalize ();
GLfloat angle_rad = deg_to_rad (angle_deg);
// M = uuT + (cos a) (1 - uuT) + (sin a) S
Mat3 tmp1 = Mat3.from_vec_mul (ref axis_normalized, ref axis_normalized);
Mat3 tmp2 = Mat3.identity ();
tmp2.sub (ref tmp1);
tmp2.mul (Math.cosf (angle_rad));
tmp1.add (ref tmp2);
Mat3 s = Mat3.from_data (
0, -axis_normalized.z, axis_normalized.y,
axis_normalized.z, 0, -axis_normalized.x,
-axis_normalized.y, axis_normalized.x, 0
);
s.mul (Math.sinf (angle_rad));
tmp1.add (ref s);
var tmp = Mat4.expand (ref tmp1);
matrix.mul_mat (ref tmp);
}
}
}