user815129
user815129

Reputation: 2314

near/far planes and z in orthographic rasterization

Ive coded tons of shaders but Ive stumbled into something I never realized before.

I needed a vertex + fragment shader with a simple orthographic projection, with no depth test.

The camera is Z-aligned with the origin.

I disabled GL_DEPTH_TEST, and masked depth writes. It was so simple infact, that I decided I didnt even need a projection matrix. In my complete ignorance and ingenuity, I thought that for any triangle vertex, the vertex shader would simply pass x,y(,z = <whatever>,w = 1) to the fragment shader.

I actually thought the fragment shader would just need x,y coordinates, because since we're talking about orthographic projection, w = 1 (no perspective division), and since depth buffer is useless, and depth writes disabled, z could literally be anything, doesnt matter.

Of course I was wrong. I quickly found that the z component is quite used by the rasterizer, infact, I have tons of triangles that simply dont get rasterized.

I also found that tweaking the z component would dramatically change the rendered triangles. So I implemented a regular orthographic projection matrix, and that of course solved the issue, now everything is properly rasterized.

Now, out of pure curiosity, I started playing with a orthographic projection matrix and see what really produced for different input vectors, with different values for near and far clipping planes, and I really cant understand. Ok, x,y and w are all as expected, but I get completely different z values based on my near/far clip planes.

The question is: the fragment shader doesnt know anything about clip planes and projection matrices, it just gets a bunch of x,y,z,w... what is the role of the z component in the rasterization process? how does it know if something is inside the viewing frustrum or not?

I mean, for any given near/far plane in the orthographic projection matrix, I get z values that are valid for a given projection matrix, and invalid for others. Since the fragment shader has no way to know which planes were used in the matrix, how can it discards fragments? Is there a relationship between every vertex of the same primitive?

PS: I know the fragment shader is not even invoked, as I setup a SSBO + Atomic Counter and checked both are unchanged.

Upvotes: 0

Views: 2282

Answers (1)

keltar
keltar

Reputation: 18399

Vertex shader transforms vertices from model-local coordinate system to clip space. If your coordinates exceeds unit cube after perspective division (cube with [-1;1] sizes for each dimension, or [-W;W] before division), then this fragments would be clipped.

If you don't care about Z and really don't want to set correct ortho projection (e.g. you don't know Z values range and hence cannot set appropriate clip planes), you can correct Z in vertex shader by clamping it to (-W; W) range (or just setting it 0).

Upvotes: 2

Related Questions