Friday, February 16, 2007

OpenGL in a Workspace

On some modern Linux systems, Croquet does not work anymore because OpenGL failes to initialize. Now, I originally wrote that code, and it worked fine for years. So it can't possibly be buggy, right? Jens Lincke of impara tracked it down to the "Composite" extension that is enabled by default nowadays. With Composite disabled, it works, enable it, and it does not.

So I turned to NVIDIA for help, thinking their driver might be buggy. Had to give them an easy way to reproduce the problem, this is the snippet I came up with:
| ogl green |
ogl := OpenGL newIn: (0@0 extent: 100@100).
green := 1.
ogl glClearColor(0, green, 0, 1).
ogl glClear(16r4000).
ogl swapBuffers.
Sensor waitClickButton.
green := 1 - green.
] repeat] ensure: [ogl destroy]
Beauty, eh? ;-) I guess nobody has done this in a workspace for a long time. Stop it with Alt-.

Anyway, NVIDIA could reproduce the problem, and found our bug:
[...] the app is trying to create a depth 24 child window of a depth 32 parent and the app specifies neither a border pixel nor a border pixmap.
Doh! I forgot to specify the border! We were just lucky that this did not happen before. Jens and y.t. made a patch, should be in the next VM. And big thanks to NVIDIA developer support!


Anonymous said...

ogl glClearColor(0, green, 0, 1).
ogl glClear(16r4000).

I'm sure I don't get that. Most of the syntax here in the snippet looks like Smalltalk syntax. But then there's this mixed in? It's amazing, because immediately I have no idea what the glClearColor arguments mean. Or the glClear. Is this one of these "on beyond Smalltalk" things I keep hearing some of the spring-from-Squeak projects talking about?

Vanessa said...

This is called "positional arguments", Andreas allowed this syntax in Croquet. The actual selector in this case is #'glClearColor()/4'.

The reason is that this is the syntax that is used in any piece of OpenGL documentation. You can alternatively write

ogl glClearColor: 0 with: green with: 0 with: 1

which I find even less readable. These are FFI methods, so they directly call out to the corresponding C function. Personally I like this, it reminds me that this is a C call rather than a Smalltalk method.

A "cool" hack is to use cascades, which allows you to copy C code almost verbatim:

glClearColor(0, green, 0, 1);

Anonymous said...

I'm looking for a way to do this cleanly within a Tweak project, but I can't seem to find the right offset(s) to put the OGL area in the project window. Any ideas?

Vanessa said...

There is no official way to convert Tweak coordinates to absolute Squeak Display coordinates. Here is a hack involving Sensor.

Besides, you might find this useful: COpenGLPlayer

Anonymous said...

Thank you! The Sensor hack is what I was looking for. I actually have the COpenGlPlayer code already, although when I tried to backtrack to find where I had gotten it, I looked everywhere except the Tweak list. Thanks again. daf