Reliably obtaining rotations for OPENGL

Post Reply
Berezovsky
Posts: 14
Joined: Tue Oct 14, 2014 9:32 pm

Reliably obtaining rotations for OPENGL

Post by Berezovsky »

I am currently writing my own graphical front end engine in openGL, on top of a Bullet physics simulation. I have successfully retrieved the world coordinates of the objects like so:

Code: Select all

spbody = body[k];
btT = spbody->getWorldTransform(); 
mbodypos = btT.getOrigin();
posis[lin  ] = (float)(mbodypos.getX());
posis[lin+1] = (float)(mbodypos.getY()); 
posis[lin+2] = (float)(mbodypos.getZ());
But now I want to retrieve the rotations of this body, in such a way as to simplify the process in openGL during drawing. In openGL, built-in matrix operations can be performed, on an item-by-item basis during draw. Like so

Code: Select all

		
glPushMatrix();
    glTranslatef(xcf, ycf, zcf );     // move item to position
    glRotatef( 1. , 1.0f, 0.0f, 0.0f );   // Rotate by 1 radian around X-axis
    glRotatef( 1. , 0.0f, 1.0f, 0.0f );  //  Rotate by 1 radian around Y-axis.
    glScalef( itemscale, itemscale, itemscale ); // item scale
    ogl.DrawItem(n);
glPopMatrix();
This is not an openGL question. This is a question of, "what-do-I-do-after-getWorldTransform()"?? Seems like to convert to openGL, I will need to drill down into the rotation components of the rigid body. But how are those stored? As quaternions?
tmason
Posts: 19
Joined: Wed Aug 27, 2014 5:02 pm

Re: Reliably obtaining rotations for OPENGL

Post by tmason »

Something like this:

Code: Select all

btTransform CurrentPhysicsPosition = btTransform();

PhysicsShapeRigidBody->getMotionState()->getWorldTransform(CurrentPhysicsPosition);

CurrentPhysicsPosition.getOpenGLMatrix(glm::value_ptr(ModelMatrix));

ModelMatrix *= glm::scale(ObjectScale);
In this case, I use the btTransform's built-in getOpenGLMatrix() member function to populate the rotation/scaling/translation data into a 4x4 Matrix that GLM (OpenGL Math Library) uses.

You could easily do something like this though:

Code: Select all

btTransform CurrentPhysicsPosition = btTransform();

PhysicsShapeRigidBody->getMotionState()->getWorldTransform(CurrentPhysicsPosition);

GLfloat* ModelMatrix = new GLfloat[16];

CurrentPhysicsPosition.getOpenGLMatrix(ModelMatrix);
And that resultant ModelMatrix is ready to pop into OpenGL, as the name of the function suggests. :D :D :D
Berezovsky
Posts: 14
Joined: Tue Oct 14, 2014 9:32 pm

Re: Reliably obtaining rotations for OPENGL

Post by Berezovsky »

All too easy... 8)
Post Reply