January 15, 2021, 06:21:22 PM

Author Topic: [bb] Displacement Mapping by Ziltch [ 1+ years ago ]  (Read 1279 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Displacement Mapping by Ziltch [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Displacement Mapping
Author : Ziltch
Posted : 1+ years ago

Description : Here is a displacement mapping function and some routines to show it off.

Function displace(mesh,texture,texsize#=256,amp#=1,wrap=false)

Mesh = the entity you wish to modify
The texture needs to be square ie 256=256x256,512=512x512
amp = The heightmap.
The wrap command is my attempt at handling spheres, etc.

You need lots of vertices in a object for this to look good.  So it not for the polygon shy!

In the example code press the space bar to bumpmap and create simple random landscapes.

The bumpmap function also needs the numcolor and gettetxcol functions if you with to use it in your own code.

Have fun!


Code :
Code: BlitzBasic
  1. ; Vetex displacment mapping function and example code
  2. ; ADAmor Ziltch 2002
  3.  
  4. Graphics3D 800,600
  5. SetBuffer BackBuffer()
  6.  
  7. light=CreateLight(2)
  8. PositionEntity light,-1000,50,-10
  9. LightColor     light,255,255,40
  10. light2=CreateLight(2)
  11. PositionEntity light2,1000,50,-10
  12. LightColor     light,55,55,255
  13. light3=CreateLight(2)
  14. PositionEntity light2,0,50,1000
  15. LightColor     light,220,210,55
  16. AmbientLight   125,125,7
  17.  
  18. Global you = CreatePivot()
  19. PositionEntity you,0,00,-300
  20. cam = CreateCamera(you)
  21. camerarange    cam,1,5000
  22.  
  23. .createscene
  24.  
  25. ;--cube grid
  26. Global cubegrid = createsquare(51)
  27. PositionEntity cubegrid,0,300,1000
  28. ScaleEntity    cubegrid,2000,200,2000
  29. entitycolor    cubegrid,50,50,150
  30. rotateEntity   cubegrid ,180,0,0
  31.  
  32. cubegridtex =  gridtex(256,32,12, 70,230,10, 120,50,250)
  33. EntityTexture  cubegrid,cubegridtex,0,1
  34.  
  35. ;--bump plane
  36. Global bumpplane = createsquare(100)
  37. PositionEntity bumpplane ,0,-500,1000
  38. ScaleEntity    bumpplane ,2000,200,2000
  39. entitycolor    bumpplane ,150,50,150
  40.  
  41. bumptex=CreateTexture(256,256)
  42. spot(bumptex,256,25)
  43. EntityTexture bumpplane,bumptex,0,0
  44.  
  45. ;--bump ball
  46. Global bumpball = createsphere(30)
  47. PositionEntity bumpball ,0,0,1000
  48. ScaleEntity    bumpball ,90,90,90
  49. entitycolor    bumpball ,150,50,150
  50.  
  51. entityTexture bumpball,cubegridtex,0,0
  52.  
  53. PointEntity you,cubegrid
  54. rotateentity you,15,0,0
  55.  
  56. setfont LoadFont("Arial",14,False,False,False)
  57.  
  58. BumpAlready = false
  59. While Not KeyHit(1)
  60.  
  61.     spd =  MouseZ()+2  ; mousewheel is speed
  62.     moveyou(spd)
  63.  
  64.     If KeyHit(57)then
  65.                   if (not BumpAlready) Then   ;space for bumpmap
  66.         displace(cubegrid,cubegridtex,256,20)
  67.         displace(bumpplane,bumptex,256,20)
  68.         displace(bumpball,cubegridtex,256,5)
  69.         BumpAlready = true
  70.       else
  71.                   freeentity cubegrid
  72.                   freeentity bumpplane
  73.                   freeentity bumpball
  74.                   goto createscene
  75.       end if
  76.     end if
  77.  
  78.     TurnEntity cubegrid,0,.1,0
  79.     TurnEntity bumpball,-.1,.1,.1
  80.     UpdateNormals cubegrid
  81.  
  82.     RenderWorld
  83.     color 250,250,100
  84.     text 10,10," Hit the SPACEBAR for displacment. Arrows,home,end,endpgup,pgdwn with ctrl for movement. Mouse wheel for speed.  W for Wireframe.  Look in code for more keys"
  85.     Flip
  86. Wend
  87.  
  88.  
  89. Function displace(mesh,texture,texsize#=256,amp#=1,wrap=false)
  90.  
  91.   For sc = 1 To CountSurfaces(mesh)
  92.  
  93.     surf = GetSurface(mesh,sc)
  94.     If surf = 0 Then
  95.       RuntimeError "Cant find surface to displace with"
  96.       End
  97.     End If
  98.     maxvert = CountVertices(surf)
  99.  
  100.     For vc = 0 To maxvert-1
  101.  
  102.        bx#  = VertexX(surf,vc)
  103.        by#  = VertexY(surf,vc)
  104.        bz#  = VertexZ(surf,vc)
  105.        bnx# = VertexNX(surf,vc)
  106.        bny# = VertexNY(surf,vc)
  107.        bnz# = VertexNZ(surf,vc)
  108.        bu#  = VertexU(surf,vc)
  109.        bv#  = VertexV(surf,vc)
  110.        
  111.        If wrap Then
  112.          If (bu = 1)  Then bu = 0
  113.          If (bv =0)  Then bu = 0 : bv = 0
  114.          If (bv =1)  Then bu = 0 : bv = 1
  115.        End If
  116.        
  117.        tx# = bu*texsize
  118.        ty# = bv*texsize
  119.        gettexcol(Texture,tx,ty)
  120.        Cr#=numcolR      ; based off red channel  as this seems to look best
  121.  
  122.        If (cr > 0) Then
  123.          bxx# = bx + bnx * (Cr/255) * (amp/10)
  124.          byy# = by + bny * (Cr/255) * (amp/10)
  125.          bzz# = bz + bnz * (Cr/255) * (amp/10)
  126.          VertexCoords surf,vc,bxx,byy,bzz
  127.        End If
  128.     Next
  129.   Next
  130.   UpdateNormals mesh
  131. End Function
  132.  
  133. Global numcolR#,numcolG,numcolB
  134. Function numcolor(num#)
  135. ;convert number to r g b values
  136.   numcolR=num  Shr 16 And %11111111
  137.   numcolG=num Shr 8 And %11111111
  138.   numcolB=num And %11111111
  139. End Function
  140.  
  141. Function gettexcol(tex,x,y)
  142. ; get results from numcolR, numcolG, numcolB
  143.   SetBuffer TextureBuffer(tex)
  144.   LockBuffer TextureBuffer(tex)
  145.   numcolor(ReadPixelFast(x,y))
  146.   UnlockBuffer TextureBuffer(tex)
  147.   SetBuffer BackBuffer()
  148. End Function
  149.  
  150.  
  151.  
  152.  
  153. ;------ VVVV  these functions are just to create an example bdisplacement map    VVVV
  154.  
  155. Function createsquare(segs#=5,parent=0)
  156.  
  157.     mesh=CreateMesh( parent )
  158.     surf=CreateSurface( mesh )
  159.  
  160.     l# =-.5
  161.     b# = -.5
  162.     tvc= 0
  163.  
  164.                 ;create all the vertices first
  165.     Repeat
  166.                   u# = l + .5
  167.                   v# = b + .5
  168.       AddVertex surf,l,0,b,u,v
  169.       tvc=tvc + 1
  170.       l = l + 1/segs
  171.       If l > .5 Then
  172.         l = -.5
  173.         b = b + 1/segs
  174.       End If
  175.     Until b > .5
  176.  
  177.     vc# =0
  178.  
  179.     ;create polys
  180.     vc# =0
  181.     Repeat
  182.  
  183.       AddTriangle (surf,vc,vc+segs+1,vc+segs+2)
  184.       AddTriangle (surf,vc,vc+segs+2,vc+1)
  185.  
  186.       vc = vc + 1
  187.       tst# =  ((vc+1) /(segs+1)) -Int ((vc+1) /(segs+1))
  188.  
  189.       If (vc > 0) And (tst=0) Then
  190.         vc = vc + 1
  191.       End If
  192.  
  193.     Until vc=>tvc-segs-1
  194.     UpdateNormals mesh
  195.     Return mesh
  196.  
  197. End Function
  198.  
  199.  
  200. Function moveyou(spd=1)
  201.  
  202.     If  KeyDown( 205 ) Then
  203.       If KeyDown(157) Then
  204.         MoveEntity you,spd,0,0    ; ctrl -> straff right
  205.       Else
  206.        TurnEntity you,0,-2,0      ; -> turn right
  207.       End If
  208.     End If
  209.     If  KeyDown( 203 ) Then
  210.       If KeyDown(157) Then
  211.         MoveEntity you,-spd,0,0   ; ctrl <- straff left
  212.       Else
  213.         TurnEntity you,0,2,0      ; <- turn left
  214.       End If
  215.     End If
  216.     If  KeyDown( 199 ) Then
  217.       If KeyDown(157) Then
  218.         TurnEntity you,0,0,2      ; ctrl home roll left
  219.       Else
  220.         TurnEntity you,-2,0,0     ; home  pitch left
  221.       End If
  222.     End If
  223.  
  224.     If  KeyDown( 207 ) Then
  225.       If KeyDown(157) Then
  226.         TurnEntity you,0,0,-2     ; ctrl end roll right
  227.       Else
  228.         TurnEntity you,2,0,0      ; end roll right
  229.       End If
  230.     End If
  231.  
  232.     If  MouseDown(1) Or KeyDown( 200 )  Then MoveEntity you,0,0,spd  ; up arrow forward
  233.     If MouseDown(2) Or KeyDown( 208 ) Then MoveEntity you,0,0,-spd   ; down arrow back
  234.     If KeyDown(201) Then MoveEntity you,0,spd,0  ; pgup raise
  235.     If KeyDown(209) Then MoveEntity you,0,-spd,0 ; pgdown lower
  236.  
  237.     If KeyHit( 17) Then wf = Not wf : WireFrame wf  ;w for wireframe
  238.  
  239.     If KeyHit(68) Then  ; F10 for snapshot
  240.        If sscnt = 0 Then sscnt = 1000
  241.        sscnt = sscnt + 1
  242.        SaveBuffer(FrontBuffer(),"snapshot"+Right(Str(sscnt),3)+".bmp")
  243.     End If
  244.  
  245.     If KeyHit(36) Then joy = Not joy   ; j key for joystick
  246.     If joy Then
  247.       jyaw#=-JoyXDir()
  248.       jpitch#=-JoyYDir()
  249.       TurnEntity you,jpitch,jyaw,0
  250.       If JoyDown(7) Then MoveEntity you,0,0,spd
  251.       If JoyDown(8) Then MoveEntity you,0,0,-spd
  252.  
  253.     End If
  254.    
  255. End Function
  256. Global WF,joy
  257.  
  258.  
  259. Function  spot(tex,texsize,numspots,clstrue=true)
  260.  
  261.         SetBuffer  TextureBuffer(tex)
  262.  
  263.   if clstrue then
  264.           color 0,0,0
  265.     Rect 0,0,texsize,texsize,1
  266.   end if
  267.        
  268.         lockbuffer TextureBuffer(tex)
  269.   for sc = 1 to numspots
  270.     spotsize=Rand(25,64): if spotsize >60 then spotsize=spotsize*2
  271.     rx=Rand(spotsize+1,texsize-spotsize)
  272.     ry=Rand(spotsize+1,texsize-spotsize)
  273.     For a# = 1 To spotsize-1 step 1
  274.                   i# = 0
  275.                         while i < 360
  276.                           sx=sin(i)*a+rx
  277.                           sy=cos(i)*a+ry
  278.                                 i = i + .5
  279.                           rc=readpixel(sx,sy,texturebuffer(tex))
  280.                        
  281.                         numcolor(rc)
  282.                   nr#=(abs (sin((a-1)/4))*(spotsize-a)*2+numcolR*.9)mod 255
  283.  
  284.         nc=Colornum(nr,numcolG+1,numcolB+1)
  285.         writepixelfast sx,sy,nc
  286.       wend
  287.  
  288.     Next
  289.         next
  290.  
  291.         unlockbuffer TextureBuffer(tex)
  292.         SetBuffer BackBuffer()
  293.        
  294. End Function
  295.  
  296.  
  297.  
  298. Function colornum(r,g,b)
  299.  return ((r Shl 16) + (g Shl 8) + b)
  300. End Function
  301.  
  302.  
  303.  
  304. Function gridtex(s=256,st=128,width=16,colr=0,colg=0,colb=0,backcolr=0,backcolg=0,backcolb=0)
  305.   tex=CreateTexture(s,s)
  306.   SetBuffer TextureBuffer(tex)
  307.   Color backcolr,backcolg,backcolb
  308.   Rect 0,0,s,s,1
  309.   a = 0
  310.   i#=s/260
  311.   Repeat
  312.     Color colr,colg,colb
  313.     For w= 0 To width-1
  314.       Line a+w,0,a+w,s
  315.       Line 0,a+w,s,a+w
  316.     Next
  317.     a = a + st
  318.   Until a => s
  319.   SetBuffer BackBuffer()
  320.   Return tex
  321.  
  322. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal