Wednesday, March 23, 2011

OpenGL one step at a time

After realizing that Oracle was going to move to JavaFX, and that JavaFX could do hardware acceleration, I decided I may as well learn 3d programming.  Since I'm a believer in cross-platformness, I am going to use OpenGL for my programming.

From my initial foray into learning OpenGL, it appears that starting with version 3.0 of OpenGL, the use of GLSL is kind of mandatory (at least if you use the core profile, I am a bit confused about the compatibility spec).  But basically, the old fixed function pipeline is being deprecated and should eventually go the way of the dodo.

So I finally got my OpenGL SuperBible 5th edition book on Friday.  I think I will create a new project under bitbucket which will basically be me following the book's examples, but using Clojure.  How will I manage that since the code examples are all C/C++?  Fortunately, lwjgl (Lightweight Java Game Library) has ported almost all of the OpenGL Core and GLSL API to java.

This will require a bit of mental juggling.  I will have to convert C/C++ to Java, and then from Java to Clojure.  This will also be interesting because OpenGL is in some ways a huge state machine, and Clojure being a functional language, takes a different spin on saving state.  That being said, I don't think there's a Java port for GLSL.  And GLSL is required for shaders.

GLSL is basically a weird version of C with very specific data types and keywords.  For example, GLSL makes a distinction between uniform data flow and varying data flow.  If you have some nested if/else blocks, or switches, then that would be varying data flow, and variables must be noted as such.  Also, GLSL has many specific built-in data types representing vectors and matrices.

What really surprised me is that shader programs are stored as strings and then fed to a compilation unit on the video card.  Basically it's compiling them on the fly.  I did see however that it appears that in OpenGL 4.1, you can store and open binary files (presumably pre-compiled shaders?).  I'll have to study the stock shaders that the book's project comes with.  Although GLSL kind of looks like C code, there are many new keywords that I don't fully understand yet.  For example, invariant, uniform, or varying.

The OpenGL SuperBible weighs in at almost 1000 pages, although a good 300+ pages are Man Pages from OpenGL.  Nevertheless, that leaves around 650 pages just on OpenGL.  It appears that it's mostly about OpenGL itself and not GLSL, though there does appear to be one chapter devoted to it.  Combined with the fact that I am also going to have to refresh my knowledge of linear algebra, this is going to be extremely slow going.

Also, the book is geared towards writing C++ programs, using GLUT or freeglut, and glew.  Furthermore, the book's project comes with a home-grown library called GLTools which has a lot of utility type functions.  In essence, I am going to have to translate all this into Java-speak, and more specifically into lwjgl speak.  For example, lwjgl comes with its own windowing classes that has the same job role as GLUT.  I haven't gotten far enough into the book, but I also wonder if it goes into any controller input issues (for example, getting input from a joystick).

I also want to get in on OpenCL, the open version of Nvidia's CUDA which allows for processing on a GPU's stream units.  The lwjgl project also has ported (most of) OpenCL which means I can use this as well.  Given the vast parallelism given by the GPU, this should be able to make for some interesting AI possibilities.  What will be really interesting as I play with this will be to see the differences between writing concurrent programs for CPU's (which generally speaking are Task oriented in OpenCL lingo), and concurrent programs for GPUs (which are generally Data oriented).

I'm really curious how I can deal with OpenGL's state machine using clojure.  But I don't know enough about the state machine to understand how to best model a clojure representation of it.

Anyhow, this is all pretty interesting, and hopefully within the next week or two, I'll be updating my sorely ignored sojourner project with some new stuff.