title: Issues when simulating directional light in OpenGL
body: <p>I'm working on OpenGL application using the QT5 Gui framework, However, I'm not an expert in OpenGL and I'm facing a couple of issues when trying to simulate directional light. I'm using 'almost' the same algorithm I used in an WebGL application which works just fine.</p>
  
  <p>The application is used to render multiple adjacent cells of a large gridblock (each of which is represented by 8 independent vertices) meaning that some vertices of the whole gridblock are duplicated in the VBO. the normals are calculated per face in geometry shader as shown below in the code.</p>
  
  <p>QOpenGLWidget paintGL() body.</p>
  
  <pre><code>void OpenGLWidget::paintGL()
  {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_CULL_FACE);        
  
      m_camera = camera.toMatrix(); 
      m_world.setToIdentity(); 
  
      m_program-&gt;bind();
      m_program-&gt;setUniformValue(m_projMatrixLoc, m_proj);
      m_program-&gt;setUniformValue(m_mvMatrixLoc, m_camera * m_world); 
  
      QMatrix3x3 normalMatrix = (m_camera * m_world).normalMatrix();
      m_program-&gt;setUniformValue(m_normalMatrixLoc, normalMatrix);
  
      QVector3D lightDirection = QVector3D(1,1,1);
      lightDirection.normalize();
      QVector3D directionalColor = QVector3D(1,1,1);
      QVector3D ambientLight = QVector3D(0.2,0.2,0.2);
      m_program-&gt;setUniformValue(m_lightDirectionLoc, lightDirection);
      m_program-&gt;setUniformValue(m_directionalColorLoc, directionalColor);
      m_program-&gt;setUniformValue(m_ambientColorLoc, ambientLight);
  
      geometries-&gt;drawGeometry(m_program);
      m_program-&gt;release();
      }
  }
  </code></pre>
  
  <p>Vertex Shader</p>
  
  <pre><code>#version 330
  layout(location = 0) in vec4 vertex; 
  uniform mat4 projMatrix;
  uniform mat4 mvMatrix;
  
  void main()
  {
    gl_Position = projMatrix * mvMatrix * vertex;
  }
  </code></pre>
  
  <p>Geometry Shader</p>
  
  <pre><code>#version 330
  layout ( triangles ) in;
  layout ( triangle_strip, max_vertices = 3 ) out;
  out vec3 transformedNormal;
  uniform mat3 normalMatrix;
  
  void main()
  {
      vec3 A = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
      vec3 B = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
  
      gl_Position = gl_in[0].gl_Position;
      transformedNormal = normalMatrix * normalize(cross(A,B));
      EmitVertex();
      gl_Position = gl_in[1].gl_Position;
      transformedNormal = normalMatrix * normalize(cross(A,B));
      EmitVertex(); 
      gl_Position = gl_in[2].gl_Position;
      transformedNormal = normalMatrix * normalize(cross(A,B));
      EmitVertex();
      EndPrimitive();
  }
  </code></pre>
  
  <p>Fragment Shader</p>
  
  <pre><code>#version 330
  in vec3 transformedNormal;
  out vec4 fColor;
  uniform vec3 lightDirection;
  uniform vec3 ambientColor;
  uniform vec3 directionalColor;
  
  void main()
  {
          highp float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0);
          vec3 vLightWeighting = ambientColor + directionalColor * directionalLightWeighting;
          highp vec3 color = vec3(1, 1, 0.0);
          fColor = vec4(color*vLightWeighting, 1.0);
  }
  </code></pre>
  
  <p>The 1st issue is that lighting on the faces seems to change whenever the camera angle changes (camera location doesn't affect it, only the angle). You can see this behavior in the following snapshot. My guess is that I'm doing something wrong when calculating the normal matrix, but I can't figure out what it is. 
  <a href="https://i.stack.imgur.com/PsyWn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PsyWn.png" alt="enter image description here"></a></p>
  
  <p>The 2nd issue (The one causing me headaches) is whenever The camera is moved, edges of the cells show blocky and rigged lines that flickers when the camera moves around. this effect gets really nasty when there are too many cells clustered together.</p>
  
  <p><a href="https://i.stack.imgur.com/wHj5l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wHj5l.png" alt="enter image description here"></a></p>
  
  <p>The model used in the snapshot is just a sample slab of 10 cells to better illustrate the faulty effects. The actual models (gridblock) contain up to 200K cells stacked together.</p>
  
  <blockquote>
    <p><strong><em>EDIT</em></strong>: 2nd issue solution.
    I was using <code>znear/zfar</code> of 0.01f and 50000.0f respecticvely, when I
    changed the znear to 1.0f, this effect disappeared. According to <a href="https://www.opengl.org/wiki/Depth_Buffer_Precision" rel="nofollow noreferrer">OpenGL Wiki</a> this is caused by a zNear clipping plane value that's too close to 0.0. As the zNear clipping plane is set increasingly closer to 0.0, the effective precision of the depth buffer decreases dramatically</p>
    
    <p><strong><em>EDIT2</em></strong>: I tried debug drawing the normals as suggested in the comments,
    I quickly realized that I probably shouldn't calculate them based on
    gl_Position (after MVP matrix multiplication in VS) instead I should use the
    original vertex locations, so i modified the the shaders as follows:</p>
  </blockquote>
  
  <p>Vertex Shader (UPDATED)</p>
  
  <pre><code>#version 330
  layout(location = 0) in vec4 vertex; 
  out vec3 vert;
  uniform mat4 projMatrix;
  uniform mat4 mvMatrix;
  
  void main()
  {
    vert = vertex.xyz;
    gl_Position = projMatrix * mvMatrix * vertex;
  }
  </code></pre>
  
  <p>Geometry Shader (UPDATED)</p>
  
  <pre><code>#version 330
  layout ( triangles ) in;
  layout ( triangle_strip, max_vertices = 3 ) out;
  in vec3 vert [];
  out vec3 transformedNormal;
  uniform mat3 normalMatrix;
  
  void main()
  {
      vec3 A = vert[2].xyz - vert[0].xyz;
      vec3 B = vert[1].xyz - vert[0].xyz;
  
      gl_Position = gl_in[0].gl_Position;
      transformedNormal = normalize(normalMatrix * normalize(cross(A,B)));
      EmitVertex();
      gl_Position = gl_in[1].gl_Position;
      transformedNormal = normalize(normalMatrix * normalize(cross(A,B)));
      EmitVertex(); 
      gl_Position = gl_in[2].gl_Position;
      transformedNormal = normalize(normalMatrix * normalize(cross(A,B)));
      EmitVertex();
      EndPrimitive();
  }
  </code></pre>
  
  <p>But even after this modification the normals of the surface still change with the camera angle, as shown below in the screenshot. I dont know if the normal calculation is wrong or the normal matrix calculation is done wrong or maybe both... </p>
  
  <p><a href="https://i.stack.imgur.com/pfqyQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pfqyQ.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>EDIT3: 1st Issue Solution: changing normal calculation in GS from
    <code>transformedNormal = normalize(normalMatrix * normalize(cross(A,B)));</code>
    to <code>transformedNormal = normalize(cross(A,B));</code> seems to solve the
    problem. Omitting the normalMatrix from the calculation fixed the
    issue and the normals dont change with the viewing angle.</p>
  </blockquote>
  
  <p>If I missed any important/relevant information, please notify me in a comment.</p>