PDA

View Full Version : (Lighting) behaviour different at each execution



hangdog
01-11-2012, 08:07 AM
Hi

I feel I've been quite successful last few days, getting my OpenGL application up and running, with textures and lights and colors and all, thanks to a great deal of very helpful documentation on the interweb. But I can find no mention on google, these forums, the FAQ or common problems lists, or elsewhere, of the particular issue I'm running into now.

My application (I won't post code at this stage, but it follows typical form from OpenGL examples, and I don't _think_ it's relevant to the issue) uses SDL to open a window with an OpenGL context. Then it does the usual basic stuff, rendering a surface with a bunch of vertices with normals, and a single light set up above it, to produce nice lit terrain. All works fine.

Sometimes. This is the issue - on some executions, it does what is expected. On some executions, the light is in the wrong place. On some, the light is entirely absent (or perhaps under the surface so I can't see its effects).

So the question is - how can the behaviour of my application be different on different instantiations? I am very willing to believe that this is an SDL-specific issue; I've searched for it under that assumption and also turned up nothing, so I figured I would eliminate it as an OpenGL issue first.

I've taken every care I can to ensure that it is not a threading issue (though it certainly sounds like one). I make sure that my OpenGL context is open and fully created (going so far as to pause for a second) before I make any calls to OpenGL from my own code. The only thread that is calling GL functions is my applications's main thread (the only other thread is whatever SDL is running in the background, and that is only servicing keyboard events).

It may be helpful to note that everything seems to work exactly correctly _except_ the lighting, and that is quite entirely variable. I can't make it go one way or the other by starting the application from a prompt (which slows it down a bit through the use of stdout) or by double-click, or through the IDE. I just can't get any hints out of it at all as to the cause :(.

Thus, I'm hoping only that someone has ever heard of such a thing. I have googled until I'm blue in the face, and seen no mention of it. In case it's a known bug in my card or drivers, my platform is Windows 7, GeForce 8600GT, OpenGL supplied by MS SDK.

Thanks, in advance
hangdog

hangdog
01-11-2012, 08:15 AM
PS. Of course, if this is not an SDL issue, an obvious possibility is that I'm using OpenGL incorrectly, for instance issuing my initialise or in-loop commands in the wrong order. If that seems likely, I can of course post code.

hangdog
01-11-2012, 10:37 AM
(SOLVED)

Meantime, I've been bashing through my code again and again. Finally spotted the problem. In case anyone else makes the same dumb mistake as me:

I was using glLightfv to set the light position, and I was not providing four elements but only three (thinking three was enough, and having read the docs with my eyes closed, apparently). Presumably, the remaining element was being initialised to something random, hence the unpredictable behaviour.

If I've learned something about OpenGL it's that I must remember that the interface is C not C++, so things that compile may be far from things that will run correctly. So, I promise to always read the API specification carefully, particularly when passing pointers to data.

Thanks

dorbie
01-11-2012, 11:25 AM
This would have been a UMR (uninitialized memory read) setting the light vector w component to some unknown value.

A UMRs like this can go undetected in C and C++, since you're just sending a pointer to an array of implied dimension.