1
votes

I'm new to OpenGL, and I'm trying to understand vertex and fragment shaders. It seems you can use a vertex shader to make a gradient if you define the color you want each of the vertices to be, but it seems you can also make gradients using a fragment shader if you use the FragCoord variable, for example.

My question is, since you seem to be able to make color gradients using both kinds of shaders, which one is better to use? I'm guessing vertex shaders are faster or something since everyone seems to use them, but I just want to make sure.

1
That depends on the kind of gradient. If you just want a (per-face) linear gradient, then use the vertex shader and let the GPU do the interpolation. For other types, you will mostly need the fragment shader.Nico Schertler
Yep, per face was what I wanted. Thanks for the infoMysteryPancake

1 Answers

2
votes

... since everyone seems to use them

Using vertex and fragment shaders are mandatory in modern OpenGL for rendering absolutely everything. So everyone uses both. It's the vertex shader responsibility to compute the color at the vertices, OpenGL's to interpolate it between them, and fragment shader's to write the interpolated value to the output color attachment.

† OK, you can also use a compute shader with imageStore, but I'm talking about the rasterization pipeline here.