OpenGL examples from the Redbook with LWJGL

Started by Ciardhubh, June 13, 2008, 16:18:15

Previous topic - Next topic

Ciardhubh

First, thanks for this great library.

I have learned OpenGL by reading the Redbook (OpenGL Programming Guide). Since theory is nothing without practice, I have implemented its examples with the LWJGL in Java. The examples from the book are very helpful in my opinion and answer many of the questions I've seen around here.

I am done with all chapters and made them available for download along with some more information here:
http://ciardhubh.de/node/13

This is probably interesting for anybody who wants to learn OpenGL with the help of this book and the LWJGL, too. Whether you just want to see the examples running or whether you want to see how you can get the desired effect in Java, this might be a help.

The code is based on the 2nd version of the book and LWJGL 2.

An older version (1.1) of the book can be found here for free: http://www.glprogramming.com/red/
The original C source code can be found here: http://www.opengl.org/resources/code/samples/redbook/

I also wrote a small launcher application to conveniently pick and run examples. It includes all libraries and no setup is required:


Here is a list of the examples which includes a topic for each example, for those without the book:


















































































































































ExampleImplementedPart OfOriginal FileTopicNote
1-1no1-2Hello World
1-2yeshello.cHello World
1-3yesdouble.cDouble Buffering






2-1no1-3Viewport Setup
2-2noUse of glVertex*()Only a collection of code snippets.
2-3yesPrimitive Drawing
2-4yesPrimitive Drawing
2-5yeslines.cLine Stippling
2-6yespolys.cPolygon Stippling
2-7yesEdge Marking
2-8noSurface NormalsMinimal code snippet without any data.
2-9yesvarray.cVertex Arrays
2-10no2-9Vertex Arrays
2-11yesVertex Arrays
2-12no2-11Vertex Arrays
2-13no2-11Vertex Arrays
2-14noVertex ArraysglMultiDrawElements() does not exist in LWJGL
2-15noVertex Arrays
2-16yesVertex Array Buffers
2-17yesVertex Array Buffers
2-18no2-17Vertex Array Buffers
2-19no2-17Vertex Array Buffers
2-20yesSurface Subdivision
2-21no2-20Surface Subdivision
2-22noSurface SubdivisionVital helper functions are missing.






3-1yescube.cModelling Transformation
3-2yesmodel.cModelling Transformation
3-3noProjection TransformationVital helper functions are missing.
3-4yesMatrix Stack
3-5yesclip.cClipping Planes
3-6yesplanet.cTransformation Composition
3-7yesrobot.cTransformation Composition
3-8yesunproject.cReverse Projection






4-1yessmooth.cShading Model






5-1yeslight.cLighting
5-2no5-1Lighting
5-3yesLighting
5-4no5-1Lighting
5-5yesLighting
5-6yesmovelight.cLighting
5-7yesLighting
5-8yesmaterial.cLighting and Materials
5-9yescolormat.cLighting and Materials






6-1yesblendeqn.cBlending
6-2yesalpha.cBlending
6-3yesalpha3D.cBlending
6-4yesaargb.cAntialiasing
6-5noaaindex.cAnti-aliasingglutSetColor() is missing in the LWJGL.
6-6yesmultisamp.cAnti-aliasing
6-7yesfog.cFog
6-8nofogindex.cFogglutSetColor() is missing in the LWJGL.
6-9yesfogcoord.cFog
6-10yespointp.cPoint Parameters
6-11yespolyoff.cPolygon Offset






7-1yestorus.cDisplay Lists
7-2yeslist.cDisplay Lists
7-3yesDisplay Lists
7-4no7-5Display Lists
7-5yesstroke.cDisplay Lists
7-6no7-8Display Lists
7-7no7-8Display Lists
7-8yesDisplay Lists
7-9yesDisplay Lists






8-1yesdrawf.cBitmap Font
8-2yesfont.cBitmap Font
8-3yesimage.cUse of glDrawPixels()
8-4no8-3Use of glDrawPixels()
8-5yescolortable.cColour Table
8-6yesconvolution.cConvolution Filters:
8-7yescolormatix.cColour Replacement
8-8yeshistogram.cHistogram
8-9yesminmax.cMin-Max Pixel Values






9-1yeschecker.cTexturing
9-2yesTexturing
9-3yestexsub.cTexturing
9-4yestexture3d.c3D Texturing
9-5yesmipmap.cMipmaps
9-6yesMipmaps
9-7yestexbind.cTexture Objects
9-8yestexgen.cTexture-Coordinate Generation
9-9yescubemap.cCubemap
9-10yesmultitex.cMulti-Texturing
9-11no9-10Multi-Texturing
9-12no9-10Multi-Texturing
9-13no9-16Combiner Functions
9-14no9-16Combiner Functions
9-15no9-16Combiner Functions
9-16yescombiner.cCombiner Functions
9-17yesshadowmap.cShadow Mapping
9-18no9-17shadowmap.cShadow Mapping
9-19no9-17shadowmap.cShadow Mapping






10-1yesstencil.cStencil Test
10-2yesOcclusion Query
10-3no10-2Occlusion Query
10-4yesaccpersp.cScene Anti-alising
10-5no10-4accpersp.cScene Anti-alising
10-6yesaccanti.cScene Anti-alising
10-7yesdof.cDepth of Field






11-1notess.cTesselationGLU tesselation is not supported by LWJGL.
11-2notess.cTesselationGLU tesselation is not supported by LWJGL.
11-3notess.cTesselationGLU tesselation is not supported by LWJGL.
11-4yesquadric.cQuadrics






12-1yesbezcurve.cBezier Functions
12-2yesbezsurf.cBezier Functions
12-3yesbezmesh.cBezier Functions
12-4yestexturesurf.cBezier Functions
12-5nosurface.cNURBSGLU NURBS are not supported by LWJGL.
12-6nosurfpoints.cNURBSGLU NURBS are not supported by LWJGL.
12-7nosurfpoints.cNURBSGLU NURBS are not supported by LWJGL.
12-8notrim.cNURBSGLU NURBS are not supported by LWJGL.






13-1no13-2Name Stack
13-2yesselect.cSelection Mode
13-3yespicksquare.cPicking
13-4yesMultiple Names
13-5no13-4Multiple Names
13-6yespickdepth.cPicking with Depth Values
13-7yesfeedback.cFeedback Mode






14-1yesError Handling
14-2yesQuerying Extension CapabilitiesReplaced 14-2 code with gluCheckExtension().
14-3noWGL Extension LocationNot supported by LWJGL






15-1yesPoint Sprite
15-2no0-2OpenGL Shading LanguageOpenGL 2 version in 0-2 (instead of ARB extension)
15-3no0-2OpenGL Shading LanguageOpenGL 2 version in 0-2 (instead of ARB extension)
15-4noOpenGL Shading LanguageIncomplete code snippet
15-5noOpenGL Shading LanguageIncomplete code snippet
15-6noOpenGL Shading LanguageIncomplete code snippet
15-7noOpenGL Shading LanguageIncomplete code snippet






0-1yesAccumulation Buffer Motion Blur
0-2yesSimple GLSL vertex and fragment shaders

Download
Source: http://ciardhubh.de/download/lwjgl_redbook_examples_src_2009-01-26.zip
Executable with launcher (requires Java 6): http://ciardhubh.de/download/lwjgl_redbook_examples_bin_2009-01-26.zip

Any feedback - whether you think they are useful or not, hard/easy to understand, etc. - is appreciated.

Ciardhubh

Added examples of chapter 8 and chapter 9.

Edit:
Finished chapter 9.

Edit2:
Added example 8-6, which is finally working.

Lars

This looks great (haven't worked through it yet, though). Thanks!

bobjob

i think there is an error on 9_17 dont know if its my card or not (ATI radeon x1400)


Ciardhubh

Quote from: bobjob on September 22, 2008, 00:29:42
i think there is an error on 9_17 dont know if its my card or not (ATI radeon x1400)
...

That's how it looks on my computer, too. It's probably not intended to look this way, but there is no screenshot on the original examples' site of this particular example and I don't have the time to set up the tools to compile the C example myself.

I assume that there is a problem with depth values when the texture is applied. I'm basically doing the same things as in the original example, though. So I'm not quite sure where the problem lies. Maybe I did something wrong or it's another bug in the original. If anybody has any hints I'd be happy to know.

blingo

Awesome job,
your work is highly appreciated,
Thanks.

Ciardhubh

Quote from: blingo on October 17, 2008, 23:00:14
Awesome job,
your work is highly appreciated,
Thanks.

Thank you, I'm glad somebody likes it ;D

I just updated the examples. Chapters 10 to 13 are now included. There's also a new launcher, so you don't have to set up libraries and manually run each example.

iamzealotwang

Hi~ thanks your Demo ,but i got some problem on it

In your Example 3-6 the plant need to run by hand

         if (eventKey == Keyboard.KEY_D && Keyboard.isKeyDown(Keyboard.KEY_D)) {
                if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) ||
                        Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) {
                    day = (day - 10) % 360;
                } else {
                    day = (day + 10) % 360;
                }
            }
