OpenGL examples from the Redbook with LWJGL
"The Lightweight Java Game Library (LWJGL) [...] provides developers access to high performance crossplatform libraries such as OpenGL (Open Graphics Library) [...]"
- The LWJGL homepage
In order to use OpenGL in Java wrappers, like the Lightweight Java Gaming Library (LWJGL), you obviously have to learn how to use both first. The de facto standard beginner book for OpenGL is the Redbook, the OpenGL Programming Guide. However, its examples are written in C. While I was learning from the book, I implemented its examples with the LWJGL in Java. These rewritten and ported examples are published here.
The use of OpenGL in the LWJGL is similar to how you use the native OpenGL libraries in C. Yet there are a few differences. These examples are a good way to get to know both OpenGL and the LWJGL.
All examples are based on revision 2 of the OpenGL Programming Guide. LWJGL 2 is used.
Differences to the book
The way this implementation is structured differs slightly from the original C examples. All examples extend a common Base class. It handles basic setup of OpenGL and the window used for display. Like in games, rendering is done in a continuous loop. There are several functions that can be overridden by examples. This has the advantage that examples contain almost only relevant code and keeps the original code identifiable. Such functions are:
- init(), contains OpenGL initialisation code and usually contains code from init() and reshape() (the window cannot be resized, so this only needs to be called once) of the original examples.
- render(), contains code from display(). It is called in a continuous loop due to slightly different semantics of the Display.
- logic(), handles animation or input via the mouse or keyboard.
- User input is being handled by LWJGL instead of GLUT. Part of this means that there is no need to write code (as in the C examples) that quits the program on ESC. This is already part of LWJGL functionality.
Another difference is that GLUT is not used. Window setups are defined in class Base. Examples in the book set up windows in the main() method. The Base class does this upon creation. A default window is used. Its properties can be set in the Base class. "Why not use GLUT?" you may ask. The answer is quite simple: GLUT does not exist in the LWJGL.
Implemented examples
Not all examples in the book are actually runnable. It is not sensible to run some of them as they are only a few incoherent lines of code. If an example has been implemented (i.e. there is a class named Example_ followed by the number of the example), it is stated in the Implemented column of the following table. Incomplete examples are either part of another example (and can be found in the other example's class) or not implemented at all.
Some of the original examples exist in C code and can be downloaded from the OpenGL homepage. If this is the case, the original file name containing the C code is given, too. I have also implemented many of the examples for which there is no C code.
| Example | Implemented | Part Of | Original File | Topic | Note |
|---|---|---|---|---|---|
| 1-1 | no | 1-2 | Hello World | ||
| 1-2 | yes | hello.c | Hello World | ||
| 1-3 | yes | double.c | Double Buffering | ||
|
|
|||||
| 2-1 | no | 1-3 | Viewport Setup | ||
| 2-2 | no | Use of glVertex*() | Only a collection of code snippets. | ||
| 2-3 | yes | Primitive Drawing | |||
| 2-4 | yes | Primitive Drawing | |||
| 2-5 | yes | lines.c | Line Stippling | ||
| 2-6 | yes | polys.c | Polygon Stippling | ||
| 2-7 | yes | Edge Marking | |||
| 2-8 | no | Surface Normals | Minimal code snippet without any data. | ||
| 2-9 | yes | varray.c | Vertex Arrays | ||
| 2-10 | no | 2-9 | Vertex Arrays | ||
| 2-11 | yes | Vertex Arrays | |||
| 2-12 | no | 2-11 | Vertex Arrays | ||
| 2-13 | no | 2-11 | Vertex Arrays | ||
| 2-14 | no | Vertex Arrays | glMultiDrawElements() does not exist in LWJGL | ||
| 2-15 | no | Vertex Arrays | |||
| 2-16 | yes | Vertex Array Buffers | |||
| 2-17 | yes | Vertex Array Buffers | |||
| 2-18 | no | 2-17 | Vertex Array Buffers | ||
| 2-19 | no | 2-17 | Vertex Array Buffers | ||
| 2-20 | yes | Surface Subdivision | |||
| 2-21 | no | 2-20 | Surface Subdivision | ||
| 2-22 | no | Surface Subdivision | Vital helper functions are missing. | ||
|
|
|||||
| 3-1 | yes | cube.c | Modelling Transformation | ||
| 3-2 | yes | model.c | Modelling Transformation | ||
| 3-3 | no | Projection Transformation | Vital helper functions are missing. | ||
| 3-4 | yes | Matrix Stack | |||
| 3-5 | yes | clip.c | Clipping Planes | ||
| 3-6 | yes | planet.c | Transformation Composition | ||
| 3-7 | yes | robot.c | Transformation Composition | ||
| 3-8 | yes | unproject.c | Reverse Projection | ||
|
|
|||||
| 4-1 | yes | smooth.c | Shading Model | ||
|
|
|||||
| 5-1 | yes | light.c | Lighting | ||
| 5-2 | no | 5-1 | Lighting | ||
| 5-3 | yes | Lighting | |||
| 5-4 | no | 5-1 | Lighting | ||
| 5-5 | yes | Lighting | |||
| 5-6 | yes | movelight.c | Lighting | ||
| 5-7 | yes | Lighting | |||
| 5-8 | yes | material.c | Lighting and Materials | ||
| 5-9 | yes | colormat.c | Lighting and Materials | ||
|
|
|||||
| 6-1 | yes | blendeqn.c | Blending | ||
| 6-2 | yes | alpha.c | Blending | ||
| 6-3 | yes | alpha3D.c | Blending | ||
| 6-4 | yes | aargb.c | Antialiasing | ||
| 6-5 | no | aaindex.c | Anti-aliasing | glutSetColor() is missing in the LWJGL. | |
| 6-6 | yes | multisamp.c | Anti-aliasing | ||
| 6-7 | yes | fog.c | Fog | ||
| 6-8 | no | fogindex.c | Fog | glutSetColor() is missing in the LWJGL. | |
| 6-9 | yes | fogcoord.c | Fog | ||
| 6-10 | yes | pointp.c | Point Parameters | ||
| 6-11 | yes | polyoff.c | Polygon Offset | ||
|
|
|||||
| 7-1 | yes | torus.c | Display Lists | ||
| 7-2 | yes | list.c | Display Lists | ||
| 7-3 | yes | Display Lists | |||
| 7-4 | no | 7-5 | Display Lists | ||
| 7-5 | yes | stroke.c | Display Lists | ||
| 7-6 | no | 7-8 | Display Lists | ||
| 7-7 | no | 7-8 | Display Lists | ||
| 7-8 | yes | Display Lists | |||
| 7-9 | yes | Display Lists | |||
|
|
|||||
| 8-1 | yes | drawf.c | Bitmap Font | ||
| 8-2 | yes | font.c | Bitmap Font | ||
| 8-3 | yes | image.c | Use of glDrawPixels() | ||
| 8-4 | no | 8-3 | Use of glDrawPixels() | ||
| 8-5 | yes | colortable.c | Colour Table | ||
| 8-6 | yes | convolution.c | Convolution Filters: | ||
| 8-7 | yes | colormatix.c | Colour Replacement | ||
| 8-8 | yes | histogram.c | Histogram | ||
| 8-9 | yes | minmax.c | Min-Max Pixel Values | ||
|
|
|||||
| 9-1 | yes | checker.c | Texturing | ||
| 9-2 | yes | Texturing | |||
| 9-3 | yes | texsub.c | Texturing | ||
| 9-4 | yes | texture3d.c | 3D Texturing | ||
| 9-5 | yes | mipmap.c | Mipmaps | ||
| 9-6 | yes | Mipmaps | |||
| 9-7 | yes | texbind.c | Texture Objects | ||
| 9-8 | yes | texgen.c | Texture-Coordinate Generation | ||
| 9-9 | yes | cubemap.c | Cubemap | ||
| 9-10 | yes | multitex.c | Multi-Texturing | ||
| 9-11 | no | 9-10 | Multi-Texturing | ||
| 9-12 | no | 9-10 | Multi-Texturing | ||
| 9-13 | no | 9-16 | Combiner Functions | ||
| 9-14 | no | 9-16 | Combiner Functions | ||
| 9-15 | no | 9-16 | Combiner Functions | ||
| 9-16 | yes | combiner.c | Combiner Functions | ||
| 9-17 | yes | shadowmap.c | Shadow Mapping | ||
| 9-18 | no | 9-17 | shadowmap.c | Shadow Mapping | |
| 9-19 | no | 9-17 | shadowmap.c | Shadow Mapping | |
|
|
|||||
| 10-1 | yes | stencil.c | Stencil Test | ||
| 10-2 | yes | Occlusion Query | |||
| 10-3 | no | 10-2 | Occlusion Query | ||
| 10-4 | yes | accpersp.c | Scene Anti-alising | ||
| 10-5 | no | 10-4 | accpersp.c | Scene Anti-alising | |
| 10-6 | yes | accanti.c | Scene Anti-alising | ||
| 10-7 | yes | dof.c | Depth of Field | ||
|
|
|||||
| 11-1 | no | tess.c | Tesselation | GLU tesselation is not supported by LWJGL. | |
| 11-2 | no | tess.c | Tesselation | GLU tesselation is not supported by LWJGL. | |
| 11-3 | no | tess.c | Tesselation | GLU tesselation is not supported by LWJGL. | |
| 11-4 | yes | quadric.c | Quadrics | ||
|
|
|||||
| 12-1 | yes | bezcurve.c | Bezier Functions | ||
| 12-2 | yes | bezsurf.c | Bezier Functions | ||
| 12-3 | yes | bezmesh.c | Bezier Functions | ||
| 12-4 | yes | texturesurf.c | Bezier Functions | ||
| 12-5 | no | surface.c | NURBS | GLU NURBS are not supported by LWJGL. | |
| 12-6 | no | surfpoints.c | NURBS | GLU NURBS are not supported by LWJGL. | |
| 12-7 | no | surfpoints.c | NURBS | GLU NURBS are not supported by LWJGL. | |
| 12-8 | no | trim.c | NURBS | GLU NURBS are not supported by LWJGL. | |
|
|
|||||
| 13-1 | no | 13-2 | Name Stack | ||
| 13-2 | yes | select.c | Selection Mode | ||
| 13-3 | yes | picksquare.c | Picking | ||
| 13-4 | yes | Multiple Names | |||
| 13-5 | no | 13-4 | Multiple Names | ||
| 13-6 | yes | pickdepth.c | Picking with Depth Values | ||
| 13-7 | yes | feedback.c | Feedback Mode | ||
|
|
|||||
| 14-1 | yes | Error Handling | |||
| 14-2 | yes | Querying Extension Capabilities | Replaced 14-2 code with gluCheckExtension(). | ||
| 14-3 | no | WGL Extension Location | Not supported by LWJGL | ||
|
|
|||||
| 15-1 | yes | Point Sprite | |||
| 15-2 | no | 0-2 | OpenGL Shading Language | OpenGL 2 version in 0-2 (instead of ARB extension) | |
| 15-3 | no | 0-2 | OpenGL Shading Language | OpenGL 2 version in 0-2 (instead of ARB extension) | |
| 15-4 | no | OpenGL Shading Language | Incomplete code snippet | ||
| 15-5 | no | OpenGL Shading Language | Incomplete code snippet | ||
| 15-6 | no | OpenGL Shading Language | Incomplete code snippet | ||
| 15-7 | no | OpenGL Shading Language | Incomplete code snippet | ||
|
|
|||||
| 0-1 | yes | Accumulation Buffer Motion Blur | |||
| 0-2 | yes | Simple GLSL vertex and fragment shaders | |||
Download the source
Download the source code of the examples: lwjgl_redbook_examples_src_2009-05-13.zip.
LWJGL 2.0 .jars are required in the classpath. You can download them here. In order to run them, you also have to use the parameter -Djava.library.path=C:/lwjgl/native/windows which should point to the native libraries for your system (Windows in this example).
Download the executable
There is also an executable distribution that includes a launcher to conveniently choose and run all examples: lwjgl_redbook_examples_bin_2009-05-13.zip.
No setup is needed. All required LWJGL-libraries are included for Windows, Mac OS X, Linux and Solaris. Just run the .jar file. The launcher requires Java 6 to run.

Screenshot of the launcher.
Awesome work Ciardhubh! Thank you very much for providing all this code, it has helped me a lot to start with LWJGL. I am trying to create my own game now, which ows a lot to this post. You can check out my progress at my blog if you like. Cheers.
- reply
Submitted by jmanji (not verified) on 26. March 2009 - 15:27.Very helpful.
Thanks for your excellent work.
- reply
Submitted by Ragnarok_X (not verified) on 4. April 2009 - 23:58.I can't get the launcher to actually launch anything.. It appears to load fine, below is my loading command. The app comes up, but then when I click on Run nothing happens... Any thoughts?
C:\lwjgldemo\lwjgl_redbook_examples_bin_2009-03-17>OpenGLTutorial.jar -classpath .\OpenGlTutorial.jar;C:\lwjgldemo\lwjgl_redbook_examples_bin_2009-03-17\lib\lwjgl.jar;C:\lwjgldemo\lwjgl_redbook_examples_bin_2009-03-17\lib\lwjgl_util.jar;C:\lwjgldemo\lwjgl_redbook_examples_bin_2009-03-17\lib\lwjgl_util_applet.jar;C:\lwjgldemo\lwjgl_redbook_examples_bin_2009-03-17\lib\lwjgl_test.jar;C:\lwjgldemo\lwjgl_redbook_examples_bin_2009-03-17\lib\jinput.jar; -Djava.library.path=C:\lwjgldemo\lwjgl_redbook_examples_bin_2009-03-17\lib\native\win32
- reply
Submitted by Ivan (not verified) on 30. April 2009 - 13:11.Thanks for the info. I accidentally included the old LWJGL 2.0 native libraries after upgrading to LWJGL 2.1. That's why the launcher wouldn't start the examples. Download the new archives for a fixed version. Double-clicking OpenGLTutorial.jar should be enough to run the launcher. It should correctly start the individual examples again.
- reply
Submitted by Ciardhubh on 13. May 2009 - 10:41.A nice description and you have just shown the right steps for implementing it. I installed it and its working great and just hope to get some more useful tricks from you in near future.
- reply
Submitted by How to draw Yoda (not verified) on 19. August 2009 - 6:40.Its a fantastic Open GL example and just tried it working on and its pretty good.
there are times when I got stuck in the coding part but your guidance and tutorial made all my doubt cleared about it.
nice post !!
- reply
Submitted by free online games (not verified) on 25. August 2009 - 9:31.Well there are many new updates on Java which is just a good way for developers to enjoy them.
OpenGL has changed it's way of accepting codes which is just too new & I think more updates on this will make it more simpler to access OpenGL.
- reply
Submitted by handbags (not verified) on 8. December 2009 - 8:33.Hey great little collection of code here,
just incase you missed it GLU tesselation is now supported by LWJGL, see the lwjgl util package.
- reply
Submitted by Anonymous (not verified) on 25. December 2009 - 23:16.zhjchv0201
Replica Handbags
Replica Handbags
Replica Handbags
Replica Handbags
Replica Handbags
Dior Handbags
Chloe handbags
Thomas Wylde Handbags
Replica Handbags
Valentino handBags
replica Hermes Belts
Marni Handbags
Replica Belts
Replica Handbags
Louis Vuitton handbags
Loewe Handbags
Bottega Veneta Handbags
Replica Versace Handbags
Gucci handbags
Prada Handbags
Hermes Handbags
Jimmy Choo Handbags
Fianl Sales Handbags
Fake handbags
Versace Handbags
Miu Miu handbags
Replica Belts
Balenciaga Handbags
Lancel Handbag
Louis Vuitton handbags
- reply
Submitted by lange (not verified) on 1. February 2010 - 4:30.10luoyuejun0209
Replica Louis Vuitton
Replica Watches
replica handbags
Louis vuitton handbags
Replica Handbags
replica watches
Wholesale Replica Handbags
Omega Replica Watches
replica Omega
Omega Classic Watches
Omega CO AXIAL Watches
Omega CONSTELLATION Watches
Omega DEVILLE Watches
Omega Museum Watches
Omega OLYMPIC COLLECTION Watches
Omega OMEGAMANIA Watches
Omega Railmaster Watches
Omega Seamaster Watches
Omega SPEEDMASTER Watches
Chanel Handbags
Replica Chanel
Chanel bags
Chanel Replica Handbags
Replica Chanel Bags
Chanel Purses
Cheap Chanel Bags
Chanel Watches
Chanel Wallets
Chanel Sunglasses
Chanel Jewelry
Chanel Replica Jewelry
Chanel Scarves
Chanel Silk Scarf
Cheap Chanel Scarves
replicat tiffany
tiffany jewelry
Replica Bags
Hermes Handbags
Replica Purses
Replica Mobile
replica scarf
Burberry scarves
Hermes scarves
Ugg boots
Ugg boots
Replica handbags
Replica Gucci
Gucci Handbags
Chanel handbags
Hermes handbags
adidas shoes, air jordans
Wholesale nike shoes, adidas shoes Blogs
air jordans, jordan fusions
air jordans, jordan fusions
jordan fusions,Chanel Shoes
Chanel Shoes, Christian Louboutin Shoes
Replica Fashion Handbags Blogs
Christian Louboutin Shoes, Manolo Blahnik Shoes
Manolo Blahnik Shoes, Shoe Trends
This week in shoes, spring 21 shoes
- reply
Submitted by replica handbags (not verified) on 9. February 2010 - 5:48.10luoyuejun0221
Replica Louis Vuitton
Replica Watches
replica handbags
Louis vuitton handbags
Replica Handbags
replica watches
Wholesale Replica Handbags
Omega Replica Watches
replica Omega
Omega Classic Watches
Omega CO AXIAL Watches
Omega CONSTELLATION Watches
Omega DEVILLE Watches
Omega Museum Watches
Omega OLYMPIC COLLECTION Watches
Omega OMEGAMANIA Watches
Omega Railmaster Watches
Omega Seamaster Watches
Omega SPEEDMASTER Watches
Chanel Handbags
Replica Chanel
Chanel bags
Chanel Replica Handbags
Replica Chanel Bags
Chanel Purses
Cheap Chanel Bags
Chanel Watches
Chanel Wallets
Chanel Sunglasses
Chanel Jewelry
Chanel Replica Jewelry
Chanel Scarves
Chanel Silk Scarf
Cheap Chanel Scarves
replicat tiffany
tiffany jewelry
Replica Bags
Hermes Handbags
Replica Purses
Replica Mobile
replica scarf
Burberry scarves
Hermes scarves
Ugg boots
Ugg boots
Replica handbags
Replica Gucci
Gucci Handbags
Chanel handbags
Hermes handbags
adidas shoes, air jordans
Wholesale nike shoes, adidas shoes Blogs
air jordans, jordan fusions
air jordans, jordan fusions
jordan fusions,Chanel Shoes
Chanel Shoes, Christian Louboutin Shoes
Replica Fashion Handbags Blogs
Christian Louboutin Shoes, Manolo Blahnik Shoes
Manolo Blahnik Shoes, Shoe Trends
This week in shoes, spring 21 shoes
- reply
Submitted by replica handbags (not verified) on 21. February 2010 - 5:31.