# [GLSL] Passing vars from a vertex prog to the fragment prog

## Recommended Posts

Hi, I'm trying to pass a varying vec3 from my GLSL vertex program to my fragment program. I'd like to interpolate the val across the face, for for now I've broken down the problem to the code below Vertex program:
varying vec3 eyeVec;

void main()
{
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex ;
eyeVec = vec3(1.0, 1.0, 1.0) ;
}

Fragment program:
varying vec3 eyeVec ;

void main()
{
gl_FragColor = vec4(eyeVec, 1.0) ;
}

All I see is a black object. From the code above I would expect a white object. When I do the same thing via a texture coordinate unit, I get the correct results. Is there some other code that I am missing out on? - Sid

##### Share on other sites
The problem here is that the vertex and fragment shaders are not run on a 1:1 ratio. Ex: If there is a triangle with three vertices, but takes up roughly 100 pixels onscreen, then the ratio will be 3:100.

If you need to calculate something that is passed into the fragment shader, it will have to be done before the object is rendered (before the vertex shader is called).

##### Share on other sites
eh? What on earth are you on about Taby?

Vertex shaders are ALWAYS run before the fragments for a triangle start to be processed, as the position needs to be calculated so that clipping can be applied.

I think you need to go and learn about how the 3D Pipeline works and how GLSL works before giving out more 'advice'.

As for the OP; I can't see anything wrong in the code you passed. Are you sure that that version is compiling and linking correctly?

##### Share on other sites
Quote:
 Original post by phantomeh? What on earth are you on about Taby?Vertex shaders are ALWAYS run before the fragments for a triangle start to be processed, as the position needs to be calculated so that clipping can be applied.I think you need to go and learn about how the 3D Pipeline works and how GLSL works before giving out more 'advice'.As for the OP; I can't see anything wrong in the code you passed. Are you sure that that version is compiling and linking correctly?

I think you've entirely misread my comment. What about it gives you the impression that I said anything about vertex shaders being run after fragment shaders? My point was that there is not a 1:1 correspondence, so EVEN THOUGH THE VERTEX SHADER IS RUN BEFORE THE FRAGMENT SHADER, one still cannot pass values between them on a 1:1 per-pixel basis, because it makes no sense.

For instance, this is why one must calculate the 4 eye rays constituting the image plane used in raytracing USING THE CPU, and THEN encode them into the 4 vertices of the fullscreen quad USING THE CPU, which are THEN passed along UNALTERED BY THE VERTEX SHADER OTHER THAN BY STANDARD PROJECTION, which are THEN used by the fragment shader to do the actual per-pixel rendering (which is in essence identical to what the OP is doing, even though they are not relying on the standard vertex variable gl_Vertex).

I understand the usage of varying, in that it allows values to be passed between shaders. If I've misinterpreted the OP's meaning, then I can accept that as my fault.

As for my experience with GLSL, if you did your research on my past work, you'd find that I have plenty of experience with it in the numerical solution of the Navier-Stokes Equations and the Einstein Field Equations. So **** off, to put it politely. I will not be forced into feeling bad for trying to help someone with something that I have 3.5 years of experience with -- or more, if you count the fact that I started by using assembly shaders in D3D 8 at around the turn of the century, or that I was writing custom shaders for use with Softimage|3D back in the mid 90's.

I've highlighted the important parts of my reply by using capital letters, for easier interpretation, although I'm sure you catch my drift perfectly well from the asterisks alone.

[Edited by - taby on May 7, 2008 2:27:46 PM]

##### Share on other sites
The shader program works fine, it must be a problem with compiling or linking. Use glGetProgramInfoLog and glGetShaderInfoLog to find out if there's anything wrong with your stuff.

##### Share on other sites
taby: The OP explicitly said they wanted to specify the value once for each vertex and then interpolate it across the polygon on a per-pixel basis, which is precisely what varying variables do. Unless I'm the one misunderstanding you...

redeemer91: As an initial troubleshooting step, you might want to try forcing one of the color channels to 1.0 in your fragment shader. That way, you can at least tell if geometry is being rendered and your shader's being run at all.

##### Share on other sites
Quote:
 Original post by dwahlertaby: The OP explicitly said they wanted to specify the value once for each vertex and then interpolate it across the polygon on a per-pixel basis, which is precisely what varying variables do. Unless I'm the one misunderstanding you...

I agree, in that I could be completely missing the OP's point.

It wouldn't be the first time that misintepretations have occurred due to a lack of context. For understanding this, and for calming me down, I've rated you as extremely helpful. As for Phantom, I don't rate people down, regardless of how bad they try to make me feel.

My assumption was that they were trying to do something like this:

// c++ code// UV texture space / vertex winding order//     _________//  v1|1       4|//    |         |//    |         | //    |         |//  v0|2_______3|//    u0       u1glBegin(GL_QUADS);	// stuff camera image plane corners into vertex colours	glColor3f(c.u0v1.x, c.u0v1.y, c.u0v1.z);	glVertex2f(0.0f, 1.0f);	glColor3f(c.u0v0.x, c.u0v0.y, c.u0v0.z);	glVertex2f(0.0f, 0.0f);	glColor3f(c.u1v0.x, c.u1v0.y, c.u1v0.z);	glVertex2f(1.0f, 0.0f);	glColor3f(c.u1v1.x, c.u1v1.y, c.u1v1.z);	glVertex2f(1.0f, 1.0f);glEnd();// vertex shadervarying vec3 look_at;void main(){	// transform precalculated vertex	gl_Position = ftransform();	// use interpolated colour, which substitutes for the xyz	// components of the four image plane eye rays (look-at vectors)	look_at = gl_Color.rgb;}// fragment shadervarying vec3 look_at;void main(){	// normalize then use per-pixel look_at vector here}

... which does not rely on anything calculated within the code of the vertex shader itself (it's interpolated, as you mention). My assumption was that the OP wanted to calculate the value of eyeVec within the code of the vertex shader, but on a per-pixel basis.

Anyway... My potential misinterpretation doesn't make me a completely ignorant moron, as expressed by Phantom -- who, by default, has volunteered to help the OP so that I can focus on other threads.

[Edited by - taby on May 7, 2008 2:13:09 PM]

##### Share on other sites
Quote:
 Original post by tabyI think you've entirely misread my comment.

No.

You miss read the OP's post and gave completely unrelated information.

Quote:
 As for my experience with GLSL, if you did your research on my past work, you'd find that I have plenty of experience with it in the numerical solution of the Navier-Stokes Equations and the Einstein Field Equations. So **** off, to put it politely. I will not be forced into feeling bad for trying to help someone with something that I have 3.5 years of experience with -- or more, if you count the fact that I started by using assembly shaders in D3D 8 at around the turn of the century, or that I was writing custom shaders for use with Softimage|3D back in the mid 90's.

tbh, I don't care.

I don't have time to go and look at what other people have done in the past, what with having to work and all that; you could be Tim Sweeney himself for all I care, if you give out bad information and appear to totally misunderstand the subject matter at hand I will call you on or some other newbie might well see your responce as authrative and frankly there is enough confusion surrounding GLSL at times as there is.

##### Share on other sites
Quote:
 Original post by phantomblah

The fact of the matter is that I might have misinterpreted the OP, but I did not go about calling them an idiot.

You on the other hand misinterpreted me and then called me an idiot. Your misinterpretation has nothing to do with mine, so don't try to downplay it like they're connected. These are not mutually exclusive things.

You should care about my experience. You explicitly asked for it when you assumed that I had no experience with GLSL. I'm not asking for a medal, I'm asking you to keep your trap shut the next time that you think you're omniscient.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628300
• Total Posts
2981894

• 9
• 9
• 11
• 10
• 10