# OpenGL glRotate doesn't work for me

This topic is 4411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm new to OpenGL and have a problem with rotating, hope someone can help .. I want to draw four cylinders in a circle in a plane perpendicular to a fifth. Each of the four cylinders is under a 90 degree angle with its neighbours, and should be under a 45 degree angle with the floor. I can draw them in the circle, but two won't go in the 45 degree angle with the floor. It ends up like this: So the right and left cylinder should be under a 45 degree angle with the floor just like the front and the back cylinder. I have this code to draw the cylinder: void SceneDrawer::drawBranch(double startX, double startY, double startZ, double angleX, double angleY, double angleZ) { GLUquadricObj *branch; branch = gluNewQuadric(); gluQuadricDrawstyle(branch, GLU_FILL); glTranslatef(startX, startY, startZ); glRotatef(angleX, 1.0, 0.0, 0.0); glRotatef(angleY, 0.0, 1.0, 0.0); glRotatef(angleZ, 0.0, 0.0, 1.0); gluCylinder(branch, 0.6, 0.4, 3, 26, 14); glRotatef(-angleZ, 0.0, 0.0, 1.0); glRotatef(-angleY, 0.0, 1.0, 0.0); glRotatef(-angleX, 1.0, 0.0, 0.0); glTranslatef(-startX, -startY, -startZ); } that is called with this code: drawBranch(0.6, 5.0, 0.0, -45.0, 90.0, 0.0); // right cylinder drawBranch(0.0, 5.0, -0.6, 45.0, 180.0, 0.0); // back cylinder drawBranch(-0.6, 5.0, 0.0, 45.0, 270.0, 0.0); // left cylinder drawBranch(0.0, 5.0, 0.6, -45.0, 0.0, 0.0); // front cylinder Positive z-direction is facing you, positive x is to the right, and positive y is directed upwards. So the ones that are going wrong are the right and left cylinder. The way I see it, the cylinder gets turned 90 degrees around the y-axis; that happens. It also needs to rotate 45 degrees around the x-axis to get an angle with the ground, but that doesn't happen. What am I doing wrong?

##### Share on other sites
PROtip:
glPush() and glPop() are your friends

##### Share on other sites
You mean instead of rotating and translating back at the end of drawBranch(..)? Yes, you're right, I use that now :) But it doesn't solve my problem, unfortunately ..

##### Share on other sites
Try pushing the model view matrix and loading an identity matrix at the beginning of each call, and end each call with popping the matrix. If there's still visual problems after that, the problem must be with your calculations or order of operations.

glMatrixMode(GL_MODELVIEW);//...//in your draw call:glPushMatrix();glLoadIdentity();// ... draw a cylinderglPopMatrix();

##### Share on other sites
uhh,
try messing with the order those rotations are applied

translate
zrotation -controls what compass direction it faces
xrotation -always +-45
yrotation - shoudl be blank anyways

that assumes that z is up for your world
and that the cylinders are aligned with z upon creation

##### Share on other sites
Well, I can't say I fully understand .. but doing rotation around Z-axis first, then Y, then Z (instead of X, Y, Z) fixes my problem! Tnx! :)

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633724
• Total Posts
3013556
×