Ocean shader demo pb5.0b7
Posted: Thu Oct 25, 2012 11:07 pm
Ocean shader demo from Ogre examples
showing how to interact with shaders via EntityCustomParameter()
press spacebar for controls
Ocean Zip
showing how to interact with shaders via EntityCustomParameter()
press spacebar for controls
Ocean Zip
Code: Select all
;Cross platform Ocean glsl vertex and fragment shader demo, from ogre 1.7.3 samples OceanDemo
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
If Not Subsystem("OpenGL")
MessageRequester("Warning :", "Please compile With OpenGL subsystem")
End
EndIf
CompilerEndIf
Structure OceanScene
entity.i
matOcean.i
matOcean2.i
matFlare.i
EndStructure
Global ocean.OceanScene
#CameraSpeed = 5
IncludeFile "Screen3DRequester.pb"
Define.f KeyX, KeyY, MouseX, MouseY
;define variables to control Ocean2 material
Global WaveFreq.f,WaveAmp.f,BumpScale.f,TextureScaleX.f,TextureScaleY.f,WaveSpeedX.f,WaveSpeedY.f,ActiveOcean.i, bshowgui.i
;shader setting index enums
Enumeration 1
#WaveFreq
#WaveAmp
#BumpScale
#TextureScaleX
#TextureScaleY
#WaveSpeedX
#WaveSpeedY
EndEnumeration
Enumeration 100
#GUIWindow
#Ocean1
#Ocean2
#Frequency
#Amplitude
#Surface
#SpeedX
#SpeedY
#ScaleX
#ScaleY
#Close
EndEnumeration
#FrequencySF = 0.001
#AmplitudeSF = 0.1
#SurfaceSF = 0.10
#SpeedXSF = 0.001
#SpeedYSF = 0.001
Procedure OpenControls(state=0)
;note you need to set AutoScaled xml attributes in the guis .font and .imageset to false
;otherwise it will scale them
If Not IsWindow3D(#GUIWindow)
OpenWindow3D(#GUIWindow, 50, 50, 300, 300, "Ocean shader",#PB_Window3D_Invisible)
OptionGadget3D(#Ocean1, 10, 30, 80,25,"Ocean1")
OptionGadget3D(#Ocean2, 100, 30, 80,25,"Ocean2")
TextGadget3D(#PB_Any,10,60,80,25,"Frequency")
SpinGadget3D(#Frequency,10,90,80,25,1,1000)
TextGadget3D(#PB_Any,100,60,80,25,"Amplitude")
SpinGadget3D(#Amplitude,100,90,80,25,1,100)
TextGadget3D(#PB_Any,190,60,80,25,"Surface")
SpinGadget3D(#Surface,190,90,80,25,1,10)
TextGadget3D(#PB_Any,10,120,80,25,"SpeedX")
SpinGadget3D(#SpeedX,10,150,80,25,1,1000)
TextGadget3D(#PB_Any,100,120,80,25,"SpeedY")
SpinGadget3D(#SpeedY,100,150,80,25,1,1000)
TextGadget3D(#PB_Any,10,180,80,25,"ScaleX")
SpinGadget3D(#ScaleX,10,210,80,25,1,100)
TextGadget3D(#PB_Any,100,180,80,25,"ScaleY")
SpinGadget3D(#ScaleY,100,210,80,25,1,100)
ButtonGadget3D(#Close,10, 250,80,25,"Close")
If ActiveOcean = #Ocean1
SetGadgetState3D(#Ocean1,1)
SetGadgetState3D(#Ocean2,0)
Else
SetGadgetState3D(#Ocean1,0)
SetGadgetState3D(#Ocean2,1)
EndIf
x.i = 1.0/#Frequencysf * WaveFreq
SetGadgetState3D(#Frequency,x)
SetGadgetText3D(#Frequency,Str(x))
x = (1.0/#AmplitudeSF) * WaveAmp
SetGadgetState3D(#Amplitude,x)
SetGadgetText3D(#Amplitude,Str(x))
x = (1.0/#SurfaceSF)*BumpScale
SetGadgetState3D(#Surface,x)
SetGadgetText3D(#Surface,Str(x))
x = (1.0/#SpeedXSF)*WaveSpeedX
SetGadgetState3D(#SpeedX,x)
SetGadgetText3D(#SpeedX,Str(x))
x = (1.0/#SpeedYSF)*WaveSpeedY
SetGadgetState3D(#SpeedY,x)
SetGadgetText3D(#SpeedY,Str(x))
SetGadgetState3D(#ScaleX,TextureScaleX)
SetGadgetText3D(#ScaleX,Str(TextureScaleX))
SetGadgetState3D(#ScaleY,TextureScaleY)
SetGadgetText3D(#ScaleY,Str(TextureScaleY))
EndIf
If state = 1
ShowGUI(128,1)
HideWindow3D(#GUIWindow,0)
Else
ShowGUI(128,0)
HideWindow3D(#GUIWindow,1)
EndIf
EndProcedure
Procedure CheckOutGui(x,y)
Protected L,R,T,B
L = WindowX3D(#GUIWindow)
R = L + WindowWidth3D(#GUIWindow)
T = WindowY3D(#GUIWindow)
B = T + WindowHeight3D(#GUIWindow)
If (x > L And x < R And y > T And y < B)
ProcedureReturn 0
EndIf
ProcedureReturn 1
EndProcedure
Procedure HandleGui()
Repeat
Event = WindowEvent3D()
Select Event
Case #PB_Event3D_Gadget
Select EventGadget3D()
Case #Close
bshowgui = 1 - bshowgui
If bshowgui
OpenControls(1)
Else
OpenControls(0)
EndIf
Case #Frequency
WaveFreq = GetGadgetState3D(#Frequency) * #FrequencySF
If bshowgui
EntityCustomParameter(ocean\entity,0,#WaveFreq,WaveFreq,0,0,0)
EndIf
Case #Amplitude
WaveAmp = GetGadgetState3D(#Amplitude) * #AmplitudeSF
If bshowgui
EntityCustomParameter(ocean\entity,0,#WaveAmp,WaveAmp,0,0,0)
EndIf
Case #Surface
BumpScale = GetGadgetState3D(#Surface) * #SurfaceSF
If bshowgui
EntityCustomParameter(ocean\entity,0,#BumpScale,BumpScale,0,0,0)
EndIf
Case #speedX
WaveSpeedX = GetGadgetState3D(#SpeedX) * #SpeedXSF
If bshowgui
EntityCustomParameter(ocean\entity,0,#WaveSpeedX,WaveSpeedX,0,0,0)
EndIf
Case #speedY
WaveSpeedY = GetGadgetState3D(#SpeedY) * #SpeedYSF
If bshowgui
EntityCustomParameter(ocean\entity,0,#WaveSpeedY,WaveSpeedY,0,0,0)
EndIf
Case #scaleX
TextureScaleX = GetGadgetState3D(#ScaleX)
If bshowgui
EntityCustomParameter(ocean\entity,0,#TextureScaleX,TextureScaleX,0,0,0)
EndIf
Case #scaleY
TextureScaleY = GetGadgetState3D(#ScaleY)
If bshowgui
EntityCustomParameter(ocean\entity,0,#TextureScaleY,TextureScaleY,0,0,0)
EndIf
Case #Ocean1
If MaterialID(ocean\matOcean) <> GetEntityMaterial(ocean\entity)
SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean))
ActiveOcean = #Ocean1
EndIf
Case #Ocean2
If MaterialID(ocean\matOcean2) <> GetEntityMaterial(ocean\entity)
SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean2))
ActiveOcean = #Ocean2
EndIf
EndSelect
EndSelect
Until Event = 0
EndProcedure
Procedure main()
If InitEngine3D(#PB_Engine3D_DebugLog)
Add3DArchive("GUI/", #PB_3DArchive_FileSystem)
Add3DArchive("Data/", #PB_3DArchive_FileSystem)
Add3DArchive("Data/cubemapsJS.zip", #PB_3DArchive_Zip)
InitSprite()
InitKeyboard()
InitMouse()
If Screen3DRequester()
Parse3DScripts() ;note you need to open a screen before calling parse3DScripts if the scripts contain glsl or hlsl programs
KeyboardMode(#PB_Keyboard_International) ;set keyboard to international so arrow keys will work
ocean\matOcean = GetScriptMaterial(#PB_Any,"Ocean") ;load the ocean material and shader programs
ocean\matOcean2 = GetScriptMaterial(#PB_Any,"Ocean2")
CreatePlane(1,5000,5000,50,50,1,1) ;create a surface to render the ocean on
ocean\entity = CreateEntity(#PB_Any, MeshID(1), MaterialID(ocean\matOcean2)) ;set the material to the entity
;set up some control variables for the ocean2 material
WaveFreq = 0.028
WaveAmp = 1.8
BumpScale = 0.2
TextureScaleX = 25
TextureScaleY = 25
WaveSpeedX = 0.015
WaveSpeedY = 0.005
;set shader values
EntityCustomParameter(ocean\entity,0,#WaveFreq,WaveFreq,0,0,0)
EntityCustomParameter(ocean\entity,0,#WaveAmp,WaveAmp,0,0,0)
EntityCustomParameter(ocean\entity,0,#BumpScale,BumpScale,0,0,0)
EntityCustomParameter(ocean\entity,0,#TextureScaleX,TextureScaleX,0,0,0)
EntityCustomParameter(ocean\entity,0,#TextureScaleY,TextureScaleY,0,0,0)
EntityCustomParameter(ocean\entity,0,#WaveSpeedX,WaveSpeedX,0,0,0)
EntityCustomParameter(ocean\entity,0,#WaveSpeedY,WaveSpeedY,0,0,0)
ocean\matFlare = CreateMaterial(#PB_Any,LoadTexture(1,"flare.png"))
MaterialBlendingMode(ocean\matFlare,#PB_Material_Add)
CreateBillboardGroup(0,MaterialID(ocean\matFlare), 2000, 2000)
AddBillboard(0,0,-5000,5000,5000)
;note you can change skybox at any time
SkyBox("morning.jpg")
CreateCamera(0, 0, 0, 100, 100)
CameraLocate(0, 0,10,0)
OpenControls()
Repeat
Screen3DEvents()
If ExamineMouse()
MouseX = -(MouseDeltaX()*0.10)*#CameraSpeed*0.5
MouseY = -(MouseDeltaY()*0.10)*#CameraSpeed*0.5
InputEvent3D(MouseX(), MouseY(),MouseButton(#PB_MouseButton_Left))
EndIf
If ExamineKeyboard()
If KeyboardPushed(#PB_Key_1) ;switch ocean materal to ocean
If MaterialID(ocean\matOcean) <> GetEntityMaterial(ocean\entity)
SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean))
ActiveOcean = #Ocean1
EndIf
EndIf
If KeyboardPushed(#PB_Key_2) ;switch ocean materail to ocean2
If MaterialID(ocean\matOcean2) <> GetEntityMaterial(ocean\entity)
SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean2))
ActiveOcean = #Ocean2
EndIf
EndIf
If KeyboardPushed(#PB_Key_Left)
KeyX = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Right)
KeyX = #CameraSpeed
Else
KeyX = 0
EndIf
If KeyboardPushed(#PB_Key_Up)
KeyY = -#CameraSpeed
ElseIf KeyboardPushed(#PB_Key_Down)
KeyY = #CameraSpeed
Else
KeyY = 0
EndIf
If KeyboardReleased(#PB_Key_Space)
bshowgui = 1 - bshowgui
If bshowgui
OpenControls(1)
Else
OpenControls(0)
EndIf
EndIf
EndIf
HandleGui()
If bshowgui
If CheckOutGui(MouseX(),MouseY())
MoveCamera(0, KeyX,0, KeyY)
RotateCamera(0, MouseY, MouseX,0, #PB_Relative)
EndIf
Else
MoveCamera(0, KeyX,0, KeyY)
RotateCamera(0, MouseY, MouseX,0, #PB_Relative)
EndIf
RotateBillboardGroup(0,0,0,-0.1,#PB_Relative)
RenderWorld()
Screen3DStats()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf
Else
MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf
EndProcedure
main()
End