user3319320
user3319320

Reputation: 259

Applying a transformation matrix in opengl

How can i apply a 4x4 rotation matrix using standard opengl commands? I have used rotation vectors before, but I cannot find how to apply a rotation matrix to an object?

Edit with more info:

I am trying to rotate a point in the origin using a direction vector as part of a physics simulator. I have been advised to use the up vector and the cross product to get a matrix:

{direction, up, cross, origin}

But I dont know how to apply this in openGL. Why is this depreciated?

Matrix that produces a 2D instead of 3D model:

Matrix44f rotation = Matrix44f(
    { crossproduct1[0], crossproduct1[1], crossproduct1[2], 0 },
    { 0, 1, 0, 0 },
    { directionvector[0], directionvector[1], directionvector[2], 0 },
    { 0, 0, 0, 1 });

    glMultMatrixf(rotation);

Upvotes: 0

Views: 1824

Answers (1)

Andon M. Coleman
Andon M. Coleman

Reputation: 43369

From your description ({direction, up, cross, origin}), what you are trying to describe is the ModelView matrix. However, your X and Z axes appear to be transposed.

The ModelView matrix is derived as such:

| Left_x  Up_x  Fwd_x  Eye_x |
| Left_y  Up_y  Fwd_y  Eye_y |
| Left_z  Up_z  Fwd_z  Eye_z |
|  0.0    0.0    0.0    1.0  |

It is actually the combination of a rotation (the top-left 3x3 matrix) and translation matrix (the last column).

Generally the X (Left), Y (Up) and Z (Forward) axes are orthogonal so you can compute Leftxyz as the cross product of Fwd and Up.


Regarding why glMultMatrixf (...) is deprecated, the OpenGL matrix stack was deemed deprecated in OpenGL 3.0 and removed beginning with OpenGL 3.1. This was done in order to eliminate fixed-function vertex processing and move to a shader-only API design.

In an OpenGL 3.0 or older context, OpenGL 3.1 with the GL_ARB_compatbility extension or a 3.2+ compatibility profile you can continue to use deprecated parts of the API. But in core OpenGL 3.1+ this part of the API no longer exists.

Upvotes: 1

Related Questions