November 28, 2020, 11:01:22 AM

Author Topic: [bb] Aqua Effect on 3D Cube by Markus Rauch [ 1+ years ago ]  (Read 711 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Aqua Effect on 3D Cube
Author : Markus Rauch
Posted : 1+ years ago

Description : Aqua is nice ! Test it !
The Code writing water hight fields in a Buffer and
copy them to a texture that is on a cube that simply
rotate in a 3D World . It has automatic rains drops but you can
hold down the left mouse button and move it !
The gray Pixel in the texture on the cube it your mouse pointer .
What you need is a Texture.bmp file in the same directory as the Code .
If you have a CPU <= 400 MHz you need a Texture with size 128x128 .
The Texture you need must have the same x and y size .
First test it in debug mode (very very very slow) , when it works disable the
debug mode to run it fast .


Code :
Code: BlitzBasic
  1. ; Aqua Effect (C) 2002 by M.Rauch from Germany
  2.  
  3. ; If DebugMode = True Then it is very slow !
  4.  
  5. ; MR 03.11.2002
  6.  
  7. .Top
  8.  
  9. Graphics3D 640,480,16,1
  10.  
  11. SetBuffer BackBuffer()
  12.  
  13. ;------------------------------------------------------------------------- Camera
  14.  
  15. .Cam
  16.  
  17. Global Camera =CreateCamera()
  18.  
  19. PositionEntity Camera,0,0,-10
  20.  
  21. CameraClsColor Camera,0,20,0
  22.  
  23. ;------------------------------------------------------------------------- Texture
  24.  
  25. .Texture
  26.  
  27. ;a Texture size 128 x 128 is very fast
  28.  
  29. Global ReflectImage=LoadTexture("Texture.bmp")    ;<---
  30. Global ReflectImageB=TextureBuffer(ReflectImage)
  31.  
  32. Global Output=CreateTexture(TextureWidth(ReflectImage),TextureHeight(ReflectImage),1) ;The same size
  33. Global OutputB=TextureBuffer(Output)
  34.  
  35. ;-------------------------------------------------------------------------
  36.  
  37. .Entitys
  38.  
  39.  Global Cube=CreateCube()
  40.  
  41.  ScaleMesh Cube,3,3,3
  42.  
  43.  EntityTexture Cube,Output
  44.  
  45. ;--------------------------------------------------------
  46.  
  47. .Light
  48.  
  49.  Global Light1 =CreateLight(1)
  50.  Global Light2 =CreateLight(1)
  51.  Global Light3 =CreateLight(1)
  52.  
  53.  LightRange Light1 ,50
  54.  LightRange Light2 ,50
  55.  LightRange Light3 ,50
  56.  
  57.  LightColor Light1, 64, 64, 64
  58.  LightColor Light2,255,255,255
  59.  LightColor Light3, 64, 64, 64
  60.  
  61.  PositionEntity Light1 ,-10, 10,-10
  62.  PositionEntity Light2 ,  0,  0,-10
  63.  PositionEntity Light3 , 10,-10,-10
  64.  
  65.  AmbientLight 0,0,0
  66.  
  67. ;------------------------------------------------------------------------- WaterSettings
  68.  
  69. .WaterSettings
  70.  
  71. Global WATERSIZE=TextureWidth(ReflectImage) ;like 128 or 256
  72.  
  73. Global RainCount=0
  74.  
  75. Global DripRadius = 12
  76. Global DripRadiusSqr = DripRadius * DripRadius
  77. Global DampingFactor# = 0.04 ;Values For damping from 0.04 - 0.0001 look pretty good (the Buffer must in float)
  78.  
  79. .WaterBuffers
  80.  
  81. Global BufferSize=(WATERSIZE * WATERSIZE)
  82.  
  83. Dim ReadBuffer #(BufferSize)
  84. Dim WriteBuffer#(BufferSize)
  85. Dim TempBuffer #(BufferSize)
  86.  
  87. Local i
  88.  
  89. For i = 0 To BufferSize
  90.  TempBuffer (i) = 0
  91.  ReadBuffer (i) = 0
  92.  WriteBuffer(i) = 0
  93. Next
  94.  
  95. ;------------------------------------------------------------------------- MainLoop
  96.  
  97. .MainLoop
  98.  
  99. While Not KeyHit(1) ; 1=Escape
  100.  
  101.  Local ti#
  102.  
  103.  ti=MilliSecs()
  104.  
  105.  SwapBuffers
  106.  Show
  107.  
  108.  CheckMouse ;<- Press left Button and move the Mouse
  109.  
  110.  TurnEntity Cube,1,-1.5,0
  111.  
  112.  RenderWorld
  113.  
  114.  Rain ;<- automatic
  115.  
  116.  ProcessWater
  117.  
  118.  While Abs(MilliSecs()-ti)<10
  119.  Wend
  120.  
  121.  Flip
  122. Wend
  123. End
  124.  
  125. ;-------------------------------------------------------------------------
  126.  
  127. .Buffers
  128.  
  129. ;-------------------------------------------------------------------------
  130.  
  131. Function SetBufferR(x,y,value#)
  132.  
  133.  ReadBuffer(x+y*WATERSIZE)=value
  134.  
  135. End Function
  136.  
  137. ;-------------------------------------------------------------------------
  138.  
  139. Function SetBufferW(x,y,value#)
  140.  
  141.  If value >  32 Then value =  32
  142.  If value < -32 Then value = -32
  143.  
  144.  WriteBuffer(x+y*WATERSIZE)=value
  145.  
  146. End Function
  147.  
  148. ;-------------------------------------------------------------------------
  149.  
  150. Function GetBufferR#(x,y)
  151.  
  152.  Return ReadBuffer(x+y*WATERSIZE)
  153.  
  154. End Function
  155.  
  156. ;-------------------------------------------------------------------------
  157.  
  158. Function GetBufferW#(x,y)
  159.  
  160.  Return WriteBuffer(x+y*WATERSIZE)
  161.  
  162. End Function
  163.  
  164. ;-------------------------------------------------------------------------
  165.  
  166. Function SwapBuffers()
  167.  
  168.  Local i
  169.  
  170.  ;Swap the buffers !
  171.  
  172.  For i = 0 To BufferSize
  173.   TempBuffer(i) =ReadBuffer(i)  
  174.   ReadBuffer(i) =WriteBuffer(i)  
  175.   WriteBuffer(i)=TempBuffer(i)
  176.  Next
  177.  
  178. End Function
  179.  
  180. ;-------------------------------------------------------------------------
  181.  
  182. .RenderTexture
  183.  
  184. Function Show()
  185.  
  186.  Local x,y
  187.  Local xoff,yoff
  188.  Local xm,ym
  189.  Local pix,pix2
  190.  Local r,g,b,a
  191.  Local bu
  192.  
  193.  xm=GraphicsWidth() /2-WATERSIZE/2
  194.  ym=GraphicsHeight()/2-WATERSIZE/2
  195.  
  196.  ;-----------------------------------------------
  197.  
  198.  LockBuffer ReflectImageB
  199.  LockBuffer OutputB
  200.  
  201.  Local cnt=0
  202.  
  203.   y=0
  204.   While y < WATERSIZE
  205.           x=0
  206.     While x < WATERSIZE
  207.  
  208.      xoff = x
  209.                  If x > 0 And x < WATERSIZE - 1 Then
  210.                         xoff =xoff- (ReadBuffer(cnt - 1))
  211.                         xoff =xoff+ (ReadBuffer(cnt + 1))
  212.                  EndIf
  213.  
  214.                  yoff = y
  215.                  If y > 0 And y < WATERSIZE - 1 Then
  216.                         yoff =yoff- ReadBuffer(cnt - WATERSIZE)
  217.                         yoff =yoff+ ReadBuffer(cnt + WATERSIZE)
  218.      EndIf
  219.  
  220.      If xoff < 0 Then xoff = 0
  221.      If yoff < 0 Then yoff = 0
  222.      If xoff > WATERSIZE-1 Then xoff = WATERSIZE-1
  223.      If yoff > WATERSIZE-1 Then yoff = WATERSIZE-1
  224.  
  225.      pix=ReadPixelFast(xoff,yoff,ReflectImageB)
  226.      r=(pix And $ff0000)/$10000
  227.      g=(pix And $ff00)/$100
  228.      b=(pix And $ff)
  229.  
  230.      ;r=128 ;<- only color
  231.      ;g=128
  232.      ;b=128
  233.  
  234.      bu=ReadBuffer(cnt)
  235.      r = r + bu
  236.      g = g + bu
  237.      b = b + bu
  238.      If r < 0 Then r = 0
  239.      If g < 0 Then g = 0
  240.      If b < 0 Then b = 0
  241.      If r > 255 Then r = 255
  242.      If g > 255 Then g = 255
  243.      If b > 255 Then b = 255
  244.  
  245.      pix2=ARGB(r,g,b)
  246.      WritePixelFast x,y,pix2,OutputB
  247.  
  248.      cnt=cnt+1
  249.  
  250.      x=x+1
  251.                 Wend
  252.    y=y+1
  253.         Wend
  254.  
  255.  UnlockBuffer OutputB
  256.  UnlockBuffer ReflectImageB
  257.  
  258. End Function
  259.  
  260. ;-------------------------------------------------------------------------
  261.  
  262. .Helpers
  263.  
  264. Function ARGB(r,g,b)
  265.  
  266.  ;Return ((128 * $1000000) Or (r * $10000) Or (g * $100) Or b)
  267.  Return ((r * $10000) Or (g * $100) Or b)
  268.  
  269. End Function
  270.  
  271. ;-------------------------------------------------------------------------
  272.  
  273. Function SquaredDist(sx, sy, dx, dy)
  274.  
  275.  ;Find the Squared distance between two 2D points
  276.  
  277.  Return ((dx - sx) * (dx - sx)) + ((dy - sy) * (dy - sy))
  278.  
  279. End Function
  280.  
  281. ;-------------------------------------------------------------------------
  282.  
  283. .MouseInput
  284.  
  285. Function CheckMouse()
  286.  
  287.  Local mx,my
  288.  
  289.  mx=MouseX()
  290.  my=MouseY()
  291.  
  292.  If mx<0 Then mx=0
  293.  If my<0 Then my=0
  294.  
  295.  If mx>WATERSIZE-1 Then mx=WATERSIZE-1
  296.  If my>WATERSIZE-1 Then my=WATERSIZE-1
  297.  
  298.  WritePixel mx,my,ARGB(128,128,128),OutputB  
  299.  
  300.  If MouseDown(1) Then
  301.   MakeDrip mx,my,4
  302.  EndIf
  303.  
  304. End Function
  305.  
  306. ;-------------------------------------------------------------------------
  307.  
  308. .RainInParadise
  309.  
  310. Function Rain()
  311.  
  312.  Local mx,my
  313.  
  314.  Local i
  315.  
  316.  RainCount=RainCount+1
  317.  
  318.  If RainCount > 10 Then
  319.  
  320.   RainCount=0
  321.  
  322.   SeedRnd MilliSecs()
  323.  
  324.   mx=Rnd(0,WATERSIZE-1)
  325.   my=Rnd(0,WATERSIZE-1)
  326.  
  327.   MakeDrip mx,my,4
  328.  
  329.  EndIf
  330.  
  331. End Function
  332.  
  333. ;-------------------------------------------------------------------------
  334.  
  335. .WaterDrip
  336.  
  337. Function MakeDrip(xm , ym , depth)
  338.  
  339.  ;Creates an initial drip in the water Field
  340.  
  341.  ;DebugLog "MakeDrip "+x+" "+y+" "+depth
  342.  
  343.         Local x,y
  344.         Local dist,finaldepth#
  345.  
  346.   y=ym - DripRadius
  347.   While y < ym + DripRadius
  348.    x=xm - DripRadius
  349.    While x < xm + DripRadius
  350.                 If x => 0 And y => 0 And x < WATERSIZE And y < WATERSIZE Then                  
  351.                                 dist = SquaredDist(x,y,xm,ym)
  352.                                 If dist < DripRadiusSqr Then                           
  353.                                         finaldepth = (depth * DripRadius - Sqr(dist))/DripRadius
  354.                                         If finaldepth >  127 Then finaldepth =  127
  355.                                         If finaldepth < -127 Then finaldepth = -127
  356.                                         SetBufferW x,y,finaldepth
  357.                                 EndIf
  358.                 EndIf
  359.     x=x+1
  360.          Wend          
  361.    y=y+1
  362.   Wend
  363.  
  364. End Function
  365.  
  366. ;-------------------------------------------------------------------------
  367.  
  368. .WaterInAction
  369.  
  370. Function ProcessWater()
  371.  
  372.  ;Calculate New values For the water height Field
  373.  
  374.  Local x,y
  375.  Local v#
  376.        
  377.   y=2
  378.   While y < WATERSIZE-2
  379.           x=2
  380.     While x < WATERSIZE-2
  381.                
  382.                         ;Sample a "circle" around the center point
  383.       v =0
  384.                         v = v + GetBufferR(x-2,y)
  385.                         v = v + GetBufferR(x+2,y)
  386.                         v = v + GetBufferR(x  ,y-2)
  387.                         v = v + GetBufferR(x  ,y+2)
  388.                         v = v + GetBufferR(x-1,y)
  389.                         v = v + GetBufferR(x+1,y)
  390.                         v = v + GetBufferR(x  ,y-1)
  391.                         v = v + GetBufferR(x  ,y+1)
  392.                         v = v + GetBufferR(x-1,y-1)
  393.                         v = v + GetBufferR(x+1,y-1)
  394.                         v = v + GetBufferR(x-1,y+1)
  395.                         v = v + GetBufferR(x+1,y+1)
  396.  
  397.                         v = v / 6.0            
  398.                         v = v - GetBufferW(x,y)        
  399.       v = v - (v * DampingFactor)
  400.  
  401.                         SetBufferW (x,y,v)
  402.  
  403.      x=x+1
  404.                 Wend
  405.    y=y+1
  406.         Wend
  407.  
  408. End Function
  409.  
  410. ;-------------------------------------------------------------------------


Comments :


Kryzon(Posted 1+ years ago)

 wow!!!! Nice water effect!


Trixx(Posted 1+ years ago)

 One more "WOW" :) - Really nice !


Clyde(Posted 1+ years ago)

 Nice effect mate :)


evil_1_24(Posted 1+ years ago)

 WOW :) nice effect


puki(Posted 1+ years ago)

 Heh, I don't think I ever saw that one before - very cool.Finding the right texture is the key.


puki(Posted 1+ years ago)

 In fact, this is quite amazing.This was done 4 years ago - my PC is well capable of running this quite fast.You can even create pixel-shader-esqe water ripples like in Morrowind - like when you move through water and turn around to see the waves you created behind you.  Disable the rotation of the cube to look at this.Very nice.


bytecode77(Posted 1+ years ago)

 i am working on a water eingine like your one with a bump mapping texture ("wateranim.png")...wait until first release :)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal