1

I'm just starting to learn graphics using opengl and barely grasp the ideas of shaders and so forth. Following a set of tutorials, I've drawn a triangle on screen and assigned a color attribute to each vertex.

Using a vertex shader I forwarded the color values to a fragment shader which then simply assigned the vertex color to the fragment.

Vertex shader:

[.....]

layout(location = 1) in vec3 vertexColor;
out vec3 fragmentColor;

void main(){
    [.....]
    fragmentColor = vertexColor;

}

Fragment shader:

[.....]

out vec3 color;

in vec3 fragmentColor;

void main()
{

color = fragmentColor;

}

So I assigned a different colour to each vertex of the triangle. The result was a smoothly interpolated coloured triangle.

My question is: since I send a specific colour to the fragment shader, where did the smooth interpolation happen? Is it a state enabled by default in opengl? What other values can this state have and how do I switch among them? I would expect to have total control over the pixel colours using a fragment shader, but there seem to be calculations behind the scenes that alter the result. There are clearly things I don't understand, can anyone help on this matter?

Tsaras
  • 455
  • 2
  • 4
  • 18
  • I believe the interpolation occurs after the vertex shader, before the fragment shader. So the fragment shader recieves the interpolated value. Think though. What would happen if a triangle had a different value for every vertex? Interpolation is necessary, and be thankful for it. edit: look at the `flat` keyword. – BWG Mar 17 '14 at 02:26
  • have a look at this answer http://stackoverflow.com/questions/4421261/vertex-shader-vs-fragment-shader this may help explain why this interpolation occurs and how you can override this default behaviour (HINT: by creating your own fragment shader to handle coloring) – Matthew Pigram Mar 17 '14 at 02:31
  • It is a good thing, I was expecting to have to write the interpolation algorithm inside one of the shaders myself, but whatever my tools do behind the scenes, I want to know of. – Tsaras Mar 17 '14 at 02:33
  • @MatthewPigram Huh. I actually didn't know you could override the behavior. I've always just took interpolation for granted. What things can you override? – BWG Mar 17 '14 at 02:36

1 Answers1

2

Within the OpenGL pipeline, between the vertex shading stages (vertex, tesselation, and geometry shading) and fragment shading, is the rasterizer. Its job is to determine which screen locations are covered by a particular piece of geometry(point, line, or triangle). Knowing those locations, along with the input vertex data, the rasterizer linearly interpolates the data values for each varying variable in the fragment shader and sends those values as inputs into your fragment shader. When applied to color values, this is called Gouraud shading.

source : OpenGL Programming Guide, Eighth Edition.

If you want to see what happens without interpolation, call glShadeModel(GL_FLAT) before you draw. The default value is GL_SMOOTH.

Michael Hagar
  • 626
  • 5
  • 20