Jump to content
• Advertisement

# OpenGL Static lighting with GL_COLOR_ARRAY

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello ! I have a terrain with normals at each vertex. And I have a light source which is the sun in my case. The light source has ambient and diffuse light. Now I want to make the terrain rendering a little bit faster by precalculating the lighting colors for all vertices so that I don't need the normals anymore. So instead of the GL_NORMAL_ARRAY I want to use GL_COLOR_ARRAY. But how can I easily precalculate the color value for each vertex to be stored in the GL_COLOR_ARRAY ? I know I can calculate the angle between vertex normal and lightvector from the light source, but I think I also have to bring the ambient AND diffuse calculations in and I don't really know how to exactly do it. Is there an OpenGL function which calculates for a given vertex and a coresponding given normal vector and a light source with position, ambient light factors and diffuse light vectors a final color value which I can use in the GL_COLOR_ARRAY for the given vertex ? Or do I really have to calculate this vertex color myself ? If yes, please can you explain me how to bring the ambient and diffuse part in ? Thanks very much for any suggestions or help ! Greetings Megelan

#### Share this post

##### Share on other sites
Advertisement
You have to do it yourself,but its not hard.

L: (normalized) Light Vector
N: Normal
Ma: Material ambient reflectance
Md: Material diffuse reflectance
La: Light ambient value
Ld: Light diffuse value

Diff=(L dot N)*(Ld*Md);
Ambient=(Ma*La);
VertexColor=Diff+Ambient;

#### Share this post

##### Share on other sites
Quote:
 Original post by mikemanYou have to do it yourself,but its not hard.L: (normalized) Light VectorN: NormalMa: Material ambient reflectanceMd: Material diffuse reflectanceLa: Light ambient value Ld: Light diffuse valueDiff=(L dot N)*(Ld*Md);Ambient=(Ma*La);VertexColor=Diff+Ambient;

Thx a lot ! It doesn't look hard but I thought the
VertexColor only holds the result of the light color
information combined with the vertex normal vector.

In the calculation above I already calculate the final
color from light source color AND material properties.
Shouldn't I only calculate the vertex color from the
light source vector and vertex normal vector and still use

glMaterialfv(GL_FRONT, GL_AMBIENT, MaterialAmbient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, MaterialDiffuse);

for the material reflectance ?

Thx a lot !
Greetings Megelan

#### Share this post

##### Share on other sites
If you're going to pre-calculate the lighting yourself and fake lighting with GL_COLOR_ARRAY,you will have to take into account the whole lighting equation when calculating the vertex colors.glMaterialfv works only when normal GL lighting is enabled,so it's useless in your case.

#### Share this post

##### Share on other sites
ah ok all right ! =)

Thank you very much. You helped me a lot ! :)

#### Share this post

##### Share on other sites
Ah I forgot to ask somehting.

Doesn't the global ambient light of the ambient lighting model
also have to be used in this calculation ? I know the light
source has an ambient factor but also the global lighting model
has a global ambient factor.

MSDN says :
In RGBA mode, the lighted color of a vertex is the sum of the material emission intensity, the product of the material ambient reflectance and the lighting model full-scene ambient intensity [...]

So what changes need to be made on the above calculation
to bring also the lighting model full-scene ambient
intensity in ?

Thanks :)

#### Share this post

##### Share on other sites
Just add together the global ambient with the one(s) from the light source(s) to get an ambient value which you can multiply by the ambient property of the material.

#### Share this post

##### Share on other sites
alright, but I think the specular calculation isn't that
easy especially for point lights where the position of
the light source AND the direction plays a role, right ?

I think it would be a nice addition to the glu functions
if there would be a function which calculates for a given
vertex and light sources + global light parameters the
final vertex color which includes ambient, diffuse and
specular stuff :)

#### Share this post

##### Share on other sites
Quote:
 Original post by Megelanalright, but I think the specular calculation isn't thateasy especially for point lights where the position ofthe light source AND the direction plays a role, right ?I think it would be a nice addition to the glu functionsif there would be a function which calculates for a givenvertex and light sources + global light parameters thefinal vertex color which includes ambient, diffuse andspecular stuff :)

Specular lighting is usually computed this way:
V:normalized eye-to-vertex vector

H=normalize(L+V)
Spec=((H dot N)^exp)*(Mat_Spec*Light_Spec);

However,specular lighting cannot be precalculated,because it is view-depedent.If you want specular lighting,you need to switch to GL lighting,or do your own lighting using shaders.

#### Share this post

##### Share on other sites
ok thanks a lot I won't care about the specular stuff
for the terrain then. :)

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5
• Advertisement

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013449
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!