OpenGL programming

How hard can this be?

Two fat books hint that there may be a lot to this:

All in all, I like the "superbible" a lot, as much as I detest any book being called a "bible". The author has a good way and a good attitude.

Let's just dive in and try a demo!

In an attempt to try a fast-track to actually getting some C-code and seeing if I can compile and run it on my system, I search online and find a bouncing ball demo on the opengl website: In particular, I got bounce.c from:
http://www.opengl.org/resources/code/samples/glut_examples/mesademos/mesademos.html

There are lots more here, use the link:

http://www.opengl.org/resources/code/samples/glut_examples

Or, check this out:

http://www.opengl.org/resources

Try a naive compile

Just a plain cc on this gets lots of errors on the include files, in particular, the file GL/glut.h cannot be found.
Not a big surprise. Without a makefile or something to set search paths for include files and libraries, this is just what we expect.

How about another demo

The resources link on the opengl website leads to the following article, along with a cube.c demo (this time with a Makefile!) This is better, but it too runs afoul of the missing glut.h

A nice intro to OpenGL

Get the GLUT package

Time to take a look at what packages I have on my system.
    yum list | grep mesa

    mesa-libOSMesa.i386                      7.0.2-3.fc8            updates         
    mesa-libOSMesa.x86_64                    7.0.2-3.fc8            updates         
    mesa-libOSMesa-devel.i386                7.0.2-3.fc8            updates         
    mesa-libOSMesa-devel.x86_64              7.0.2-3.fc8            updates         

    yum list | grep glut

    freeglut.x86_64                          2.4.0-11.fc8           installed       
    freeglut.i386                            2.4.0-11.fc8           fedora          
    freeglut-devel.i386                      2.4.0-11.fc8           fedora          
    freeglut-devel.x86_64                    2.4.0-11.fc8           fedora          
    hugs98-glut.x86_64                       2006.09-3.fc7          fedora          

I have a strong suspicion that I may need the freeglut-devel package (since the devel package will probably have the headers, and I do:

	yum install freeglut-devel.x86_64

The cube demo works now!

And now the cube demo compiles (with some warnings that look like they stem from stricter prototypes for library functions). It even runs!

But bounce needs a Makefile

Bounce isn't going to get anywhere without a Makefile, so I simply copy the makefile from the cube demo and string substitute cube to bounce. now it compiles (with only one warning), but trying to run it gives me:
    (~/Mesa/bounce) cholla $ ./bounce
    freeglut (./bounce):  ERROR:  Internal error  in function fgOpenWindow
    X Error of failed request:  BadWindow (invalid Window parameter)
    Major opcode of failed request:  4 (X_DestroyWindow)
    Resource id in failed request:  0x0
    Serial number of failed request:  14
    Current serial number in output stream:  17

What is GLUT?

Anything with a name like this invites my admiration and sparks my curiosity. Ignoring for now why the above blows up, and trying to get more of the big picture, I find the following link instructive:

http://www.opengl.org/resources/libraries/glut/

GLUT stands for OpenGL Utility Toolkit. It claims to be (and no doubt is) a portable API that allows a person to write simple OpenGL applications. It is not open source! I guess that is what the freeglut packages are all about. Actually GLUT turns out to be a portable API for all the non-portable aspects of an OpenGL application, there is a sizeable OpenGL core API that is the same everywhere.

A wild guess gets the bounce demo working

Back to the question of what is wrong with the bounce demo, I try, just on a hunch, changing the display parameters as follows:
    /*
    glutInitDisplayMode(GLUT_INDEX | GLUT_DOUBLE);
    */
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
And now it works, but much too fast to look like a bouncing ball on my machine (which is a dual core 3.2 Ghz amd64). We'll figure out how to throttle that later. Apparently "INDEX' mode is not supported on my graphics hardware, but "RGBA" mode is, hmmm. GLUT_DOUBLE says to do double buffering (nothing to do with floating point single/double).

Although the cube demo seems to work just fine, (even the keys to stop and change speed all work as advertised); when I run "top" to look at cpu activity, it shows the CPU running "cube" at 100 percent, which certainly doesn't sound like hardware acceleration.


Feedback? Questions? Drop me a line!

Uncle Tom's Computer Info /[email protected]