Draw A cube opengl - Java javax.media.opengl

Java examples for javax.media.opengl:GL

Description

Draw A cube 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{
    /**/* w ww.j  a v  a  2 s  .c  o  m*/
     * Draw A cube.
     */
    public static void cube(GL2 gl) {

        //Multi-colored side - FRONT
        gl.glColor3f(1.0f, 0.0f, 0.0f);
        gl.glBegin(GL_QUADS);

        gl.glVertex3f(0.5f, -0.5f, -0.5f); // P1 is red
        gl.glVertex3f(0.5f, 0.5f, -0.5f); // P2 is green
        gl.glVertex3f(-0.5f, 0.5f, -0.5f); // P3 is blue
        gl.glVertex3f(-0.5f, -0.5f, -0.5f); // P4 is purple
        gl.glEnd();

        // White side - BACK
        gl.glColor3f(1.0f, 1.0f, 1.0f);
        gl.glBegin(GL_QUADS);
        gl.glVertex3f(0.5f, -0.5f, 0.5f);
        gl.glVertex3f(0.5f, 0.5f, 0.5f);
        gl.glVertex3f(-0.5f, 0.5f, 0.5f);
        gl.glVertex3f(-0.5f, -0.5f, 0.5f);
        gl.glEnd();

        // Purple side - RIGHT
        gl.glColor3f(1.0f, 0.0f, 1.0f);
        gl.glBegin(GL_QUADS);

        gl.glVertex3f(0.5f, -0.5f, -0.5f);
        gl.glVertex3f(0.5f, 0.5f, -0.5f);
        gl.glVertex3f(0.5f, 0.5f, 0.5f);
        gl.glVertex3f(0.5f, -0.5f, 0.5f);
        gl.glEnd();

        // Green side - LEFT
        gl.glBegin(GL_QUADS);
        gl.glColor3f(0.0f, 1.0f, 0.0f);
        gl.glVertex3f(-0.5f, -0.5f, 0.5f);
        gl.glVertex3f(-0.5f, 0.5f, 0.5f);
        gl.glVertex3f(-0.5f, 0.5f, -0.5f);
        gl.glVertex3f(-0.5f, -0.5f, -0.5f);
        gl.glEnd();

        // Blue side - TOP
        gl.glBegin(GL_QUADS);
        gl.glColor3f(0.0f, 0.0f, 1.0f);
        gl.glVertex3f(0.5f, 0.5f, 0.5f);
        gl.glVertex3f(0.5f, 0.5f, -0.5f);
        gl.glVertex3f(-0.5f, 0.5f, -0.5f);
        gl.glVertex3f(-0.5f, 0.5f, 0.5f);
        gl.glEnd();

        // Red side - BOTTOM
        gl.glBegin(GL_QUADS);
        gl.glColor3f(1.0f, 0.0f, 5.0f);
        gl.glVertex3f(0.5f, -0.5f, -0.5f);
        gl.glVertex3f(0.5f, -0.5f, 0.5f);
        gl.glVertex3f(-0.5f, -0.5f, 0.5f);
        gl.glVertex3f(-0.5f, -0.5f, -0.5f);
        gl.glEnd();

    }
}

Related Tutorials