# 3D Rotating cube

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

## Recommended Posts

I'm struggling to get this code to rotate a cube. It uses Glut so it works on different platforms. It's difficult to describe the effect I'm getting - the cube sort of gently begins to rotate one way before moving back the other way and back again. I've tried doing it by rotating the object and by changing the viewpoint and I've ended up with the same effect each way. If I don't add the +5 into this method:
void rotateY(float ang)
{
x = cos(ang);
z = sin(ang);
gluLookAt(x, y, z+5,  lx, ly, lz, 0.0f,1.0f,0.0f);
}

Then I _think_ it rotates correctly, but I want the viewpoint further back so I can see the cube. Anyway, I'd appreciate any help, thanks :)

##### Share on other sites
Ah. I was moving the center point of the eye's circle 5 away from the center of the cube. What I should've done was increase the radius by 5. That did it :)

##### Share on other sites
I'm not sure what effect you're going for, but simple rotations are easily achieved with glRotate3f. You shouldn't have to reset the viewpoint to rotate one object. Can you think of how crazy it would be to manage a scene with 10 objects?

If your object seems to be rotating about one of the corners, it's because the rotations take place at the origin, so if you've drawn the cube starting from the origin and going out to one side, rotating about the origin would spin the cube about the side on the origin. The solution is to draw the cube centered on the origin.

Anyway, if you're doing what you're doing for a specific reason, then go for it, but I'd use glRotate3f if I were you.

Hope this helps!

##### Share on other sites
I've tried rotating without much luck. As it happens, it doesn't matter with the final object I want whether I rotate the view or the object, but I'd like to know how to do it anyway - how do you tell OpenGL which object you want to rotate?

##### Share on other sites
Quote:
 Original post by cs02rm0I've tried rotating without much luck. As it happens, it doesn't matter with the final object I want whether I rotate the view or the object, but I'd like to know how to do it anyway - how do you tell OpenGL which object you want to rotate?

Call glRotatef() before you render it.
glPushMatrix();glRotatef(angle, x, y, z);//Draw object hereglPopMatrix();

EDIT: I suggest you (re-)read the Red Book, specifically chapter 3. Check the forum faq for a link to the online version if you don't have it.