LWJGL Basics 3 (The Quad)

Introduction
This tutorial will explain how to access OpenGL with LWJGL and is not intended to teach you OpenGL. The LWJGL OpenGL API pretty much maps 1:1 onto the C version (with a few exceptions). This allows one to easily use or adapt any of the many OpenGL tutorials and guides available online and elsewhere for use with LWJGL.

Structure
All the OpenGL methods are found in the org.lwjgl.opengl.* package and are stored in relevant named classes e.g. GL11, GL12, GL13, GL20, ARBVertexShader, ARBFragmentShader, etc.

All methods from OpenGL 1.1 are found in the class GL11, All methods introduced in OpenGL 1.2 are found in the class GL12, etc. This allows one to easily identify and target specific versions or extensions of OpenGL.

Drawing a Quad with OpenGL
The code from the Display tutorial will be used with a few modifications to draw a quad on the Display. A 2d view should be sufficient for drawing a quad, this will be set by using an orthographic matrix of size 800*600 with a clipping distance between 1 and -1. This only needs to be set once and will be called outside the main loop.  GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity; GL11.glOrtho(0, 800, 0, 600, 1, -1); GL11.glMatrixMode(GL11.GL_MODELVIEW);

The remaining code needs to be called every frame and will go in the main loop. This will clear the screen, set the color of the quad, and draw the verticies of the quad.

 // Clear the screen and depth buffer GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // set the color of the quad (R,G,B,A) GL11.glColor3f(0.5f,0.5f,1.0f); // draw quad GL11.glBegin(GL11.GL_QUADS); GL11.glVertex2f(100,100); GL11.glVertex2f(100+200,100); GL11.glVertex2f(100+200,100+200); GL11.glVertex2f(100,100+200); GL11.glEnd;

Example
A full working example is found below

 import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; public class QuadExample { public void start { try { Display.setDisplayMode(new DisplayMode(800,600)); Display.create; } catch (LWJGLException e) { e.printStackTrace; System.exit(0); }	// init OpenGL GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity; GL11.glOrtho(0, 800, 0, 600, 1, -1); GL11.glMatrixMode(GL11.GL_MODELVIEW); while (!Display.isCloseRequested) { // Clear the screen and depth buffer GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // set the color of the quad (R,G,B,A) GL11.glColor3f(0.5f,0.5f,1.0f); // draw quad GL11.glBegin(GL11.GL_QUADS); GL11.glVertex2f(100,100); GL11.glVertex2f(100+200,100); GL11.glVertex2f(100+200,100+200); GL11.glVertex2f(100,100+200); GL11.glEnd; Display.update; }	Display.destroy; }   public static void main(String[] argv) { QuadExample quadExample = new QuadExample; quadExample.start; } }

This should be enough to get started with OpenGL and LWJGL. The LWJGL Javadoc contains a full list of all classes and methods supported by LWJGL and should be used as a reference to find specific OpenGL functionality.

Static Import
All OpenGL methods in LWJGL are accessed statically, meaning the class name is present before the method name. Depending on your coding style you may prefer not to type the class name on every OpenGL method. Java's static import feature can be used here to hide the class names.

 import static org.lwjgl.opengl.GL11.*;

The above line imports all the methods from the GL11 class to allow use without specifying the class. The code can now be typed as follows:

 // clear the screen and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // set the color of the quad (R,G,B,A) glColor3f(0.5f,0.5f,1.0f); // draw quad glBegin(GL_QUADS); glVertex2f(100,100); glVertex2f(100+200,100); glVertex2f(100+200,100+200); glVertex2f(100,100+200); glEnd;

Credit
Tutorial Credit - Ninja Cave