I need to pass near and far value in glPerspective in opengl code. I am getting all my vertices in eye space by multiplying with ModelViewMatrix in the vertex shader. My problem is that, I need to find the minimum and maximum value out of this, so that I can pass that value to glPerspective. How would I do that? Do I need to calculate them in the vertex shader or in the client space( C code) ?
Share
near and far are typically not calculated, but just set to reasonable values. ‘near’ should be close enough that near objects are not clipped, but not so near that all z-buffer precision is gone. ‘far’ just needs to be far enough away for anything you want to render.
In any event the vertex shader isn’t the best place to calculate them, because the matrices get passed in to the shader, so you need to know the values before you get that far.
(it can be viable/useful to calculate near/far dynamically for things like shadows, where you want high-precision – in which case, base it on bounding volumes of the objects you want to render, or some other such approximation).