Here are some snippets which will hopefully help.
This sets up the shadow camera. You'll have to play with the camerazoom to get the view to completly fill the viewport.
shadow_cam=CreateCamera()
shadow=CreateTexture(256,256,48)
TextureBlend shadow,2
CameraViewport shadow_cam,0,0,256,256
PositionEntity shadow_cam,0,2000,0
CameraRange shadow_cam,1,2000
RotateEntity shadow_cam,90,0,0
CameraProjMode shadow_cam,2
CameraClsColor shadow_cam,255,255,255
camerazoom shadow_cam,.0105
This updates the shadow each frame where grid is the landscape. Note I'm not using any lights other than ambient. If you have any others you will have to hide them before renderworld().
Function update_shadow()
ShowEntity shadow_cam
HideEntity player_cam
HideEntity grid
AmbientLight 0,0,0
RenderWorld()
CopyRect 0,0,256,256,0,0,BackBuffer(),TextureBuffer(shadow)
HideEntity shadow_cam
ShowEntity player_cam
ShowEntity grid
AmbientLight 255,255,255
End Function
This is the terrain update function - note how 'tu' and 'tv' are constantly adjusted.
Function update_terrain()
FreeEntity pivot:pivot=CreatePivot()
ax#=Floor(player(cam)\x):az#=Floor(player(cam)\z)
fx#=(player(cam)\x) - ax:fz#=(player(cam)\z) - az
For z=-divs To divs+1
For x=-divs To divs+1
;grid positions
nx#=qwrap(ax+x,size)
nz#=qwrap(az+z,size)
;vertex positions
vy#=terrain(nx,nz)\height
vx#=qlimit( x-fx,-divs,divs)
vz#=qlimit( z-fz,-divs,divs)
;shadow texture coords
tu#=.5+(vx/divs)*.5 + (.5/512.0)
tv#=.5+(vz/divs)*.5 + (.5/512.0)
;If point on edge of grid smooth height
If divs < 18
If Abs(vx)=divs Or Abs(vz)=divs Then vy#=get_height( player(cam)\x+vx , player(cam)\z+vz)
EndIf
;vertex positions
x1 = ( x + divs ) * 2 - ( x > -divs )
z1 = ( z + divs ) * 2 - ( z > -divs )
For iz = 0 To ( z > -divs And z < divs+1 )
For ix = 0 To ( x > -divs And x < divs+1 )
x2 = x1 + ix:z2 = z1 + iz
v=x2+z2*(divg+1)
VertexCoords surf,v, vx * scale , vy , -vz * scale
VertexTexCoords surf,v,tu,tv
Next
Next
;vertex colours
If x < divs+1 And z < divs+1
x1 = ( x + divs ) * 2
z1 = ( z + divs ) * 2
For iz = 0 To 1
For ix = 0 To 1
x2 = x1 + ix:z2 = z1 + iz
v=x2+z2*(divg+1)
VertexColor surf,v,terrain(nx,nz)\r,terrain(nx,nz)\g,terrain(nx,nz)\b
Next
Next
EndIf
Next
Next
End Function
Hope this helps you. Give me a shout if your unsure of anything. Oh.. and using the textureflag + 256 should speed up the copyrect process for you - it doesn't do anything for me and a lowly gf2.
Stevie