...etc

and i want the plant run by itself 
this is my code:

    protected void logic(long timeDelta) {
       day = (day + 10) % 360;
       year = (year + 5) % 360;
    }

the Base class had been set

   protected void run() {
        while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && !Display.isCloseRequested()) {
            now = System.currentTimeMillis();
            timeDelta = now - lastTime;
            lastTime = now;
            if (Display.isVisible()) {
                logic(timeDelta);
                render();
            } else {
                if (Display.isDirty()) {
                    render();
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                }
            }
            Display.update();
        }
    }

but it's don't work.... plant run so fast...

i also check the FullScreenWindowedTest (http://lwjgl.org/demos.php)

      while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && !Display.isCloseRequested()) {
         if (Display.isVisible()) {
            // check keyboard input
            processKeyboard();
            // do "game" logic, and render it
            logic();
            render();
         } else {
            // no need to render/paint if nothing has changed (ie. window
            // dragged over)
            if (Display.isDirty()) {
               render();
            }
            // don't waste cpu time, sleep more
            try {
               Thread.sleep(100);            
                                                         } catch (InterruptedException inte) {
            }
         }
         // Update window
         Display.update();
      }


the  FullScreenWindowedTest  runs well but my code is not

why?

Ciardhubh

Quote from: iamzealotwang on October 24, 2008, 09:01:25
    protected void logic(long timeDelta) {
       day = (day + 10) % 360;
       year = (year + 5) % 360;
    }

This always runs at maximum speed, i.e. as fast as your computer can render the scene. That is why it's too fast. The mentioned Thread.sleep() is only executed if the application is not visible (e.g. minimised). Otherwise it is skipped.

To run the animation at a better speed, you have to make it dependent on the time passed since the last frame (the above is FPS dependent), e.g.:
    protected void logic(long timeDelta) {
       day = (day + DEGREES_PER_MILLISECOND * timeDelta) % 360;
       year = (year + DEGREES_PER_MILLISECOND * timeDelta) % 360;
    }

Where DEGREES_PER_MILLISECOND is the number of degrees you want to move per millisecond; something like 0.05f for example.

iamzealotwang

Thanks~

Now I set my demo likes:

private static final int FPS = 60;
private static final long SLLEEP_TIME = 1000/FPS;

protected void run() {
     while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) && !Display.isCloseRequested()) {
          now = System.currentTimeMillis();
          timeDelta = now - lastTime;
          lastTime = now;
          if (Display.isVisible()) {
              logic();
              render();
           } else {
              if (Display.isDirty()) {
                  render();
           }
           }
           
         if(timeDelta < SLLEEP_TIME)
            try {
                Thread.sleep(SLLEEP_TIME-timeDelta);
            } catch (InterruptedException ex) {
            }
            Display.update();
        }
    }

and it look's great now

O(∩_∩)O

Ciardhubh

Quote from: iamzealotwang on October 27, 2008, 11:14:40
            try {
                Thread.sleep(SLLEEP_TIME-timeDelta);
            } catch (InterruptedException ex) {
            }

If you want to sync to a certain framerate, it is probably better to use Display.sync(...). Just replace the cited code with Display.sync(FPS);


ilazarte

this is very much appreciated, thanks for your work.

Ciardhubh

Quote from: ilazarte on January 08, 2009, 07:16:17
this is very much appreciated, thanks for your work.

Thanks.

By the way, I've finished all chapters and updated the downloads. Time to get a book about GLSL or some other advanced topic, I guess :D

ilazarte

Interestingly enough I couldn't run the examples from the jar, my guess is some exception was being swallowed when I tried to click run.  However, downloading the source and running them from Eclipse is working great.  Thanks again.  Hard to believe its been almost a year since I've had a chance to look at lwjgl.  But work/life is finally opening up some time so .... :)