Cylinder rotation

I’m attempting to translate a cylinder so that its center is at the origin, and then make it rotate. However, the origin appears to be
moving with the translation so that the leftmost end of the cylinder is always at the origin. How can I rotate the cylinder with its center at the origin? Is it possible to rotate it within its own local coordinate space (i.e. its center already at local origin)?

The code I’m using is:

  

Gl.glPushMatrix();

Gl.glLoadIdentity();

//translate center of cylinder to origin
Gl.glTranslatef(-1.25f, 0.0f, 0.0f);
Gl.glRotatef(a, 0.0f, 1.0f, 0.0f);

Glu.gluQuadricNormals(q1, Glu.GLU_SMOOTH);
Glu.gluQuadricDrawStyle(q1, Glu.GLU_FILL);
Glu.gluQuadricOrientation(q1, Glu.GLU_INSIDE);
Glu.gluQuadricTexture(q1, Gl.GL_TRUE);

Gl.glTranslatef(0.0f, 0.0f, 0.0f);
Glu.gluCylinder(q1, 0.6, 0.6, 2.5, 25, 25);
   
Gl.glPopMatrix();

Gl.glFlush(); 

Thanks for any suggestions

By default, the cylinder is oriented along the z-axis. I just switched the x and z components in your first Tranlatef() call, and removed the 2nd (superfluous) Translatef() call. I also named all the constants for easy reading.

float baseRadius = 0.6;
float topRadius  = 0.6;
float height     = 2.5;
int   slices     = 25;
int   stacks     = 25;
float a          = ???;
   
Gl.glPushMatrix();
  
// Are you sure you want an identity here?
// Try it without this. If the camera is at the
// top of the stack, you'll want to remove 
// this line.
Gl.glLoadIdentity();
  
// Rotate at origin 2nd.
Gl.glRotatef(a, 0.0f, 1.0f, 0.0f);
 
// Translate to origin 1st.
Gl.glTranslatef(0, 0, -height/2);
 
Glu.gluQuadricNormals(q1, Glu.GLU_SMOOTH);
Glu.gluQuadricDrawStyle(q1, Glu.GLU_FILL);
Glu.gluQuadricOrientation(q1, Glu.GLU_INSIDE);
Glu.gluQuadricTexture(q1, Gl.GL_TRUE);
Glu.gluCylinder(q1, baseRadius, topRadius, height, slices, stacks);   
Gl.glPopMatrix();
Gl.glFlush(); 

edit:

Changed wording.

Thanks, that worked a treat.

When you say the cylinder is oriented in the z-axis, how do I set it up to be oriented in the x or y axis? (This is all pretty new to me!)

Cheers for the help

There’s some info on quadrics here:

http://www.parallab.uib.no/SGI_bookshelv…html#id5518152

To orient quadrics, or anything else for that matter, just use the basic transformations. glRotate*(), glTranslate*(), and glScale*() can be used to achieve any orientation you like.

For example, to orient the cylinder along the x-axis, you might do this:

...
glRotatef( 90, 0, 1, 0 );
glTranslatef( 0, 0, -height/2 );
...

Or, to orient along the y-axis, you might do this

...
glRotatef( 90, 1, 0, 0 );
glTranslatef( 0, 0, -height/2 );
...

All this could be in addition to any other transforms you might want. The important thing is to understand that the order of transformations is very important.

You may find this chapter of the Red Book helpful:

http://fly.cc.fer.hr/~unreal/theredbook/chapter03.html

Thanks for all the info, it’s making sense now