Skip to content

Instantly share code, notes, and snippets.

@Kinwailo
Created May 18, 2017 09:39
Show Gist options
  • Save Kinwailo/22268f047134f3b69fa0962437d3d0e7 to your computer and use it in GitHub Desktop.
Save Kinwailo/22268f047134f3b69fa0962437d3d0e7 to your computer and use it in GitHub Desktop.
Extract planes from projection matrix
struct Matrix4x4
{
// The elements of the 4x4 matrix are stored in
// column-major order (see "OpenGL Programming Guide",
// 3rd edition, pp 106, glLoadMatrix).
float _11, _21, _31, _41;
float _12, _22, _32, _42;
float _13, _23, _33, _43;
float _14, _24, _34, _44;
};
void ExtractPlanesGL(
Plane * p_planes,
const Matrix4x4 & comboMatrix,
bool normalize)
{
// Left clipping plane
p_planes[0].a = comboMatrix._41 + comboMatrix._11;
p_planes[0].b = comboMatrix._42 + comboMatrix._12;
p_planes[0].c = comboMatrix._43 + comboMatrix._13;
p_planes[0].d = comboMatrix._44 + comboMatrix._14;
// Right clipping plane
p_planes[1].a = comboMatrix._41 - comboMatrix._11;
p_planes[1].b = comboMatrix._42 - comboMatrix._12;
p_planes[1].c = comboMatrix._43 - comboMatrix._13;
p_planes[1].d = comboMatrix._44 - comboMatrix._14;
// Top clipping plane
p_planes[2].a = comboMatrix._41 - comboMatrix._21;
p_planes[2].b = comboMatrix._42 - comboMatrix._22;
p_planes[2].c = comboMatrix._43 - comboMatrix._23;
p_planes[2].d = comboMatrix._44 - comboMatrix._24;
// Bottom clipping plane
p_planes[3].a = comboMatrix._41 + comboMatrix._21;
p_planes[3].b = comboMatrix._42 + comboMatrix._22;
p_planes[3].c = comboMatrix._43 + comboMatrix._23;
p_planes[3].d = comboMatrix._44 + comboMatrix._24;
// Near clipping plane
p_planes[4].a = comboMatrix._41 + comboMatrix._31;
p_planes[4].b = comboMatrix._42 + comboMatrix._32;
p_planes[4].c = comboMatrix._43 + comboMatrix._33;
p_planes[4].d = comboMatrix._44 + comboMatrix._34;
// Far clipping plane
p_planes[5].a = comboMatrix._41 - comboMatrix._31;
p_planes[5].b = comboMatrix._42 - comboMatrix._32;
p_planes[5].c = comboMatrix._43 - comboMatrix._33;
p_planes[5].d = comboMatrix._44 - comboMatrix._34;
// Normalize the plane equations, if requested
if (normalize == true)
{
NormalizePlane(p_planes[0]);
NormalizePlane(p_planes[1]);
NormalizePlane(p_planes[2]);
NormalizePlane(p_planes[3]);
NormalizePlane(p_planes[4]);
NormalizePlane(p_planes[5]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment