openglglslfragment-shadervertex-shadergouraud

How are colour-values interpolated in a Gouraud-shaderscript?


The gouraud-shader computes lighting at the corners of each triangle and linearly interpolates the resulting colours for each pixel covered by the triangle.

How would you program this certain interpolation (in GLSL-code) ?

Do you even have to code this yourself or does OpenGL interpolate (internally) single-handedly ?


Solution

  • From https://www.opengl.org/wiki/Type_Qualifier_(GLSL) :

    Interpolation qualifiers

    Certain inputs and outputs can use interpolation qualifiers. These are for any values which could be interpolated as a result of rasterization. These include:

    Interpolation qualifiers control how interpolation of values happens across a triangle or other primitive. There are three basic interpolation qualifiers.

    flat​

    The value will not be interpolated. The value given to the fragment shader is the value from the Provoking Vertex for that primitive.

    noperspective​

    The value will be linearly interpolated in window-space. This is usually not what you want, but it can have its uses.

    smooth​

    The value will be interpolated in a perspective-correct fashion. This is the default if no qualifier is present.


    Since smooth is the default one, OpenGL interpolates it for you. If you would not want the interpolation, you would define your color in your shaders with keyword flat, something like this:

    flat vec3 color;