Search code examples
c#opengl3dglslopentk

How to use flat shading in OpenTK.Graphics.OpenGL4?


I need to use flat shading in OpenTK.

I know there is a function in OpenGL (c++) called glShadeModel, and it was in OpenTK named (GL.)ShadeModel, but it's only in OpenTK.Graphics.ES11, and it's pretty old.

How can I do it using OpenTK.Graphics.OpenGL4?


Solution

  • glShadeModel has been replaced by Interpolation qualifiers.

    However glShadeModel is still supportend in "desktop" OpenGL if you are using compatibility profile OpenGL Context and are not using a shader program. This means you need to use the immediate mode and you have to draw by glBegin/glEnd sequences or fixed function attributes, without a shader program.

    If you are using a core profile OpenGL context and/or a shader program and you want to achieve "flat" shading, you have to use the flat Interpolation qualifier for the vertex shader output variables. For instance:

    Vertex shader

    #version 460
    
    // [...]
    in vec3 aColor;
    
    flat out vec3 vColor;
    
    void main()
    {
        vColor = aColor;
    
        // [...]
    } 
    

    Fragment shader

    #version 460
    
    flat in vec3 vColor;
    out vec4 fragColor; 
    
    void main()
    {
        fragColor = vec4(vColor, 1.0);
    }