type Vector3Data X# Y# Z# endtype function Shadow_Init(Width#,Height#,Near#,Far#,Distance#) global Shadow_ObjectID as integer[] global Shadow_ShadowShaderID global Shadow_DepthRenderImageID global Shadow_DepthSpriteID global Shadow_DepthShaderID global Shadow_CameraWidth# global Shadow_CameraHeight# global Shadow_CameraNear# global Shadow_CameraFar# global Shadow_CameraNearWidth# global Shadow_CameraNearHeight# global Shadow_CameraFarWidth# global Shadow_CameraFarHeight# global Shadow_EyeVectorID global Shadow_AtVectorID global Shadow_SunVectorID global Shadow_MinX# global Shadow_MinY# global Shadow_MinZ# global Shadow_MaxX# global Shadow_MaxY# global Shadow_MaxZ# global Shadow_Distance# Shadow_EyeVectorID=CreateVector3() Shadow_AtVectorID=CreateVector3() Shadow_SunVectorID=CreateVector3(-1,-1,-1) Shadow_DepthShaderID=LoadShader("Depth.vs","Depth.ps") Shadow_DepthRenderImageID=CreateRenderImage(2048,2048,1,0) SetImageMinFilter(Shadow_DepthRenderImageID,1) SetImageMagFilter(Shadow_DepthRenderImageID,1) SetImageWrapU(Shadow_DepthRenderImageID,0) SetImageWrapV(Shadow_DepthRenderImageID,0) Shadow_ShadowShaderID=LoadShader("Object.vs","Object.ps") Shadow_CameraWidth#=Width# Shadow_CameraHeight#=Height# Shadow_CameraNear#=Near# Shadow_CameraFar#=Far# Shadow_Distance#=Distance# DeviceWidth#=GetDeviceWidth() DeviceHeight#=GetDeviceHeight() AspectRatio#=DeviceWidth#/DeviceHeight# SetCameraRange(1,Shadow_CameraNear#,Shadow_CameraFar#) endfunction function Shadow_AddObject(ObjectID) SetObjectImage(ObjectID,Shadow_DepthRenderImageID,1) SetObjectShader(ObjectID,Shadow_ShadowShaderID) SetObjectScreenCulling(ObjectID,0) Shadow_ObjectID.insert(ObjectID) endfunction function Shadow_RemoveObject(ObjectID) SetObjectShader(ObjectID,0) SetObjectScreenCulling(ObjectID,0) ID=Shadow_FindObjectID(ObjectID) Shadow_ObjectID.remove(ID) endfunction function Shadow_FindObjectID(ObjectID) repeat ID=ID+1 until Shadow_ObjectID[ID]=ObjectID endfunction ID /* function Shadow_SetSunPosition(PosX#,PosY#,PosZ#,DirX#,DirY#,DirZ#) AtVectorID=CreateVector3(PosX#+DirX#,PosY#+DirY#,PosZ#+DirZ#) EyeVectorID=CreateVector3(PosX#,PosY#,PosZ#) ViewMatrixID=BuildLookAtLhMatrix4(AtVectorID,EyeVectorID) DeleteVector3(AtVectorID) DeleteVector3(EyeVectorID) ProjectionMatrixID=creatematrix4() OrthographicProjectionMatrix4(ProjectionMatrixID,Shadow_CameraWidth#,Shadow_CameraHeight#,Shadow_CameraNear#,Shadow_CameraFar#) ModelMatrixID=CreateMatrix4() SetIdentityMatrix4(ModelMatrixID) LightMVPMatrixID=CreateMatrix4() Multiply2Matrix4(LightMVPMatrixID,ModelMatrixID,ViewMatrixID) Multiply2Matrix4(LightMVPMatrixID,ProjectionMatrixID,LightMVPMatrixID) SetShaderConstantArrayByName(Shadow_DepthShaderID,"LightMVPVector",0,GetMatrix4Element(LightMVPMatrixID,0),GetMatrix4Element(LightMVPMatrixID,4),GetMatrix4Element(LightMVPMatrixID,8),GetMatrix4Element(LightMVPMatrixID,12)) SetShaderConstantArrayByName(Shadow_DepthShaderID,"LightMVPVector",1,GetMatrix4Element(LightMVPMatrixID,1),GetMatrix4Element(LightMVPMatrixID,5),GetMatrix4Element(LightMVPMatrixID,9),GetMatrix4Element(LightMVPMatrixID,13)) SetShaderConstantArrayByName(Shadow_DepthShaderID,"LightMVPVector",2,GetMatrix4Element(LightMVPMatrixID,2),GetMatrix4Element(LightMVPMatrixID,6),GetMatrix4Element(LightMVPMatrixID,10),GetMatrix4Element(LightMVPMatrixID,14)) SetShaderConstantArrayByName(Shadow_DepthShaderID,"LightMVPVector",3,GetMatrix4Element(LightMVPMatrixID,3),GetMatrix4Element(LightMVPMatrixID,7),GetMatrix4Element(LightMVPMatrixID,11),GetMatrix4Element(LightMVPMatrixID,15)) SetShaderConstantArrayByName(Shadow_ShadowShaderID,"LightMVPVector",0,GetMatrix4Element(LightMVPMatrixID,0),GetMatrix4Element(LightMVPMatrixID,4),GetMatrix4Element(LightMVPMatrixID,8),GetMatrix4Element(LightMVPMatrixID,12)) SetShaderConstantArrayByName(Shadow_ShadowShaderID,"LightMVPVector",1,GetMatrix4Element(LightMVPMatrixID,1),GetMatrix4Element(LightMVPMatrixID,5),GetMatrix4Element(LightMVPMatrixID,9),GetMatrix4Element(LightMVPMatrixID,13)) SetShaderConstantArrayByName(Shadow_ShadowShaderID,"LightMVPVector",2,GetMatrix4Element(LightMVPMatrixID,2),GetMatrix4Element(LightMVPMatrixID,6),GetMatrix4Element(LightMVPMatrixID,10),GetMatrix4Element(LightMVPMatrixID,14)) SetShaderConstantArrayByName(Shadow_ShadowShaderID,"LightMVPVector",3,GetMatrix4Element(LightMVPMatrixID,3),GetMatrix4Element(LightMVPMatrixID,7),GetMatrix4Element(LightMVPMatrixID,11),GetMatrix4Element(LightMVPMatrixID,15)) DeleteMatrix4(ModelMatrixID) //~ DeleteMatrix4(ViewMatrixID) DeleteMatrix4(ProjectionMatrixID) DeleteMatrix4(LightMVPMatrixID) endfunction */ function Shadow_SetSunDirection(DirX#,DirY#,DirZ#) SetVector3(Shadow_SunVectorID,DirX#,DirY#,DirZ#) NormalizeVector3(Shadow_SunVectorID) endfunction function Shadow_Update() for ObjectID=0 to Shadow_ObjectID.length SetObjectCullMode(Shadow_ObjectID[ObjectID],2) SetObjectShader(Shadow_ObjectID[ObjectID],Shadow_DepthShaderID) next ObjectID Shadow_UpdateFrustumVertices() SetRenderToImage(0,Shadow_DepthRenderImageID) Update3D(0) ClearScreen() Render3D() for ObjectID=0 to Shadow_ObjectID.length SetObjectCullMode(Shadow_ObjectID[ObjectID],1) SetObjectShader(Shadow_ObjectID[ObjectID],Shadow_ShadowShaderID) next ObjectID SetRenderToScreen() endfunction function Shadow_UpdateFrustumVertices() CameraVectorID=CreateVector3(GetCameraX(1),GetCameraY(1),GetCameraZ(1)) NearLeftTopVectorID=CreateVector3(Get3DVectorXFromScreen(0,0),Get3DVectorYFromScreen(0,0),Get3DVectorZFromScreen(0,0)) FarLeftTopVectorID=CreateVector3(GetVector3X(NearLeftTopVectorID)*Shadow_Distance#,GetVector3Y(NearLeftTopVectorID)*Shadow_Distance#,GetVector3Z(NearLeftTopVectorID)*Shadow_Distance#) GetVector3Add(NearLeftTopVectorID,CameraVectorID) GetVector3Add(FarLeftTopVectorID,CameraVectorID) NearRightTopVectorID=CreateVector3(Get3DVectorXFromScreen(GetDeviceWidth(),0),Get3DVectorYFromScreen(GetDeviceWidth(),0),Get3DVectorZFromScreen(GetDeviceWidth(),0)) FarRightTopVectorID=CreateVector3(GetVector3X(NearRightTopVectorID)*Shadow_Distance#,GetVector3Y(NearRightTopVectorID)*Shadow_Distance#,GetVector3Z(NearRightTopVectorID)*Shadow_Distance#) GetVector3Add(NearRightTopVectorID,CameraVectorID) GetVector3Add(FarRightTopVectorID,CameraVectorID) NearLeftBottomVectorID=CreateVector3(Get3DVectorXFromScreen(0,GetDeviceHeight()),Get3DVectorYFromScreen(0,GetDeviceHeight()),Get3DVectorZFromScreen(0,GetDeviceHeight())) FarLeftBottomVectorID=CreateVector3(GetVector3X(NearLeftBottomVectorID)*Shadow_Distance#,GetVector3Y(NearLeftBottomVectorID)*Shadow_Distance#,GetVector3Z(NearLeftBottomVectorID)*Shadow_Distance#) GetVector3Add(NearLeftBottomVectorID,CameraVectorID) GetVector3Add(FarLeftBottomVectorID,CameraVectorID) NearRightBottomVectorID=CreateVector3(Get3DVectorXFromScreen(GetDeviceWidth(),GetDeviceHeight()),Get3DVectorYFromScreen(GetDeviceWidth(),GetDeviceHeight()),Get3DVectorZFromScreen(GetDeviceWidth(),GetDeviceHeight())) FarRightBottomVectorID=CreateVector3(GetVector3X(NearRightBottomVectorID)*Shadow_Distance#,GetVector3Y(NearRightBottomVectorID)*Shadow_Distance#,GetVector3Z(NearRightBottomVectorID)*Shadow_Distance#) GetVector3Add(NearRightBottomVectorID,CameraVectorID) GetVector3Add(FarRightBottomVectorID,CameraVectorID) DeleteVector3(CameraVectorID) local VectorID as integer[7] VectorID[0]=NearLeftTopVectorID VectorID[1]=FarLeftTopVectorID VectorID[2]=NearRightTopVectorID VectorID[3]=FarRightTopVectorID VectorID[4]=NearLeftBottomVectorID VectorID[5]=FarLeftBottomVectorID VectorID[6]=NearRightBottomVectorID VectorID[7]=FarRightBottomVectorID //~ if GetRawKeyPressed(70)=1 //~ for i=0 to 7 //~ String$=String$+str(GetVector3X(VectorID[i]))+", "+str(GetVector3Y(VectorID[i]))+", "+str(GetVector3Z(VectorID[i]))+chr(10) //~ next i //~ message(String$) //~ endif Shadow_MinX#=GetVector3X(VectorID[0]) Shadow_MinY#=GetVector3Y(VectorID[0]) Shadow_MinZ#=GetVector3Z(VectorID[0]) Shadow_MaxX#=GetVector3X(VectorID[0]) Shadow_MaxY#=GetVector3Y(VectorID[0]) Shadow_MaxZ#=GetVector3Z(VectorID[0]) for ID=1 to 7 if GetVector3X(VectorID[ID])>Shadow_MaxX# Shadow_MaxX#=GetVector3X(VectorID[ID]) elseif GetVector3X(VectorID[ID])Shadow_MaxY# Shadow_MaxY#=GetVector3Y(VectorID[ID]) elseif GetVector3Y(VectorID[ID])Shadow_MaxZ# Shadow_MaxZ#=GetVector3Z(VectorID[ID]) elseif GetVector3Z(VectorID[ID])