This post contains the LWJGL base class that I use throughout the tutorials and examples. It basically sets up LWJGL and OpenGL which I otherwise would have to do over and over again in the individual examples. In addition, this makes the code in the tutorials and examples more readable by focusing on the important things.

So let’s dive into the code. I basically provide this class as a copy-paste template so that you have as little hassle as possible to set up the examples.

// import GL11 statically which makes the GL code
// more readable
import static org.lwjgl.opengl.GL11.*;

// some more LWJGL imports that I omitted for brevity.

public abstract class LwjglExampleBase 
{
	/** width of window */
	private int viewportWidth = 800;
	
	/** height of window */
	private int viewportHeight = 600;
	
	/**
	 * Rudimentary OpenGL setup with LWJGL. Use perspective
	 * projection with a field of view of 45 degrees. 
	 */
	public void setUp() 
	{
		// set up LWJGL display
		try
		{
			Display.setDisplayMode(new DisplayMode(viewportWidth, viewportHeight));
			Display.create();
		}
		catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
		// init viewport and projection matrix
		glViewport(0, 0, viewportWidth, viewportHeight);
		glMatrixMode(GL11.GL_PROJECTION);
		glLoadIdentity();
		GLU.gluPerspective( 45.0f, (float)viewportWidth/(float)viewportHeight, 1.0f, 200.0f ); 
		glMatrixMode(GL11.GL_MODELVIEW);
		
		glEnable(GL_DEPTH_TEST);
		glShadeModel (GL_SMOOTH);
		glClearColor (0f, 0f, 0f, 0f);
	}

	/**
	 * Game loop is basically an infinite loop that runs
	 * until the window is closed. It calls the abstract
	 * render() method which is to be implemented by the
	 * specific example class. 
	 */
	public void gameLoop()
	{
		while (!Display.isCloseRequested())
		{
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glLoadIdentity();
			GLU.gluLookAt(0, 15,0, 
					0, 0, 0, 
					0, 0, 1);

                        // call the abstract render method implemented by the
                        // extending class (eg. example, tutorial, etc.)
			render(); 
			
                        // sync to 60 frames per second
			Display.update();
			Display.sync(60);
		}
	
		Display.destroy();
	}

        /**
         * Abstract render method which is supposed to be
         * implemented by the extending tutorial or example
         * class.
         */
	public abstract void render();

	public int getViewportWidth() {
		return viewportWidth;
	}

	public void setViewportWidth(int viewportWidth) {
		this.viewportWidth = viewportWidth;
	}

	public int getViewportHeight() {
		return viewportHeight;
	}

	public void setViewportHeight(int viewportHeight) {
		this.viewportHeight = viewportHeight;
	}
}

That’s already it! Please make use of it as good as you can and leave a comment if you liked it or have questions.

Categories: OpenGL

2 Comments

amir hossein bakhtiary · August 20, 2012 at 1:55 pm

I used the code, may the force be with you. 🙂

OpenGL Display Lists · September 25, 2011 at 6:18 pm

[…] far for dry theory, let’s look at the example. Please note that this example makes use of my LWJGL example base class which I recommend to copy first so that the example runs out of the […]

Leave a Reply

Your email address will not be published. Required fields are marked *