Take the 2-minute tour ×

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!

share|improve this question
Screenshots? What do you mean by "doesn't work"? –  a stray cat Apr 17 '10 at 2:30
It doesn't display anything on the screen because the coordinates are not getting homogenized, while 2 is showing the result correctly.. I'll post a screenshot soon. Thanks! –  Kay Apr 22 '10 at 20:30
add comment

2 Answers

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 ?

share|improve this answer
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 Apr 19 '10 at 0:13
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 Apr 22 '10 at 20:33
add comment

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.

share|improve this answer
So it should be homogenized automatically, right? –  Kay Apr 22 '10 at 20:30
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 Apr 22 '10 at 21:52
add comment

Your Answer


