Kelvin McClean
Kelvin McClean

Reputation: 11

OpenGL ES 2.0 Directional lighting issues (diffuse)

I'm writing a lighting shader at the moment for my OpenGLES 2.0 Renderer. I've messed around with a sample project's shader and have gotten directional lighting working on that. But when I try to implement it into our own code, it acts really strange- certain parts seemingly lighting up when they shouldn't, etc.

I'll post the shader code here, if you'd like more, please request it. Me and a team-mate have been trying to fix this for the past week and a half with no progress, we really need some help. Thanks.

final String vertexShader =
            "uniform mat4 u_MVPMatrix;"     // A constant representing the combined model/view/projection matrix.
                    + "uniform mat4 u_MVMatrix;"        // A constant representing the combined model/view matrix.
                    + "uniform vec3 u_LightPos;"        // The position of the light in eye space.
                    + "uniform vec3 u_VectorToLight;"

                    + "attribute vec4 a_Position;"      // Per-vertex position information we will pass in.
                    + "attribute vec4 a_Color;"     // Per-vertex color information we will pass in.
                    + "attribute vec3 a_Normal;"        // Per-vertex normal information we will pass in.


                    + "varying vec4 v_Color;"       // This will be passed into the fragment shader.

                    +"vec3 modelViewVertex;"
                    +"vec3 modelViewNormal;"

                    +"vec4 getPointLighting();"
                    +"vec4 getAmbientLighting();"
                    +"vec4 getDirectionalLighting();"
                    + "void main()"     // The entry point for our vertex shader.
                    + "{"
                    // Transform the vertex into eye space.
                    + "   modelViewVertex = vec3(u_MVMatrix * a_Position);"
                    // Transform the normal's orientation into eye space.
                    + "   modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));"
                    // Will be used for attenuation.

                    // Multiply the color by the illumination level. It will be interpolated across the triangle.
                   // + "   v_Color = getAmbientLighting();"
                    + "   v_Color = getDirectionalLighting();"
                    //+ "   v_Color += getPointLighting();                                       \n"

                    // gl_Position is a special variable used to store the final position.
                    // Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
                    + "   gl_Position = u_MVPMatrix * a_Position;"
                    + "}"

                    + "vec4 getAmbientLighting(){"
                    + "   return a_Color * 0.1;"
                    + "}"

                    + "vec4 getPointLighting(){"
                    + "   float distance = length(u_LightPos - modelViewVertex);"
                    // Get a lighting direction vector from the light to the vertex.
                    + "   vec3 lightVector = normalize(u_LightPos - modelViewVertex);"
                    // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
                    // pointing in the same direction then it will get max illumination.
                    + "   float diffuse = max(dot(modelViewNormal, lightVector), 0.0);"
                    // Attenuate the light based on distance.
                    + "   diffuse = diffuse * (1.0 / (1.0 + distance));" +
                    "   return a_Color * diffuse;"
                    +"}"

                    +"vec4 getDirectionalLighting(){" +

                        "vec3 lightVector = normalize(u_VectorToLight - modelViewVertex);"+
                        "float diffuse = max(dot(modelViewNormal, lightVector), 0.0);" +
                        "diffuse *= .3;"+
                        "return a_Color * diffuse;"+
                    "}";

I've left all of it in there, even stuff that we've given up on. Thanks again.

Edit: I should probably also that transforms are standard; the camera is rotated and moved by changing the view matrix, the object is rotated by changing the model matrix.

Upvotes: 1

Views: 802

Answers (1)

Stone
Stone

Reputation: 1169

In directional lighting model, all light rays are shooting in a single, uniform direction, in other words they are all parallel to each other. However, in your function getDirectionalLighting() the lightVector is dependent on the modelViewVertex which seems to be causing the the issue. Try to change it as

vec3 lightVector = normalize(u_VectorToLight);

or simply

vec3 lightVector = normalize(-u_LightPos);

Upvotes: 1

Related Questions