Ocean shader demo pb5.0b7

Everything related to 3D programming
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Ocean shader demo pb5.0b7

Post by idle »

Ocean shader demo from Ogre examples
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
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Ocean shader demo pb5.0b7

Post by Comtois »

Great. Thanks for sharing.
Please correct my english
http://purebasic.developpez.com/
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Ocean shader demo pb5.0b7

Post by djes »

Yeah, what a nice example :)
Nicer :

Code: Select all

CreatePlane(1,1000,1000,256,128,1,1)
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Ocean shader demo pb5.0b7

Post by Fred »

Excellent !
flood
User
User
Posts: 32
Joined: Sun Aug 14, 2011 10:32 pm

Re: Ocean shader demo pb5.0b7

Post by flood »

Thanks for this. I figured out how to add more parameters, like changing the color etc, but do you know if there is a way to change the transparency? Also, is it possible to send a string to a .material script?

Thanks!
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 206
Joined: Mon Jun 09, 2003 8:30 am

Re: Ocean shader demo pb5.0b7

Post by bobobo »

excellent only when you don't have a onboard Intel 965 Express

I see a gray plane and no ocean.

that is indeed better than the water.pb in examples.
in opengl-mode this will hang at RenderWorld()

but in "normal" mode this runs ok including those nice underwater-rays
사십 둘 .
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Ocean shader demo pb5.0b7

Post by idle »

Thanks guys!
flood wrote:Thanks for this. I figured out how to add more parameters, like changing the color etc, but do you know if there is a way to change the transparency? Also, is it possible to send a string to a .material script?

Thanks!
flood, I haven't really looked into it. I was just interested in getting shader parameters working.
Not sure how you'd do transparency and I don't think you can send strings to a material script.
if you want to look into shaders in more detail get a copy of the ogre sdk and look at it's material scripts
and check out lighthouse3d.com the glsl tutorials and examples.
http://www.lighthouse3d.com/tutorials/glsl-tutorial/
Windows 11, Manjaro, Raspberry Pi OS
Image
dman
User
User
Posts: 48
Joined: Thu Oct 01, 2009 2:10 pm

Re: Ocean shader demo pb5.0b7

Post by dman »

WOW! that is impressive! Thanks for sharing!
PureBasic
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Ocean shader demo pb5.0b7

Post by DK_PETER »

Backup Screen3DRequester.pb and ocean.pb located in ocean.zip (first post)

Cross platform Ocean glsl vertex and fragment shader demo, from ogre 1.7.3 samples OceanDemo
Code by Idle
Working with PB 5.31 - by DK_Peter
NOTE:
I've done so you can watch the water.
Further error corrections and changes - I leave completely up to you.

File: Screen3DRequester.pb

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - Common 3D functions
;
;    (c) 2003 - Fantaisie Software
;
; ------------------------------------------------------------
;

#WINDOW_Screen3DRequester = 0

#GADGET_FullScreen        = 1
#GADGET_Windowed          = 2
#GADGET_ScreenModesLabel  = 3
#GADGET_WindowedModes     = 4
#GADGET_Launch            = 5
#GADGET_Cancel            = 6
#GADGET_Logo              = 7
#GADGET_Frame             = 8
#GADGET_ScreenModes       = 9

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
   If Not Subsystem("OpenGL")
     MessageRequester("Warning :", "Please compile With OpenGL subsystem") 
     End 
   EndIf   
CompilerEndIf

Structure ScreenModes
  width.i
  height.i
  depth.i
  refresh.i
EndStructure 

Global Screen3DRequester_FullScreen, Screen3DRequester_ShowStats

