Search code examples
openglopengl-3

OpenGL projection clipping


For example if we have ortho projection:

left = -aspect, right = aspect, top = 1.0, bottom = -1.0, far = 1.0, near = -1.0

And will draw triangle at -2.0, it will be cut of by near clipping plane. Will it really saves some precious rendering time?

Culling determinate if we need to draw something and discards if out of our view (written by programer in vertex shader/in main program). Clipping == cheap auto culling?

Also just in theme of cheap culling - will be

if(dist(cam.pos, sprite.pos) < MAX_RENDER_DIST)
draw_sprite(sprite);

just enough for simple 2d game?


Solution

  • Default OpenGL clipping space is -1 to +1, for x, y and z.

    The conditional test for sprite distance will work. It is kind of not needed, as the far clipping plane will do almost the same thing. Usually it is good enough. There are cases where the test is needed. Objects at the corner inside the clipping planes may come outside the far clipping plane with the camera turns. The reason is that distance from the camera to the corner is longer than the perpendicular distance from the camera to the far clipping plane. This is not a problem if you have a 2D game and do not allow changes of the camera viewing angle.

    If you have a simple 2D game, chances are high that you do not need to worry about graphics optimization. If you are drawing sprites outside of the clipping planes, you save time. But how much time you save depends. If a huge amount of the sprites are outside, you may save considerable time. But then, you should probably consider what algorithm you use, and not draw things that are not going to be shown anyway. If only a small percentage of the sprites are outside, then the time saved will be negligible.