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

Java examples for javax.media.opengl:Draw

Description

opengl Method to draw a sphere in OpenGL.

Demo Code


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

public class Main{
    public static void drawSphere(GL gl, Point3d c, double r, int n) {
        int i, j;
        double theta1, theta2, theta3;
        Point3d e = new Point3d();
        Point3d p = new Point3d();

        if (c == null) {
            c = new Point3d(0, 0, 0);
        }//from w ww  .j  ava2 s .  c o m

        double twoPi = Math.PI * 2;
        double piD2 = Math.PI / 2;
        if (r < 0)
            r = -r;
        if (n < 0)
            n = -n;
        if (n < 4 || r <= 0) {
            gl.glBegin(GL.GL_POINTS);
            gl.glVertex3d(c.x, c.y, c.z);
            gl.glEnd();
            return;
        }

        for (j = 0; j < n / 2; j++) {
            theta1 = j * twoPi / n - piD2;
            theta2 = (j + 1) * twoPi / n - piD2;

            gl.glBegin(GL.GL_QUAD_STRIP);
            //            gl.glBegin(GL.GL_TRIANGLE_STRIP);
            for (i = 0; i <= n; i++) {
                theta3 = i * twoPi / n;

                e.x = Math.cos(theta2) * Math.cos(theta3);
                e.y = Math.sin(theta2);
                e.z = Math.cos(theta2) * Math.sin(theta3);
                p.x = c.x + r * e.x;
                p.y = c.y + r * e.y;
                p.z = c.z + r * e.z;

                gl.glNormal3d(e.x, e.y, e.z);
                gl.glTexCoord2d(i / (double) n, 2 * (j + 1) / (double) n);
                gl.glVertex3d(p.x, p.y, p.z);

                e.x = Math.cos(theta1) * Math.cos(theta3);
                e.y = Math.sin(theta1);
                e.z = Math.cos(theta1) * Math.sin(theta3);
                p.x = c.x + r * e.x;
                p.y = c.y + r * e.y;
                p.z = c.z + r * e.z;

                gl.glNormal3d(e.x, e.y, e.z);
                gl.glTexCoord2d(i / (double) n, 2 * j / (double) n);
                gl.glVertex3d(p.x, p.y, p.z);
            }
            gl.glEnd();
        }
    }
    public static void drawSphere(GL gl, Point3d c, double r, int n,
            int method, double theta1, double theta2, double phi1,
            double phi2) {
        int i, j;
        double t1, t2, t3;
        Point3d e = new Point3d();
        Point3d p = new Point3d();

        if (c == null) {
            c = new Point3d(0, 0, 0);
        }

        /* Handle special cases */
        if (r < 0)
            r = -r;
        if (n < 0)
            n = -n;
        if (n < 4 || r <= 0) {
            gl.glBegin(GL.GL_POINTS);
            gl.glVertex3d(c.x, c.y, c.z);
            gl.glEnd();
            return;
        }

        for (j = 0; j < n / 2; j++) {
            t1 = phi1 + j * (phi2 - phi1) / (n / 2);
            t2 = phi1 + (j + 1) * (phi2 - phi1) / (n / 2);

            if (method == 0)
                gl.glBegin(GL.GL_QUAD_STRIP);
            else
                gl.glBegin(GL.GL_TRIANGLE_STRIP);

            for (i = 0; i <= n; i++) {
                t3 = theta1 + i * (theta2 - theta1) / n;

                e.x = Math.cos(t1) * Math.cos(t3);
                e.y = Math.sin(t1);
                e.z = Math.cos(t1) * Math.sin(t3);
                p.x = c.x + r * e.x;
                p.y = c.y + r * e.y;
                p.z = c.z + r * e.z;
                gl.glNormal3d(e.x, e.y, e.z);
                gl.glTexCoord2d(i / (double) n, 2 * j / (double) n);
                gl.glVertex3d(p.x, p.y, p.z);

                e.x = Math.cos(t2) * Math.cos(t3);
                e.y = Math.sin(t2);
                e.z = Math.cos(t2) * Math.sin(t3);
                p.x = c.x + r * e.x;
                p.y = c.y + r * e.y;
                p.z = c.z + r * e.z;
                gl.glNormal3d(e.x, e.y, e.z);
                gl.glTexCoord2d(i / (double) n, 2 * (j + 1) / (double) n);
                gl.glVertex3d(p.x, p.y, p.z);

            }
            gl.glEnd();
        }
    }
    /**
     * 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
     *            number of sub-divisions along the latitude
     * @param longs
     *            number of sub-divisions along the longitude
     */
    static public void drawSphere(GL gl, double radius, int lats, int longs) {
        /*
         * This algorithm moves along the z-axis, for PI radians and then at
         * each point rotates around the z-axis drawing a QUAD_STRIP.
         * 
         * If you you start i at a non-zero value then you will see an open end
         * along the z-axis. If you start j at a none zero-axis then you will
         * see segements taken out.
         */

        for (int i = 0; i <= lats; i++) {
            double lat0 = Math.PI * (-0.5 + (double) (i - 1) / lats);
            double z0 = Math.sin(lat0) * radius;
            double zr0 = Math.cos(lat0) * radius;

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

            gl.glBegin(GL.GL_QUAD_STRIP);
            for (int j = 0; j <= longs; j++) {
                float pc1 = j / longs;
                float pc2 = j + 1 / longs;

                double lng = 2 * Math.PI * (double) (j - 1) / longs;
                double x = Math.cos(lng) * radius;
                double y = Math.sin(lng) * radius;

                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