title: OpenGL - vertex normals in OBJ
body: <p>I want to know how can I use the vertex normals for lightning effect? Currently what I have is I can send both vertex and texture coords to the shader and use them but with normals, I don't know how to use them in the shader program. Below is what I have so far.</p>
  
  <pre><code>    // vertex shader
      layout(location = 0) in vec4 vert;
      layout(location = 1) in vec4 color;
      layout(location = 2) in vec2 texcoord;
      uniform mat4 m_model;
      uniform mat4 m_view;
      uniform mat4 m_proj;
      void main() {
          gl_Position = m_proj * m_view * m_model * vert;
      }
  
      // fragment shader
      in vec2 fragtexcoord;
      out vec4 color;
      uniform sampler2D textureunit;
      void main(void) {
          color = texture(textureunit, fragtexcoord);
      }
  </code></pre>
  
  <p><strong>EDIT</strong>
  Here are my shaders for now.</p>
  
  <p>vertex shader</p>
  
  <pre><code>    layout(location = 0) in vec4 vert;
      layout(location = 1) in vec4 color;
      layout(location = 2) in vec2 texcoord;
      layout(location = 3) in vec4 normal;
      out vec4 LightIntensity;
      uniform vec4 LightPosition;
      uniform vec4 Kd; 
      uniform vec4 Ld;
      uniform mat4 m_model;
      uniform mat4 m_view;
      uniform mat4 m_proj;
      void main() {
          gl_Position = m_proj * m_view * m_model * vert;
  
          mat4 normalmatrix = transpose(inverse(m_view));
  
          vec4 tnorm = normalize(normalmatrix * normal);
          vec4 eyeCoords = m_model * vec4(vert);
          vec4 s = normalize(vec4(LightPosition - eyeCoords));
  
          LightIntensity = Ld * Kd * max(dot(s, tnorm), 0.0);
      }
  </code></pre>
  
  <p>Fragment shader.</p>
  
  <pre><code>    in vec4 LightIntensity;
      out vec4 color;
      void main(void) {
          color = vec4(LightIntensity);
      }
  </code></pre>
  
  <p>Currently getting a black cube with no shading. Probably I did something wrong here in the shader which I don't have any idea which one :(</p>
  
  <p>UPDATE :</p>
  
  <p>vertex</p>
  
  <pre><code>    layout(location = 0) in vec4 vert;
      layout(location = 1) in vec4 color;
      layout(location = 2) in vec2 texcoord;
      layout(location = 3) in vec4 normal;
      out vec2 fragtexcoord;
      out vec4 fragnormal;
      uniform mat4 m_model;
      uniform mat4 m_view;
      uniform mat4 m_proj;
      void main() {
          gl_Position = m_proj * m_view * m_model * vert;
          fragtexcoord = texcoord;
          fragnormal = normal;
      }
  </code></pre>
  
  <p>fragment</p>
  
  <pre><code>    in vec2 fragtexcoord;
      in vec4 fragnormal;
      out vec4 fragment_color;
      uniform sampler2D textureunit;
      void main(void) {
          vec4 lt_ambient = vec4(0.2, 0.2, 0.2, 1.0);
          vec4 lt_direct = vec4(0.8, 0.8, 0.8, 1.0);
          vec4 lt_direct_dir = vec4(1.5, 1.0, 1.0, 1.0);
          vec4 color = texture(textureunit, fragtexcoord);
          fragment_color = (lt_ambient + (lt_direct * dot(lt_direct_dir, -fragnormal))) * color;
      }
  </code></pre>
  
  <p>I don't know what to put for lt_direct_dir that's why it has values like that :)</p>
  
  <p><strong>UPDATE :</strong> Below is the working shaders for me</p>
  
  <pre><code>    // vertex shader
      layout(location = 0) in vec4 vert;
      layout(location = 1) in vec4 color;
      layout(location = 2) in vec2 texcoord;
      layout(location = 3) in vec4 normal;
  
      out vec4 fragposition;
      out vec4 fragcolor;
      out vec4 fragnormal;
      out vec2 fragtexcoord;
  
      uniform mat4 m_model;
      uniform mat4 m_view;
      uniform mat4 m_proj;
      uniform vec4 lightpos;
      void main() {
          gl_Position = m_proj * m_view * m_model * vert;
          mat4 m_normal = transpose(inverse(m_model));
          fragposition = m_model * vert;
          fragnormal = m_normal * normal;
          fragtexcoord = texcoord;
      }
  
      // fragment shader
      in vec4 fragposition;
      in vec4 fragnormal;
      in vec2 fragtexcoord;
  
      out vec4 fragment_color;
  
      uniform sampler2D textureunit;
  
      void main() {
          vec4 lt_pnt_pos = vec4(2.5, 2.5, 2.5, 1.0);
          vec4 lt_pnt_col = vec4(0.8, 0.8, 0.8, 1.0);
          vec4 lt_amb_col = vec4(0.2, 0.2, 0.2, 1.0);
  
          vec4 lt_dir = normalize(lt_pnt_pos - fragposition);
          float li = dot(fragnormal, lt_dir);
          if(li &lt; 0.0) {
              li = 0.0;
          }
          vec4 color = texture(textureunit, fragtexcoord);
          fragment_color = color * (lt_amb_col + (lt_pnt_col * li));
      }
  </code></pre>