OpenGL rotation/translation tutorials/help - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# OpenGL rotation/translation tutorials/help

Posts: 557Member
Hi.

I'm having some trouble using the glRotate and glTranslate in the proper way. Are there any devent turorials?

My problem can be stated as follows:

I have a object that will have to be rotated around itself before being put somewhere in the "landscape", which is also translated and rotated.

The object is placed at location Xo,Yo,Zo and must be rotated with Thetao and Phio.
The landscape is also moved and rotated (or the camera is moved...) using X,Y,Z,Phi,and Theta...
I have tried:

- creating the object at 0,0,0, rotating, translating it, and then the landscape. Seemingly, the object is not rotated with the landscape at all.
- placing the object at Xo,Yo,Zo and all the same. Seemingly, the object is not only rotated around itself, but also some other point...

The main problem is probably that I haven't got a clue how to use the matrix functions - seemingly, no "Add" function for matrixes in OpenGL exists? Should I study some more 3D maths?? :-)

The code for rotation the landscape, and then the object:
[code]
glTranslatef(0.f,0.f,(float)-nZDist);
glRotated(dTheta,1.,0.,0.);
glRotated(dPhi,0.,0.,1.);
glTranslatef((float)nX,(float)nY,float(nZ));
glCallList(1);
// Object...
glTranslatef(fOX,fOY,fOZ);
glRotatef(Thetao,1.f,0.f,0.f);
glRotatef(Phio,0.f,0.f,1.f);
glTranslatef(-fOX,-fOY,-fOZ);
glTranslatef(0.f,0.f,(float)-nZDist); // Same as above
glRotated(dTheta,1.,0.,0.);
glRotated(dPhi,0.,0.,1.);
glTranslatef((float)nX,(float)nY,float(nZ));
glCallList(2);
[/code]

As said, I'd be more than happy if someone could just point me to a good turorial regarding this.

Rune

• Posts: 557Member
Once again, I was too fast for you... The code was right, but had to be called in the reversed order. Seems strange to me, but works )
[code]
glTranslatef(0.f,0.f,(float)-nZDist);
glRotated(dTheta,1.,0.,0.);
glRotated(dPhi,0.,0.,1.);
glTranslatef((float)nX,(float)nY,float(nZ));
glTranslatef(fX,fY,fZ); // Moved this to AFTER the other stuff..
glRotatef(pPlayers[nPlayer].GetTheta(),1.f,0.f,0.f);
glRotatef(pPlayers[nPlayer].GetPhi(),0.f,0.f,1.f);
glTranslatef(-fX,-fY,-fZ);
glCallList(2);
[/code]
• Posts: 642Member
Think in the terms local coordinate system.
The opengl red book points out this specific mistake while explaining about the coordinate system in opengl. Maybe you should look at the red books explanation.

: Once again, I was too fast for you... The code was right, but had to be called in the reversed order. Seems strange to me, but works )
: [code]
: glTranslatef(0.f,0.f,(float)-nZDist);
: glRotated(dTheta,1.,0.,0.);
: glRotated(dPhi,0.,0.,1.);
: glTranslatef((float)nX,(float)nY,float(nZ));
: glTranslatef(fX,fY,fZ); // Moved this to AFTER the other stuff..
: glRotatef(pPlayers[nPlayer].GetTheta(),1.f,0.f,0.f);
: glRotatef(pPlayers[nPlayer].GetPhi(),0.f,0.f,1.f);
: glTranslatef(-fX,-fY,-fZ);
: glCallList(2);
: [/code]
: