Rotation problem

In java2D you have to call affinetransform after rotation to stop rotation to affect other objects what I don’t want to rotate. What do I have to do in jogl to do same thing? ???

The simplest answer is that you need to reset the rotation to the identity matrix if I’m understanding you correctly. In openGL there are various ways in which you can control the overall transform, the most useful (IMO) the matrix stack.

There are 3 matrix stacks in opengl, 1 for projection matrices (3d to 2d space), 1 for transforming texture coordinates, and 1 for transforming 3d points (this is called the modelview matrix).

Only 1 matrix stack is active at one time. Any call to glLoadIdentity, glLoadMatrix, glMultMatrix, glTranslate, glRotate, gluLookAt, etc. modify the top of the matrix stack. A call to glPushMatrix() pushes a copy of the top matrix onto the stack (ie calls to glPushMatrix() don’t change the applied transform matrix). A call to glPopMatrix() replaces the top matrix with the matrix saved just before it.

So here is a simple example (uses a GL called gl, and a GLU called glu):


// setup the projection matrix
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(fov, aspect, znear, zfar);

gl.glMatrixMode(GL.GL_MODELVIEW);
// setup the view portion of the modelview matrix
gl.glLoadIdentity();
glu.gluLookAt(...);
gl.glPushMatrix(); // save the view matrix, since we want that constant for every model

gl.glRotate(...); // 1st model transform
gl.glBegin(..);
// draw stuff here
gl.glEnd();

gl.glPopMatrix(); // reset the modelview matrix back to the matrix state at last glPushMatrix call (e.g. setup by glu.gluLookAt(...))

// render more stuff, without the rotation.

HTH

Thank you very much!

I am going to learn this thing because it seems to be one of the most important things to learn in opengl (I’we seen many demos where this is used but I didn’t get why it has been used). :slight_smile:

Thanks again! :slight_smile:

Yes, it helped. I’ve been coding one jogl program for a few days now and I did it without matrix stack and now I tried to do same thing with matrix stack. I made it work both ways and the amount of code was about the same but the program was about 5 percent faster when using matrix stack. this was probably due to that I didn’t have to call some methods like glu.gluLookAt() for every object.

thx

You have to call gluLookAt only once when the model-view matrix is loaded.

Thanks for information.