Stuff

This page contains various ramlings on different subjects. The first bit

3D at Breakpoint - Part I: LWJGL

Spring 2010

Just got home from Breakpoint 2010, and as usual I spend some time there fiddling around with OpenGL in Java. This year, the main subject was implementing shadows in a simple scene. It turned out to be a good challenge and learning experience, so I decided to write down a few words on the subject. Hopefully, this will also help myself better understand the more technical parts.
This first part is mostly an introduction to how to get OpenGL to work in Java.

In the past I have used JOGL to get access to OpenGL from Java. This time, I decided to give The Lightweight Java Game Library (LWJGL) a go.

It turned out to be very easy to set up a window and actually get to the fun part: Making some 3D. Based on a tutorial, I have made a stripped-down example on how little code is needed to draw a rotating triangle:

Code:

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;


public class SimpleTest
{
    public static void main(String[] args)
    {
        // Create display
        try
        {            
            Display.create();
        }
        catch (LWJGLException e)
        {
            Sys.alert("Unable to create display.", e.getMessage());
            System.exit(0);
        }
        
        // Start loop
        boolean loopRunning = true;
        while (loopRunning)
        {
            // Render OpenGL
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            GL11.glRotatef(0.5f, 0, 0, 1);
            GL11.glBegin(GL11.GL_TRIANGLES);
                GL11.glVertex2f(-0.4f, -0.4f);
                GL11.glVertex2f(0.4f, -0.4f);
                GL11.glVertex2f(0.0f, 0.4f);
            GL11.glEnd();
            
            // Update display
            Display.update();
            Display.sync(60);

            // End loop if display is closed
            if (Display.isCloseRequested())
            {
                loopRunning = false;
                Display.destroy();
            }
        }
    }
}


Simple LWJGL example

To actually run the above code, however, you need the LWJGL library and its native .dll files. I simply downloaded LWJGL, unzipped it, and dragged it into my project in Eclipse (Note: Not all files in the zip necessarily used, but this is the quick way).
I then added "lwjgl.jar" (OpenGL) and "lwjgl_util.jat" (GLUT) to Libraries in the project's Build Path settings. Finally, I created a new Run Configuration for the project and added the following parameter to the VM arguments: -Djava.library.path=lwjgl-2.3/native/windows. This argument tells LWJGL where to find the correct native .dll files for your OS. If LWJGL cannot find the libraries, it will throw an exception on startup.

3D at Breakpoint - Part II: Shadows

Spring 2010

When I got LWJGL up and running, I started to toy around a bit and ended up deciding to make a simple sci-fi hallway. I made a small method to take a line in 2D and extrude it to a shape in 3D.
After a "That's not too bad - now you just need some lights and shadows"-comment from the guy next to me, I decided to google around for shadowing techniques in OpenGL. I quickly discarded anything involing calculating 3D shadow volumes, as that seemed too complicated. Instead, shadow mapping seemed like a better alternative.
With shadow mapping, you render the scene from the light source's perspective and use that information to determine which points are in shadow. A detailed description of this may follow later when I get more into the technical details myself.


Sci-Fi'esh hallway with shadow mapped shadows

I found some examples on the web and started porting the code from C to Java.
After bumping into various problems, I ended up with the results shown on the picture to the right.