Sign in to follow this  
cs02rm0

3D Rotating cube

Recommended Posts

cs02rm0    122
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);
	glLoadIdentity();
	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 this post


Link to post
Share on other sites
cs02rm0    122
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 this post


Link to post
Share on other sites
CyberSlag5k    514
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 this post


Link to post
Share on other sites
cs02rm0    122
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 this post


Link to post
Share on other sites
Kalidor    1087
Quote:
Original post by cs02rm0
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?


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


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this