Draw A Torus With Normal opengl - Java javax.media.opengl

Java examples for javax.media.opengl:GL

Description

Draw A Torus With Normal opengl

Demo Code


import java.nio.IntBuffer;
import javax.media.opengl.GL2;
import static javax.media.opengl.GL2.*;
import javax.media.opengl.GL3;

public class Main{
    /**//from   w w w  .j  ava  2  s .c o m
     * Draw A Torus With Normals.
     */
    public static void torus(GL2 gl, float MinorRadius, float MajorRadius) {
        int i, j;
        gl.glBegin(GL_TRIANGLE_STRIP); // Start A Triangle Strip
        for (i = 0; i < 20; i++) // Stacks
        {
            for (j = -1; j < 20; j++) // Slices
            {
                float wrapFrac = (j % 20) / (float) 20;
                double phi = Math.PI * 2.0 * wrapFrac;
                float sinphi = (float) (Math.sin(phi));
                float cosphi = (float) (Math.cos(phi));

                float r = MajorRadius + MinorRadius * cosphi;

                gl.glNormal3d(
                        (Math.sin(Math.PI * 2.0 * (i % 20 + wrapFrac)
                                / (float) 20))
                                * cosphi,
                        sinphi,
                        (Math.cos(Math.PI * 2.0 * (i % 20 + wrapFrac)
                                / (float) 20))
                                * cosphi);
                gl.glVertex3d(
                        (Math.sin(Math.PI * 2.0 * (i % 20 + wrapFrac)
                                / (float) 20))
                                * r,
                        MinorRadius * sinphi,
                        (Math.cos(Math.PI * 2.0 * (i % 20 + wrapFrac)
                                / (float) 20))
                                * r);

                gl.glNormal3d(
                        (Math.sin(Math.PI * 2.0 * (i + 1 % 20 + wrapFrac)
                                / (float) 20))
                                * cosphi,
                        sinphi,
                        (Math.cos(Math.PI * 2.0 * (i + 1 % 20 + wrapFrac)
                                / (float) 20))
                                * cosphi);
                gl.glVertex3d(
                        (Math.sin(Math.PI * 2.0 * (i + 1 % 20 + wrapFrac)
                                / (float) 20))
                                * r,
                        MinorRadius * sinphi,
                        (Math.cos(Math.PI * 2.0 * (i + 1 % 20 + wrapFrac)
                                / (float) 20))
                                * r);
            }
        }
        gl.glEnd(); // Done Torus
    }
}

Related Tutorials