opengl draws a cube, with extents being defined by the parameter values. - Java javax.media.opengl

Java examples for javax.media.opengl:Draw

Description

opengl draws a cube, with extents being defined by the parameter values.

Demo Code


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

public class Main{
    /**/*  www. ja va  2  s  .  c om*/
     * This draws a cube, with extents being defined by the parameter values.
     * Note that the front is considered to be the face that is facing towards
     * the negative z values. This is important to note, as it will affect the
     * normals.
     * 
     * The normals are all facing the outside of the cube.
     * 
     * @param gl
     * @param width
     * @param height
     * @param length
     * @param colors an array of 6 elements representing the colours of the sides
     */
    public static void drawCube(GL gl, float left, float right, float top,
            float bottom, float front, float back, Color[] colors) {

        // Front

        gl.glBegin(GL.GL_QUADS);

        applyColor(gl, colors[0]);

        gl.glNormal3f(0.0f, 0.0f, -1.0f);

        gl.glVertex3f(left, bottom, front);
        gl.glVertex3f(right, bottom, front);
        gl.glVertex3f(right, top, front);
        gl.glVertex3f(left, top, front);

        // Back

        applyColor(gl, colors[1]);

        gl.glNormal3f(0.0f, 0.0f, 1.0f);

        gl.glVertex3f(left, bottom, back);
        gl.glVertex3f(right, bottom, back);
        gl.glVertex3f(right, top, back);
        gl.glVertex3f(left, top, back);

        // Left

        applyColor(gl, colors[2]);

        gl.glNormal3f(-1.0f, 0.0f, 0.0f);

        gl.glVertex3f(left, top, front);
        gl.glVertex3f(left, top, back);
        gl.glVertex3f(left, bottom, back);
        gl.glVertex3f(left, bottom, front);

        // Right

        applyColor(gl, colors[3]);

        gl.glNormal3f(1.0f, 0.0f, -0.0f);

        gl.glVertex3f(right, top, front);
        gl.glVertex3f(right, top, back);
        gl.glVertex3f(right, bottom, back);
        gl.glVertex3f(right, bottom, front);

        // Top

        applyColor(gl, colors[4]);

        gl.glNormal3f(0.0f, 1.0f, -0.0f);

        gl.glVertex3f(left, top, front);
        gl.glVertex3f(right, top, front);
        gl.glVertex3f(right, top, back);
        gl.glVertex3f(left, top, back);

        // Bottom

        applyColor(gl, colors[5]);

        gl.glNormal3f(0.0f, -1.0f, 0.0f);

        gl.glVertex3f(left, bottom, front);
        gl.glVertex3f(right, bottom, front);
        gl.glVertex3f(right, bottom, back);
        gl.glVertex3f(left, bottom, back);

        gl.glEnd();

    }
    /**
     * This draws a cube, with extents being defined by the parameter values.
     * Note that the front is considered to be the face that is facing towards
     * the negative z values. This is important to note, as it will affect the
     * normals.
     * 
     * The normals are all facing the outside of the cube.
     * 
     * @param gl
     * @param width
     * @param height
     * @param length
     */
    public static void drawCube(GL gl, float left, float right, float top,
            float bottom, float front, float back) {

        // Front

        gl.glBegin(GL.GL_QUADS);

        gl.glColor3f(0.5f, 0.2f, 0.2f);

        gl.glNormal3f(0.0f, 0.0f, -1.0f);

        gl.glVertex3f(left, bottom, front);
        gl.glVertex3f(right, bottom, front);
        gl.glVertex3f(right, top, front);
        gl.glVertex3f(left, top, front);

        // Back

        gl.glNormal3f(0.0f, 0.0f, 1.0f);

        gl.glVertex3f(left, bottom, back);
        gl.glVertex3f(right, bottom, back);
        gl.glVertex3f(right, top, back);
        gl.glVertex3f(left, top, back);

        // Left

        gl.glNormal3f(-1.0f, 0.0f, 0.0f);

        gl.glVertex3f(left, top, front);
        gl.glVertex3f(left, top, back);
        gl.glVertex3f(left, bottom, back);
        gl.glVertex3f(left, bottom, front);

        // Right

        gl.glNormal3f(1.0f, 0.0f, -0.0f);

        gl.glVertex3f(right, top, front);
        gl.glVertex3f(right, top, back);
        gl.glVertex3f(right, bottom, back);
        gl.glVertex3f(right, bottom, front);

        // Top

        gl.glNormal3f(0.0f, 1.0f, -0.0f);

        gl.glVertex3f(left, top, front);
        gl.glVertex3f(right, top, front);
        gl.glVertex3f(right, top, back);
        gl.glVertex3f(left, top, back);

        // Bottom

        gl.glNormal3f(0.0f, -1.0f, 0.0f);

        gl.glVertex3f(left, bottom, front);
        gl.glVertex3f(right, bottom, front);
        gl.glVertex3f(right, bottom, back);
        gl.glVertex3f(left, bottom, back);

        gl.glEnd();

    }
    /**
     * This draws a cube of the given dimensions, centered around the point
     * (0,0,0). The normals are all facing the outside of the cube.
     * 
     * @param gl
     * @param width
     * @param height
     * @param length
     */
    public static void drawCube(GL gl, float width, float height,
            float length) {

        float left = (width / 2.0f) * -1;
        float right = (width / 2.0f);

        float top = (height / 2.0f);
        float bottom = (height / 2.0f) * -1;

        float front = (length / 2.0f) * -1;
        float back = (length / 2.0f);

        drawCube(gl, left, right, top, bottom, front, back);
    }
    public static void applyColor(GL gl, Color c) {
        //        gl.glColor3i(c.getRed(), c.getGreen(), c.getBlue());//, c.getAlpha());
        //        gl.glColor3f(1.0f,1.0f,1.0f);
        gl.glColor4f(c.getRed() / 255.0f, c.getGreen() / 255.0f,
                c.getBlue() / 255.0f, c.getAlpha() / 255.0f);
    }
}

Related Tutorials