Frustum culling on
How it should look
It's almost like it's rendering the opposite of what is expected, however that's not constantly the case. It's highly dependent on the view rotation and position.
Frustum code
Frustum viewFrustum(renderContext.projectionMatrix()*renderContext.viewMatrix());
Frustum::Frustum(const glm::mat4& matrix)
{
// Extract the frustum faces from the projection*view matrix
// http://web.archive.org/web/20120531231005/http://crazyjoke.free.fr/doc/3D/plane%20extraction.pdf
mPlanes[Face::LEFT].n.x = matrix[3][0] + matrix[0][0];
mPlanes[Face::LEFT].n.y = matrix[3][1] + matrix[1][0];
mPlanes[Face::LEFT].n.y = matrix[3][2] + matrix[2][0];
mPlanes[Face::LEFT].d = matrix[3][3] + matrix[3][0];
mPlanes[Face::RIGHT].n.x = matrix[3][0] - matrix[0][0];
mPlanes[Face::RIGHT].n.y = matrix[3][1] - matrix[1][0];
mPlanes[Face::RIGHT].n.y = matrix[3][2] - matrix[2][0];
mPlanes[Face::RIGHT].d = matrix[3][3] - matrix[3][0];
mPlanes[Face::DOWN].n.x = matrix[3][0] + matrix[0][1];
mPlanes[Face::DOWN].n.y = matrix[3][1] + matrix[1][1];
mPlanes[Face::DOWN].n.y = matrix[3][2] + matrix[2][1];
mPlanes[Face::DOWN].d = matrix[3][3] + matrix[3][1];
mPlanes[Face::UP].n.x = matrix[3][0] - matrix[0][1];
mPlanes[Face::UP].n.y = matrix[3][1] - matrix[1][1];
mPlanes[Face::UP].n.y = matrix[3][2] - matrix[2][1];
mPlanes[Face::UP].d = matrix[3][3] - matrix[3][1];
mPlanes[Face::FAR].n.x = matrix[3][0] - matrix[0][2];
mPlanes[Face::FAR].n.y = matrix[3][1] - matrix[1][2];
mPlanes[Face::FAR].n.y = matrix[3][2] - matrix[2][2];
mPlanes[Face::FAR].d = matrix[3][3] - matrix[3][2];
mPlanes[Face::NEAR].n.x = matrix[3][0] + matrix[0][2];
mPlanes[Face::NEAR].n.y = matrix[3][1] + matrix[1][2];
mPlanes[Face::NEAR].n.y = matrix[3][2] + matrix[2][2];
mPlanes[Face::NEAR].d = matrix[3][3] + matrix[3][2];
for (int i = 0; i < 6; i++)
{
mPlanes[i].normalize();
}
}
bool Frustum::sphereIntersects(const glm::vec3& pos, float radius)
{
// cheack against each plane
for (int i = 0; i < 6; i++)
{
if (glm::dot(mPlanes[i].n, pos) + mPlanes[i].d + radius <= 0)
{
return false;
}
}
return true;
}
Where I check each point glm::vec4 point = renderContext.projectionMatrix()*renderContext.viewMatrix() *
glm::vec4((float)x*World::CHUNK_SIZE, (float)c*World::CHUNK_SIZE, (float)z*World::CHUNK_SIZE, 0);
if (!viewFrustum.sphereIntersects(glm::vec3(point.x, point.y, point.z), 16.f))
{
continue;
}