How to detect if an object has moved out of the visible window in OpenGL ES?
In my Android application I am using GLSurfaceView to draw 3d object like a box object. I animate this object by translating it using OpenGL ES apis. At some point during animation, the object would have gone out of the visible viewport window. At this point I must remove this object from the collection of other objects that are being drawn. How can I detect that th开发者_运维技巧e object has gone out of the view? Short code sample would be appreciated.
Here is a frustum class for android, a port of the code from here. It allows you to extract the frustum parameters from OpenGL, and then test various geometric primitives (points, spheres, axis-aligned cuboids) for inclusion in the view volume.
Two things to look out for:
- The should-be-redundant error handling code in the update() method. See here for the story on this. Looks like it's a heisenbug.
- extractFromOGL() uses glGetFloat - this method is not implemented in the PixelFlinger software renderer. If you are targeting really low-end devices or using the emulator, you'll have to compute your projection and modelview matrices yourself and call update() manually.
What you you're looking from is called viewing frustum culling.
The viewing frustum is a geometric representation of the volume visible to the virtual camera. Naturally, objects outside this volume will not be visible in the final image, so they are discarded. Often, objects lie on the boundary of the viewing frustum. These objects are cut into pieces along this boundary in a process called clipping, and the pieces that lie outside the frustum are discarded as there is no place to draw them.
Several tutorials on the topic:
http://www.lighthouse3d.com/opengl/viewfrustum/
http://www.crownandcutlass.com/features/technicaldetails/frustum.html
精彩评论