I finally have some frustum culling code working but it is very accurate. It's so accurate that when you move or make changes to the frustum, some of the edge triangles disappear. Quite odd.
Is there a way to fix this? It's only during movement this happens. On the next frame, the frustum is restored and everything looks fine.
The culling works very good, however.
This is a modification of some code I found on the net that wasn't working correctly. So I fixed it and it works very well. If you understand the theory I'm simply finding the positive and negative vertices of an axis-aligned bounding box in world space. If the positive vertex of the AABB is on the wrong side of the frustum plane, then I do an early out since this box is outside of the frustum.Code:WORD Cull(D3DXVECTOR3 &min,D3DXVECTOR3 &max) { bool bIntersect=false; WORD result=0; D3DXVECTOR3 n(min.x,min.y,min.z); D3DXVECTOR3 p(min.x,min.y,min.z); for (WORD i=0;i<6;i++) { if (m_Planes[i].m_Normal.x <= 0) { n.x = max.x; p.x= min.x; } else { n.x=min.x; p.x=max.x; } if (m_Planes[i].m_Normal.y <= 0) { n.y=max.y; p.y=min.y; } else { n.y=min.y; p.y=max.y; } if (m_Planes[i].m_Normal.z <= 0) { n.z=max.z; p.z=min.z; } else { n.z=min.z; p.z=max.z; } if (m_Planes[i].DistanceToPoint(&p) < 0) { return CULL_OUT; } if (m_Planes[i].DistanceToPoint(&n) >= 0) { bIntersect=true; } } if (bIntersect) { return CULL_INTERSECT; } else return CULL_IN; }
In my code I found it takes more time to actually clip and intersected volume than it does just to send the vertices to the hardware and let it clip them. So if the result is CULL_IN or CULL_INTERSECT, I simply draw the object. For a quad tree terrain system I'm working on if the result is CULL_INTERSECT then I will recurse deeper into the tree and test each child node until I reach a leaf at which point the vertices in the leaf will be drawn.
But this issue of watching tri's disappear while moving toward the terrain in the vertical, or yawing left or right is not cool. It does not happen when you are moving away from the terrain in the vertical.
The current system is not using the quadtree, but it is ready for it.
This system can do sphere intersection tests, AABBs (and soon OB's using a nice transform from Moller and Haines), and points.
This terrain is culling on per-cell basis or two triangles at once. Perhaps that is why it is getting fragged at the edges?