Question

How to transfer data from vertex shader to fragment shader without changes? I need to say to the vertex pixels that they have this color. This color I can obtain only in the vertex shader.

Was it helpful?

Solution

Here's a good tutorial on GLSL: NeHe GLSL tutorial

If you want to share data between vertex and fragment shaders use one of the built in types, for example gl_Color

If you want to pass through the color computed by the vertex shader to through the fragment shader you would create a fragment shader with the following line: gl_FragColor = gl_Color

gl_Color will be automatically set for you from the colors written by the vertex shader. You write a color from the vertex shader by setting one of the built-in variables, like gl_FrontColor, or one of it's peers: gl_BackColor etc.

OTHER TIPS

You have to use a varying, because each fragment is "influenced" by more than one vertex (unless you are rendering GL_POINTS), so you have to interpolate them across the line/polygon. Recent versions of GLSL allow to specify flat shading interpolation, which doesn't interpolate the value throughout the primitive, ignoring the values from the other vertices.

I suspect thought that what you want to do is to render only the pixels corresponding to the vertices in a different color, is that correct? In that case it's not so easy, you would probably want to render the filled polygons first, and then re-render as GL_POINTS. At that point, varying variables are not interpolated because each fragment is influenced by a single vertex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top