[OpenGL] Lighting causes all colours to go grey
Archon

I've got two games using this same 'framework' which is just a shared object file that I'm putting common OpenGL functionality in.

Anyway, I'm trying to set up a good lighting system here:

//Gl is a namespace BTW
            Gl.glEnable(Gl.GL_LIGHT0);
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_AMBIENT, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
            Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_DIFFUSE, new float[] { 1.0f, 1.0f, 1.0f });
            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, new float[] { 0.5f, 0.5f, 0.5f, 0.0f });
            Gl.glLightModelfv(Gl.GL_LIGHT_MODEL_AMBIENT, new float[] { 0.5f, 0.5f, 0.5f, 0.0f });

Both games have virtually identical classes|objects at the start of the game (introduction and main menu, etc) but in one game, the texture and all of the glColor3f seems to be overridden because all of the colours are greyish - but the other game is fine.

Firstly, is there any basic attributes that I'm misconfiguring or not configuring in the code above?

Secondly, what could override all of the glColor functions to produce all-grey results?

Mr. Big

Well, if I remember correctly, what overrides the 'glColor' calls is the fact that you're using lighting. ;D
You need to set up the colors using 'glMaterial', which you are setting to gray in your code...

Archon
Quote:

Well, if I remember correctly, what overrides the 'glColor' calls is the fact that you're using lighting. ;D

Nah, light simply affects the colours but it doesn't make everything the colour of the nearest light or anything. If you project a green light on a green object, that object would look fine but if you project the same light on a purely red object, it should turn black - this is assuming that you're using diffuse lighting though. But that's the general idea.

Quote:

You need to set up the colors using 'glMaterial', which you are setting to gray in your code...

Are you certain? If I set glMaterial to 0,0,0 , wont I get black? And if I set it to 1,1,1 wont I get white in that case? Which parameter(s) should I be configuring?

What does get me the most is that it does fine with the other game, which has almost the same code up until past the main menu...

X-G

He said it right. If lighting is enabled, glColor does nothing. Instead, use glMaterial to modify the diffuse parameter. It does the same thing.

Archon

Ah yes, now I understand.

slaps forehead quite hard

I just realised that the texture is actually grey in colour and that the colours came from the glColor parameter - I was hoping of animating the title a little, and the menu selection is just a change in the glColor parameters.

But it's good to know that glMaterial would fix this up.

Bob

You can also enable GL_COLOR_MATERIAL, which takes the material color out of glColor.

X-G

But that way he won't learn anything... ;_;

Thomas Fjellstrom

He just did learn something. Two somethings in fact. glColor is ignored by default for lighting, and GL_COLOR_MATERIAL will enable using the regular colors again for lighting....

X-G

Yes, but the right way to deal with lighting is to learn to use the material properties, not to hack around it with GL_COLOR_MATERIAL.

Thomas Fjellstrom

If all you want is a colored light, just use colors :P

Archon
Quote:

He just did learn something.

Yes! I are smart!

23yrold3yrold

I had been wondering this myself, actually. It was very annoying behavior. :) Gotta try it later ....

Tobias Dammers
Quote:

Yes, but the right way to deal with lighting is to learn to use the material properties, not to hack around it with GL_COLOR_MATERIAL.

Not necessarily.
If you want your rendering code to render the model correctly both with and without lighting, GL_COLOR_MATERIAL is actually a choice that makes sense. The object rendering code doesn't need to know whether lighting is enabled, it just sets the color through glColor() and all other properties through glMaterial().
Though in practice, your objects would typically all be lit at all times.

Thread #589874. Printed from Allegro.cc