Chapter 0 - Getting Started with LWJGL

Installing the JDK, Eclipse, and LWJGL, and your first Display

Hey all! Coffee Bean Code here with the first of a series of tutorials for Java OpenGL programming! In this tutorial we’ll be going over the basics of starting an Eclipse Project using the LWJGL API.

The focus of these tutorials is to provide readers lessons on setting up a basic game engine which we can use to design a multitude of different types of 2D and 3D games.

This series assumes no knowledge of OpenGL, however it will assume you understand the basics of Java programming. More complex topics will be discussed and taught as they come up.

First off, you will need to download and install the JDK (Java Development Kit), found here: https://www.oracle.com/technetwork/java/javase/downloads/index.html

These tutorials advise usage of Eclipse, which can be installed at https://www.eclipse.org/downloads/ Once the installer has been downloaded, you will need to install the Java IDE

With JDK and Eclipse downloaded, you will need to download the current build of LWJGL 3, found here. Now, for the sake of keeping things simple, we'll only be using the Minimal OpenGL preset.

Starting out, we’ll need to create a new project in Eclipse. To do so, go into your workplace and click the File dropdown menu and select New->Java Project. You’ll be greeted with this window:

For the sake of these tutorials, I’ll be naming the project “Opengl Tutorial”

Once you’ve named your project, hit finish! Now, from here, we’ll need to load the LWJGL libraries. To do this, right click on your project in the Package Explorer, and select Properties. From there, navigate on the left panel to Java Build Path, and go to the Libraries tab. You should be greeted with this window:

From here, select the option Add External JARs, and navigate to the location where you downloaded LWJGL. You will need to select .jar file and add it to your project.

Now that LWJGL is set up, it’s time to set up the main class. This time, we’ll go to the main dropdown and select File->New->Class. Once again, we’ll be greeted with the New File window we saw when we created the project:

It’s advised that you separate your code by Packages, in this case we’re going to create a package named “tutorials”, whereas the class will be named “Boot”

If you want, you can click the option to generate the main method, which will save you a bit of time. Once you’re done, click finish and you’ll be greeted with an editor for your Boot class!

First thing you’ll want to do is import the OpenGL code for the Display window. To do this, at the top of the class file, before public class Boot { you need to include the following lines of code after package tutorials:

Boot.java
import java.nio.IntBuffer;

import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.glfw.Callbacks;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;

import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

Now, let's get started on our first window! Going into the Boot class, you will need to create the following:

Boot.java
private long window;

public void run(){

}

public void init(){

}

public void loop(){

}

The window variable represents the ID of the window we'll be creating.

Focusing on the init() class, we'll need to add the following code inside:

Boot.java
GLFWErrorCallback.createPrint(System.err).set();
		
if(!GLFW.glfwInit()) {
		throw new IllegalStateException("Unable to initialize GLFW");
}
		
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE);
		
window = GLFW.glfwCreateWindow(640, 480, "LWJGL Bootcamp", NULL, NULL);
if(window == NULL) {
		throw new IllegalStateException("Unable to create GLFW Window");
}
		
GLFW.glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {});
		
try(MemoryStack stack = stackPush()){
		IntBuffer pWidth = stack.mallocInt(1);
		IntBuffer pHeight = stack.mallocInt(1);
		
		GLFW.glfwGetWindowSize(window, pWidth, pHeight);
			
		GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
		
		GLFW.glfwSetWindowPos(window,(vidmode.width() - pWidth.get(0)) / 2,(vidmode.height() - pHeight.get(0)) / 2);
			
		GLFW.glfwMakeContextCurrent(window);
		GLFW.glfwSwapInterval(1);
		GLFW.glfwShowWindow(window);
}

For a brief rundown, we're creating the main Callbacks and tying them to LWJGL/GLFW. Once we do that we intialize the window variable, which creates the GLFW Window. Inside the try block, we get the window size again, set the window to the center of the screen, and show the window on the screen!

Continuing on, to make sure the Window remains and lets us do all sorts of cool neatness (the correct Computer Science term), we'll need to start the Main Game Loop. To do so, put this code into the loop() method:

GL.createCapabilities();

while(!GLFW.glfwWindowShouldClose(window)) {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
			
		GLFW.glfwSwapBuffers(window);
			
		GLFW.glfwPollEvents();
}

GL.createCapabilities() essentially finishes the initializing process.

Now, a while loop is a section of code that will continue repeating until the designated circumstances are met. In this case, until you click that X button on your window, this code will keep running.

GL11.glClear() is a method that needs to be called before rendering, preferrably at the beginning of the loop. It's job is to clear everything in the specified buffer, in this case the Color and Depth buffer (more on that later).

glfwSwapBuffers(...) and glfwPollEvents() are essentially the modern LWJGL equivalents to Display.Update() and Display.sync(...), and hand additional Events that return immediately after being called.

With these done, let's take another look at that run() class, and put everything together!

Boot.java
init();
loop();
		
Callbacks.glfwFreeCallbacks(window);
GLFW.glfwDestroyWindow(window);
		
GLFW.glfwTerminate();
GLFW.glfwSetErrorCallback(null).free();

Now that we're done, let's run the co-wait a minute... There's no main() method! All of our work is for naught!

Not so. At the end of the class, before the final closing bracket, insert the following code:

Boot.java
public static void main(String[] args){
    new Boot().run();
}

What the main method does is provides a starting point for your program. From here, it calls the Boot constructor and creates the window. By now, you should see this:

Congrats! You've begun to weave the canvas of which all your games will be shown!

Next tutorial we'll be covering drawing an object in the window.

Last updated