Viewing transformations cleared after glLoadMatrix

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Viewing transformations cleared after glLoadMatrix

andys
Hi,

 this is a questions about fundamental understanding of the matrix. And i am honestly thinking over this a couple of days now. In the JBullet Demos the objects (RigidBodies) are transformed in their proper location by loading the ModelView Matrix with new matrix values :

float[] glMat = new float[16];
// trans is the com.bulletphysics.linearmath.Transform  object representing the Bodys translation & rotation.
trans.getOpenGLMatrix(glMat);
gl.glLoadMatrixf(glMat, 0);
gl.glBegin(GL2.GL_QUADS);
.... draw body .....
gl.glEnd();


So far so good - all bodys are drawn perfectly correct. But my View transformation which i setup at the very beginning of the display() method is wipped out. Regardless what transformation/rotation i do before/after this glLoadMatrix() command - it is ignored.  PushMatrix/PopMatrix (as i normally do it for all my JOGL testcode) doesn't help in any way. And also keeping a Backup of the original ModelView Matrix which i reloaded after gl.glEnd doesn't help.  I honestly do not understand this at all. I always thought it is "standard" and bulletprove operation to Push the Matrix, transform object to its location i 3d world, draw it  and then Restore the Matrix via Pop.  But in this case this glLoadMatrix seems to make an unrecoverable change of my Modelview matrix?

As far i could dig out of the JBullet demo code to me it looks like the projection matrix is changed all the time - which probably sets the proper view for the user. But no idea if this indeed is the reason why the view is correct in the original JBullet Demos. In my stripped down version of the "BasicDemo" i only get my proper view if i extract Translation & Rotation parameters from the Transfrom object:

gl.glPushMatrix();
gl.glTranslatef(trans.origin.x, trans.origin.y, trans.origin.z);
Quat4f glRot = new Quat4f();
trans.getRotation(glRot);
gl.glRotatef(glRot.w * 360, glRot.x, glRot.y, glRot.z);
... draw object ....
gl.glPopMatrix();


But there seems to be a lot of math  involved in order to get the rotation out of a rotation matrix (at least the Bullet source for Matrix3x3.getRoatation() shows that:  getRotation Bullet src ) so i thought it might be better to stay with the glLoadMatrix version.  But for this i would need a way to keep my view transformation.

Also for my fundamental understanding it would be very beneficial if somebody please could explain to me why this glLoadMatrix overrides all my other transformations (even if a backup matrix via push/pop).

Cheers
    Andy

Reply | Threaded
Open this post in threaded view
|

Re: Viewing transformations cleared after glLoadMatrix

gouessej
Administrator
Hi!

Your other transformations modify the model-view matrix and glLoadMatrix replaces this matrix by the one you provide. I don't understand what is wrong in your case.

Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Viewing transformations cleared after glLoadMatrix

andys
puuhhh,

another 2 hours hard thinking and endless try and error code testing i found this fix for my glLoadMatrix() thing:

I shouldn't load the matrix via glLoadMatrix!

    gl.glLoadMatrixf(glMat, 0);   ---> draws objects o.k., but wipes out my view transformation even if i Push/Pop Modelview Matrix


Instead:
i should multiply current ModelView Matrix via glMultMatrix with it!

   gl.glMultMatrixf(glMat, 0);   ---> draws objects o.k. AND keeps my view transformation

Honestly: i have no idea why i need to multiply instead of just replacing the current ModelView matrix. I just tried all possible commands which manipulate the matrix - and this one did exactly what i need to keep my view transformation. The main thing what REALLY puzzles me is:  why the glLoadMatrix change cannot be undone via a Push/Pop?????

Still i do absolutely not understand why  my view transformation is gone if i
-) Push Matrix - i even pushed all Attributes via glPushAttrib(GL2.GL_ALL_ATTRIB_BITS)
-) replace the current modelview matrix via glLoadMatrix
-) draw the object
-) Pop Matrix (and all Attributes)

Maybe somebody has a basic explanation for me - could it be something inside JBullet  code which neutralizes any previous,post transformations? .

I have to admit that my level of Matrix understanding is still pretty basic. Getting normals via cross product is o.k. , but beyond that ... very feeble knowledge, but improving .

Cheers
   Andy
Reply | Threaded
Open this post in threaded view
|

Re: Viewing transformations cleared after glLoadMatrix

gouessej
Administrator
No, glLoadMatrix works as expected, you can undo the changes with glPushMatrix() and glPopMatrix() but JBullet or you changes the model-view matrix before calling glPushMatrix and this change is not used when you use glLoadMatrix unlike when you use glMultMatrix. Maybe use an engine that already supports JBullet and JOGL if you don't succeed in using them directly...
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Viewing transformations cleared after glLoadMatrix

andys
The hint with  "changes the model-view matrix" made me thinking again about my fundamental understanding of glRotate and glTranslate. So i looked  up again the API spec from OpenGL 2.1 .... and there it says:

glRotate — multiply the current matrix by a rotation matrix
glTranslate — multiply the current matrix by a translation matrix

So even those fundamental commands do actually multiplications .... somehow i understand it a bit more now ....