Code: Select all
;SkyBox
;By DK_PETER
;Simple animated skybox...
EnableExplicit
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
Structure _TheSet
id.i
ms.i
ma.i
cl.i
tx.i
EndStructure
Structure _Sky
lf._TheSet
rt._TheSet
up._TheSet
dn._TheSet
bk._TheSet
fr._TheSet
node.i
twink.i
SkySize.i
EndStructure
Structure _Vars
Scw.i
Sch.i
FScr.i
Win.i
Cam.i
Path.s
EndStructure
Declare.i SetResolution(Width.i = -1, Height.i = -1, FS.i = #False)
Declare.i CreateSkyboxImages(Size.i = 2048)
Declare.i DoBaseStars(Img.i, Total.i = 2000)
Declare.i DoNebulae(Fract.i = 3)
Declare.i CreateSkyboxImages(Size.i = 2048)
Declare.i MyBox(Name.s)
Global va._Vars, sk._Sky
Procedure SetResolution(Width.i = -1, Height.i = -1, FS.i = #False)
va\FScr = FS
If Width = -1 Or Height= -1
ExamineDesktops()
va\Scw = DesktopWidth(0):va\Sch = DesktopHeight(0)
Else
va\Scw = Width:va\Sch = Height
EndIf
If FS = #False
va\Win = OpenWindow(#PB_Any, 0, 0, va\Scw, va\Sch, "Launch",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(va\Win), 0, 0, va\Scw, va\Sch, #False, 0, 0)
Else
OpenScreen(va\Scw, va\Sch, 32, "Launch")
EndIf
va\Cam = CreateCamera(#PB_Any, 0, 0, 100, 100)
sk\node = CreateNode(#PB_Any, 0, 0, 0)
EndProcedure
Procedure.i DoBaseStars(Img.i, Total.i = 2000)
Protected count.i, tmcol.i, alp.i
StartDrawing(TextureOutput(Img))
DrawingMode(#PB_2DDrawing_AlphaBlend)
For count = 1 To Total
tmcol = Random(255,100)
alp = Random(255,50)
Circle(Random(TextureWidth(Img)-1, 1), Random(TextureHeight(Img)-1,1),2,RGBA(tmcol,tmcol,tmcol,alp))
Next Count
StopDrawing()
EndProcedure
Procedure.i DoTwink()
Protected newtx.i, count.i
newtx = CreateTexture(#PB_Any, sk\SkySize, sk\SkySize)
StartDrawing(TextureOutput(newtx))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(0,0,sk\SkySize, sk\SkySize, $0)
For count = 1 To 4500
Circle(Random(TextureWidth(newtx)-1, 1), Random(TextureHeight(newtx)-1,1),1,$58FFFFFF)
Next Count
StopDrawing()
ProcedureReturn newtx
EndProcedure
Procedure.i DoNebulae(Fract.i = 3)
Protected pox.i, poy.i, raax.i, raay.i,TheMax.i, x.i, y.i, tx.i
tx = CreateTexture(#PB_Any, sk\SkySize, sk\SkySize)
StartDrawing(TextureOutput(tx))
Box(0, 0, TextureWidth(tx), TextureHeight(tx), $0)
DrawingMode(#PB_2DDrawing_Gradient|#PB_2DDrawing_AlphaBlend)
TheMax = TextureWidth(tx)/4
For y = 0 To Fract
BackColor(RGBA(Random(255,50),Random(255,80),Random(255,80),Random(30,10))):FrontColor($02111111)
For x = 0 To 5
pox = Random(TextureWidth(tx)-TheMax, TheMax)
poy = Random(TextureWidth(tx)-TheMax, TheMax)
raax = Random(TheMax,TheMax/2):raay = Random(TheMax,TheMax/2)
EllipticalGradient(pox, poy, raax, raay)
Ellipse(pox, poy, raax, raay)
Next x
Next y
StopDrawing()
ProcedureReturn tx
EndProcedure
Procedure.i CreateSkyboxImages(Size.i = 2048)
sk\SkySize = Size
sk\twink = DoTwink()
sk\bk\tx = CreateTexture(#PB_Any, Size, Size)
DoBaseStars(sk\bk\tx,Random(3000,1000))
sk\bk\cl = DoNebulae(Random(4,0))
sk\dn\tx = CreateTexture(#PB_Any, Size, Size)
DoBaseStars(sk\dn\tx,Random(3000,1000))
sk\dn\cl = DoNebulae(Random(4,0))
sk\fr\tx = CreateTexture(#PB_Any, Size, Size)
DoBaseStars(sk\fr\tx,Random(3000,1000))
sk\fr\cl = DoNebulae(Random(4,0))
sk\lf\tx = CreateTexture(#PB_Any, Size, Size)
DoBaseStars(sk\lf\tx,Random(3000,1000))
sk\lf\cl = DoNebulae(Random(4,0))
sk\rt\tx = CreateTexture(#PB_Any, Size, Size)
DoBaseStars(sk\rt\tx,Random(3000,1000))
sk\rt\cl = DoNebulae(Random(4,0))
sk\up\tx = CreateTexture(#PB_Any, Size, Size)
DoBaseStars(sk\up\tx,Random(3000,1000))
sk\up\cl = DoNebulae(Random(4,0))
EndProcedure
Procedure.i MakeSkybox(Name.s = "Sky")
With sk\fr
\ms = CreatePlane(#PB_Any, TextureWidth(\tx), TextureWidth(\tx), 1, 1, 1, 1)
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
MaterialBlendingMode(\ma, #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(sk\twink),#PB_Material_Modulate)
AddMaterialLayer(\ma, TextureID(\cl), #PB_Material_AlphaBlend)
ScrollMaterial(\ma, 0.001, 0.003, #PB_Material_Animated, 1)
\id = CreateEntity(#PB_Any, MeshID(\ms),MaterialID(\ma),0 + MeshRadius(\ms)/2, MeshRadius(\ms)/2,-MeshRadius(\ms)/2)
RotateEntity(\id, -90, 0, 0, #PB_Absolute)
MoveEntity(\id, CameraX(va\cam), CameraY(va\cam), CameraZ(va\cam) + TextureWidth(\tx)/2, #PB_Absolute)
AttachNodeObject(sk\node, EntityID(\id))
EndWith
With sk\bk
\ms = CreatePlane(#PB_Any, TextureWidth(\tx), TextureWidth(\tx), 1, 1, 1, 1)
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
MaterialBlendingMode(\ma, #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(sk\twink),#PB_Material_Modulate)
AddMaterialLayer(\ma, TextureID(\cl), #PB_Material_AlphaBlend)
ScrollMaterial(\ma, 0.001, 0.003, #PB_Material_Animated, 1)
\id = CreateEntity(#PB_Any, MeshID(\ms),MaterialID(\ma), 0+MeshRadius(\ms)/2, MeshRadius(\ms)/2,-MeshRadius(\ms)/2)
RotateEntity(\id, 90, 180, 0, #PB_Absolute)
MoveEntity(\id, CameraX(va\cam), CameraY(va\cam), CameraZ(va\cam)-TextureWidth(\tx)/2, #PB_Absolute)
AttachNodeObject(sk\node, EntityID(\id))
EndWith
With sk\up
\ms = CreatePlane(#PB_Any, TextureWidth(\tx), TextureWidth(\tx), 1, 1, 1, 1)
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
MaterialBlendingMode(\ma, #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(sk\twink),#PB_Material_Modulate)
AddMaterialLayer(\ma, TextureID(\cl), #PB_Material_AlphaBlend)
ScrollMaterial(\ma, 0.001, 0.003, #PB_Material_Animated, 1)
\id = CreateEntity(#PB_Any, MeshID(\ms),MaterialID(\ma), 0+MeshRadius(\ms)/2, MeshRadius(\ms)/2,-MeshRadius(\ms)/2)
RotateEntity(\id, 180, 0, 0, #PB_Absolute)
MoveEntity(\id, CameraX(va\cam), CameraY(va\cam)+TextureWidth(\tx)/2, CameraZ(va\cam), #PB_Absolute)
AttachNodeObject(sk\node, EntityID(\id))
EndWith
With sk\dn
\ms = CreatePlane(#PB_Any, TextureWidth(\tx), TextureWidth(\tx), 1, 1, 1, 1)
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
MaterialBlendingMode(\ma, #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(sk\twink),#PB_Material_Modulate)
AddMaterialLayer(\ma, TextureID(\cl), #PB_Material_AlphaBlend)
ScrollMaterial(\ma, 0.001, 0.003, #PB_Material_Animated, 1)
\id = CreateEntity(#PB_Any, MeshID(\ms),MaterialID(\ma), 0+MeshRadius(\ms)/2, MeshRadius(\ms)/2,-MeshRadius(\ms)/2)
RotateEntity(\id, 0, 0, 0, #PB_Absolute)
MoveEntity(\id, CameraX(va\cam), CameraY(va\cam)-TextureWidth(\tx)/2, CameraZ(va\cam), #PB_Absolute)
AttachNodeObject(sk\node, EntityID(\id))
EndWith
With sk\rt
\ms = CreatePlane(#PB_Any, TextureWidth(\tx), TextureWidth(\tx), 1, 1, 1, 1)
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
MaterialBlendingMode(\ma, #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(sk\twink),#PB_Material_Modulate)
AddMaterialLayer(\ma, TextureID(\cl), #PB_Material_AlphaBlend)
ScrollMaterial(\ma, 0.001, 0.003, #PB_Material_Animated, 1)
\id = CreateEntity(#PB_Any, MeshID(\ms),MaterialID(\ma), 0+MeshRadius(\ms)/2, MeshRadius(\ms)/2,-MeshRadius(\ms)/2)
RotateEntity(\id, -90, 0, 90, #PB_Absolute)
MoveEntity(\id, CameraX(va\cam)+TextureWidth(\tx)/2, CameraY(va\cam), CameraZ(va\cam), #PB_Absolute)
AttachNodeObject(sk\node, EntityID(\id))
EndWith
With sk\lf
\ms = CreatePlane(#PB_Any, TextureWidth(\tx), TextureWidth(\tx), 1, 1, 1, 1)
\ma = CreateMaterial(#PB_Any, TextureID(\tx))
MaterialBlendingMode(\ma, #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(\tx), #PB_Material_Add)
AddMaterialLayer(\ma, TextureID(sk\twink),#PB_Material_Modulate)
AddMaterialLayer(\ma, TextureID(\cl), #PB_Material_AlphaBlend)
ScrollMaterial(\ma, 0.001, 0.003, #PB_Material_Animated, 1)
\id = CreateEntity(#PB_Any, MeshID(\ms),MaterialID(\ma), 0+MeshRadius(\ms)/2, MeshRadius(\ms)/2,-MeshRadius(\ms)/2)
RotateEntity(\id, -90, 0, -90, #PB_Absolute)
MoveEntity(\id, CameraX(va\cam)-TextureWidth(\tx)/2, CameraY(va\cam), CameraZ(va\cam), #PB_Absolute)
AttachNodeObject(sk\node, EntityID(\id))
EndWith
EndProcedure
Procedure.i RunNow()
Protected ev.i
Repeat
If va\FScr = #False
Repeat
ev = WindowEvent()
Until ev = 0
EndIf
ExamineMouse()
RotateCamera(va\Cam, -MouseDeltaY() * 0.03, -MouseDeltaX() * 0.03, 0, #PB_Relative)
;MoveNode(sk\node, CameraX(va\Cam), CameraY(va\Cam), CameraZ(va\Cam))
;To those I teach (Not active forum members):
;Test demo på pc 1 til 8.
RenderWorld()
ExamineKeyboard()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
End
EndProcedure
SetResolution()
CreateSkyboxImages()
MakeSkybox()
RunNow()

