Help with opengl es 2.0 light

I’ve been to several tutorial sites and have read most of the “OpenGL ES 2.0 programming guide”, only to have myself confused by the fact that there are no enabled lights (unlike opengl 3.0) and I need to provide my own.

could anyone provide me with any guide, tutorial, or a post (I’ve searched the forum) about this issue? It would allow me to see how others have solved this. the shaders that I have compiled thus far work perfectly fine and I’m simply stuck with this issue for several days now.

I would be most grateful if anyone could help me
Thanks.

So your issue isn’t with how to write shaders, but rather how to implement a “light source” using shaders?

There is an excellent description of lighting equations in the OpenGL Programming Guide (red book). Also, for a more GLSL centric view, see the OpenGL Shading Language book (Orange book). This has code you can copy/paste verbatim.

And there are a number of GLSL lighting tutorials out there. Google “GLSL lighting tutorial”. For instance: Lighthouse3D: GLSL Lighting tutorial. This also has code you can copy/paste verbatim.

And there’s also 3D Labs ShaderGen, which’ll generate the code for light sources and other fixed function pipeline stuff.
Here: http://mew.cx/glsl/shadergen/ and here: http://3dshaders.com/home/index.php?option=com_weblinks&catid=14&Itemid=34. This too has code to copy/paste verbatim.

The problem is that in Lighthouse3D they are using gl_LightSource[0].position which doesnt exist on ES 2.0, since the decleration of light has been removed. As far as I understand all the diffuse, specular, spot cutoff and others need to be defined inside the shader and calculated… So, while these uniforms are understood, others, like halfplane and attenuation factors are hard.

Thanks for your answer! :slight_smile:

Makes sense. The references I gave will still tell you what you need to know, in English, and the latest Orange book (3rd ed) gives you GLSL source for both directional and positional light sources that doesn’t presume gl_LightSource[]. But most of the gl_LightSource[] things are just uniform values you’d pass GL directly anyway.

First of all, if you’re new to lighting equations, I’d suggest you implement a directional light source first. The code is very short. And to make it even shorter, only compute the ambient and diffuse components. Add specular and emission after you get that working. See Listing 9.6 in the Orange book. After that go for a positional light source (see Listing 9.7 in the Orange book).

As to your specific questions above (“others, like halfplane and attenuation factors are hard” – you probably mean half-vector instead of halfplane), they’re really pretty easy, you just haven’t seen the write-up yet. The Red Book has it: http://glprogramming.com/red/chapter05.html. In particular, see The Mathematics of Lighting.

To your specific questions, the half-vector is the vector halfway between the light source and the eye (from the vantage point of the surface point we’re lighting). It’s used for typical specular calculations, and is commonly computed in a shader as:

normalize( L + eye )

where L is a unit vector toward the light and eye is a unit vector toward the eye. The half vector is the vector “s” on the above page. Note that the half vector is a constant for directional light sources (L is constant) if you assume that the eyepoint isn’t very close (i.e. eye is constant), aka non-local viewer.

The distance attenuation coefficients are even simpler – see that same section for the equation. You don’t need to mess with this until you implement a positional light source and you want the light source intensity to fall off with distance.

Just ask if you have any other questions. Try some shader code for ambient and diffuse for a basic directional light source.

Thanks for explanation, I’ve got into glprogramming.com and found out some very usefull links! :slight_smile:

I still do have one final question: gl_FrontColor and gl_FrontSecondaryColor have been removed from the ES version, Now, as far I understand it those are in effect built-in varying variables to store color values for Front facing, and so when the Fragment Shader receives the varying color its actually an interpolated color -

“depending on the orientation of the current primitive, i.e. the interpolation is done using either the gl_FrontColor or the gl_BackColor values”
(http://www.lighthouse3d.com/opengl/glsl/index.php?color)

How can I modify the Shader to interpolate the color values without this command? To preserve its functionality via building my own version. I’ve tried to get the color data straight to the gl_FragColor using vec3/vec4 Varying Variables and got no results.

Well, there are two concepts you mention here:
[ol][li] One is Color vs. SecondaryColor (used to implement legacy separate specular color, i.e. GL_SEPARATE_SPECULAR_COLOR, with which you can have white specular highlights with a red MODULATE texture, for instance).[] And FrontColor vs. Back*Color (used to implement legacy two-sided lighting, i.e. GL_LIGHT_MODEL_TWO_SIDE).[/ol]Ignoring the first, since it’s just a trivial bolt-on that works with or without the second…[/li]
I haven’t worked with or read up on OpenGL ES, but clearly to implement two-sided lighting (in one pass), “something” has to do the “front or back” determination. The question is whether that “something” has been removed from your ES’s built-in behavior (you need to find out), and if so, where “you” are going to determine it.

A few options:
[ol][li]You can render the geometry twice, each with one-sided lighting, and get two-sided lighting that way.[]You can determine which side the eye can see in the vertex shader, and then only pass down the color for the appropriate side for interpolation[]You can pass both colors down from the vertex shader for interpolation, and wait until the fragment shader to determine which side you can see and then use the appropriate color (wastes an interpolator)[*]If you’ve got fast geometry shaders, maybe you can use it to determine which side is visible, and pipe down only the side color you can see.[/ol][/li]I’d search the GLES spec you’re developing for for the word “facing” and see what turns up. In the latest GLSL spec for instance, there’s a bool gl_FrontFacing fragment shader input you could use. Though you can do your own dot product of normal and eye vector instead.

Thank you :slight_smile: