0

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.

2
  • 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. Commented 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. Commented May 5, 2011 at 2:19

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

I think that it does not work, I don`t see the result and the vertex shader returns: warning C7564: assignment to attribute gl_Color
Right. Should have mentioned that you should set gl_FrontColor ( or one of it's peers like gl_BackColor ) in the vertex shader. gl_Color will be automatically set for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.