PDA

View Full Version : Screen coordinates in shader



SKoder
10-18-2004, 11:13 PM
How can i calculate screen position of a vertex
in vertex shader?[HLSL, GLSL]

In GLSL i used gl_FragCoord.xy , but i got very poor speed!

1024
10-18-2004, 11:47 PM
Umm... screen position of a vertex in vertex shader AND gl_FragCoord confuse me.

Anyway: screen pos is postTransformVertexCoord.xy/postTransformVertexCoord.w. +some scaling and remaping as you need it.

SKoder
10-19-2004, 12:33 AM
Your solution look's not right!

I will tell what i'am doing in details:
Scene is draws into two passes:

First pass i draw into a texture, for each pixel i get some value, as a result I obtain the following:
Image (http://evolution.times.lv/paint1.jpg)
And in the second pass I need to read values of the pixel i'am rendering.

SKoder
10-19-2004, 02:12 AM
Thanks, i have done it!

vince
10-19-2004, 06:23 AM
Just like 1024, I'm also skeptical about the use of gl_FragCoord in a vertex shader. On the other hand, I had the same problem and I fixed it by using the following:

screenCoord = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = screenCoord;
screenCoord /= screenCoord.w; // perspective divide
screenCoord.x = (screenCoord.x+1.)*fViewport[2]/2. + fViewport[0]; // viewport transformation
screenCoord.y = (screenCoord.y+1.)*fViewport[3]/2. + fViewport[1]; // viewport transformation

Unfortunately, the viewport isn't directly accessible from the shader and you need to pass the parameters. It's just redoing what the vertex postprocessing would do.

macarter
10-19-2004, 09:52 AM
Originally posted by SKoder:
How can i calculate screen position of a vertex
in vertex shader?[HLSL, GLSL]

In GLSL i used gl_FragCoord.xy , but i got very poor speed!Starting with Catalyst 4.8 referencing the fragment position with a non-zero polygon offset resulted in software rendering. I contacted ATI about the problem and got them to admit that using x,y, or w should not require software rendering. Only z is a problem. A bug fix request was entered.

SKoder
10-22-2004, 02:34 AM
vince
I don't understand what is a viewport transformation!

What values should i put in fViewport

Also I have tryed another way:

In vertex shader:

vec4 texpos = pos * gl_ModelViewProjectionMatrix;

and in pixel shader:

vec4 color = texture2DProj(TexSample, texpos);

but I get next:
Image (http://evolution.times.lv/Error.jpg)