views:

622

answers:

2

Hi everyone, I was expecting gl_Position to automatically get homogenized (divided by w), but it seems not working.. Why do the followings make different results?

1) void main() { vec4 p; ... omitted ... gl_Position = projectionMatrix * p; }

2) ... same as above ... p = projectionMatrix * p; gl_Position = p / p.w;

I think the two are supposed to generate the same results, but it seems it's not the case. 1 doesn't work while 2 is working as expected.. Could it possibly be a precision problem? Am I missing something? This is driving me almost crazy.. helps needed. Many thanks in advance!

+1  A: 

From the GLSL spec 1.2:

The variable gl_Position is available only in the vertex language and is intended for writing the homogeneous vertex position.

So it's not automatically homogenized.

Maurice Gilden
So it should be homogenized automatically, right?
Kay
Yes. I think your (1) should be correct while the other one shouldn't be. If they produces different results that may be because gl_Position gets clipped before getting homogenized and not after. Maybe your object is too near/far away?
Maurice Gilden
+1  A: 

the perspective divide cannot be done before clipping, which happens after the vertex shader is completed. So there is no reason that you could observe the w divide in the vertex shader.

The GL will do the perspective divide before the rasterization of the triangles, before the fragment shader runs, though.

What are you trying to do that does not work ?

Bahbar
Note that if you have enabled geometry shader stage, perspective divide is done after GS. In all cases, it's done just before the fragment program gets invoked.
Stringer Bell
Thanks for the answers, I understand that the homogenized result can't be seen in the vertex shader stage, but the problem is that the two above generate different final result. If coordinates get homogenized automatically, they should display the same things eventually, shouldn't they? I'll post a screenshot soon, thank you very much for the answers..
Kay