Given a quad as a series of points in counter-clockwise order, put glVertex3f commands that form that quad and also include glNormal3f commands automatically. - Java java.lang

Java examples for java.lang:Math Curve

Description

Given a quad as a series of points in counter-clockwise order, put glVertex3f commands that form that quad and also include glNormal3f commands automatically.

Demo Code


import java.awt.Color;
import javax.vecmath.Vector3f;

public class Main{
    /**//from   w  w  w . ja va 2s  .  co  m
     * A vector that may be used in methods in this class. Spares object
     * allocations for frequent calls.
     */
    private static Vector3f d1 = new Vector3f();
    /**
     * A vector that may be used in methods in this class. Spares object
     * allocations for frequent calls.
     */
    private static Vector3f d2 = new Vector3f();
    /**
     * A vector that may be used in methods in this class. Spares object
     * allocations for frequent calls.
     */
    private static Vector3f normal = new Vector3f();
    /**
     * Given a quad as a series of points in counter-clockwise order, put
     * glVertex3f commands that form that quad and also include glNormal3f
     * commands automatically.
     * 
     * <p>The normal is being calculated as follows: consider the points as
     * follows: <code>v1, v2, v3, v4</code>. We now first calculate
     * <code>d1 = v2 - v1</code> and <code>d2 = v4 - v1</code>. The normal is
     * now calculated as follows: <code>n = d1 x d2</code>.
     * 
     * @param quad The quad to put, given as a series of 4 points in counter-
     *             clockwise order.
     * @throws IllegalArgumentException In case {@code quad == null} or when
     *             {@code quad.length != 4}.
     */
    public static void drawQuadAndNormals(Vector3f[] quad) {
        if (quad == null || quad.length != 4) {
            throw new IllegalArgumentException(
                    "Cannot draw a quad that does "
                            + "not have exactly 4 points.");
        }

        d1.sub(quad[1], quad[0]);
        d2.sub(quad[3], quad[0]);
        normal.cross(d1, d2);
        normal.normalize();

        glNormal3f(normal);
        for (int i = 0; i < 4; i++) {
            glVertex3f(quad[i]);
        }
    }
    /**
     * Given a quad as a series of points in counter-clockwise order, put
     * glVertex3f commands that form that quad and also include glNormal3f
     * commands automatically.
     * 
     * <p>The normal is being calculated as follows: consider the points as
     * follows: <code>v1, v2, v3, v4</code>. We now first calculate
     * <code>d1 = v2 - v1</code> and <code>d2 = v4 - v1</code>. The normal is
     * now calculated as follows: <code>n = d1 x d2</code>.
     * 
     * @param quad The quad to put, given as a series of 4 points in counter-
     *             clockwise order.
     * @param scaleFactor Factor to multiply each and every coordinate with.
     * @throws IllegalArgumentException In case {@code quad == null} or when
     *             {@code quad.length != 4}.
     */
    public static void drawQuadAndNormals(Vector3f[] quad, float scaleFactor) {
        if (quad == null || quad.length != 4) {
            throw new IllegalArgumentException(
                    "Cannot draw a quad that does "
                            + "not have exactly 4 points.");
        }

        d1.sub(quad[1], quad[0]);
        d2.sub(quad[3], quad[0]);
        normal.cross(d1, d2);
        normal.normalize();

        glNormal3f(normal);
        for (int i = 0; i < 4; i++) {
            glVertex3f(quad[i], scaleFactor);
        }
    }
    /**
     * Issues a glNormal3f(...) call based on a Vector3f.
     * @param v The vector to draw.
     */
    public static void glNormal3f(Vector3f v) {
        org.lwjgl.opengl.GL11.glNormal3f(v.x, v.y, v.z);
    }
    /**
     * Issues a glVertex3f(...) call based on a Vector3f.
     * @param v The vector to draw.
     */
    public static void glVertex3f(Vector3f v) {
        org.lwjgl.opengl.GL11.glVertex3f(v.x, v.y, v.z);
    }
    /**
     * Issues a glVertex3f(...) call based on a Vector3f.
     * @param v The vector to draw.
     * @param scaleFactor Factor to multiply each and every coordinate with.
     */
    public static void glVertex3f(Vector3f v, float scaleFactor) {
        org.lwjgl.opengl.GL11.glVertex3f(v.x * scaleFactor, v.y
                * scaleFactor, v.z * scaleFactor);
    }
}

Related Tutorials