Game Development Reference
In-Depth Information
A frustum can be seen as a shape like a pyramid in 3D space with the
converging end at the camera and the body containing everything the
camera can see.
Check this Wikipedia article on viewing frustum at
http://
Also, read this wonderful article to get a good understanding about
frustum and camera at
http://www.badlogicgames.com/
LibGDX provides some very easy methods to check if an object is inside the frustum.
Add the following code to
MyModelTest.java
:
private Vector3 position = new Vector3();
private boolean isVisible(final Camera cam, final ModelInstance
instance) {
instance.transform.getTranslation(position);
return cam.frustum.pointInFrustum(position);
}
Here, we added
Vector3
to hold the position. In the
isVisible()
method, we
fetch the position of
ModelInstance
and next we check if that position is inside the
frustum using the function
pointInFrustum()
.
Add
isVisible()
to the
render()
function:
@Override
public void render() {
...
modelBatch.begin(cam);
int count = 0;
for (ModelInstance instance : instances) {
if (isVisible(cam, instance)) {
modelBatch.render(instance, environment);
count++;
}
}
modelBatch.end();
...
}