I am trying to rotate shapes in opengl. But I want to rotate them on there position so that they just look like they are spinning instead of rotating around a point.

You are currently rotating the object around the x axis. You need to rotate it around the center of the object. So instead of
glRotatef ((GLfloat) weaponangle, 1.0, 0.0, 0.0);
You need to use
glRotatef ((GLfloat) weaponangle, x, y, z);
where x,y, and z are the x,y,and z coordinates of the center of the sphere however it was defined.

0

tango2009Author Commented:

The things that I want to rotate around are a sphere is that drawn with glutSolidSphere like below

glutSolidSphere (1.0, 20, 16);

and a torus like this

glutSolidTorus (0.275, 0.85, 16, 16);

how do I know the coordinates of the the center of these objects.

When you create the objects, they are centered at the modeling coordinates. If it's currently rotating around a 'corner' of the object, then use radius/2 for each coordinate (so 1/2 for the sphere and .85/2 for the torus).

0

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

>> But I want to rotate them on there position so that they just look like they are spinning instead of rotating around a point.

Your code looks good (except the indention ;)
If you want to rotate the object around its center at a given position, you have to 1st translate (glTranslatef) the object to that position and then rotate it (glRotatef)

glPushMatrix();
// put the object at its position
glTranslatef(pos[0], pos[1], pos[2]);
// rotate the object inplace around the x-axis
glRotatef(angle, 1.0f, 0.0f, 0.0f);
glutWireSphere (1.0, 20, 16);
glPopMatrix();

If your code does not work, then most likely you make wrong translations/rotations in the DrawWeapon-function, try the aboce instead.

That's it, if you have further questions feel free to ask :)

Assuming that your viewpoint is at position 0,0,0 and looking in the direction of the x-axis, you only want to move the model that you are rendering. The transformation functions (glTranslate, glScale and glRotate) affect the axis system used to render the model, not the model itself, although in this case it is the same.

Rotate always rotates around the origin, scale scales things toward/away from the origin and translate moves the world so that the origin is in a new place relative to it.

This means that you should first rotate, then translate, if you want to rotate around the center of the object.

glMatrixMode(GL_MODELVIEW);glPushMatrix();glRotatef(angle, 1.0f, 0.0f, 0.0f); // Rotate angle degrees around x-axisglTranslatef(posX, posY, posZ); // Put the object where it needs to beDrawWeapon(); // Draw the object around position 0,0,0; the transformations will automatically put it where it belongs.glPopMatrix();

>> This means that you should first rotate, then translate, if you want to rotate around the center of the object.

Hey StormSeed, this is true for DirectX but not for OpenGL, because DirectX uses row-major-matrices with pre-multiplying, but OpenGL uses the opposite: column-major-matrices with post-multiplying, so all matrix transformation (multiplications) in OpenGL are excactly in the opposite order than in DirectX.

"Note that post-multiplying with column-major matrices produces the same result as pre-multiplying with row-major matrices"

For DirectX there is the SRT-Rule to rotate an object around its center at a given position (and additionally scale it around its center):

DirectX: SRT -> Scale - Rotate - Translate

for OpenGL its the exact opposite order:

OpenGL : TRS -> Translate - Rotate - Scale

as shown in my previous post above, so one must in fact first translate, then rotate in OpenGL in order to have the object rotating around its center at a given position.

I'm pretty sure that tango2009's problem is that he does not use the matrix for translation, while still using the matrix for rotation, causing the order of the two transformations to be incorrect.

Translate followed by rotate (as you suggest) will work for transforming the object, as I can see from one of my own games' source code.

However, for moving the camera, the opposite order should be used, since we are actually moving the world.

0

Featured Post

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

glRotatef ((GLfloat) weaponangle, 1.0, 0.0, 0.0);

You need to use

glRotatef ((GLfloat) weaponangle, x, y, z);

where x,y, and z are the x,y,and z coordinates of the center of the sphere however it was defined.