shading between vertices/fragments

hello.

i have started reading about opengl(es) eg from “opengl es programming guide 2.0”. Conceptually when drawing a simple triangle and setting a different colour for each vertex (eg vertex1 = red, vertex2 = blue, vertex 3 = green) my understanding is …
the vertex shader shades each vertex with its allocated colour (it could do more but it does this too).
then opengl transform the three vertices to fragments (which are esentially pixels, if theyre not culled or excluded from the rendered scene for some reason). the fragment shader then (among other possible things) shades each generated fragment.
its helpful/necessary to separate the vertex and fragment shaders since a single vertex could map to many fragments (or in fact possibly more than one vertex could map to the same fragment).

(1) is this understanding (if somewhat simplistic) essentially correct?
in this simple case (just drawing a triangle with a different colour for each vertex) the fragment shader is esentially superfluous…it would just set the colour for the corresponding fragments that was set for the corresponding vertices by the vertex shader?

(2) supposing i wanted to (have opengl) interpolate the colours of the vertices so that the interior of the triangle was coloured with a kind of gradient depending on how far it was from each vertex. where would this happen? the fragment shader only gets the pixels corresponding to the vertices of the triangle and not the interior, (so i cant do it there) right?

thanks very much in advance for any clarification.

[QUOTE=p8mode;1247228]hello.
i have started reading about opengl(es) eg from “opengl es programming guide 2.0”. Conceptually when drawing a simple triangle and setting a different colour for each vertex (eg vertex1 = red, vertex2 = blue, vertex 3 = green) my understanding is …
[/QUOTE]
OpenGL ES 2.0 Programming guide is very well written book. Conceptually maybe the best about OpenGL (of course it is my personal opinion). :slight_smile:
But your understanding is not correct.

the vertex shader shades each vertex with its allocated colour (it could do more but it does this too).
then opengl transform the three vertices to fragments (which are esentially pixels, if theyre not culled or excluded from the rendered scene for some reason). the fragment shader then (among other possible things) shades each generated fragment.
its helpful/necessary to separate the vertex and fragment shaders since a single vertex could map to many fragments (or in fact possibly more than one vertex could map to the same fragment).

What does it mean “shades”? Vertex shader transforms vertices and calculate per vertex attributes.
Find 3D pipeline somewhere and distinct operations in each stage. OpenGL Shading Language (3rd Edition) by Randi J. Rost is pretty good literature.

(1) is this understanding (if somewhat simplistic) essentially correct?
in this simple case (just drawing a triangle with a different colour for each vertex) the fragment shader is esentially superfluous…it would just set the colour for the corresponding fragments that was set for the corresponding vertices by the vertex shader?

Without fragmet shaders there is no output. Fixed pipeline stages do rasterization, and you cannot change position of each fragment, but all other transformations are done in FS.

(2) supposing i wanted to (have opengl) interpolate the colours of the vertices so that the interior of the triangle was coloured with a kind of gradient depending on how far it was from each vertex. where would this happen? the fragment shader only gets the pixels corresponding to the vertices of the triangle and not the interior, (so i cant do it there) right?

FS executes for each fragment! Not just 3 times for each vertex of the triangle, but, if the triangle is huge, maybe several hundred or thousand times for each fragment generated by the rasterization stage.

Hello,

and thanks very much for the prompt reply.

Ive already read quite a lot of technical literature but found it difficult to see through the technical jargon at times.
My question here is quite simple, and even though your answer is certainly fully correct (Im sure), Im not sure I can see how it answers my question.

Even though there’s obviously plenty I dont know and plenty of details Im brushing over, I still think it should be possible to understand what Im trying to understand in simpler terms.

>What does it mean “shades”?

In simple terms (to me) it means set various properties (“attributes”) for each vertex. In my simple example Im primarily interested in the colour.

> Without fragmet shaders there is no output. Fixed pipeline stages do rasterization, and you cannot change position of each fragment, but all other transformations are done in FS.

I cant really see how this answers my question (even though Im sure it is certainly correct).

> FS executes for each fragment! Not just 3 times for each vertex of the triangle, but, if the triangle is huge, maybe several hundred or thousand times for each fragment generated by the rasterization stage.

So youre saying that the fragment shader does in fact also get passed a fragment corresponding to each “point” in the interior of the triangle, and not just those corresponding to the vertexes as I had supposed?
I couldnt see eg from the book in question that this was clear.

Thanks very much in Advance

[QUOTE=p8mode;1247231]Hello,
My question here is quite simple, and even though your answer is certainly fully correct (Im sure), Im not sure I can see how it answers my question.
[/QUOTE]
I order to have clear answers, you need to ask clear questions.

Now I have more time to spare so let’s analyze each statement…

its helpful/necessary to separate the vertex and fragment shaders since a single vertex could map to many fragments (or in fact possibly more than one vertex could map to the same fragment).

Word map is not correct. Fragments are generated according to vertices atributes, but there is no mapping between vertices and fragments.

(1) is this understanding (if somewhat simplistic) essentially correct?

Since you assumes fragment shaders are superfluous, and they are mandatory, obviously something is incorrect. :slight_smile:

in this simple case (just drawing a triangle with a different colour for each vertex) the fragment shader is esentially superfluous…it would just set the colour for the corresponding fragments that was set for the corresponding vertices by the vertex shader?

In vertex shader a “vertex color” is set. There are no corresponding fragments. The interior of the primitive is filled (potentially) with plenty of fragments. Each vertex of the primitive (or just one in flat shading using desktop GL) influence values of all fragments in the interior of the primitive. That’s why I claim there is no “corresponding” fragment(s), unless you assume all fragments of the interior are corresponding to all vertices of the primitive together.

(2) supposing i wanted to (have opengl) interpolate the colours of the vertices so that the interior of the triangle was coloured with a kind of gradient depending on how far it was from each vertex. where would this happen?

It is done automatically. After vertex transformation, primitives are assembled, then rasterized, and during rasterization vertex output variables (varyings/out attributes) are interpolated. In desktop GL, there are three interpolated qualifiers: smooth, flat and noperspective. Those qualifiers define how interpolation is done. Unfortunatelly, as far as I know, OpenGL ES 2.0 does not support defining interpolation. It allows just linear interpolation

the fragment shader only gets the pixels corresponding to the vertices of the triangle and not the interior, (so i cant do it there) right?

NO! See above.

Ive already read quite a lot of technical literature but found it difficult to see through the technical jargon at times.

Unfortunately, you need to accept that technical jargon as soon as possible. It is not such hard, just requires some time and reading to place right terms in their context.

So youre saying that the fragment shader does in fact also get passed a fragment corresponding to each “point” in the interior of the triangle, and not just those corresponding to the vertexes as I had supposed?
I couldnt see eg from the book in question that this was clear.

Since I have OpenGL ES 2.0 Programming Gide in my hands now, I can point to the exact locations in the book. :slight_smile:
Reread pages 3 to 11. The section “OpenGL ES 2.0” gives overview of the pipeline.

Hello,
and thank you for the detailed and helpful explanation.

>Reread pages 3 to 11. The section “OpenGL ES 2.0” gives overview of the pipeline.

Thanks for pointing this out. It mentions for example “the mechanism used to generate a varying value for each fragment from the vary values assigned to each vertex of the primitive is called interpolation”. I think this is about all in says directly in relation to what was puzzling me. I obviously misinterpreted what is being interpolated and why (when reading this the first time). I also had read in “WebGL: up and running” (chapter 1,page 14) “the fragment shader is responsible for generating the final color output of each pixel for the transformed vertices”.
I think one could say the former is somewhat ambiguous and the latter somewhat misleading.
Thanks to your remarks I believe I understand now.

For any one else who might be interested I have also since found an excellent and clear explanation here
http://iphonedevelopment.blogspot.de/2010/11/opengl-es-20-for-ios-chapter-4.html
which contains the explicitly clear explanation I was missing in (the otherwise excellent OpenGL ES 2.0 Programming Guide)

Thank again and
Best Wishes.