Chafari, I liked your shadowing example, and I hope you don't mind but out of interest I modified one of your posted codes (your 2nd-to-latest, iirc) to run at realtime speeds.
set display mode desktop width(), desktop height(), 32, 1
sync on : sync rate 30 : sync
global scrW, scrH
scrW = screen width()
scrH = screen height()
autocam off
//floor_ texture -------------------------------------------
ink rgb(150,140,130),1
box 0,0,20,20
ink rgb(90,70,60),1
line 10,0,10,10
line 0,9,20,9
line 0,19,20,19
line 0,10,0,20
get image 30,0,0,20,20
for a= 1 to 10
for i= 1 to 10
paste image 30,-20+i*20,-20+a*20
next i
next a
get image 31,0,0,200,200
//----------------------------------------------------------
// scene
// 1: ground
// 2-maxObj: shadow casting objects
// 2: "frame" object
// 3: -
// 4: temp, used for csg with 2
// 5 - maxObj-1: generic boxes
// maxObj: large rotating box
// 100: light position
#constant maxObj 16
#constant luz 100
// lighting
set ambient light 30
hide light 0
make light 1:set light range 1,1500
// scene objects
make object box 1,300,0,300:texture object 1,31:offset limb 1,0,150,0,150
make object box 2,1,10,8
make object sphere luz,1:set object light luz,2
position object luz,0,15,0
position camera -50,160,-50
point camera 100,0,100
make object box 4,5,8,6
perform csg difference 2,4
delete object 4
scale object 2,200,200,200
position object 2,60,10,60
for i= 4 to maxObj-1
if i<6
make object box i,5,20,5
else
make object box i, 2+rnd(5), 15+rnd(10), 2+rnd(5)
position object i, rnd(300), 10, rnd(300)
endif
next i
make object box maxObj,40,10,10
position object 4 ,90,10,30
position object 5,50,10,90
position object 6,20,10,20
position object maxObj,130,10,130
// use sparky's collision on objects for fast raycast for shadow map drawing.
for n = 1 to maxObj
if object exist(n) then sc_setupcomplexobject n, 0, 2
next n
hide mouse
// Main loop.
do
// update input.
mmx = mousemovex()
mmy = mousemovey()
position mouse scrW/2, scrH/2
// move light (mousemove rotated to be camera-relative (mmx:horiz, -mmy:vert)
Rotate2D(mmx, -mmy, -45, 0, 0)
x# = x# + retVector2d.x * 0.5
z# = z# + retVector2d.y * 0.5
position object luz,x#,35,z#
position light 1,x#,50,z#
// animate some objects.
for n = 7 to maxObj-1
position object n, object position x(n), 10+cos(timer()*0.05+n*(360.0/maxObj))*5, object position z(n)
sc_updateobject n
next n
rotate object maxObj, object angle x(maxObj)+0.4, object angle y(maxObj)+0.52, object angle z(maxObj)+0.12
sc_updateobject maxObj
// user prompt.
ink rgb(255,255,255),1
set cursor 5,5:print "move mouse to position the lights >> LEFT MOUSE=render shadows"
if mouseclick()=1 then gosub render
sync
loop
render:
a2fillbox 0,0,300,300,0xffb4b4b4 `mask that make the initial light on the floor
// light position.
x=object position x(luz)
y=object position y(luz)
z=object position z(luz)
// shadow map res. 1 ~ 300.
szImg = 160
sf# = 300.0 / szImg
// draw shadows as batch of dots on shadow map.
a2startdotbatch szImg*szImg
for i = 1 to szImg
for a = 1 to szImg
// raycast on scene, dist# is distance to nearest scene intersection.
if sc_raycast(0, x, y, z, i*sf#, 0, a*sf#, 1) <> 0
dist# = sc_getcollisiondistance()
col=90-dist#
if col<0 then col=0
if col>90 then col=90
if dist#>0 then a2dot i,szImg-a,rgb(col,col,col)
endif
next a
next i
a2endbatch
`finalise shadow map.
get image 1,1,1,szImg,szImg,3 // get shadow map.
ik blur image 1, 4, 2 // slight blur.
if bitmap exist(1)=0 then create bitmap 1, 300,300 // only create bitmap 1 once.
set current bitmap 1` : cls 0xffb4b4b4
ik paste image 1, 0, 0, 299, 299 // paste image upscaled.
get image 1,1,1,299,299,1 // grab image back.
set current bitmap 0
paste image 1, 0, 0 // show.
set light mapping on 1,1 // update ground shadow map.
return
// http://www.euclideanspace.com/maths/geometry/affine/aroundPoint/matrix2d/index.htm
type Vector2d x as float, y as float endtype
global retVector2d as Vector2d // use for passing vector from function.
function Rotate2D(xin#, yin#, angle#, originx#, originy#)
retVector2d.x = cos(angle#) * xin# + (-sin(angle#)) * yin# + originx# - cos(angle#) * originx# - (-sin(angle#)) * originy#
retVector2d.y = sin(angle#) * xin# + cos(angle#) * yin# + originy# - sin(angle#) * originx# - cos(angle#) * originy#
endfunction
It
does use imagekit, advanced2d
and sparky's collision, though, so this is a useless post to anyone lacking them