3

I am not able to get the right shading on all the faces of the cube I drew. I get a smooth transition from one of the face to another face which does not show the edge properly.

One a different face (where I get the desired edge), I get the shading such that it shows the two triangles which make up that face. I believe the problem is with the normals I am specifying. I am attaching my vertex and normal matrix and my vertex and fragment shader code. vertex and normal matrix are same.

I guess the problem is with the normals but I have tried almost everything with them but the effect does not change.

//normal matrix and vertex matrix are same

static const float normals[]=

    {
         //v0,v1,v2,v3,
         //v4,v5,v6,v7
        0.5,0.5,0.5, -0.5,0.5,0.5, -0.5,-0.5,0.5, 0.5,-0.5,0.5, 

        0.5,0.5,-0.5, -0.5,0.5,-0.5, -0.5,-0.5,-0.5, 0.5,-0.5,-0.5 

    };

//vertex shader

attribute vec4 color;
attribute vec4 position; 

attribute vec4 normal;

uniform mat4 u_mvpMatrix;
uniform vec4 lightDirection;
uniform vec4 lightDiffuseColor;
uniform float translate;

varying vec4 frontColor;



//varying vec4 colorVarying;



void main()

{

   vec4 normalizedNormal = normalize(u_mvpMatrix* normal);
   vec4 normalizedLightDirection = normalize(lightDirection);

   float nDotL = max(dot(normalizedNormal, normalizedLightDirection), 0.0);


   frontColor =  color * nDotL * lightDiffuseColor;


  gl_Position = u_mvpMatrix * position;


}

//fragment shader

varying lowp vec4 frontColor;

void main()
{


        gl_FragColor = frontColor;


}

Please help? Thanks in advance !!

pad
  • 41,040
  • 7
  • 92
  • 166
  • You know, you're not supposed to answer your question to elaborate it. I just stumbled on your answer by accident. If you want to communicate on a given answer, you're supposed to 'add a comment' ; this way, the poster is notified and can answer. Then, for new question material, it is usual to edit the original question to add more information. – rotoglup Jun 07 '11 at 18:56
  • Apparently your answer has been deleted, it would be helpful if you posted a screenshot of your problem. – rotoglup Jun 07 '11 at 18:57

1 Answers1

2

Your problem is not related to your normal matrix, it is related to your input normals.

From what I read, you provide 8 normals, one for each vertex, meaning that you provide pre-averaged normals ; there is no way your vertex shader can un-average them.

To have proper discontinuities in your lighting, you need discontinuous normals : for each quad, you have 4 vertices, each with a normal matching the quad normal. So you end up with 6x4 = 24 normals (so as many vertices).

You may want to take a look at this for more detailed explanation : http://www.songho.ca/opengl/gl_vertexarray.html

rotoglup
  • 5,223
  • 25
  • 37