Well DBP's is flawed in some cases. Like I said if any part of the model in in the cameras view, it draws the entire thing. Even if it's just an offset (which is odd and only on some models), it doesn't even have to be a poly. Say you have a model with all of the polys offset to make a 10 by 10 by 10 box to be positioned at 1000, 1000, 1000 but the object's position is 0,0,0. DBP seems to make a 1010, 1010, 1010, box for the frustum check instead of a 10, 10, 10 box. So I guess in some cases you could use this for objects as well. I'll have to get a demo going lol.
[edit] It seems the offset bug may be fixed in 5.9
Here is a code showing DBP's frustum culling working on objects.
sync on : sync rate 0
autocam off
position camera 0, 0, -1000
for i = 1 to 50
make object cube i, 10
position object i, rnd(1000)-500, rnd(1000)-500, 0
next i
do
rotate camera camera angle x()+(mousemovey()/2),camera angle y()+(mousemovex()/2),0
if camera angle x()>90 then xrotate camera 90
if camera angle x()<-90 then xrotate camera -90
cx#=camera angle x() : cy#=camera angle y() : cz#=camera angle z()
if upkey()=1
move camera 3
endif
if downkey()=1
move camera -3
endif
if rightkey()=1
xrotate camera 0: yrotate camera cy# + 90: move camera 3: xrotate camera cx#: yrotate camera cy#
endif
if leftkey()=1
xrotate camera 0: yrotate camera cy# - 90: move camera 3: xrotate camera cx#: yrotate camera cy#
endif
set cursor 0,0
print "FPS : ", screen fps()
print "Polys rendered : ", statistic(1)
sync
loop
Just rotate the camera around and watch the number of polys raise and lower depending on the number of objects in sight.
[edit] Here is my code culling limbs. there is no culling unless you press the return key so you can see the difference. It would be way faster if I had an octree setup.
[edit2] And here it is a little faster but not much due to optimizing the get frustum code.
`dim arrays for culling
dim frustum#(5,3)
dim clip#(15)
`display stuff
sync on : sync rate 0
set camera range .5, 10000
autocam off
position camera 0, 30, 0
hide mouse
cubesize# = 10.0
`make test object with 1024 limbs
a = 1
depth# = -320
wide# = -320
`tall
for j = 1 to 32
`across
for k = 1 to 32
if j = 1 and k = 1
make object cube 1, cubesize#
make mesh from object 1, 1
position object a, wide#, 0, depth#
else
add limb 1, a, 1
offset limb 1, a, (k-1)*20, 0, (j-1)*20
inc a, 1
endif
next k
next j
`the sphere needs to totally enclose the cube so the size is the distance
`between 2 diagonal corners (corners that do not share sides)
test_sphere_size# = sqrt((cubesize#*cubesize#)+(cubesize#*cubesize#)+(cubesize#*cubesize#))
`main loop
do
`mlook
rotate camera camera angle x()+(mousemovey()/2),camera angle y()+(mousemovex()/2),0
if camera angle x()>90 then xrotate camera 90
if camera angle x()<-90 then xrotate camera -90
cx#=camera angle x() : cy#=camera angle y() : cz#=camera angle z()
`simple flying movement
if upkey()=1
move camera 3
endif
if downkey()=1
move camera -3
endif
if rightkey()=1
xrotate camera 0: yrotate camera cy# + 90: move camera 3: xrotate camera cx#: yrotate camera cy#
endif
if leftkey()=1
xrotate camera 0: yrotate camera cy# - 90: move camera 3: xrotate camera cx#: yrotate camera cy#
endif
`cull if wanted
if returnkey() = 1
begtime# = timer()
`get the camers frustum to check against as it changes with the position
`and rotation of the camera
gosub Extractfrustum
frustumtime# = timer()-begtime#
begtime# = timer()
`test each limb against the frustum (you would use an octree to cut down
`the cheks)
for i = 0 to 1023
`use a sphere to test as it is faster than a box or cube
if SphereInfrustum( limb position x( 1, i), limb position y( 1, i), limb position z( 1, i), test_sphere_size# ) > 0
if limb visible(1,i) = 0
show limb 1, i
endif
else
if limb visible(1,i) = 1
hide limb 1, i
endif
endif
next i
culltime# = timer()-begtime#
endif
set cursor 0,0
print "Hold returnkey for culling."
print "FPS : ",screen fps()," Polys : ",statistic(1)
print "Time to get frustum : ",frustumtime#," Culling time : ",culltime#
sync
loop
null=delete matrix4(ProjectionMatrix)
null=delete matrix4(ViewMatrix)
null=delete matrix4(ClipMatrix)
end
Extractfrustum:
t# = 0
ClipRange = 1
ProjectionMatrix = 1
ViewMatrix = 2
ClipMatrix = 3
null=MAKE MATRIX4(ProjectionMatrix)
null=MAKE MATRIX4(ViewMatrix)
null=MAKE MATRIX4(ClipMatrix)
`get the screen space matrix
Projection Matrix4 ProjectionMatrix
`get the camera space matrix
VIEW MATRIX4 ViewMatrix
`multiply the camera space matrix by the screen space matrix
`to get the clip matrix to get the frustum planes from
MULTIPLY MATRIX4 ClipMatrix, ViewMatrix, ProjectionMatrix
`assign the clip matrix elements to an array so we can use them
for i = 0 to 15
clip#(i) = DK Get Matrix4 Element( ClipMatrix, i )
next i
`/* Extract the numbers for the RIGHT plane */
frustum#(0,0) = clip#( 3) - clip#( 0);
frustum#(0,1) = clip#( 7) - clip#( 4);
frustum#(0,2) = clip#(11) - clip#( 8);
frustum#(0,3) = clip#(15) - clip#(12);
`/* Normalize the result */
rangeClipx# = frustum#(0,0)
rangeClipy# = frustum#(0,1)
rangeClipz# = frustum#(0,2)
null=make vector3(ClipRange)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(0,0) = frustum#(0,0) / t#
frustum#(0,1) = frustum#(0,1) / t#
frustum#(0,2) = frustum#(0,2) / t#
frustum#(0,3) = frustum#(0,3) / t#
`/* Extract the numbers for the LEFT plane */
frustum#(1,0) = clip#( 3) + clip#( 0);
frustum#(1,1) = clip#( 7) + clip#( 4);
frustum#(1,2) = clip#(11) + clip#( 8);
frustum#(1,3) = clip#(15) + clip#(12);
`/* Normalize the result */
rangeClipx# = frustum#(1,0)
rangeClipy# = frustum#(1,1)
rangeClipz# = frustum#(1,2)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(1,0) = frustum#(1,0) / t#
frustum#(1,1) = frustum#(1,1) / t#
frustum#(1,2) = frustum#(1,2) / t#
frustum#(1,3) = frustum#(1,3) / t#
`/* Extract the BOTTOM plane */
frustum#(2,0) = clip#( 3) + clip#( 1);
frustum#(2,1) = clip#( 7) + clip#( 5);
frustum#(2,2) = clip#(11) + clip#( 9);
frustum#(2,3) = clip#(15) + clip#(13);
`/* Normalize the result */
rangeClipx# = frustum#(2,0)
rangeClipy# = frustum#(2,1)
rangeClipz# = frustum#(2,2)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(2,0) = frustum#(2,0) / t#
frustum#(2,1) = frustum#(2,1) / t#
frustum#(2,2) = frustum#(2,2) / t#
frustum#(2,3) = frustum#(2,3) / t#
`/* Extract the TOP plane */
frustum#(3,0) = clip#( 3) - clip#( 1);
frustum#(3,1) = clip#( 7) - clip#( 5);
frustum#(3,2) = clip#(11) - clip#( 9);
frustum#(3,3) = clip#(15) - clip#(13);
`/* Normalize the result */
rangeClipx# = frustum#(3,0)
rangeClipy# = frustum#(3,1)
rangeClipz# = frustum#(3,2)
`get distance from corner to corner of plane
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
`use distance to normalize data
frustum#(3,0) = frustum#(3,0) / t#
frustum#(3,1) = frustum#(3,1) / t#
frustum#(3,2) = frustum#(3,2) / t#
frustum#(3,3) = frustum#(3,3) / t#
`/* Extract the FAR plane */
frustum#(4,0) = clip#( 3) - clip#( 2);
frustum#(4,1) = clip#( 7) - clip#( 6);
frustum#(4,2) = clip#(11) - clip#(10);
frustum#(4,3) = clip#(15) - clip#(14);
`/* Normalize the result */
rangeClipx# = frustum#(4,0)
rangeClipy# = frustum#(4,1)
rangeClipz# = frustum#(4,2)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(4,0) = frustum#(4,0) / t#
frustum#(4,1) = frustum#(4,1) / t#
frustum#(4,2) = frustum#(4,2) / t#
frustum#(4,3) = frustum#(4,3) / t#
`/* Extract the NEAR plane */
frustum#(5,0) = clip#( 3) + clip#( 2);
frustum#(5,1) = clip#( 7) + clip#( 6);
frustum#(5,2) = clip#(11) + clip#(10);
frustum#(5,3) = clip#(15) + clip#(14);
`/* Normalize the result */
rangeClipx# = frustum#(5,0)
rangeClipy# = frustum#(5,1)
rangeClipz# = frustum#(5,2)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(5,0) = frustum#(5,0) / t#
frustum#(5,1) = frustum#(5,1) / t#
frustum#(5,2) = frustum#(5,2) / t#
frustum#(5,3) = frustum#(5,3) / t#
null=delete vector3(ClipRange)
return
`distance from plane
`distance = A * X + B * Y + C * Z + D
function PointInfrustum( x#, y#, z# )
`check point against each plane of the frustum
for p = 0 to 5
`if behind any of them then exit
if (frustum#(p,0) * x#) + (frustum#(p,1) * y#) + (frustum#(p,2) * z#) + frustum#(p,3) <= 0
exitfunction 0
endif
next p
`if not behind any of them then it is inside and visible
endfunction 1
`returns 0 for outside and distance of sphere from camera if in
function SphereInfrustum( x#, y#, z#, radius# )
`set result to default
result# = 0
`check to see if the center point is within the radius of the sphere
`to the planes
for p = 0 to 5
d# = (frustum#(p,0) * x#) + (frustum#(p,1) * y#) + (frustum#(p,2) * z#) + frustum#(p,3)
`if not then it is out
if d# <= 0-radius#
exitfunction result#
endif
next p
`get the distange from the sphere to the camera if inside
`usefull for LOD calcs ;-)
result# = d# + radius#;
endfunction result#
function CubeInfrustum( x#, y#, z#, size# )
`test each point of the box against each plane
for p = 0 to 5
a = 0
if (frustum#(p,0) * (x# - size#)) + (frustum#(p,1) * (y# - size#)) + (frustum#(p,2) * (z# - size#)) + frustum#(p,3) > 0
a = 1
endif
if a = 0
if (frustum#(p,0) * (x# + size#)) + (frustum#(p,1) * (y# - size#)) + (frustum#(p,2) * (z# - size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - size#)) + (frustum#(p,1) * (y# + size#)) + (frustum#(p,2) * (z# - size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + size#)) + (frustum#(p,1) * (y# + size#)) + (frustum#(p,2) * (z# - size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - size#)) + (frustum#(p,1) * (y# - size#)) + (frustum#(p,2) * (z# + size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + size#)) + (frustum#(p,1) * (y# - size#)) + (frustum#(p,2) * (z# + size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - size#)) + (frustum#(p,1) * (y# + size#)) + (frustum#(p,2) * (z# + size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + size#)) + (frustum#(p,1) * (y# + size#)) + (frustum#(p,2) * (z# + size#)) + frustum#(p,3) > 0
a = 1
endif
endif
`if none of the points are in then exit test
if a = 0
exit
endif
next p
`if exited from none in give 0 as a result
if a = 0
exitfunction 0
endif
`else give a 1
endfunction 1
function BoxInfrustum( x#, y#, z#, sizex#, sizey#, sizez# )
for p = 0 to 5
a = 0
if (frustum#(p,0) * (x# - sizex#)) + (frustum#(p,1) * (y# - sizey#)) + (frustum#(p,2) * (z# - sizez#)) + frustum#(p,3) > 0
a = 1
endif
if a = 0
if (frustum#(p,0) * (x# + sizex#)) + (frustum#(p,1) * (y# - sizey#)) + (frustum#(p,2) * (z# - sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - sizex#)) + (frustum#(p,1) * (y# + sizey#)) + (frustum#(p,2) * (z# - sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + sizex#)) + (frustum#(p,1) * (y# + sizey#)) + (frustum#(p,2) * (z# - sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - sizex#)) + (frustum#(p,1) * (y# - sizey#)) + (frustum#(p,2) * (z# + sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + sizex#)) + (frustum#(p,1) * (y# - sizey#)) + (frustum#(p,2) * (z# + sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - sizex#)) + (frustum#(p,1) * (y# + sizey#)) + (frustum#(p,2) * (z# + sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + sizex#)) + (frustum#(p,1) * (y# + sizey#)) + (frustum#(p,2) * (z# + sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
exit
endif
next p
if a = 0
exitfunction 0
endif
endfunction 1
[edit3] I had no idea that the limb position commands were so slow. Save the limb positions to an array before the loop and use the array instead and the culling code is 8 times faster
`dim arrays for culling
dim frustum#(5,3)
dim clip#(15)
`display stuff
sync on : sync rate 0
set camera range .5, 10000
autocam off
position camera 0, 30, 0
hide mouse
cubesize# = 10.0
`make test object with 1024 limbs
a = 1
depth# = -640
wide# = -640
`tall
for j = 1 to 64
`across
for k = 1 to 64
if j = 1 and k = 1
make object cube 1, cubesize#
make mesh from object 1, 1
position object a, wide#, 0, depth#
else
add limb 1, a, 1
offset limb 1, a, (k-1)*20, 0, (j-1)*20
inc a, 1
endif
next k
next j
`the sphere needs to totally enclose the cube so the size is the distance
`between 2 diagonal corners (corners that do not share sides)
test_sphere_size# = sqrt((cubesize#*cubesize#)+(cubesize#*cubesize#)+(cubesize#*cubesize#))
dim LimbPos#(0, 4095, 2)
for i = 0 to 4095
LimbPos#(0, i, 0) = limb position x(1, i)
LimbPos#(0, i, 1) = limb position y(1, i)
LimbPos#(0, i, 2) = limb position z(1, i)
next i
`main loop
do
`mlook
rotate camera camera angle x()+(mousemovey()/2),camera angle y()+(mousemovex()/2),0
if camera angle x()>90 then xrotate camera 90
if camera angle x()<-90 then xrotate camera -90
cx#=camera angle x() : cy#=camera angle y() : cz#=camera angle z()
`simple flying movement
if upkey()=1
move camera 3
endif
if downkey()=1
move camera -3
endif
if rightkey()=1
xrotate camera 0: yrotate camera cy# + 90: move camera 3: xrotate camera cx#: yrotate camera cy#
endif
if leftkey()=1
xrotate camera 0: yrotate camera cy# - 90: move camera 3: xrotate camera cx#: yrotate camera cy#
endif
`cull if wanted
if returnkey() = 1
begtime# = hitimer(1000)
`get the camers frustum to check against as it changes with the position
`and rotation of the camera
gosub Extractfrustum
frustumtime# = hitimer(1000)-begtime#
begtime# = hitimer(1000)
`test each limb against the frustum (you would use an octree to cut down
`the cheks)
for i = 0 to 4095
`use a sphere to test as it is faster than a box or cube
if SphereInfrustum( LimbPos#(0, i, 0), LimbPos#(0, i, 1), LimbPos#(0, i, 2), test_sphere_size# ) > 0
if limb visible(1,i) = 0
show limb 1, i
endif
else
if limb visible(1,i) = 1
hide limb 1, i
endif
endif
next i
culltime# = hitimer(1000)-begtime#
endif
set cursor 0,0
print "Hold returnkey for culling."
print "FPS : ",screen fps()," Polys : ",statistic(1)
print "Time to get frustum : ",frustumtime#," Culling time : ",culltime#
sync
loop
null=delete matrix4(ProjectionMatrix)
null=delete matrix4(ViewMatrix)
null=delete matrix4(ClipMatrix)
end
Extractfrustum:
t# = 0
ClipRange = 1
ProjectionMatrix = 1
ViewMatrix = 2
ClipMatrix = 3
null=MAKE MATRIX4(ProjectionMatrix)
null=MAKE MATRIX4(ViewMatrix)
null=MAKE MATRIX4(ClipMatrix)
`get the screen space matrix
Projection Matrix4 ProjectionMatrix
`get the camera space matrix
VIEW MATRIX4 ViewMatrix
`multiply the camera space matrix by the screen space matrix
`to get the clip matrix to get the frustum planes from
MULTIPLY MATRIX4 ClipMatrix, ViewMatrix, ProjectionMatrix
`assign the clip matrix elements to an array so we can use them
for i = 0 to 15
clip#(i) = DK Get Matrix4 Element( ClipMatrix, i )
next i
`/* Extract the numbers for the RIGHT plane */
frustum#(0,0) = clip#( 3) - clip#( 0);
frustum#(0,1) = clip#( 7) - clip#( 4);
frustum#(0,2) = clip#(11) - clip#( 8);
frustum#(0,3) = clip#(15) - clip#(12);
`/* Normalize the result */
rangeClipx# = frustum#(0,0)
rangeClipy# = frustum#(0,1)
rangeClipz# = frustum#(0,2)
null=make vector3(ClipRange)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(0,0) = frustum#(0,0) / t#
frustum#(0,1) = frustum#(0,1) / t#
frustum#(0,2) = frustum#(0,2) / t#
frustum#(0,3) = frustum#(0,3) / t#
`/* Extract the numbers for the LEFT plane */
frustum#(1,0) = clip#( 3) + clip#( 0);
frustum#(1,1) = clip#( 7) + clip#( 4);
frustum#(1,2) = clip#(11) + clip#( 8);
frustum#(1,3) = clip#(15) + clip#(12);
`/* Normalize the result */
rangeClipx# = frustum#(1,0)
rangeClipy# = frustum#(1,1)
rangeClipz# = frustum#(1,2)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(1,0) = frustum#(1,0) / t#
frustum#(1,1) = frustum#(1,1) / t#
frustum#(1,2) = frustum#(1,2) / t#
frustum#(1,3) = frustum#(1,3) / t#
`/* Extract the BOTTOM plane */
frustum#(2,0) = clip#( 3) + clip#( 1);
frustum#(2,1) = clip#( 7) + clip#( 5);
frustum#(2,2) = clip#(11) + clip#( 9);
frustum#(2,3) = clip#(15) + clip#(13);
`/* Normalize the result */
rangeClipx# = frustum#(2,0)
rangeClipy# = frustum#(2,1)
rangeClipz# = frustum#(2,2)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(2,0) = frustum#(2,0) / t#
frustum#(2,1) = frustum#(2,1) / t#
frustum#(2,2) = frustum#(2,2) / t#
frustum#(2,3) = frustum#(2,3) / t#
`/* Extract the TOP plane */
frustum#(3,0) = clip#( 3) - clip#( 1);
frustum#(3,1) = clip#( 7) - clip#( 5);
frustum#(3,2) = clip#(11) - clip#( 9);
frustum#(3,3) = clip#(15) - clip#(13);
`/* Normalize the result */
rangeClipx# = frustum#(3,0)
rangeClipy# = frustum#(3,1)
rangeClipz# = frustum#(3,2)
`get distance from corner to corner of plane
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
`use distance to normalize data
frustum#(3,0) = frustum#(3,0) / t#
frustum#(3,1) = frustum#(3,1) / t#
frustum#(3,2) = frustum#(3,2) / t#
frustum#(3,3) = frustum#(3,3) / t#
`/* Extract the FAR plane */
frustum#(4,0) = clip#( 3) - clip#( 2);
frustum#(4,1) = clip#( 7) - clip#( 6);
frustum#(4,2) = clip#(11) - clip#(10);
frustum#(4,3) = clip#(15) - clip#(14);
`/* Normalize the result */
rangeClipx# = frustum#(4,0)
rangeClipy# = frustum#(4,1)
rangeClipz# = frustum#(4,2)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(4,0) = frustum#(4,0) / t#
frustum#(4,1) = frustum#(4,1) / t#
frustum#(4,2) = frustum#(4,2) / t#
frustum#(4,3) = frustum#(4,3) / t#
`/* Extract the NEAR plane */
frustum#(5,0) = clip#( 3) + clip#( 2);
frustum#(5,1) = clip#( 7) + clip#( 6);
frustum#(5,2) = clip#(11) + clip#(10);
frustum#(5,3) = clip#(15) + clip#(14);
`/* Normalize the result */
rangeClipx# = frustum#(5,0)
rangeClipy# = frustum#(5,1)
rangeClipz# = frustum#(5,2)
set vector3 ClipRange, rangeClipx#, rangeClipy#, rangeClipz#
rangeClipresult#=length vector3(ClipRange)
t# = rangeClipresult#
frustum#(5,0) = frustum#(5,0) / t#
frustum#(5,1) = frustum#(5,1) / t#
frustum#(5,2) = frustum#(5,2) / t#
frustum#(5,3) = frustum#(5,3) / t#
null=delete vector3(ClipRange)
return
`distance from plane
`distance = A * X + B * Y + C * Z + D
function PointInfrustum( x#, y#, z# )
`check point against each plane of the frustum
for p = 0 to 5
`if behind any of them then exit
if (frustum#(p,0) * x#) + (frustum#(p,1) * y#) + (frustum#(p,2) * z#) + frustum#(p,3) <= 0
exitfunction 0
endif
next p
`if not behind any of them then it is inside and visible
endfunction 1
`returns 0 for outside and distance of sphere from camera if in
function SphereInfrustum( x#, y#, z#, radius# )
`set result to default
result# = 0
`check to see if the center point is within the radius of the sphere
`to the planes
for p = 0 to 5
d# = (frustum#(p,0) * x#) + (frustum#(p,1) * y#) + (frustum#(p,2) * z#) + frustum#(p,3)
`if not then it is out
if d# <= 0-radius#
exitfunction result#
endif
next p
`get the distange from the sphere to the camera if inside
`usefull for LOD calcs ;-)
result# = d# + radius#;
endfunction result#
function CubeInfrustum( x#, y#, z#, size# )
`test each point of the box against each plane
for p = 0 to 5
a = 0
if (frustum#(p,0) * (x# - size#)) + (frustum#(p,1) * (y# - size#)) + (frustum#(p,2) * (z# - size#)) + frustum#(p,3) > 0
a = 1
endif
if a = 0
if (frustum#(p,0) * (x# + size#)) + (frustum#(p,1) * (y# - size#)) + (frustum#(p,2) * (z# - size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - size#)) + (frustum#(p,1) * (y# + size#)) + (frustum#(p,2) * (z# - size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + size#)) + (frustum#(p,1) * (y# + size#)) + (frustum#(p,2) * (z# - size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - size#)) + (frustum#(p,1) * (y# - size#)) + (frustum#(p,2) * (z# + size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + size#)) + (frustum#(p,1) * (y# - size#)) + (frustum#(p,2) * (z# + size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - size#)) + (frustum#(p,1) * (y# + size#)) + (frustum#(p,2) * (z# + size#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + size#)) + (frustum#(p,1) * (y# + size#)) + (frustum#(p,2) * (z# + size#)) + frustum#(p,3) > 0
a = 1
endif
endif
`if none of the points are in then exit test
if a = 0
exit
endif
next p
`if exited from none in give 0 as a result
if a = 0
exitfunction 0
endif
`else give a 1
endfunction 1
function BoxInfrustum( x#, y#, z#, sizex#, sizey#, sizez# )
for p = 0 to 5
a = 0
if (frustum#(p,0) * (x# - sizex#)) + (frustum#(p,1) * (y# - sizey#)) + (frustum#(p,2) * (z# - sizez#)) + frustum#(p,3) > 0
a = 1
endif
if a = 0
if (frustum#(p,0) * (x# + sizex#)) + (frustum#(p,1) * (y# - sizey#)) + (frustum#(p,2) * (z# - sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - sizex#)) + (frustum#(p,1) * (y# + sizey#)) + (frustum#(p,2) * (z# - sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + sizex#)) + (frustum#(p,1) * (y# + sizey#)) + (frustum#(p,2) * (z# - sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - sizex#)) + (frustum#(p,1) * (y# - sizey#)) + (frustum#(p,2) * (z# + sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + sizex#)) + (frustum#(p,1) * (y# - sizey#)) + (frustum#(p,2) * (z# + sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# - sizex#)) + (frustum#(p,1) * (y# + sizey#)) + (frustum#(p,2) * (z# + sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * (x# + sizex#)) + (frustum#(p,1) * (y# + sizey#)) + (frustum#(p,2) * (z# + sizez#)) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
exit
endif
next p
if a = 0
exitfunction 0
endif
endfunction 1