8

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?

4 Answers 4

16

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.

3
  • 1
    Strictly speaking, normal vectors do have magnitudes. However, the point is that the magnitude is exactly equal to 1 (you know, give or take floating point rounding error) so the need to account for the length vanishes in a lot of places. For example, to project onto an ordinary vector, you need to divide out the length; to project onto a normal, you don't, because the length is 1 and anything divided by 1 is itself.
    – anon
    Apr 9, 2018 at 3:03
  • @NicHartley You're right, should've been more explicit. I guess I was trying to differentiate between unit normals and normals in general; the latter being useful for its direction than its length, which may or may not be 1, but usually 1 for simpler formulas and computation. Clarified now :)
    – legends2k
    Apr 9, 2018 at 3:08
  • Nice! I wasn't actually expecting a reply, haha. It's also not super important in the end -- the distinct is fairly minimal. However, in the few cases where it does matter, it can really bite you in the butt not to know that normalized vectors are just normal vectors with a magnitude of 1, so I figured I'd at least clarify in a comment.
    – anon
    Apr 9, 2018 at 3:23
7

Normalizes a vector, ie scales its elements so that returned vectors length is 1. Many graphic related functions require passed vectors to be normalized.

6

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.

4

You can read more (and find answer) about this library here:

It will help you to understand what this library is and how does it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.