Procedure Screen3DRequester()
  Protected ratio.f
 
  OpenPreferences("Demos3D.prefs")
  FullScreen          = ReadPreferenceLong  ("FullScreen"        , 1)
  FullScreenMode$     = ReadPreferenceString("FullScreenMode" , "")
  WindowedScreenMode$ = ReadPreferenceString("WindowedScreenMode", "")


  If OpenWindow(#WINDOW_Screen3DRequester, 0, 0, 396, 205, "PureBasic - 3D Demos", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_Invisible)
    
    Top = 6
    
    ImageGadget  (#GADGET_Logo, 6, Top, 0, 0, LoadImage(0,"Data/PureBasicLogo.bmp"), #PB_Image_Border) : Top+76
    
    FrameGadget(#GADGET_Frame, 6, Top, 384, 80, "", 0) : Top+20
    
    OptionGadget(#GADGET_FullScreen, 70, Top, 100, 25, "Fullscreen") : Top+25
    OptionGadget(#GADGET_Windowed  , 70, Top, 100, 25, "Windowed")   : Top-25
   
    ComboBoxGadget (#GADGET_ScreenModes  , 190, Top, 150, 30) : Top+25
    ComboBoxGadget (#GADGET_WindowedModes, 190, Top, 150, 30) : Top+45
   
    ButtonGadget (#GADGET_Launch,   6, Top, 180, 25, "Launch", #PB_Button_Default)
    ButtonGadget (#GADGET_Cancel, 200, Top, 190, 25, "Cancel")
    
   Protected NewList modes.ScreenModes()

    If ExamineScreenModes()
          
          While NextScreenMode()
          
            Width       = ScreenModeWidth()
            Height      = ScreenModeHeight()
            Depth       = ScreenModeDepth()
            RefreshRate = ScreenModeRefreshRate()
            
          If (Depth > 8 And RefreshRate > 0) 
             AddElement(modes())
             modes()\depth = Depth
             modes()\height = Height
             modes()\width = width
             modes()\refresh = RefreshRate
          EndIf
           
          Wend        
          SortStructuredList(modes(),#PB_Sort_Descending,OffsetOf(ScreenModes\width),#PB_Integer) 
         
     EndIf
        
     ForEach modes() 
        strmode.s = Str(modes()\width) + "x" + Str(modes()\height) + "x" + Str(modes()\depth) + "@" + Str(modes()\refresh)
        AddGadgetItem(#GADGET_ScreenModes, -1, strmode)
         If  FullScreenMode$ = ""
           FullScreenMode$ = strmode
         EndIf
         If Not ratio 
           ratio = modes()\width / modes()\height 
         EndIf  
     Next   
    
    ExamineDesktops()
    NbScreenModes = 7
    
  If ratio < 1.6
    Restore WindowedScreenDimensions
  Else 
    Restore WindowedScreenWideDimensions  
  EndIf 
  
   Repeat      
      Read.l CurrentWidth
      Read.l CurrentHeight
      
      If CurrentWidth < DesktopWidth(0) And CurrentHeight < DesktopHeight(0)
        AddGadgetItem(#GADGET_WindowedModes, -1, Str(CurrentWidth)+ "x"+Str(CurrentHeight))
        NbScreenModes - 1
      Else
        NbScreenModes = 0
      EndIf
      If  WindowedScreenMode$ = "" 
        WindowedScreenMode$ = Str(CurrentWidth)+ "x"+Str(CurrentHeight)
      EndIf   
    Until NbScreenModes = 0
    
    SetGadgetState(#GADGET_FullScreen, FullScreen)
    SetGadgetState(#GADGET_Windowed  , 1-FullScreen)

    SetGadgetText (#GADGET_ScreenModes  , FullScreenMode$)
    SetGadgetText (#GADGET_WindowedModes, WindowedScreenMode$)
    
    DisableGadget (#GADGET_ScreenModes  , 1-FullScreen)
    DisableGadget (#GADGET_WindowedModes, FullScreen)
    
    HideWindow(#WINDOW_Screen3DRequester, 0)
    
    Repeat
      
      Event = WaitWindowEvent()
      
      Select Event
        
      Case #PB_Event_Gadget
        
        Select EventGadget()
          
        Case #GADGET_Launch
          Quit = 2
          
        Case #GADGET_Cancel
          Quit = 1
          
        Case #GADGET_FullScreen
          DisableGadget(#GADGET_ScreenModes  , 0)
          DisableGadget(#GADGET_WindowedModes, 1)
        
        Case #GADGET_Windowed
          DisableGadget(#GADGET_ScreenModes  , 1)
          DisableGadget(#GADGET_WindowedModes, 0)
                 
        EndSelect
        
      Case #PB_Event_CloseWindow
        Quit = 1
        
      EndSelect
      
    Until Quit > 0
    
    FullScreen          = GetGadgetState(#GADGET_FullScreen)
    FullScreenMode$     = GetGadgetText (#GADGET_ScreenModes)
    WindowedScreenMode$ = GetGadgetText (#GADGET_WindowedModes)
    
    CloseWindow(#WINDOW_Screen3DRequester)
      
  EndIf
  
  If Quit = 2 ; Launch button has been pressed
  
    CreatePreferences("Demos3D.prefs")
      WritePreferenceLong  ("FullScreen"        , FullScreen)          
      WritePreferenceString("FullScreenMode"    , FullScreenMode$)     
      WritePreferenceString("WindowedScreenMode", WindowedScreenMode$) 

    If FullScreen
      ScreenMode$ = FullScreenMode$
    Else
      ScreenMode$ = WindowedScreenMode$
    EndIf
    
    RefreshRate = Val(StringField(ScreenMode$, 2, "@"))
    
    ScreenMode$ = StringField(ScreenMode$, 1, "@") ; Remove the refresh rate info, so we can parse it easily
    
    ScreenWidth  = Val(StringField(ScreenMode$, 1, "x"))
    ScreenHeight = Val(StringField(ScreenMode$, 2, "x"))
    ScreenDepth  = Val(StringField(ScreenMode$, 3, "x"))
    
    Screen3DRequester_FullScreen = FullScreen ; Global variable, for the Screen3DEvents
    
    If FullScreen
      Result = OpenScreen(ScreenWidth, ScreenHeight, ScreenDepth, "3D Demos", #PB_Screen_WaitSynchronization, RefreshRate)
    Else
      If OpenWindow(0, 0, 0, ScreenWidth, ScreenHeight+MenuHeight(), "PureBasic - 3D Demos", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
      
        CreateMenu(0, WindowID(#WINDOW_Screen3DRequester))
          MenuTitle("Project")
          MenuItem(0, "Quit")
    
          MenuTitle("About")
          MenuItem(1, "About...")
              
        Result = OpenWindowedScreen(WindowID(#WINDOW_Screen3DRequester), 0, 0, ScreenWidth, ScreenHeight, 0, 0, 0)
      EndIf
    EndIf
  EndIf
     
  ProcedureReturn Result
EndProcedure


Procedure Screen3DEvents()

  If Screen3DRequester_FullScreen = 0 ; Handle all the events relatives to the window..
  
    Repeat
      Event = WindowEvent()
      
      Select Event
      
        Case #PB_Event_Menu
          Select EventMenu()
          
            Case 0
              Quit = 1
          
            Case 2
              MessageRequester("Info", "Windowed 3D in PureBasic !", 0)
              
          EndSelect
             
        Case #PB_Event_CloseWindow
          Quit = 1
        
      EndSelect
      
      If Quit = 1 : End : EndIf  ; Quit the app immediately
    Until Event = 0
    
  EndIf
  
  If ExamineKeyboard()
    If KeyboardReleased(#PB_Key_F1)
      Screen3DRequester_ShowStats = 1-Screen3DRequester_ShowStats ; Switch the ShowStats on/off
    EndIf
  EndIf
          
EndProcedure


Procedure Screen3DStats()
  If Screen3DRequester_ShowStats
    ; Nothing printed for now
  EndIf
EndProcedure

DataSection
  WindowedScreenDimensions:
    Data.l  320, 240
    Data.l  512, 384      
    Data.l  640, 480
    Data.l  800, 600     
    Data.l 1024, 768
    Data.l 1280, 1024
    Data.l 1600, 1200
  WindowedScreenWideDimensions:
    Data.l 512,   320
    Data.l 640,   400
    Data.l 800,   600
    Data.l 1024, 768
    Data.l 1280, 1024 
    Data.l 1440, 900
    Data.l 1600, 1200
 EndDataSection

File: ocean.pb

Code: Select all


CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_Windows
If Not Subsystem("OpenGL")
   MessageRequester("Warning :", "Please compile With OpenGL subsystem") 
   End 
 EndIf   
CompilerEndIf
CompilerIf  #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
  ImportC ""
    glLightfv(light,pname,*param)
  EndImport  
CompilerElse 
  Import ""
    glLightfv(light,pname,*param)
  EndImport  
CompilerEndIf

#GL_LIGHT = $4000
#GL_AMBIENT = $1200 
#GL_DIFFUSE = $1201 
#GL_SPECULAR = $1202 
#GL_POSITION  = $1203 

CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #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

Procedure SetShaderInput(pos,x.f,y.f,z.f,w.f)
  Static light
  If Not light 
   light = CreateLight(1,0)
  EndIf   
  Dim arr.f(4)
  arr(0) = x : arr(1) = y : arr(2) = z : arr(3) = w
  Select pos
    Case 1 
      glLightfv(#GL_LIGHT+1, #GL_AMBIENT, @arr(0))
    Case 2   
      glLightfv(#GL_LIGHT+1, #GL_DIFFUSE, @arr(0))
    Case 3   
      glLightfv(#GL_LIGHT+1, #GL_POSITION, @arr(0))
    Case 4   
      glLightfv(#GL_LIGHT+1, #GL_SPECULAR, @arr(0))
  EndSelect
EndProcedure   

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

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  
  OpenWindow3D(#GUIWindow, 50, 50, 300, 300, "Ocean shader",#PB_Window3D_Invisible)
  If IsWindow3D(#GUIWindow)
		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 
		Debug "yed"
		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))
    
  If state = 1
    ShowGUI(128,1)
	  HideWindow3D(#GUIWindow,0)
	Else 
	  ShowGUI(128,0)
	  HideWindow3D(#GUIWindow,1)
	EndIf   
 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
      		        SetShaderInput(1,WaveFreq,WaveAmp,Bumpscale,0)
      		      EndIf   
      		     Case #Amplitude
      		      WaveAmp = GetGadgetState3D(#Amplitude) * #AmplitudeSF  
      		      If bshowgui
      		        SetShaderInput(1,WaveFreq,WaveAmp,Bumpscale,0)
      		      EndIf   
      		    Case #Surface
      		      BumpScale = GetGadgetState3D(#Surface) * #SurfaceSF
      		      If bshowgui
      		        SetShaderInput(1,WaveFreq,WaveAmp,Bumpscale,0)
      		      EndIf 
      		    Case #speedX
      		      WaveSpeedX = GetGadgetState3D(#SpeedX) * #SpeedXSF
      		      If bshowgui
      		        SetShaderInput(3,WaveSpeedX,WaveSpeedY,0,0)
      		      EndIf   
      		    Case #speedY
      		      WaveSpeedY = GetGadgetState3D(#SpeedY) * #SpeedYSF
      		      If bshowgui 
      		        SetShaderInput(3,WaveSpeedX,WaveSpeedY,0,0)
      		      EndIf   
      		    Case #scaleX
      		      TextureScaleX = GetGadgetState3D(#ScaleX)
      		      If bshowgui
      		        SetShaderInput(2,TextureScaleX,TextureScaleY,0,0) 
      		      EndIf   
 		   		    Case #scaleY   
      		      TextureScaleY = GetGadgetState3D(#ScaleY)
      		      If bshowgui
      		        SetShaderInput(2,TextureScaleX,TextureScaleY,0,0) 
      		      EndIf   
 		  		    Case #Ocean1
 		  		      If MaterialID(ocean\matOcean) <> FetchEntityMaterial(ocean\entity, ocean\matOcean)
 		  		       SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean)) 
                 ActiveOcean = #Ocean1
               EndIf
             Case #Ocean2 
               If MaterialID(ocean\matOcean2) <> FetchEntityMaterial(ocean\entity, ocean\matOcean2)
                SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean2)) 
                ActiveOcean = #Ocean2  
              EndIf  
             EndSelect
         EndSelect
     	Until Event = 0
  EndProcedure   
  
Procedure main()
     
  If InitEngine3D()
    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   
      
      ;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
    
      ;we pass parameters directly to the vertex shader via calls to gllightfv so we can set 12 floats per light. 
      ;The vertex shader reads the values directly from calls to gl_lightsource[1].ambient / diffuse / position / specular 
      SetShaderInput(1,WaveFreq,WaveAmp,Bumpscale,0)
      SetShaderInput(2,TextureScaleX,TextureScaleY,0,0) 
     	SetShaderInput(3,WaveSpeedX,WaveSpeedY,0,0)
        
      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
      
      ocean\matFlare = CreateMaterial(#PB_Any,LoadTexture(1,"flare.png")) 
      MaterialBlendingMode(ocean\matFlare,#PB_Material_Add)                      
      CreateBillboardGroup(0,MaterialID(ocean\matFlare), 2000, 2000)            
      AddBillboard(0,-5000,5000,5000)                                                              
      
      ;note you can change skybox at any time 
      SkyBox("morning.jpg")  
       		
      CreateCamera(0, 0, 0, 100, 100)
         
      MoveCamera(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), Input$)
        EndIf      
      
        If ExamineKeyboard()
          If KeyboardPushed(#PB_Key_1)  ;switch ocean materal to ocean
            If MaterialID(ocean\matOcean) <> FetchEntityMaterial(ocean\entity, ocean\matOcean)
              SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean)) 
              ActiveOcean = #Ocean1
            EndIf   
          EndIf 
          If KeyboardPushed(#PB_Key_2) ;switch ocean materail to ocean2 
             If MaterialID(ocean\matOcean2) <> FetchEntityMaterial(ocean\entity,ocean\matOcean2)
              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
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Ocean shader demo pb5.0b7

Post by applePi »

here is the idle ocean inside a teacup, thanks DK_PETER for the update to pb5.31.
the cylinder instead of a plane is like this:
CreateCylinder(1,10,0)
CreateEntity(1,MeshID(1), #PB_Material_None )
ScaleEntity(1,1,1,0
)
so its Lenght=0 and scaled on z to 0 , if not scaled to 0 we will see like tides on the suface. also change WaveFreq and others to other values (just experimental).
also try the surface as sphere. i get a pure liquid ball if we use:
CreateSphere(1,10)
;CreateCylinder(1,10,0) ; ;create a surface to render the tea liquid on
CreateEntity(1,MeshID(1), #PB_Material_None, 0,100,0 )
ScaleEntity(1,1,1,0)

unexpected really, the thin sphere body with z scaled to 0 will be 100 units in the sky but (say the sphere soul) will stay spherical inside the teacup
seems to me the wave mathematics in file Ocean2GLSL.vert are advanced and using calculus so there is a possibility to use these equation in the normal waving flag mesh, but the the cpu will not work good like in this shader which use i think the gpu !!!

use the DK_PETER Screen3DRequester.pb updated file above with the idle ocean zip at the top of the page.
the following file is the ocean.pb updated by DK_PETER , but changing the surface to cylinder, and disabling the space key. compile With OpenGL subsystem

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_Windows
If Not Subsystem("OpenGL")
   MessageRequester("Warning :", "Please compile With OpenGL subsystem") 
   End 
 EndIf   
CompilerEndIf
CompilerIf  #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
  ImportC ""
    glLightfv(light,pname,*param)
  EndImport  
CompilerElse 
  Import ""
    glLightfv(light,pname,*param)
  EndImport  
CompilerEndIf

#GL_LIGHT = $4000
#GL_AMBIENT = $1200 
#GL_DIFFUSE = $1201 
#GL_SPECULAR = $1202 
#GL_POSITION  = $1203 

CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #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

Procedure SetShaderInput(pos,x.f,y.f,z.f,w.f)
  Static light
  If Not light 
   light = CreateLight(1,0)
  EndIf   
  Dim arr.f(4)
  arr(0) = x : arr(1) = y : arr(2) = z : arr(3) = w
  Select pos
    Case 1 
      glLightfv(#GL_LIGHT+1, #GL_AMBIENT, @arr(0))
    Case 2   
      glLightfv(#GL_LIGHT+1, #GL_DIFFUSE, @arr(0))
    Case 3   
      glLightfv(#GL_LIGHT+1, #GL_POSITION, @arr(0))
    Case 4   
      glLightfv(#GL_LIGHT+1, #GL_SPECULAR, @arr(0))
  EndSelect
EndProcedure   

Global ocean.OceanScene

#CameraSpeed = 1

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

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  
  OpenWindow3D(#GUIWindow, 50, 50, 300, 300, "Ocean shader",#PB_Window3D_Invisible)
  If IsWindow3D(#GUIWindow)
      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 
      Debug "yed"
      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))
    
  If state = 1
    ShowGUI(128,1)
     HideWindow3D(#GUIWindow,0)
   Else 
     ShowGUI(128,0)
     HideWindow3D(#GUIWindow,1)
   EndIf   
 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
                    SetShaderInput(1,WaveFreq,WaveAmp,Bumpscale,0)
                  EndIf   
                 Case #Amplitude
                  WaveAmp = GetGadgetState3D(#Amplitude) * #AmplitudeSF  
                  If bshowgui
                    SetShaderInput(1,WaveFreq,WaveAmp,Bumpscale,0)
                  EndIf   
                Case #Surface
                  BumpScale = GetGadgetState3D(#Surface) * #SurfaceSF
                  If bshowgui
                    SetShaderInput(1,WaveFreq,WaveAmp,Bumpscale,0)
                  EndIf 
                Case #speedX
                  WaveSpeedX = GetGadgetState3D(#SpeedX) * #SpeedXSF
                  If bshowgui
                    SetShaderInput(3,WaveSpeedX,WaveSpeedY,0,0)
                  EndIf   
                Case #speedY
                  WaveSpeedY = GetGadgetState3D(#SpeedY) * #SpeedYSF
                  If bshowgui 
                    SetShaderInput(3,WaveSpeedX,WaveSpeedY,0,0)
                  EndIf   
                Case #scaleX
                  TextureScaleX = GetGadgetState3D(#ScaleX)
                  If bshowgui
                    SetShaderInput(2,TextureScaleX,TextureScaleY,0,0) 
                  EndIf   
                    Case #scaleY   
                  TextureScaleY = GetGadgetState3D(#ScaleY)
                  If bshowgui
                    SetShaderInput(2,TextureScaleX,TextureScaleY,0,0) 
                  EndIf   
                   Case #Ocean1
                     If MaterialID(ocean\matOcean) <> FetchEntityMaterial(ocean\entity, ocean\matOcean)
                      SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean)) 
                 ActiveOcean = #Ocean1
               EndIf
             Case #Ocean2 
               If MaterialID(ocean\matOcean2) <> FetchEntityMaterial(ocean\entity, ocean\matOcean2)
                SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean2)) 
                ActiveOcean = #Ocean2  
              EndIf  
             EndSelect
         EndSelect
        Until Event = 0
  EndProcedure   
  
Procedure main()
     
  If InitEngine3D()
    Add3DArchive("GUI/", #PB_3DArchive_FileSystem)
    Add3DArchive("Data/", #PB_3DArchive_FileSystem)
    Add3DArchive("Data/cubemapsJS.zip", #PB_3DArchive_Zip)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
       
     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   
      
      ;set up some control variables for the ocean2 material      
      ;WaveFreq = 0.028 
      WaveFreq = 0.1 
      WaveAmp = 1
      BumpScale = 0.2 
      TextureScaleX = 25
      TextureScaleY = 25 
      ;WaveSpeedX = 0.015
      ;WaveSpeedY = 0.005
      WaveSpeedX = 0.045
      WaveSpeedY = 0.05
    
      ;we pass parameters directly to the vertex shader via calls to gllightfv so we can set 12 floats per light. 
      ;The vertex shader reads the values directly from calls to gl_lightsource[1].ambient / diffuse / position / specular 
      SetShaderInput(1,WaveFreq,WaveAmp,Bumpscale,0)
      SetShaderInput(2,TextureScaleX,TextureScaleY,0,0) 
        SetShaderInput(3,WaveSpeedX,WaveSpeedY,0,0)
        
      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 
      ;CreatePlane(1,20,20,50,50,1,1)  ;create a surface to render the ocean on 
      ;CreateSphere(1,10)
      CreateCylinder(1,10,0) ; ;create a surface to render the tea liquid on 
      CreateEntity(1,MeshID(1), #PB_Material_None )
      ScaleEntity(1,1,1,0)
      
      CreateMaterial(5,LoadTexture(5,"Geebee2.bmp"))
      MaterialCullingMode(5, #PB_Material_NoCulling )
      
      CreateCylinder(2,10,25, 8,0,0) ; the teacup
      CreateEntity(2,MeshID(2), MaterialID(5), 0,-7,0 )
      CreateMaterial(6,LoadTexture(6,"ground_diffuse.png"))
      
      CreatePlane(3,1000,1000,50,50,10,10)
      CreateEntity(3,MeshID(3), MaterialID(6), 0,-17,0)
      ;ScaleEntity(1,1,0.1,0.1)
      ocean\entity = CreateEntity(#PB_Any, MeshID(1), MaterialID(ocean\matOcean2)) ;set the material to the entity
      
      ocean\matFlare = CreateMaterial(#PB_Any,LoadTexture(1,"flare.png")) 
      MaterialBlendingMode(ocean\matFlare,#PB_Material_Add)                      
      CreateBillboardGroup(0,MaterialID(ocean\matFlare), 2000, 2000)            
      AddBillboard(0,-5000,5000,5000)                                                              
      
      ;note you can change skybox at any time 
      SkyBox("morning.jpg")  
             
      CreateCamera(0, 0, 0, 100, 100)
         
      MoveCamera(0, 0,30,40)
      CameraLookAt(0,0,0,0)
      
      OpenControls()
    
      Repeat
        Screen3DEvents()
        
        If ExamineMouse()
          MouseX = -(MouseDeltaX()*0.10)*#CameraSpeed*2
          MouseY = -(MouseDeltaY()*0.10)*#CameraSpeed*2
          ;InputEvent3D(MouseX(), MouseY(), MouseButton(#PB_MouseButton_Left), Input$)
        EndIf      
      
        If ExamineKeyboard()
          If KeyboardPushed(#PB_Key_1)  ;switch ocean materal to ocean
            If MaterialID(ocean\matOcean) <> FetchEntityMaterial(ocean\entity, ocean\matOcean)
              SetEntityMaterial(ocean\entity, MaterialID(ocean\matOcean)) 
              ActiveOcean = #Ocean1
            EndIf   
          EndIf 
          If KeyboardPushed(#PB_Key_2) ;switch ocean materail to ocean2 
             If MaterialID(ocean\matOcean2) <> FetchEntityMaterial(ocean\entity,ocean\matOcean2)
              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
User avatar
DK_PETER
Addict
Addict
Posts: 904
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: Ocean shader demo pb5.0b7

Post by DK_PETER »

@applePi

Nice! Only problem is the cylinder..If it isn't flat, you'll be able to see artifacts from the cylinder itself...(bug already reported some time ago).

I've take idle's project further even though I wrote, that I wouldn't.

Here's the new Idle's watershader project. Completely PB 5.31 compatible.

https://www.dropbox.com/s/uax6seaejale7 ... w.zip?dl=0

EDIT: You still switch ocean material using keys 1 and 2.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Ocean shader demo pb5.0b7

Post by Fred »

New version packaged in a zip for 5.40+: www.purebasic.com/download/ocean.zip
Post Reply