简体   繁体   中英

OpenGL, when should I draw and when should I not

Imagine Im having my camera, and two squares in a 3D openGL context (using perspective) as follows :

From Top:

               /
             /    
           /      Square 1  
Camera -> +          V  Square 2
           \         |     V
             \       |     |
               \     |

So, what I will do is draw both using glBegin() and glEnd() and let the Z buffer do it's job. So far so good.

Now, Imagine I want to draw 1 million of those squares someones will be behind others of course. What will be faster, doing the last mentioned proccess for all or maybe I can make some math and discard the ones I DONT need to draw. Example:

if (should_I_Draw_It)
{
    glBegin();
    /*Draw*/
    glEnd();
}

EDIT:

It's a dynamic scene, objects may be created, destroyed, moved and/or modified.

What you want to do is called occlusion culling. Simple algorithms are very inefficient on the CPU and they should be used only when there are big objects in the foreground and small objects in the background.

Nvidia describes in the GPU Gems Chapter 29 an efficient way for occlusion culling. You can try this to improve the efficiency of your rendering

Occlusion culling for so many dynamic objects would almost always be slower than just drawing everything. Your best bet in a dynamic scene may be to just do very simple view frutrum culling. The issue is that since you are only drawing boxes with 6 quads each, it probably is still faster to just draw them all instead of spending the time to decide if you should draw them.

Regardless the simplest test you can do is check to see if the box is directly behind/perpendicular to the camera relative to the direction you are looking and if it is far enough away (bounding radius) from the camera to not intersect it.

Modern graphics drivers will automatically optimize what it does/does not draw to some degree so you can rely on that to help a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM