Try this code and compare with my values. Hold the 1 key down for clip matrix values, 2 for view matrix, and 3 for projection matrix.
set window on
set display mode 800, 600, 32
sync on : Sync rate 60
backdrop on
autocam off
set camera range 0.5, 3000
position camera 0, 0, -25
make object cube 1, 10
set object wireframe 1, 1
`dim arrays for culling
dim frustum#(5,3)
dim testdata#(31)
dim clip#(15)
`make vectors and matrix4 for distance and frustum checks
ProjectionMatrix = 1
ViewMatrix = 2
ClipMatrix = 3
ClipRange = 4
null=MAKE MATRIX4(ProjectionMatrix)
null=MAKE MATRIX4(ClipMatrix)
null=MAKE MATRIX4(ViewMatrix)
null=make vector3(ClipRange)
`get the screen space matrix (you don't need to update this matrix4 unless
`resolution or aspect etc changes
Projection Matrix4 ProjectionMatrix
` main program loop
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
mval = mouseclick()
if mval = 1
move camera 1
endif
if mval = 2
move camera -1
endif
gosub Extractfrustum
resultcheck = CubeInfrustum( 0, 0, 0, 5 )
set cursor 0,0
print resultcheck
`press the 1 key for clip matrix values
if keystate(2) = 1
for i = 0 to 15 step 2
print clip#(i)," : ",clip#(i+1)
next i
endif
`press the 2 key for view matrix values
if keystate(3) = 1
for i = 0 to 15 step 2
print testdata#(i)," : ",testdata#(i+1)
next i
endif
`press the 3 key for Projection matrix values
if keystate(4) = 1
for i = 16 to 31 step 2
print testdata#(i)," : ",testdata#(i+1)
next i
endif
sync
loop
null=delete matrix4(ProjectionMatrix)
null=delete matrix4(ViewMatrix)
null=delete matrix4(ClipMatrix)
null=delete vector3(ClipRange)
end
Extractfrustum:
t# = 0
`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
for i = 0 to 15
testdata#(i) = DK Get Matrix4 Element( ViewMatrix, i )
next i
for i = 0 to 15
testdata#(i+16) = DK Get Matrix4 Element( ProjectionMatrix, 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)
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 */
`use distance from right plane to normalize data
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 */
`use distance from bottom plane 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#
return
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
Sorry but I am too sleepy to convert it over. And do not move your mouse at all until you check all 3 values. I ran the exe 3 times and hit print screen each.
My values are in the SS attached. You values should be close.
well its 9am here. I'm gonna get some sleep. I'll check back around 2pm