Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's what I think is happening: The far plane is getting defined correctly but in my testing the D value of that plane is coming out much too small. So objects are getting accepted as being on the correct side of the far plane because the math is forcing the far plane to actually be much farther away than you want.</p> <p>Try a different approach: (http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-extracting-the-planes/)</p> <pre><code>float tang = tanf(fov * PI / 360.0f); float nh = near * tang; // near height float nw = nh * aspect; // near width float fh = far * tang; // far height float fw = fh * aspect; // far width glm::vec3 p,nc,fc,X,Y,Z,Xnw,Ynh; //camera position p = glm::vec3(viewMatrix[3][0],viewMatrix[3][1],viewMatrix[3][2]); // the left vector glm::vec3 X = glm::vec3(viewMatrix[0][0], viewMatrix[1][0], viewMatrix[2][0]); // the up vector glm::vec3 Y = glm::vec3(viewMatrix[0][1], viewMatrix[1][1], viewMatrix[2][1]); // the look vector glm::vec3 Z = glm::vec3(viewMatrix[0][2], viewMatrix[1][2], viewMatrix[2][2]); nc = p - Z * near; // center of the near plane fc = p - Z * far; // center of the far plane // the distance to get to the left or right edge of the near plane from nc Xnw = X * nw; // the distance to get to top or bottom of the near plane from nc Ynh = Y * nh; // the distance to get to the left or right edge of the far plane from fc Xfw = X * fw; // the distance to get to top or bottom of the far plane from fc Yfh = Y * fh; ntl = nc + Ynh - Xnw; // "near top left" ntr = nc + Ynh + Xnw; // "near top right" and so on nbl = nc - Ynh - Xnw; nbr = nc - Ynh + Xnw; ftl = fc + Yfh - Xfw; ftr = fc + Yfh + Xfw; fbl = fc - Yfh - Xfw; fbr = fc - Yfh + Xfw; m_Frustum[TOP] = planeWithPoints(ntr,ntl,ftl); m_Frustum[BOTTOM] = planeWithPoints(nbl,nbr,fbr); m_Frustum[LEFT] = planeWithPoints(ntl,nbl,fbl); m_Frustum[RIGHT] = planeWithPoints(nbr,ntr,fbr); m_Frustum[FRONT] = planeWithPoints(ntl,ntr,nbr); m_Frustum[BACK] = planeWithPoints(ftr,ftl,fbl); // Normalize all the sides NormalizePlane(m_Frustum, LEFT); NormalizePlane(m_Frustum, RIGHT); NormalizePlane(m_Frustum, TOP); NormalizePlane(m_Frustum, BOTTOM); NormalizePlane(m_Frustum, FRONT); NormalizePlane(m_Frustum, BACK); </code></pre> <p>Then planeWithPoints would be something like:</p> <pre><code>planeWithPoints(glm::vec3 a, glm::vec3 b, glm::vec3 c){ double A = a.y * (b.z - c.z) + b.y * (c.z - a.z) + c.y * (a.z - b.z); double B = a.z * (b.x - c.x) + b.z * (c.x - a.x) + c.z * (a.x - b.x); double C = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); double D = -(a.x * (b.y * c.z - c.y * b.z) + b.x * (c.y * a.z - a.y * c.z) + c.x * (a.y * b.z - b.y * a.z)); return glm::vec4(A,B,C,D); } </code></pre> <p>I didn't test any of the above. But the original reference is there if you need it.</p> <p>Previous Answer: <s>OpenGL and GLSL matrices are stored and accessed in column-major order when the matrix is represented by a 2D array. This is also true with GLM as they follow the GLSL standards.</p> <p>You need to change your frustum creation to the following.</p> <pre><code>// Calculate the LEFT side (column1 + column4) m_Frustum[LEFT][A] = (mat[3][0]) + (mat[0][0]); m_Frustum[LEFT][B] = (mat[3][1]) + (mat[0][1]); m_Frustum[LEFT][C] = (mat[3][2]) + (mat[0][2]); m_Frustum[LEFT][D] = (mat[3][3]) + (mat[0][3]); // Calculate the RIGHT side (-column1 + column4) m_Frustum[RIGHT][A] = (mat[3][0]) - (mat[0][0]); m_Frustum[RIGHT][B] = (mat[3][1]) - (mat[0][1]); m_Frustum[RIGHT][C] = (mat[3][2]) - (mat[0][2]); m_Frustum[RIGHT][D] = (mat[3][3]) - (mat[0][3]); // Calculate the TOP side (-column2 + column4) m_Frustum[TOP][A] = (mat[3][0]) - (mat[1][0]); m_Frustum[TOP][B] = (mat[3][1]) - (mat[1][1]); m_Frustum[TOP][C] = (mat[3][2]) - (mat[1][2]); m_Frustum[TOP][D] = (mat[3][3]) - (mat[1][3]); // Calculate the BOTTOM side (column2 + column4) m_Frustum[BOTTOM][A] = (mat[3][0]) + (mat[1][0]); m_Frustum[BOTTOM][B] = (mat[3][1]) + (mat[1][1]); m_Frustum[BOTTOM][C] = (mat[3][2]) + (mat[1][2]); m_Frustum[BOTTOM][D] = (mat[3][3]) + (mat[1][3]); // Calculate the FRONT side (column3 + column4) m_Frustum[FRONT][A] = (mat[3][0]) + (mat[2][0]); m_Frustum[FRONT][B] = (mat[3][1]) + (mat[2][1]); m_Frustum[FRONT][C] = (mat[3][2]) + (mat[2][2]); m_Frustum[FRONT][D] = (mat[3][3]) + (mat[2][3]); // Calculate the BACK side (-column3 + column4) m_Frustum[BACK][A] = (mat[3][0]) - (mat[2][0]); m_Frustum[BACK][B] = (mat[3][1]) - (mat[2][1]); m_Frustum[BACK][C] = (mat[3][2]) - (mat[2][2]); m_Frustum[BACK][D] = (mat[3][3]) - (mat[2][3]); </code></pre> <p></s></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload