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.
-
What do you mean by "This color I can obtain a color only in the vertex shader.?" Normally you can use color values in the fragment shader that are created by the vertex shader. So, I'm not sure what you are asking.Himadri Choudhury– Himadri Choudhury2011-05-05 02:10:18 +00:00Commented May 5, 2011 at 2:10
-
Sorry, this is a misprint.) @DasBoot "Normally you can use color values in the fragment shader that are created by the vertex shader." - How to do this? I mean how to transfer it.itun– itun2011-05-05 02:19:02 +00:00Commented May 5, 2011 at 2:19
2 Answers
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.
Comments
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.