Sea Water Demo


Tweet blitz3d code-archives effects
(Posted 3 weeks ago) RonTek

A simple water demo with waves using mouse clicks

Author: Reda Borchardt

Image

Full assets

; Sea Demo
; Written by Reda Borchardt

;Include "start.bb"
Graphics3D 800,600,0,2
SetBuffer BackBuffer()

; Camera + Light
Global campivot = CreatePivot()
Global camera = CreateCamera(campivot)
CameraZoom camera,0.8
CameraRange camera,0.1,10000
CameraFogMode camera,1
CameraFogRange camera,0,500
CameraFogColor camera,200,255,255

PositionEntity camera,1,13,-240
PointEntity camera, campivot
light = CreateLight(2)
PointEntity light, campivot
PositionEntity light,300,300,-10000
LightColor light,0,0,255
LightRange light,500
light2 = CreateLight(2,spotpivot)
PointEntity light2, campivot
PositionEntity light2,10,2,300
LightConeAngles light,0,320
LightColor light2,255,255,255
light3 = CreateLight(3)
PointEntity light3, campivot
PositionEntity light3,2,500,-800
LightConeAngles light3,0,180
LightColor light3,-255,255,255
AmbientLight 255,255,255
light4 = CreateLight(3)
PointEntity light4, campivot
PositionEntity light4,2,500,0
LightConeAngles light4,0,180
LightColor light4,-200,-200,-200
AmbientLight 255,255,255

sky = CreateSphere(32,camera)
ScaleEntity sky,300,240,500
EntityFX sky,1
EntityColor sky,0,100,255
FlipMesh sky

; Dim Array
Dim buffer1#(100,100)
Dim buffer2#(100,100)

For x = 0 To 100
For y = 0 To 100
Buffer1#(x,y) = 0
Next
Next

; Load Patch
water = CreateFace(100,1)
Global watersurf = GetSurface(water,1)
EntityAlpha water,0.9
EntityShininess water,1
ScaleEntity water,700,100,500
RotateEntity water,0,300,0
tex = LoadTexture ("water.tga",1)
EntityTexture water,tex
mirror = CreateMirror()
ground = CreatePlane()
EntityAlpha ground,0.5
EntityColor ground,0,255,255
EntityTexture ground,tex
PositionEntity mirror,0,-5,0
PositionEntity ground,0,-5,0

; Main Loop
While Not KeyHit(1)
    ProcessWater()
    RegisterDrop()
    UpdateWorld
    RenderWorld
    Text 10,10,"Press Left and/or Right Mouse Button For Ripples"
    Text 10,20,"Written by Reda Borchardt"
    Flip
Wend

; Process Water Ripples
Function ProcessWater()
    For x = 1 To 99
        For y = 1 To 99
            Buffer2#(x,y) = (Buffer1#(x-1,y) + Buffer1#(x+1,y) + Buffer1#(x,y+1) + Buffer1#(x,y-1)) / 2.1-Buffer2#(x,y) 
            Buffer2#(x,y) = Buffer2#(x,y) / 1.02
        Next
    Next
    PatchTransform()
    SwapWaterBuffer()
End Function

; Swap The Buffer
Function SwapWaterBuffer()
    For x = 0 To 100
        For y = 0 To 100
            temp1# = Buffer1#(x,y)
            temp2# = Buffer2#(x,y)
            Buffer1#(x,y) = temp2#
            Buffer2#(x,y) = temp1#
        Next
    Next
End Function

; Register Drop
Function RegisterDrop()
    Rect MouseX(),MouseY(),2,2
    If MouseHit(1)
        For i = 50 To 55
            For j = 25 To 30
                buffer2#(i,j)=0.04
            Next
        Next
    End If
    If MouseHit(2)
        For i = 75 To 80
            For j = 75 To 80
                buffer2#(i,j)=0.04
            Next
        Next
    End If
End Function

; PatchTransform
Function PatchTransform()
    For i = 0 To 100
        For j = 0 To 100
            VertexCoords(watersurf,k,VertexX(watersurf,k),buffer2#(j,i),VertexZ(watersurf,k))
            k=k+1
        Next
    Next
End Function

;Creates a single sided face
;segmented
Function CreateFace(segs=1,double=False,parent=0)
mesh=CreateMesh( parent )
surf=CreateSurface( mesh )
stx#=-.5
sty#=stx
stp#=Float(1)/Float(segs)
y#=sty
For a=0 To segs
x#=stx
v#=a/Float(segs)
For b=0 To segs
u#=b/Float(segs)
AddVertex(surf,x,0,y,u,v) ; swap these for a different start orientation
x=x+stp
Next
y=y+stp
Next
For a=0 To segs-1
For b=0 To segs-1
v0=a*(segs+1)+b:v1=v0+1
v2=(a+1)*(segs+1)+b+1:v3=v2-1
AddTriangle( surf,v0,v2,v1 )
AddTriangle( surf,v0,v3,v2 )
Next
Next
UpdateNormals mesh
If double=True Then EntityFX mesh,16
Return mesh
End Function

Reply To Topic (minimum 10 characters)

Please log in to reply