glut primitive object scaling

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

glut primitive object scaling

bcothren
I'm seeing some strange, and not always consistent behavior when it comes to the scaling of primitive objects within the GLUT package.  For example, take the three primitives Cylinder, Sphere, and Cube.  When you scale the Sphere or Cube in any of the 3 axes, you get what you'd expect to see (the object gets [scaleFactor] size bigger in that axis).  However, the Cylinder doesn't have this behavior - a scaling in the Z-axis does not behave like the X and Y axis scaling.  When you scale a cylinder in the X axis, it grows in both the +X and -X, and same for the Y axis.  The Z axis, however, doesn't do this.  A 2x scale in the Z axis will cause the cylinder to grow in only the +Z direction.  Is this the correct behavior?

Calling the following method from display() will demonstrate what I'm seeing:

    private void drawObjects(GL2 gl){
    gl.glPushMatrix();
   
    boolean drawSpheres = true;
    boolean drawCylinders = false;
    boolean drawCubes = false;
   
                double scaleFactor = 2;
       
                // non-scaled (white)
                GLUT glut = new GLUT();
                gl.glColor3f(1.0f, 1.0f, 1.0f);
                if(drawCylinders){
                        glut.glutSolidCylinder(50, 50, 20, 1);
                }else if(drawSpheres){
                        glut.glutSolidSphere(25, 25, 25);
                }else if(drawCubes){
                        glut.glutSolidCube(50);
                }
               
                // scaled in z (red)
                gl.glColor3f(1.0f, 0.0f, 0.0f);
                gl.glScaled(1, 1, scaleFactor);
                gl.glTranslated(125, 0, 0);
                if(drawCylinders){
                        glut.glutSolidCylinder(50, 50, 20, 1);
                }else if(drawSpheres){
                        glut.glutSolidSphere(25, 25, 25);
                }else if(drawCubes){
                        glut.glutSolidCube(50);
                }
                gl.glPopMatrix();
               
                // scaled in x (green)
                gl.glPushMatrix();
                gl.glColor3f(0.0f, 1.0f, 0.0f);
                gl.glScaled(scaleFactor, 1, 1);
                gl.glTranslated(0, 0, -125);
                if(drawCylinders){
                        glut.glutSolidCylinder(50, 50, 20, 1);
                }else if(drawSpheres){
                        glut.glutSolidSphere(25, 25, 25);
                }else if(drawCubes){
                        glut.glutSolidCube(50);
                }
                gl.glPopMatrix();
               
                // scaled in y (blue)
                gl.glPushMatrix();
                gl.glColor3f(0.0f, 0.0f, 1.0f);
                gl.glScaled(1, scaleFactor, 1);
                gl.glTranslated(-125, 0, 0);
                if(drawCylinders){
                        glut.glutSolidCylinder(50, 50, 20, 1);
                }else if(drawSpheres){
                        glut.glutSolidSphere(25, 25, 25);
                }else if(drawCubes){
                        glut.glutSolidCube(50);
                }
                gl.glPopMatrix();
    }

Thanks,

bcothren
Reply | Threaded
Open this post in threaded view
|

Re: glut primitive object scaling

Wade Walker
Administrator
This is because the cylinder isn't centered lengthwise on the z axis. The base of the cylinder sits at z = 0, and the top at z = height, so any growth will be in the positive z direction.
Reply | Threaded
Open this post in threaded view
|

Re: glut primitive object scaling

bcothren
so you're saying that if the cylinder, base to height, was -25 to 25, the growth would be in both directions?
Reply | Threaded
Open this post in threaded view
|

Re: glut primitive object scaling

Wade Walker
Administrator
Yes. When you scale, you're just multiplying the coordinates by the scaling factor. So for the z coordinate of the base, you're doing Znew = Zold * scale = 0 * scale = 0  For the other end it works fine, because Znew = Zold * scale = height * scale.
Reply | Threaded
Open this post in threaded view
|

Re: glut primitive object scaling

bcothren
So why doesn't this seem to happen for spheres and cubes?
Reply | Threaded
Open this post in threaded view
|

Re: glut primitive object scaling

Wade Walker
Administrator
The sphere and cube are centered at (0, 0, 0), so scaling them will grow them in all directions. You can get this same effect for the cylinder by translating its center to the origin (translate the whole thing by -height/2 in the z direction). After this translation, scaling it will work like it does for the sphere and cube.

Reply | Threaded
Open this post in threaded view
|

Re: glut primitive object scaling

bcothren
Ok, got it.  Thanks Wade for your help.