opengl Draw a torus, using a helix as the guiding point. - Java javax.media.opengl

Java examples for javax.media.opengl:Draw

Description

opengl Draw a torus, using a helix as the guiding point.

Demo Code


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

public class Main{
    /**//from  w w  w  . j a va2  s .  c om
     * Draw a torus, using a helix as the guiding point. This uses
     * GL_QUAD_STRIP, numWraps=20 and numPerWrap=8
     * 
     * @see OpenGlUtils#drawTorus(GL, double, double, int, int, boolean)
     * 
     * @param gl
     * @param majorRadius
     *            the major radius
     * @param minorRadius
     *            the minor radius
     */
    public static void drawTorus(GL gl, double majorRadius,
            double minorRadius) {
        boolean quadMode = true;

        int numWraps = 20;
        int numPerWrap = 8;

        drawTorus(gl, majorRadius, minorRadius, numWraps, numPerWrap,
                quadMode);
    }
    /**
     * Draw a torus, using a helix as the guiding point.
     * 
     * @param gl
     * @param majorRadius
     *            the major radius
     * @param minorRadius
     *            the minor radius
     * @param numWraps
     *            frequency of rotation
     * @param numPerWrap
     *            segments in 'ribbon'
     * @param quadMode
     *            whether to use GL_QUAD_STRIP (true) or GL_TRIANGLE_STRIP
     *            (false)
     */
    public static void drawTorus(GL gl, double majorRadius,
            double minorRadius, int numWraps, int numPerWrap,
            boolean quadMode) {

        if (quadMode) {
            gl.glBegin(GL.GL_QUAD_STRIP);
        } else {
            gl.glBegin(GL.GL_TRIANGLE_STRIP);
        }

        for (int i = 0; i < numWraps; i++) {
            for (int j = 0; j < numPerWrap; j++) {
                putVertex(gl, majorRadius, minorRadius, numWraps,
                        numPerWrap, i, j);
                putVertex(gl, majorRadius, minorRadius, numWraps,
                        numPerWrap, (i + 1), j);
            }
        }
        putVertex(gl, majorRadius, minorRadius, numWraps, numPerWrap, 0.0,
                0.0);
        putVertex(gl, majorRadius, minorRadius, numWraps, numPerWrap, 1.0,
                0.0);
        gl.glEnd();

    }
    /**
     * Issue vertex command for segment number j of wrap number i
     */
    private static void putVertex(GL gl, double majorRadius,
            double minorRadius, int numWraps, int numPerWrap, double i,
            double j) {
        double wrapFrac = j / (float) numPerWrap;
        double phi = Math.PI * 2.0 * wrapFrac;
        double theta = Math.PI * 2.0 * (i + wrapFrac) / (double) numWraps;

        double sinphi = Math.sin(phi);
        double cosphi = Math.cos(phi);
        double sintheta = Math.sin(theta);
        double costheta = Math.cos(theta);

        float y = (float) (minorRadius * (float) Math.sin(phi));
        double r = majorRadius + minorRadius * Math.cos(phi);
        float x = (float) (Math.sin(theta) * r);
        float z = (float) (Math.cos(theta) * r);

        gl.glNormal3d(sintheta * cosphi, sinphi, costheta * cosphi);
        gl.glVertex3f(x, y, z);

    }
}

Related Tutorials