How to get flat normals on a cube

后端 未结 3 1200
北海茫月
北海茫月 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:15

    Since a geometry shader can "see" all three vertices of a triangle at once, you can use a geometry shader to calculate the normals and send them to your fragment shader. This way, you don't have to duplicate vertices.

    // Geometry Shader
    
    #version 330 
    
    layout(triangles) in;
    layout(triangle_strip, max_vertices = 3) out;
    
    out vec3 gNormal;
    
    // You will need to pass your untransformed positions in from the vertex shader
    in vec3 vPosition[];
    
    uniform mat3 normalMatrix;
    
    void main()
    {
        vec3 side2 = vPosition[2] - vPosition[0];
        vec3 side0 = vPosition[1] - vPosition[0];
        vec3 facetNormal = normalize(normalMatrix * cross(side0, side2));
    
        gNormal = facetNormal;
        gl_Position = gl_in[0].gl_Position;
        EmitVertex();
    
        gNormal = facetNormal;
        gl_Position = gl_in[1].gl_Position;
        EmitVertex();
    
        gNormal = facetNormal;
        gl_Position = gl_in[2].gl_Position;
        EmitVertex();
    
        EndPrimitive();
    }
    

提交回复
热议问题