سؤال

There is a function GLRotation

inline const mat4 GLRotation(float x, float y, float z)
{
    const float cx = cosf(x * math_radians), sx = sinf(x * math_radians),
                cy = cosf(y * math_radians), sy = sinf(y * math_radians),
                cz = cosf(z * math_radians), sz = sinf(z * math_radians);

    // rotationX * rotationY * rotationZ
    return mat4(cy * cz, -cy * sz, sy, 0,
                cx * sz + sx * cz * sy, cx * cz - sx * sy * sz, -cy * sx, 0,
                sx * sz - cx * cz * sy, cz * sx + cx * sy * sz, cx * cy, 0,
                0, 0, 0, 1);
}

And use can call it like this GLRotation(v.rotation) where v.rotation - vector(x, y, z).

What function I need to use in glm(library) to get the same result?

هل كانت مفيدة؟

المحلول 2

Your function GLRotation() specifies the angle in radians to rotate in each principle axis. On the other hand glm::rotate(angle, axis) specifies a rotation about a provided axis. So, strictly speaking you can define your GLRotation as follows:

inline mat4 
GLRotation(float x, float y, float z)
{
    return 
        glm::rotate(x, 1, 0, 0) * 
        glm::rotate(y, 0, 1, 0) * 
        glm::rotate(z, 0, 0, 1);
}

Although I wouldn't advise this, because describing rotations in this way can lead to Gimbal lock (when you rotate in one axis in such a way that it becomes aligned with another axis, losing you one degree of freedom).

Better to look into Axis-Angle representations of rotations, which is what glm::rotate uses. They aren't susceptible to gimbal lock, and can represent any 3D rotation about an axis through the origin.

نصائح أخرى

you can use the following to get a rotaion using glm, although it requires slightly more input:

glm::rotate(ModelViewProjectionMatrix4x4Here,angleToRotateHere,directionToRotate)

which follows the format layed out in the glm api page

rotate (detail::tmat4x4< T > const &m, T angle, T x, T y, T z)

so you can pass it the matrix you need to operate on, the angle to rotate by and the vector around which to rotate.

glm::rotate(yourMatrixHere, angle_in_degrees, glm::vec3(x, y, z)); // where x, y, z is axis of rotation (e.g. 0 1 0 is rotate around the y-axis)

If that exact function definition does not match what you need there are others on the same web-page which take arguments in varying ways.

Let me know if you need any more information:)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top