Game Development Community

Gfx help - how to calculate exact screen area covered by a quad?

by Orion Elenzil · in Torque Game Engine · 02/14/2007 (3:04 pm) · 2 replies

Hey all.

i would like to efficiently and at least somewhat accurately calculate the percentage of screen area covered by a simple quad.


thinking so far:

first test the world-aligned bounding box against the camera frustum,
throwing out quads who do fail that test.

for the following steps, the quad will be treated as two triangles,
but i'm going to refer to them in the singular.

next, test the actual triangle for intersection with the frustum.
- not sure this step is really a good idea.

next, and here's where it gets tricky,
i would like to employ the standard gfx pipeline clipping of the triangle against the frustum by which a single triangle which is possibly only partially onscreen is broken up into several entirely on-screen triangles.

then project the triangles and add up their areas.

- that's the actually accurate approach,
but i worry that it may be prohibitively costly.

i'd like to do this test against say 20 quads at least every third frame or so.

any thoughts ?

#1
02/15/2007 (12:59 am)
If you just want the projected screen area of a quad, look up "gluProject". You can use "glGet" to get the modelview and projection matrices that this function needs. You can project it first and then do the clipping in screen space, it's much easier that way.

If you're trying to compute how much area of a quad is actually visible and not covered up by other stuff, then it's much harder. I'd reccommend looking into something like NVOcclusionQuery, which can tell you how many pixels of a quad would be rasterized and visible to the player, taking into accout clipping and zbuffer and everything else.

Any of these approaches should be very fast, you could test thousands of quads per frame, although using NVOcclusionQuery (or the equivalent for other hardware) obviously needs a graphics card that supports it.

My question is, what are you trying to achieve with this? There may already be some feature in Torque that can do what you want without having to write such a low-level thing yourself.

Joel
#2
02/15/2007 (1:39 am)
Hi joel - thanks for writing.

the application here is getting metrics for in-game advertisement "impressions".

one of the basic parameters into measuring an impression is how many pixels of the advertisement quad are actually visible.

as you note, this has two obvious levels of accuracy: how much of the quad is within the frustum, and then how much of it is not occluded by other junk. i'd like to be as accurate as possible for the first part, and then i'm content with being approximate for the second.

for the first part, it's the screen-space clipping which i'm balking at, but maybe i shouldn't be.
now where's my Foley & VanDamme got to ? ;)

NVOcclusionQuery sounds very interesting; i hadn't heard of anything like that.
i'll look into it. thanks!

orion