Setting light position (trap report)

Hi there,

This is not a question but a warning of a trap, and probably a request for a jogl feature. Those who can cite the OpenGL specification from memory probably won’t find this very entertaining, but still, I found it worth mentioning after investing two hours into debugging :slight_smile:

I recently enabled lighting operations in my jogl-based engine and observed some weird behavior, with lit faces flickering between almost correct result color and ambient base color randomly.

I had initialized my light’s position like this …

a_gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[]{10f,10f,10f});

… assuming that a 3-vector would be required. Of course, this is wrong, light position is set in homogenous coordinates, the correct call being …

a_gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[]{10f,10f,10f,1f});

Now the problem is that jogl did tell me exactly nothing about this. I mean, one would exepect some range checking and an error message, or at least a crash, but jogl silently accepts the too-short array and obviously sets the missing parameter … to 0, which is fatal if you are working with homogenous coordinates, if a default value then it should be 1, which would allow people to pass 3-Vectors and get valid results.

So newbies (like me :wink: ) don’t fall for it, and jogl-team, if you find the time, think of something.

Cheers

Wolfgang

Why should JOGL be doing the checking? This is standard behaviour of OpenGL. If you’re going to start putting checks in there, then you may as well build a high level API like Java3D.

Just so as you know, setting the 4th coordinate to 0 makes the light purely directional, which makes lighting calculations a bit faster.

Okay, I had that coming for not being more specific ;D

I know about the fourth coordinate and its meaning. I was talking about it being omitted, passing a too-short array. What happens in C when you issue glLight with GL_POSITION and a 3-float array? I assume some kind of error will be thrown? If not, then of course forget about my suggestion, JOGL shouldn’t do more than emulate standard OpenGL behavior, I didn’t ask for more.

Cheers

Wolfgang

Since C has no array-boundary functionality at all, it just takes whatever there is next in memory as 4th component…