Hi
I have the following method that draws a cylinder using gluCylinder().
The GL quadric is oriented so that its axis is along the z-axis (0,0,1). Since my Cylinder object can be arbitrarily oriented it is necessary to correctly rotate the cylinder. This is done using the method directionAngles(), which computes the direction angles between the rotated cylinder z-axis and the fixed global axes (1,0,0), (0,1,0) and (0,0,1).
For cylinder axes (1,0,0) and (0,1,0) the cylinder is correctly oriented. However, if I set the axis to lie along th z-axis (0,0,1) then it incorrectly points in the x-direction.
Can anyone see what I’m doing wrong?
Thanks
Graham
public void toOpenGL(final javax.media.opengl.GLAutoDrawable gLDrawable)
{
javax.media.opengl.GL gl = gLDrawable.getGL();
javax.media.opengl.glu.GLU glu = new javax.media.opengl.glu.GLU();
// cylinder base circle 1 centre and axis
Point3D base1Centre = getEndCircle1Centre();
Vector3D cylinderAxis = getAxisVector();
double baseCircle1Radius = getEndCircle1Radius();
double baseCircle2Radius = getEndCircle2Radius();
double axisLength = axisLength();
Double alpha = new Double();
Double beta = new Double();
Double gamma = new Double();
cylinderAxis.directionAngles(alpha,beta,gamma,true);
gl.glTranslated(base1Centre.getX(),base1Centre.getY(),base1Centre.getZ()); // translate wrt base circle 1
gl.glRotated(alpha.getValue(),1.0,0.0,0.0); // rotate the cylinder on the x axis
gl.glRotated(beta.getValue(),0.0,1.0,0.0); // rotate the cylinder on the y axis
gl.glRotated(gamma.getValue(),0.0,0.0,1.0); // rotate the cylinder on the z axis
javax.media.opengl.glu.GLUquadric quadric = glu.gluNewQuadric(); // create a pointer to the quadric object
glu.gluQuadricNormals(quadric,javax.media.opengl.glu.GLU.GLU_SMOOTH); // create smooth normals
glu.gluQuadricTexture(quadric,true); // Create Texture Coords
glu.gluQuadricOrientation(quadric,javax.media.opengl.glu.GLU.GLU_OUTSIDE); // normals acting outwards
if (joglObject.getIsWireframe())
glu.gluQuadricDrawStyle(quadric,javax.media.opengl.glu.GLU.GLU_LINE);
else
glu.gluQuadricDrawStyle(quadric,javax.media.opengl.glu.GLU.GLU_FILL);
glu.gluCylinder(quadric,baseCircle1Radius,baseCircle2Radius,axisLength,25,15); // quadric - base1 radius - base2 radius - axis length - circ div - axis div
glu.gluDeleteQuadric(quadric); // free up quadric memory
}
public void directionAngles(Double alpha, Double beta, Double gamma, final boolean degrees)
{
double n = norm();
if (Functions.abs(n) > Tolerances.TOLERANCE)
{
if (!degrees)
{
alpha.setValue(Functions.acos(ma/n));
beta.setValue(Functions.acos(mb/n));
gamma.setValue(Functions.acos(mc/n));
}
else
{
alpha.setValue(Functions.radiansToDegrees(Functions.acos(ma/n)));
beta.setValue(Functions.radiansToDegrees(Functions.acos(mb/n)));
gamma.setValue(Functions.radiansToDegrees(Functions.acos(mc/n)));
}
}
}