opengl to draw a sphere in OpenGL. - Java javax.media.opengl

Java examples for javax.media.opengl:Draw

Description

opengl to draw a sphere in OpenGL.

Demo Code


import java.awt.Color;
import javax.media.opengl.GL;
import javax.vecmath.Point3d;

public class Main{
    /**//from  w w w  . j a v  a  2  s.  c  o  m
     * Method to draw a sphere in OpenGL.
     * 
     * Source taken from: http://ozark.hendrix.edu/~burch/cs/490/sched/feb8/
     * 
     * @param gl
     * @param radius
     * @param lats
     * @param longs
     */
    static public void drawEllipsoid(GL gl, double radiusH, double radiusV,
            float lats, float longs) {
        for (int i = 0; i <= lats; i++) {
            double lat0 = Math.PI * (-0.5 + (double) (i - 1) / lats);
            double z0 = Math.sin(lat0) * radiusH;
            double zr0 = Math.cos(lat0) * radiusV;

            double lat1 = Math.PI * (-0.5 + (double) i / lats);
            double z1 = Math.sin(lat1) * radiusH;
            double zr1 = Math.cos(lat1) * radiusV;

            gl.glBegin(GL.GL_QUAD_STRIP);
            for (int j = 0; j <= longs; j++) {
                double lng = 2 * Math.PI * (double) (j - 1) / longs;
                double x = Math.cos(lng) * radiusH;
                double y = Math.sin(lng) * radiusV;

                gl.glNormal3d(x * zr0, y * zr0, z0);
                gl.glVertex3d(x * zr0, y * zr0, z0);
                gl.glNormal3d(x * zr1, y * zr1, z1);
                gl.glVertex3d(x * zr1, y * zr1, z1);
            }
            gl.glEnd();
        }
    }
}

Related Tutorials