It's normalizing the animatedJoint.m_Orient
vector, by taking the normal of the vector and copying it back to the vector itself. The glm::normalize()
method does not modify the object you pass to it.
What glm::normalize does?
-
01-06-2022 - |
Question
I trying to write an MD5 loader in Java from C++ source but I cannot find out what is this line doing:
animatedJoint.m_Orient = glm::normalize(animatedJoint.m_Orient);
where the animatedJoint.m_Orient
is vec4
. What does it do?
Solution 3
OTHER TIPS
What glm::normalize does?
Short answer: It normalizes a vector i.e. sets length to 1.
Details
A normalized vector is one which is often used just to denote pure directions without bothering about the magnitude (set to 1; hence their other, more common name unit vector) i.e. how far the vector pushes doesn't matter but in what direction does it point/push matters. This also simplifies computations -- both on paper and the machine (e.g. dot products become purely cosine's result, omission of division by length, etc.)
If v = <v.x, v.y, v.z>
some non-unit vector i.e. a vector of length/magnitude not equal to 1, then to get normalized(v), we've to divide each of its component by its length.
vec3 normalize(const vec3 &v)
{
float length_of_v = sqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
return vec3(v.x / length_of_v, v.y / length_of_v, v.z / length_of_v);
}
An older term for a unit vector is direction cosines. Say vector v makes an angle α with X-axis, β with Y-axis and γ with Z-axis then its direction cosines or the unit vector along v is given by <cos α, cos β, cos γ>
. This is helpful when we don't know the components of v but its angles with the cardinal axes.
The reason cosine function and unit vectors are related will be clear with a simple example in 2D which can be extended to higher dimensions. Say for a vector
v = <3, 4> = 3i + 4j (3 units along X-axis and 4 units along Y-axis)
we're to find the unit vector u along v.
length of v = √(3² + 4²) = 5
u = <3/5, 4/5>
Now the X component (along basis i) 3/5 is nothing but the length along the X-axis (adjacent) divided by the length of the vector (hypotenuse), since cos α = adj/hyp = 3/5, we would've arrived at the same result if we'd known α. The same holds for Y component (along basis j) too, which is nothing but cos β, where β is with respect to the Y-axis, or if you want to measure it with respect to the X-axis, then it'll be 90-β which is nothing but α, that's the reason we've v = <cos α, sin α>
, the abscissa and ordinate of a point on the unit circle, the vector from the origin to a point on the circle with length (radius) 1.
Normalizes a vector, ie scales its elements so that returned vectors length is 1. Many graphic related functions require passed vectors to be normalized.
You can read more (and find answer) about this library here:
- home page: http://glm.g-truc.net/
- manual: https://glm.g-truc.net/0.9.3/glm-0.9.3.pdf
- method API description: https://glm.g-truc.net/0.9.2/api/index.html
It will help you to understand what this library is and how does it work.