# LWJGL Determining whether or not a polygon is on-screen

Not sure whether this is an LWJGL or math question. I want to check whether a shape is on-screen, so that I don't have to render it if it isn't. First of all, is there any simple way to do this that I am overlooking? Like some method or something that I haven't found? I'm going to assume there isn't.

I tried using my trigonometry skills, but it is hard to do this because of how glRotate also distorts the image a little for perspective and realism. Or, is there any way to easily determine if a ray starting from the camera, and going outward in a straight line intersects a shape? (I can probably do it with my math skillz, but is there an easier way?)

By the way, I can easily determine the angle at which the camera is facing around the x and y axis.

EDIT: Or, possibly, I could get the angles of a vector from the camera to the object, and compare those angles to my camera angles. But I have a feeling that the distorts from glRotate and glTranslate would be an issue. I'll try it though.

## 2 Answers

You could research frustum culling. Ideally done as coarsely as possible (say on a whole object - with maybe a bounding sphere), from this you know if it's completely inside (and all visible), part-in, or completely outside and therefore no rendering needed. Depending on what you're trying to achieve, it's possibly faster for the GPU to render some of these primitives than to try and determine their visibility first. The GPU is very good at discarding triangles that are completely outside the screen. So it might be good enough to just throw away objects that are definitely not visible and render the rest. You could cull individual polygons against the frustum too, but I'd profile this to see if it's actually faster or slowing you down.

You can use Occlusion Query for this very efficiently. It is simple thing saying how many fragments were rasterized. So first you draw all the renders to set z-buffer and than you ran occlusion query to know how big portion of the object is visible. If you need exact portion you first run query before rendering (you get number for fully visible object) and one after rendering (you get visible portion). Then divide the first value with second one(this is useful when size of object on screen changes every frame)

As this thread says, occlusion queries are supported by lwjgl.

And GPU Gems article for more details

• The lwjgl link is dead – rpax Mar 3 '17 at 15:14