@green gandalf,
here is the game code:
`Culling Theory Ported over frum this guide by Mark Morley
`http://crownandcutlass.com/features/technicaldetails/frustum.html
`Frustum culling
`======================
`©Lost In Thought
`======================
`Main Source File
`My culling changes need this at the top of your code
`*****************************************************************************
`*****************************************************************************
`dim arrays for culling
dim frustum#(5,3)
dim clip#(15)
`gloabls for multiple objects
global maxLimbs = 0
global NumCullObjs = 0
global num_Objs = 0
`setup type for cull objs
type Cull_Obj_Data
Objnum as integer
NumLimbs as integer
CullType as integer
Cull_on as boolean
Check as boolean
Hidden as boolean
Xpos as float
Ypos as float
Zpos as float
Cubesize as float
BoxXsize as float
BoxYsize as float
BoxZsize as float
Spheresize as float
endtype
dim Limb_Info(0, 0) as Cull_Obj_Data
`set default culling to boxes
cullmode = 3
`fast vector distance check declarations
global ClipRange = 4
null=make vector3(ClipRange)
`make vectors and matrix4 for distance and frustum checks
ProjectionMatrix = 1
ViewMatrix = 2
ClipMatrix = 3
null=MAKE MATRIX4(ProjectionMatrix)
null=MAKE MATRIX4(ClipMatrix)
null=MAKE MATRIX4(ViewMatrix)
`*****************************************************************************
`*****************************************************************************
`display stuff
sync on : sync rate 0
`set display mode 1024,768,32,1
`set display mode 800 , 600 , 32
set camera range 0, 0.5, 2000
autocam off
position camera 0, 0, -30
hide mouse
color backdrop 0
set global collision off
`set normalization on
`set ambient light 0
`*****************************************************************************
`*****************************************************************************
`get the screen space matrix (you don't need to update this matrix4 unless
`resolution, range, or aspect etc changes
Projection Matrix4 ProjectionMatrix
`*****************************************************************************
`*****************************************************************************
cubesize# = 10.0
`make test object with 4096 limbs
num_Objs = 200000
testobj = num_Objs
a = 1
depth# = 0
wide# = 0
remstart
`tall
for j = 1 to 64
`across
for k = 1 to 64
if j = 1 and k = 1
make object cube testobj, cubesize#
make mesh from object testobj, 1
position object testobj, 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
calculate object bounds 1
set object collision off 1
inc num_Objs, 1
remend
`enter the object into the culling system
`setup_Cull_obj( object number, culling type )
`use 0 for limb culling type and 1 for object culling type
remstart
for i = 1 to 500
make object box i, 10,10,10
position object i, 0,0,i*15
move object left i, 1000
setup_Cull_obj( i, 1 )
next i
remend
`-----------------------------------------------------
`MATHS
`remstart
`light vectors and matrices
light = 11
null = make vector4(light)
mat = make matrix4(22)
mat = make matrix4(33)
mat = make matrix4(44)
mat = make matrix4(55)
null = make vector4(66)
flashvec = 77
null = make vector4(flashvec)
playervec = 88
null = make vector4(playervec)
`remend
`-----------------------------------------------------
`SHADER INIT
load effect "shaders/flashlight.fx",1,0
`---
make camera 1
color backdrop 1,0
load camera effect "shaders/bloom.fx",10,0
set camera effect 1,10,1
make object plain 2222,2,2,1
load effect "shaders/Quad.fx",100,0
set object effect 2222,100
set vector4 light,1024, 768, 0, 0
texture object 2222,0,1
`---
`shader textures
load image "shaders/FlashLight.dds",4,2
set effect technique 1, "Light0"
position camera 0, 80,300,1500
rotate camera 0, 0,180,0
`----------
`variables, IDs and structs
ruelle1 as integer
ruelle1 = 200
ruelle1escaliers as integer
ruelle1escaliers = 201
ruelle1shadows as integer
ruelle1shadows = 202
ruelle1vegetation as integer
ruelle1vegetation = 203
`-------------
`Objects of this TEST
load object "models/world/ruelle1/ruelle/ruelle.dbo",ruelle1
load object "models/world/ruelle1/ruelle/escaliers.dbo",ruelle1escaliers
load object "models/world/ruelle1/ruelleshadows/shadowsfull.dbo",ruelle1shadows
load object "models/world/ruelle1/ruellevegetation/ruellevegetation.dbo",ruelle1vegetation
`------------
set object effect ruelle1,1
texture object ruelle1,2,4
`calculate object bounds ruelle1 `not recomended
set object collision off ruelle1
setup_Cull_obj( ruelle1, 0 )
set object normals ruelle1
set global shadow color 0,0,0,225
`enable object zbias ruelle1,1,1
`------------
set object collision off ruelle1escaliers
exclude object on ruelle1escaliers
setup_Cull_obj( ruelle1escaliers, 0 )
set object normals ruelle1escaliers
`------------
`calculate object bounds 201 `not recomended
set object collision off ruelle1shadows
set alpha mapping on ruelle1shadows, 0
make mesh from object ruelle1shadows, ruelle1shadows
set shadow shading on ruelle1shadows,ruelle1shadows,10000,1
setup_Cull_obj( ruelle1shadows, 0 )
set object normals ruelle1shadows
`------------
set object transparency ruelle1vegetation, 4
`calculate object bounds ruelle1vegetation
set object effect ruelle1vegetation,1
texture object ruelle1vegetation,2,4
`set object normals ruelle1vegetation
`calculate object bounds ruelle1vegetation `not recomended
set object collision off ruelle1vegetation
`enable object zbias ruelle1vegetation,1,1
`save object "ruelle1/ruellevegetation/ruellevegetationf.dbo",ruellevegetation
setup_Cull_obj( ruelle1vegetation, 0 )
`----------
`Shader config
`shadows color
set global shadow color 0,0,0,225
`ambient light enviroment
set vector4 light,0.003, 0.003, 0.003, 1
`techniques : flashlight + normallight = 2 lights
set effect technique 1, "Light0"
`set effect constant float 1, "Ambient", 0.003
`set effect constant float 1, "BrightNess", 2.0
`SET EFFECT TECHNIQUE 1, "Light0"
`-----------------------------------------------------
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
`SET LIGHT TO CAMERA
`----------
`remstart
set vector4 light,camera position x(), camera position y(), camera position z(), 1
`set vector4 light,object position x(stephen.ModelID), object position y(stephen.ModelID)+150, object position z(stephen.ModelID), 1
set effect constant vector 1,"LightPosition",light
angX#=curveangle(camera angle x(),angX#,4)
angY#=curveangle(camera angle y(),angY#,4)
rotate x matrix4 22,(angX#)/57.3
rotate y matrix4 33,(angY#)/57.3
rotate z matrix4 44,(angZ#)/57.3
multiply matrix4 55,22,33:multiply matrix4 55,55,44
set effect constant matrix 1,"LightAngle",55
`remend
`----------
`This culling code goes inside your Main loop
`****************************************************************************
`****************************************************************************
`set camera range 0, 0.5, 2000
if keystate(2) = 1
cullmode = 1
endif
if keystate(3) = 1
cullmode = 2
endif
if keystate(4) = 1
cullmode = 3
endif
if keystate(5) = 1
cullmode = 4
endif
if keystate(11) = 1
cullmode = 0
endif
remstart
`---------------
distance# = MATH_Distance_2_Points(object position x(shadows1),object position y(shadows1),object position z(shadows1), camera position x(0),camera position y(0), camera position z(0), 3)
if distance# > 2000
exclude object on shadows1
else
exclude object off shadows1
endif
`shadowalpha# = 226-((distance#*0.000769)*225)
if distance# > 1800
screenfps# = screen fps()
shadowalpha# = 226-(((distance#-1800)/200)*225)
alpha# = CURVEVALUE(225, shadowalpha#, screenfps#)
set global shadow color 0,0,0,alpha#
endif
if distance# < 1800
screenfps# = screen fps()
shadowalpha# = 226-((distance#/2000)*225)
alpha# = CURVEVALUE(shadowalpha#, 225, screenfps#)
set global shadow color 0,0,0,alpha#
endif
remend
`-----------------------
`****************************************************************************
`****************************************************************************
`Update shadows position
if shiftkey() = 1
set shadow position -1,camera position x(0),camera position y(0),camera position z(0)
endif
`set camera range 0, 0.5, 10000
`----------------------------------------
`set effect technique 10, "Blur"
position camera 1, camera position x(0),camera position y(0), camera position z(0)
rotate camera 1, camera angle x(0), camera angle y(0), camera angle z(0)
`----------
exclude object on 2222
exclude object off ruelle1
exclude object off ruelle1shadows
exclude object off ruelle1vegetation
sync camera 1
set effect technique 100, "screenquad"
`sync mask 2^1
`----------
cullbegtime = hitimer(10000)
`Update object culling
Update_cull_objs( cullmode )
cullendtime = (hitimer(10000) - cullbegtime) * .1
`print culling result info
framerate=screen fps()
set cursor 0, 30
print "FPS: ",framerate
print "Polys: ",statistic(1)
print "use keys 1-3 for different culling 1 sphere, 2 cube, 3 box"
print "key 4 to turn of culling but keep culled limbs hidden"
print "key 0 showes all : Cull time = ", cullendtime, "ms"
`print "Objet Total Limbs : ",totallimbs#
`----------
exclude object off 2222
exclude object on ruelle1
exclude object on ruelle1shadows
exclude object on ruelle1vegetation
sync mask 2^0
sync
`----------------------------------------
`sync mask 2^0
`fastsync
loop
`This goes after your main loop
`****************************************************************************
`****************************************************************************
null=delete matrix4(ProjectionMatrix)
null=delete matrix4(ViewMatrix)
null=delete matrix4(ClipMatrix)
null=delete vector3(ClipRange)
end
function Update_cull_objs( cmode as integer )
resultcheck# = 0
gosub Extractfrustum
`check each object setup for culling
for j = 1 to NumCullObjs
`test each limb against the frustum (you would use an octree to cut down
`the checks)
for i = 0 to Limb_Info(j, 0).NumLimbs
if Limb_Info(j, 0).NumLimbs = 0
LtestX# = object position x( Limb_Info(j, 0).Objnum )
LtestY# = object position y( Limb_Info(j, 0).Objnum )
LtestZ# = object position z( Limb_Info(j, 0).Objnum )
else
LtestX# = 0
LtestY# = 0
LtestZ# = 0
endif
`use to turn culling off for this limb permanently
if Limb_Info(j, i).Cull_on = 1
`will be used to turn culling off temporarily for octrees later
if Limb_Info(j, i).Check = 1
if cmode > 0
select cmode
case 1
resultcheck# = SphereInfrustum( Limb_Info(j, i).Xpos + LtestX#, Limb_Info(j, i).Ypos + LtestY#, Limb_Info(j, i).Zpos + LtestZ#, Limb_Info(j, i).Spheresize )
endcase
case 2
if SphereInfrustum( Limb_Info(j, i).Xpos + LtestX#, Limb_Info(j, i).Ypos + LtestY#, Limb_Info(j, i).Zpos + LtestZ#, Limb_Info(j, i).Spheresize ) > 0
resultcheck# = CubeInfrustum( Limb_Info(j, i).Xpos + LtestX#, Limb_Info(j, i).Ypos + LtestY#, Limb_Info(j, i).Zpos + LtestZ#, Limb_Info(j, i).Cubesize )
else
resultcheck# = 0
endif
endcase
case 3
if SphereInfrustum( Limb_Info(j, i).Xpos + LtestX#, Limb_Info(j, i).Ypos + LtestY#, Limb_Info(j, i).Zpos + LtestZ#, Limb_Info(j, i).Spheresize ) > 0
resultcheck# = BoxInfrustum( Limb_Info(j, i).Xpos + LtestX#, Limb_Info(j, i).Ypos + LtestY#, Limb_Info(j, i).Zpos + LtestZ#, Limb_Info(j, i).BoxXsize, Limb_Info(j, i).BoxYsize, Limb_Info(j, i).BoxZsize )
else
resultcheck# = 0
endif
endcase
endselect
if cmode < 4
if resultcheck# > 0
if Limb_Info(j, i).Hidden = 1
if Limb_Info(j, 0).NumLimbs > 0
show limb Limb_Info(j, 0).Objnum, i
else
exclude object off Limb_Info(j, 0).Objnum
endif
Limb_Info(j, i).Hidden = 0
endif
else
if Limb_Info(j, i).Hidden = 0
if Limb_Info(j, 0).NumLimbs > 0
hide limb Limb_Info(j, 0).Objnum, i
else
exclude object on Limb_Info(j, 0).Objnum
endif
Limb_Info(j, i).Hidden = 1
endif
endif
endif
else
if Limb_Info(j, i).Hidden = 1
if Limb_Info(j, 0).NumLimbs > 0
show limb Limb_Info(j, 0).Objnum, i
else
exclude object off Limb_Info(j, 0).Objnum
endif
Limb_Info(j, i).Hidden = 0
endif
endif
endif
endif
next i
next j
endfunction
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
`````DK GET MATRIX4 ELEMENT ES DEL PLUGIN DK PLUGINS GET MATRIX4 ELEMENT c a DBP 6.6
for i = 0 to 15
`````DK GET MATRIX4 ELEMENT ES DEL PLUGIN DK PLUGINS GET MATRIX4 ELEMENT c a DBP 6.6
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)
t#=1/ClipDistance( rangeClipx#, rangeClipy#, rangeClipz# )
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)
t#=1/ClipDistance( rangeClipx#, rangeClipy#, rangeClipz# )
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)
t#=1/ClipDistance( rangeClipx#, rangeClipy#, rangeClipz# )
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)
t#=1/ClipDistance( rangeClipx#, rangeClipy#, rangeClipz# )
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 setup_Cull_obj( objnum as integer, ctype as integer )
`use ctype = 0 for limb culling and ctype = 1 for whole object
inc NumCullObjs, 1
if ctype = 0
perform checklist for object limbs objnum
numterrlimbs = checklist quantity()
empty checklist
if numterrlimbs-1 > maxLimbs then maxLimbs = numterrlimbs-1
else
numterrlimbs = 1
endif
dim Limb_Info(NumCullObjs, maxLimbs) as Cull_Obj_Data
Limb_Info(NumCullObjs, 0).CullType = ctype
`store object numbers
Limb_Info(NumCullObjs, 0).Objnum = objnum
Limb_Info(NumCullObjs, 0).NumLimbs = numterrlimbs-1
for i = 0 to numterrlimbs-1
if ctype = 0
make object from limb num_Objs, objnum, i
ellipposx# = object collision center x(num_Objs)
ellipposy# = object collision center y(num_Objs)
ellipposz# = object collision center z(num_Objs)
ellipsizex# = object size x(num_Objs, 1)*0.5
limb_max_size# = ellipsizex#
ellipsizey# = object size y(num_Objs, 1)*0.5
if ellipsizey# > limb_max_size# then limb_max_size# = ellipsizey#
ellipsizez# = object size z(num_Objs, 1)*0.5
if ellipsizez# > limb_max_size# then limb_max_size# = ellipsizez#
delete object num_Objs
`positions to use for all 3 types of testing
Limb_Info(NumCullObjs, i).Xpos = ellipposx# + limb position x(objnum, i)
Limb_Info(NumCullObjs, i).Ypos = ellipposy# + limb position y(objnum, i)
Limb_Info(NumCullObjs, i).Zpos = ellipposz# + limb position z(objnum, i)
else
ellipposx# = object collision center x(objnum)
ellipposy# = object collision center y(objnum)
ellipposz# = object collision center z(objnum)
ellipsizex# = object size x(objnum, 1)*0.5
limb_max_size# = ellipsizex#
ellipsizey# = object size y(objnum, 1)*0.5
if ellipsizey# > limb_max_size# then limb_max_size# = ellipsizey#
ellipsizez# = object size z(objnum, 1)*0.5
if ellipsizez# > limb_max_size# then limb_max_size# = ellipsizez#
`positions to use for all 3 types of testing
Limb_Info(NumCullObjs, 0).Xpos = ellipposx#
Limb_Info(NumCullObjs, 0).Ypos = ellipposy#
Limb_Info(NumCullObjs, 0).Zpos = ellipposz#
endif
`size to use for cube testing
Limb_Info(NumCullObjs, i).Cubesize = limb_max_size#
`sizes to use for box testing
Limb_Info(NumCullObjs, i).BoxXsize = ellipsizex#
Limb_Info(NumCullObjs, i).BoxYsize = ellipsizey#
Limb_Info(NumCullObjs, i).BoxZsize = ellipsizez#
`size to use for sphere testing
Limb_Info(NumCullObjs, i).Spheresize = ClipDistance( ellipsizex#, ellipsizey#, ellipsizez# )
Limb_Info(NumCullObjs, i).Cull_on = 1
Limb_Info(NumCullObjs, i).Check = 1
Limb_Info(NumCullObjs, i).Hidden = 0
next i
endfunction
`distance from plane
`distance = A * X + B * Y + C * Z + D
`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
minx# = x#-size#
maxx# = x#+size#
miny# = y#-size#
maxy# = y#+size#
minz# = z#-size#
maxz# = z#+size#
for p = 0 to 5
a = 0
if (frustum#(p,0) * minx#) + (frustum#(p,1) * miny#) + (frustum#(p,2) * minz#) + frustum#(p,3) > 0
a = 1
endif
if a = 0
if (frustum#(p,0) * maxx#) + (frustum#(p,1) * miny#) + (frustum#(p,2) * minz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * minx#) + (frustum#(p,1) * maxy#) + (frustum#(p,2) * minz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * maxx#) + (frustum#(p,1) * maxy#) + (frustum#(p,2) * minz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * minx#) + (frustum#(p,1) * miny#) + (frustum#(p,2) * maxz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * maxx#) + (frustum#(p,1) * miny#) + (frustum#(p,2) * maxz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * minx#) + (frustum#(p,1) * maxy#) + (frustum#(p,2) * maxz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * maxx#) + (frustum#(p,1) * maxy#) + (frustum#(p,2) * maxz#) + 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# )
minx# = x#-sizex#
maxx# = x#+sizex#
miny# = y#-sizey#
maxy# = y#+sizey#
minz# = z#-sizez#
maxz# = z#+sizez#
for p = 0 to 5
a = 0
if (frustum#(p,0) * minx#) + (frustum#(p,1) * miny#) + (frustum#(p,2) * minz#) + frustum#(p,3) > 0
a = 1
endif
if a = 0
if (frustum#(p,0) * maxx#) + (frustum#(p,1) * miny#) + (frustum#(p,2) * minz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * minx#) + (frustum#(p,1) * maxy#) + (frustum#(p,2) * minz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * maxx#) + (frustum#(p,1) * maxy#) + (frustum#(p,2) * minz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * minx#) + (frustum#(p,1) * miny#) + (frustum#(p,2) * maxz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * maxx#) + (frustum#(p,1) * miny#) + (frustum#(p,2) * maxz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * minx#) + (frustum#(p,1) * maxy#) + (frustum#(p,2) * maxz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
if (frustum#(p,0) * maxx#) + (frustum#(p,1) * maxy#) + (frustum#(p,2) * maxz#) + frustum#(p,3) > 0
a = 1
endif
endif
if a = 0
exit
endif
next p
if a = 0
exitfunction 0
endif
endfunction 1
`simple and fast box in box function Thanks to Ideas from Sparky
`returns 0 if totally outside, 1 if partially overlapping, and
`-1 if the source (box 1) is totally inside the destination (box 2)
function box_in_Box(posx1#, posy1#, posz1#, posx2#, posy2#, posz2#, sizex1#, sizey1#, sizez1#, sizex2#, sizey2#, sizez2#)
minx1# = posx1# - sizex1#
miny1# = posy1# - sizey1#
minz1# = posz1# - sizez1#
maxx1# = posx1# + sizex1#
maxy1# = posy1# + sizey1#
maxz1# = posz1# + sizez1#
minx2# = posx2# - sizex2#
miny2# = posy2# - sizey2#
minz2# = posz2# - sizez2#
maxx2# = posx2# + sizex2#
maxy2# = posy2# + sizey2#
maxz2# = posz2# + sizez2#
if ( minx2#>maxx1# || minx1#>maxx2# ) then exitfunction 0
if ( miny2#>maxy1# || miny1#>maxy2# ) then exitfunction 0
if ( minz2#>maxz1# || minz1#>maxz2# ) then exitfunction 0
a = 0
if ( minx1#>=minx2# && maxx1#<=maxx2# ) then inc a, 1
if ( miny1#>=miny2# && maxy1#<=maxy2# ) then inc a, 1
if ( minz1#>=minz2# && maxz1#<=maxz2# ) then inc a, 1
if a = 3 then exitfunction -1
endfunction 1
`simple and fast distance function using vectors
function ClipDistance( x as float, y as float, z as float )
set vector3 ClipRange, x, y, z
temp#=length vector3(ClipRange)
endfunction temp#
`****************************************************************************
`****************************************************************************
`------------------------------------------------------
`My liter funtion
Function ExcludeObjectOnOff( ObjectNum, Dimension as integer, MaxDistance as float)
local X#, Y#, Z#, Distance# as float
If Dimension=3
vecteur=make vector3(1000)
X1#= object position x(ObjectNum)
Y1#= object position y(ObjectNum)
Z1#= object position z(ObjectNum)
X2#= camera position x(0)
Y2#= camera position y(0)
Z2#= camera position z(0)
X#=X1#-X2#
Y#=Y1#-Y2#
Z#=Z1#-Z2#
set vector3 1000,X#,Y#,Z#
Distance#=length vector3(1000)
EndIf
If Dimension=2
vecteur=make vector2(1000)
X1#= object position x(ObjectNum)
Z1#= object position z(ObjectNum)
X2#= camera position x(0)
Z2#= camera position z(0)
X#=X1#-X2#
Z#=Z1#-Z2#
set vector2 1000,X#,Z#
Distance#=length vector2(1000)
EndIf
if Distance# > MaxDistance
exclude object on ObjectNum
else
exclude object off ObjectNum
endif
EndFunction `ResultExclude#
`------------------------------------------------------
Function Distance_2_Points(X1#, Y1#, Z1#, X2#, Y2#, Z2# as float, Dimension as integer)
local X#, Y#, Z#, Distance# as float
If Dimension=3
vecteur=make vector3(2000)
X#=X1#-X2#
Y#=Y1#-Y2#
Z#=Z1#-Z2#
set vector3 2000,X#,Y#,Z#
Distance#=length vector3(2000)
EndIf
If Dimension=2
vecteur=make vector2(2000)
X#=X1#-X2#
Z#=Z1#-Z2#
set vector2 2000,X#,Z#
Distance#=length vector2(2000)
EndIf
EndFunction Distance#
`------------------------------------------------------
and here is the DarkShader fx code:
string Description = "This shader adds a glow effect to all objects in the scene";
string Thumbnail = "Bloom.png";
float2 ViewSize : ViewSize;
//box filter, declare in pixel offsets convert to texel offsets in PS
float2 DownFilterSamples[9] =
{
{ -1, -1 },
{ -1, 0 },
{ -1, 1 },
{ 0, 1 },
{ 1, 1 },
{ 1, 0 },
{ 1, -1 },
{ 0, -1 },
{ 0, 0 },
};
static const int FilterSize = 13;
float2 PixelOffsets [ FilterSize ] =
{
{ -6, 0 },
{ -5, 0 },
{ -4, 0 },
{ -3, 0 },
{ -2, 0 },
{ -1, 0 },
{ 0, 0 },
{ 1, 0 },
{ 2, 0 },
{ 3, 0 },
{ 4, 0 },
{ 5, 0 },
{ 6, 0 },
};
static const float BlurWeights [ FilterSize ] =
{
0.002216,
0.008764,
0.026995,
0.064759,
0.120985,
0.176033,
0.200496,
0.176033,
0.120985,
0.064759,
0.026995,
0.008764,
0.002216,
};
float BloomScale
<
string UIWidget = "slider";
float UIMax = 2.0;
float UIMin = 0.0;
float UIStep = 0.01;
> = 1.970000;
float BloomPower
<
string UIWidget = "slider";
float UIMax = 4.0;
float UIMin = 0.4;
float UIStep = 0.01;
> = 1.30000;
texture frameImg : RENDERCOLORTARGET
<
string ResourceName = "";
float2 ViewportRatio = { 1.0, 1.0 };
>;
sampler2D frameImgSamp = sampler_state {
Texture = < frameImg >;
MinFilter = Linear; MagFilter = Linear; MipFilter = Linear;
AddressU = Wrap; AddressV = Wrap;
};
//downsample image
texture Downsample1Img : RENDERCOLORTARGET
<
string ResourceName = "";
float2 ViewportRatio = { 0.25, 0.25 };
>;
sampler2D Downsample1Samp = sampler_state {
Texture = < Downsample1Img >;
MinFilter = Linear; MagFilter = Linear; MipFilter = Linear;
AddressU = Clamp; AddressV = Clamp;
};
//downsmaple image2
texture Downsample2Img : RENDERCOLORTARGET
<
string ResourceName = "";
float2 ViewportRatio = { 0.0625, 0.0625 };
>;
sampler2D Downsample2Samp = sampler_state {
Texture = < Downsample2Img >;
MinFilter = Linear; MagFilter = Linear; MipFilter = Linear;
AddressU = Clamp; AddressV = Clamp;
};
//blur image1
texture Blur1Img : RENDERCOLORTARGET
<
string ResourceName = "";
float2 ViewportRatio = { 0.0625, 0.0625 };
>;
sampler2D Blur1Samp = sampler_state {
Texture = < Blur1Img >;
MinFilter = Linear; MagFilter = Linear; MipFilter = Linear;
AddressU = Clamp; AddressV = Clamp;
};
//blur image2
texture Blur2Img : RENDERCOLORTARGET
<
string ResourceName = "";
float2 ViewportRatio = { 0.0625, 0.0625 };
>;
sampler2D Blur2Samp = sampler_state {
Texture = < Blur2Img >;
MinFilter = Linear; MagFilter = Linear; MipFilter = Linear;
AddressU = Clamp; AddressV = Clamp;
};
struct input
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
struct output {
float4 pos: POSITION;
float2 uv: TEXCOORD0;
};
output VS( input IN )
{
output OUT;
//quad needs to be shifted by half a pixel.
//Go here for more info: http://www.sjbrown.co.uk/?article=directx_texels
float4 oPos = float4( IN.pos.xy + float2( -1.0f/ViewSize.x, 1.0f/ViewSize.y ),0.0,1.0 );
OUT.pos = oPos;
float2 uv = (IN.pos.xy + 1.0) / 2.0;
uv.y = 1 - uv.y;
OUT.uv = uv;
return OUT;
}
float4 PSDownsample( output IN, uniform sampler2D srcTex, uniform float reduceRatio ) : COLOR
{
float4 color = 0;
float2 scale = reduceRatio/ViewSize;
for (int i = 0; i < 9; i++)
{
//convert pixel offsets into texel offsets via the inverse view values.
color += tex2D( srcTex, IN.uv + DownFilterSamples[i].xy*scale );
}
return color / 9;
}
float4 PSBlurH( output IN, uniform sampler2D srcTex ) : COLOR
{
float4 color = 0;
float2 scale = 1.0f/ViewSize;
for (int i = 0; i < FilterSize; i++) {
//convert pixel offsets into texel offsets via the inverse view values.
color += tex2D( srcTex , IN.uv + PixelOffsets[i].xy*scale ) * BlurWeights[i];
}
return color;
}
float4 PSBlurV( output IN, uniform sampler2D srcTex ) : COLOR
{
float4 color = 0;
float2 scale = 1.0f/ViewSize;
for (int i = 0; i < FilterSize; i++) {
//convert pixel offsets into texel offsets via the inverse view values.
color += tex2D( srcTex, IN.uv + PixelOffsets[i].yx*scale ) * BlurWeights[i];
}
return color;
}
float4 PSCombine( input IN ) : COLOR
{
float3 color = tex2D( frameImgSamp, IN.uv );
color += pow(tex2D( Blur2Samp, IN.uv ),BloomPower) * BloomScale;
return float4( color, 1.0f );
}
technique Blur
<
//specify the starting image where the original scene will be drawn
string RenderColorTarget = "frameImg";
>
{
//first pass downsample the image to quarter the size
pass Downsample
<
string RenderColorTarget = "Downsample1Img";
>
{
ZEnable = False;
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PSDownsample( frameImgSamp, 0.25 );
}
//downsample the image smaller again
pass Downsample2
<
string RenderColorTarget = "Downsample2Img";
>
{
ZEnable = False;
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PSDownsample( Downsample1Samp, 0.25 );
}
//blur this small image horizontally and save it in BlurImg1
pass BlurH
<
string RenderColorTarget = "Blur1Img";
>
{
ZEnable = False;
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PSBlurH( Downsample2Samp );
}
//blur the image vertically and save it in BlurImg2
pass BlurV
<
string RenderColorTarget = "Blur2Img";
>
{
ZEnable = False;
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PSBlurV( Blur1Samp );
}
//repeat the blur to get a larger glowing effect
pass BlurH
<
string RenderColorTarget = "Blur1Img";
>
{
ZEnable = False;
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PSBlurH( Blur2Samp );
}
pass BlurV
<
string RenderColorTarget = "Blur2Img";
>
{
ZEnable = False;
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PSBlurV( Blur1Samp );
}
//combine the glow image with the original to achieve a bloom effect
pass Combine
<
//blank target indicates this is the final pass and should be drawn to the final results image
string RenderColorTarget = "";
>
{
ZEnable = False;
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PSCombine();
}
}
The Quad.fx file:
float2 InvViewSize;
texture background < string Name=""; >;
sampler2D background_smp = sampler_state
{
Texture = <background>;
AddressU = Clamp; AddressV = Clamp;
MinFilter = Linear; MagFilter = Linear; MipFilter = None;
};
struct output
{
float4 opos : POSITION;
float2 uv : TEXCOORD0;
};
output VS( float4 pos : POSITION )
{
output OUT;
OUT.uv = pos.xy * float2(0.5,-0.5) + 0.5;
pos.xy = pos.xy + float2( -InvViewSize.x, InvViewSize.y );
OUT.opos = pos;
return OUT;
}
float4 PS(output IN) : COLOR
{
return tex2D(background_smp,IN.uv);
}
technique screenquad
{
pass p0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_1_1 PS();
}
}
By the way, am using LiT frustum culling, dont worry, see the section of shader init and the update section on the main loop...
Am using 6.5 and its works but its to slow, more than ver 6.6b.
Regards ...
Forgotten Memories W.I.P