OpenGL gl_Postion in geometry shader

77 Views Asked by At

I am passing vertices from the vertex shader to the geometry shader. I am doing a perspective projection in the geometry shader. I understand that before the fragment shader, OpenGL divides the gl_Position vector by the w component. Does this happen before or after the geometry shader? If it is after, is there a quick way to do it before without manually dividing by w in the vertex shader?

Edit: Right now this is the end of my vertex shader.

gl_Position = proj*view*model*vPos;
gl_Position = (1.0/gl_Position.w)*gl_Position;
1

There are 1 best solutions below

4
Nicol Bolas On

OpenGL divides the gl_Position vector by the w component.

No, it does not. It divides the clip-space position of a vertex to be rasterized by the clip-space position's w coordinate. The clip-space position of a vertex is the value of gl_Position emitted for that vertex from the last vertex processing stage before rasterization. All of the shader stages before rasterization are vertex processing stages.

A vertex processing stage which is not the last such stage can output to gl_Position, but this has no special meaning. The next vertex processing stage could take that as an input, but the value is passed through as is.

If it is after, is there a quick way to do it before without manually dividing by w in the vertex shader?

You really shouldn't want to. It is perfectly reasonable to do processing on vertices in post-projection space as 4D values. You can apply rotation, translation, etc to these vertices as you see fit, and the result will still "make sense" if you do it in the correct space. You can even apply a second projection to them.