Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [OpenGL] Lighting causes all colours to go grey

Credits go to Mr. Big and X-G for helping out!
This thread is locked; no one can reply to it. rss feed Print
[OpenGL] Lighting causes all colours to go grey
Archon
Member #4,195
January 2004
avatar

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
Member #6,196
September 2005

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
Member #4,195
January 2004
avatar

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
Member #856
December 2000
avatar

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

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Archon
Member #4,195
January 2004
avatar

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
Free Market Evangelist
September 2000
avatar

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

--
- Bob
[ -- All my signature links are 404 -- ]

X-G
Member #856
December 2000
avatar

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

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Thomas Fjellstrom
Member #476
June 2000
avatar

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....

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

X-G
Member #856
December 2000
avatar

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.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Thomas Fjellstrom
Member #476
June 2000
avatar

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

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Archon
Member #4,195
January 2004
avatar

Quote:

He just did learn something.

Yes! I are smart!

23yrold3yrold
Member #1,134
March 2001
avatar

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

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Tobias Dammers
Member #2,604
August 2002
avatar

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.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: