How to avoid drawing triangles that are outsid the viewport?

Started by
8 comments, last by 21st Century Moose 13 years, 6 months ago
Hello everybody,
I have a technical question for the experts in here:

I am developing an OpenGL application for iPhone using openGL 1.x .
When i render a big item on the device it become obviously slower and that's seem quite normal to me.
But i would have expected an increase of framerate when the object is highly zoomed in as the majority of the vertexes are outside the viewport's bounds.

I also try with glEnable(GL_SCISSOR_TEST)/glScissor(0,0,320,480) but the result is always the same?
This is really driving me crazy!

Why the not shown triangles are always computed(or why the speed remain the same while drawing a smaller portion of the object)?
There is a way to avoid this and subsequently increase the framerate?

Thank you in advance.
Greetings.

Peppe.
Advertisement
Have a look at frustum culling. The basic idea is that you test each object to see whether it lies within the frustum and only draw those that do.
OpenGL still needs to at the very least run the vertexes through the modelview and projection matrixes before it can determine whether or not they're outside the view frustum. I don't know the internal details fully, but I would expect that there are then 3 cases: fast accept (triangle is fully inside the view frustum), fast reject (triangle is fully outside the view frustum) or a third case where the triangle intersects the view frustum and it needs to go down to per-fragment testing. Older drivers might not even be able to fast accept or fast reject entire triangles. There is also vertex submission overhead on top of this; if your scene has 30,000 triangles in it, every one of them must be submitted, processed and tested by OpenGL before it can decide to accept or reject.

Running your own frustum culling in software in advance of this has traditionally been the solution. The idea is that you check on groups of triangles rather than individual ones. So if you have 300 triangles in a model, you run your own test on the entire model and just don't add it to your draw list if it fails.

There are plenty of articles on frustum culling on the web, and "bounding box" or "bounding sphere" are also good search keywords. Here's a reasonable place to start: http://www.lighthouse3d.com/opengl/viewfrustum/

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Frustum culling an entire object is a good idea. I compute each object's bounding sphere offline. You might also need to optimize your glDrawElements call by optimizing for the pre and post transform cache. There is a discussion about this in the OpenGL.org forum, in the advanced section.

Warning : it is 4 pages long!
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=257252#Post257252
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Thank you all for your replies,.
So, if I catch it, using scissor test on the entire screen drives to even worse performance because I will do the check twice.
Am I right?

I will try to implement the frustum culling using bounding box.
Thank you again for your support.
Hi,

Consider that in most cases when people talk about frustrum culling they're talking about culling on a per-object basis. What you want is to cull triangles
from being sent to the rendering pipeline that are part of the same object.

Now, there are methods to do that but I guess not every engine needs those. The
easiest is to subdivide the triangles to groups, partitioning them.

In many cases its done with a bounding volume hierarchy but that's not the only option available to you. Even a small grid or AABBs or spheres may do the job
nicely if you can get away with something simple.
-----------------------------He moves in space with minimum waste and maximum joyGalactic Conflict demo reel -http://www.youtube.com/watch?v=hh8z5jdpfXY
My object are divided into bodies, and each body is divided into faces.
Each body and face have their own BoundingBox that i previously use for Ray-intersecting purpose.
I will now use these boxes to implement a simpe frustum culling.
What do you think?
Are you certain you are vertex bound and not fill rate bound?

If you are the latter then all the offscreen triangle culling in the world won't help you out...
(which could well be the case given you experiance no speed up when zooming into the object)
The first attemp to implement a frustum culling gave me quite good result(still working on it).
Now i was wondering how to reduce the number of triangles when the objcect is really zoomed out( the inverse problem).
I was thinking to hide faces that have thier bounding box diagonal smaller than a pixel.

This way when the object is fully visibile(not zoomed) all face pass the frustum test but a lot are discarded by the "Diagonal test",when the object is very zoommed all face pass the "Diagonal test" but very few the frustum one.

What do you thing?
Just using a lower detailed mesh and using it beyond a certain range is probably the simplest way to start. Memory is cheap and plentiful, and is a resource to be used, so go ahead and use it.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement