How to get flat normals on a cube

后端 未结 3 1204
北海茫月
北海茫月 2021-01-05 22:12

I am using OpenGL without the deprecated features and my light calculation is done on fragment shader. So, I am doing smooth shading.

My problem, is that when I am d

3条回答
  •  一向
    一向 (楼主)
    2021-01-05 23:00

    If you do the lighting in camera-space, you can use dFdx/dFdy to calculate the normal of the face from the camera-space position of the vertex.

    So the fragment shader would look a little like this.

    varying vec3 v_PositionCS; // Position of the vertex in camera/eye-space (passed in from the vertex shader)
    
        void main()
        { 
          // Calculate the face normal in camera space
          vec3 normalCs = normalize(cross(dFdx(v_PositionCS), dFdy(v_PositionCS)));
    
          // Perform lighting 
          ...
          ...
        }
    

提交回复
热议问题