Archived

This topic is now archived and is closed to further replies.

noob4life

Problems with frustum culling with the SDK d3dutil sample...should this work?

Recommended Posts

First post ever... here goes I''m trying to make a very basic terrain engine using VB and DirectX. My problem is that most frustum culling tutorials deal with either C++ or opengl ... Then I found this code to calculate the view frustum planes in d3dutil.bas which comes with the SDK for directx8. I really have no idea how it figures out where the frustum planes are, but I think that it is getting the top and bottom clipping planes wrong... Here is the code ''----------------------------------------------------------------------------- '' Name: D3DUtil_ComputeClipPlanes ''----------------------------------------------------------------------------- Sub D3DUtil_ComputeClipPlanes(veye As D3DVECTOR, vat As D3DVECTOR, vUp As D3DVECTOR, fov As Single, front As Single, back As Single, aspect As Single) Dim vDir As D3DVECTOR Dim vright As D3DVECTOR Dim vFrontCenter As D3DVECTOR Dim vFrontUp As D3DVECTOR Dim vFrontRight As D3DVECTOR Dim vBackCenter As D3DVECTOR Dim vBackRight As D3DVECTOR Dim vbackLeft As D3DVECTOR Dim vBackRightTop As D3DVECTOR Dim vBackLeftTop As D3DVECTOR Dim vBackRightBot As D3DVECTOR Dim vBackLeftBot As D3DVECTOR Dim dx As Single Dim dy As Single ''Establish our basis vector D3DXVec3Subtract vDir, vat, veye D3DXVec3Normalize vDir, vDir D3DXVec3Normalize vUp, vUp D3DXVec3Cross vright, vDir, vUp dx = Tan(fov / 2) * back dy = dx * aspect '' '' '' /| vbackleft (top,bot) '' / | '' vfront | '' /| | '' eye ----| vbackcenter '' \| | '' \ |dx '' \ | '' \| vbackright (top,bot) '' '' ''compute vbackcenter D3DXVec3Scale vBackCenter, vDir, back D3DXVec3Add vBackCenter, vBackCenter, veye ''compute vbackright D3DXVec3Scale vBackRight, vright, dx D3DXVec3Add vBackRight, vBackCenter, vBackRight ''compute vbackleft D3DXVec3Scale vbackLeft, vright, -dx D3DXVec3Add vbackLeft, vBackCenter, vbackLeft ''compute vbackrighttop D3DXVec3Scale vBackRightTop, vUp, dy D3DXVec3Add vBackRightTop, vBackRight, vBackRightTop ''compute vbacklefttop D3DXVec3Scale vBackLeftTop, vUp, dy D3DXVec3Add vBackLeftTop, vBackRight, vBackLeftTop ''compute vbackrightbot D3DXVec3Scale vBackRightBot, vUp, -dy D3DXVec3Add vBackRightBot, vBackRight, vBackRightBot ''compute vbackleftbot D3DXVec3Scale vBackLeftBot, vUp, -dy D3DXVec3Add vBackLeftBot, vBackRight, vBackLeftBot ''compute vfrontcenter D3DXVec3Scale vFrontCenter, vDir, front D3DXVec3Add vFrontCenter, vFrontCenter, veye ''compute vfrontright D3DXVec3Scale vFrontRight, vright, dx D3DXVec3Add vFrontRight, vFrontCenter, vFrontRight ''compute vfrontup D3DXVec3Scale vFrontUp, vUp, dy D3DXVec3Add vFrontUp, vFrontCenter, vFrontUp ReDim g_ClipPlanes(6) g_numClipPlanes = 6 ''front plane D3DXPlaneFromPointNormal g_ClipPlanes(0), veye, vDir ''back plane Dim vnegdir As D3DVECTOR D3DXVec3Scale vnegdir, vDir, -1 D3DXPlaneFromPointNormal g_ClipPlanes(1), vBackCenter, vnegdir ''right plane D3DXPlaneFromPoints g_ClipPlanes(2), veye, vBackRightTop, vBackRightBot ''left plane D3DXPlaneFromPoints g_ClipPlanes(3), veye, vBackLeftTop, vBackLeftBot ''top plane D3DXPlaneFromPoints g_ClipPlanes(4), veye, vBackLeftTop, vBackRightTop ''bot plane D3DXPlaneFromPoints g_ClipPlanes(5), veye, vBackRightBot, vBackLeftBot g_numClipPlanes = 4 End Sub And here is the code to check if a sphere is inside of the view frustum... ''----------------------------------------------------------------------------- '' Name: D3DUtil_IsSphereVisible ''----------------------------------------------------------------------------- Function D3DUtil_IsSphereVisible(SphereCenterInWorldSpace As D3DVECTOR, SphereRadius As Single) As Long Dim i As Long Dim dist As Single For i = 0 To g_numClipPlanes - 1 dist = D3DXMATH_PLANE.D3DXPlaneDotCoord(g_ClipPlanes(i), SphereCenterInWorldSpace) If dist < -1 * SphereRadius Then ''sphere is completely behind the plane ''if its behind any plane then its clipped ''Debug.Print SphereRadius '' If i = 2 Then Stop D3DUtil_IsSphereVisible = 0 Exit Function End If Next D3DUtil_IsSphereVisible = 1 End Function This code only checks against the front, back, left and right planes and skips the top and bottom; I didnt write it, so I assume whoever did couldn''t get them to work. The remmed out debug.print in there is another good hint towards that point... When I set it to use the top and bottom clipping planes it sometimes skips drawing alot of things that should have been drawn depending on the camera angle and position. I''m very sure that I''m passing the correct values to these (been fighting with it for a week)... without the top and bottom clipping planes, it likes to draw almost my entire terrain every frame when the camera is aimed straight down and its just too slow. Does anyone see an error in the code, or have an idea what I could be doing wrong?

Share this post


Link to post
Share on other sites