PDA

View Full Version : Getting color from FFP in standalone frag shader?



Verdagon
02-04-2011, 03:24 AM
Hi everyone, I'm writing a very, very simple fragment shader. This is compiled by itself, so that the FFP can supply its own vertex shader.


void main() {
vec4 color = gl_ColorSoFar;
gl_FragData[0] = vec4(color.rgb * color.a, color.a);
gl_FragData[1] = vec4(1.0);
//gl_FragColor = color;
}

I'm sure you've spotted the error: there's no such thing as gl_ColorSoFar! I have no idea what to put there. I know that in the previous stage of the FFP, the vertex shader spits out some color, how do I grab it and continue with it?

Thanks!

mobeen
02-04-2011, 04:42 AM
Hi,
I can give u two solutions for this, the first is the answer (using FFP) and the second is what u should use now (modern opengl 3.0 and above GLSL version 330).
Old GLSL version using FFP
The per vertex color attribute comes in gl_Color register in the fragment shader if you assign to gl_FrontColor in the vertex shader. Add this to your vertex shader.


//vertex shader
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
}

Then in the fragment shader, you may use gl_Color attributes as follows.



//Fragment shader
void main() {
gl_FragColor = gl_Color;
}


Modern GLSL version
Now u assign the per vertex attributes yourself and also maintain the matrices. So the same shader becomes this.


//vertex shader
#version 330
in vec4 vVertex;
in vec4 vColor;

smooth out vec4 oColor;
uniform mat4 MVP;
void main() {
gl_Position = MVP * vVertex;
oColor = vColor;
}



//fragment shader
#version 330
smooth in vec4 oColor;
out vec4 vFragColor;
void main() {
vFragColor = oColor;
}

Note that you need to maintain the attributes and matrices yourself.

Hope this helps,
Mobeen

Verdagon
02-04-2011, 01:33 PM
Thank you so much! It worked perfectly.

That stuff is really interesting...

So it used to be that glVertex() would assign a value to gl_FragCoord, but now we have to make our own function to use instead, that will bind a position to vVertex?

Alfonse Reinheart
02-04-2011, 02:29 PM
So it used to be that glVertex() would assign a value to gl_FragCoord

Not directly. glVertex() would be transformed by the current matrices into a clip-space value, which then would be processed by the OpenGL hardware into a window-space coordinate. The interpolation of that coordinate over a triangle's face would result in gl_FragCoord for a particular fragment.