Ooops
March 02, 2021, 02:08:07 PM

Author Topic: [bb] Height Map Generator by Rob Farley [ 1+ years ago ]  (Read 427 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Height Map Generator by Rob Farley [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Height Map Generator
Author : Rob Farley
Posted : 1+ years ago

Description : When you run this it assumes you have a grass.bmp in the same folder, this is needed to show the landscape after it is created.

Also it creates a file "heightmap.bmp" in the folder once it's done, if you want to create more heightmaps you'll have to rename the file otherwise it will be overwritten.

Cheers, Av


Code :
Code: BlitzBasic
  1. ; Height Map Generator
  2.  
  3. ; Rob Farley 2001
  4. ; rob@mentalhangover.co.uk
  5. ; http://www.mentalhangover.co.uk
  6.  
  7. ; *** This assumes you have a grass.bmp file in the same folder, please put a grass texture here. ***
  8.  
  9.  
  10. Graphics 640,480
  11. SetBuffer BackBuffer()
  12.  
  13. ; set up varibles
  14.  
  15.  
  16. size=256
  17. ; Play with these varibles to change the look of the landscape
  18.  
  19. scale=5
  20. hill_size=50
  21. hill_number=30
  22. dip_size=100
  23. dip_number=10
  24.  
  25. menu=1
  26.  
  27. While Not KeyHit(28)
  28. Cls
  29. Color 255,255,255
  30. Text 320,0,"Height Map Generator",True,False
  31. Text 320,250,"Use Arrow keys to adjust settings, enter to confirm selections",True,False
  32. Color 255,255,0
  33. Text 320,20,"Rob Farley 2001",True,False
  34. Color 0,128,128
  35.  
  36. If KeyDown(208)=True Then menu=menu+1
  37. If KeyDown(200)=True Then menu=menu-1
  38. If menu>6 Then menu=1
  39. If menu<1 Then menu=6
  40.  
  41. Color 0,128,128
  42. If menu=1
  43.         Color 0,255,255
  44.         If KeyDown(205)=True Then size=size+256
  45.         If KeyDown(203)=True Then size=size-256
  46.         If size<256 Then size=256
  47.         If size>1024 Then size=1024
  48.         EndIf
  49. Text 320,100,"Size:"+size,True,False
  50. Color 0,128,128
  51. If menu=2
  52.         Color 0,255,255
  53.         If KeyDown(205)=True Then scale=scale+1
  54.         If KeyDown(203)=True Then scale=scale-1
  55.         If scale<0 Then scale=0
  56.         If scale>50 Then scale=50
  57.         EndIf
  58. Text 320,120,"Scale:"+scale,True,False
  59. Color 0,128,128
  60. If menu=3
  61.         Color 0,255,255
  62.         If KeyDown(205)=True Then hill_size=hill_size+1
  63.         If KeyDown(203)=True Then hill_size=hill_size-1
  64.         If hill_size<0 Then hill_size=0
  65.         EndIf
  66. Text 320,140,"Hill Size:"+hill_size,True,False
  67. Color 0,128,128
  68. If menu=4
  69.         Color 0,255,255
  70.         If KeyDown(205)=True Then hill_number=hill_number+1
  71.         If KeyDown(203)=True Then hill_number=hill_number-1
  72.         If hill_number<0 Then hill_number=0
  73.         EndIf
  74. Text 320,160,"Hill Quantity:"+hill_number,True,False
  75. Color 0,128,128
  76. If menu=5
  77.         Color 0,255,255
  78.         If KeyDown(205)=True Then dip_size=dip_size+1
  79.         If KeyDown(203)=True Then dip_size=dip_size-1
  80.         If dip_size<0 Then dip_size=0
  81.         EndIf
  82. Text 320,180,"Dip Size:"+dip_size,True,False
  83. Color 0,128,128
  84. If menu=6
  85.         Color 0,255,255
  86.         If KeyDown(205)=True Then dip_number=dip_number+1
  87.         If KeyDown(203)=True Then dip_number=dip_number-1
  88.         If dip_number<0 Then dip_number=0
  89.         EndIf
  90. Text 320,200,"Dip Quantity:"+dip_number,True,False
  91. Flip
  92. Delay 100
  93. Wend
  94.  
  95.  
  96.  
  97. Dim land#(size,size)
  98. Dim land2#(size,size)
  99. Dim hill#(45,45)
  100. Dim blur#(9,9)
  101. Data 0,0,0,1,1,1,0,0,0
  102. Data 0,0,1,2,3,2,1,0,0
  103. Data 0,1,2,3,5,3,2,1,0
  104. Data 1,2,3,5,7,5,3,1,1
  105. Data 1,3,5,7,9,7,5,3,1
  106. Data 1,2,3,5,7,5,3,1,1
  107. Data 0,1,2,3,5,3,2,1,0
  108. Data 0,0,1,2,3,2,1,0,0
  109. Data 0,0,0,1,1,1,0,0,0
  110.  
  111. For x=1 To 9
  112.         For y=1 To 9
  113.         Read blur#(x,y)
  114.         blur#(x,y)=blur#(x,y)/10
  115.         Next
  116. Next
  117.  
  118. For x=0 To 20
  119. For r=0 To 359
  120. sx=(Sin(r)*x)+23
  121. sy=(Cos(r)*x)+23
  122. hill#(sx,sy)=Cos(x*4)*10
  123. Next
  124. Next
  125.  
  126.  
  127.  
  128.  
  129.  
  130. SeedRnd MilliSecs()
  131.  
  132. ; Make Landscape - Drawing edges...
  133. printstuff(1)
  134. Text 320,100,"Drawing Edges...",True,False
  135. Flip
  136. h=120
  137.         For x=0 To size-1
  138.         land#(x+1,1)=h
  139.         h=h+Rnd(0,scale)-(scale/2)
  140.         Next
  141. h=120
  142.         For x=0 To size-1
  143.         land#(1,x+1)=h
  144.         h=h+Rnd(0,scale)-(scale/2)
  145.         Next
  146.  
  147.  
  148. ; make landscape - fill in middle
  149. printstuff(2)
  150. Text 320,120,"Filling Middle...",True,False
  151. Flip
  152. For x=2 To size
  153.         For z=2 To size
  154.         h=land#(x-1,z)
  155.         i=land#(x,z-1)
  156.         j=(h+i)/2
  157.         h=j+Rnd(0,scale)-(scale/2)
  158.         land#(x,z)=h
  159.         Next
  160. Next
  161.  
  162.  
  163. ; make landscape - Create hills
  164. For n=1 To hill_number
  165. printstuff(3)
  166. Text 320,140,"Creating Hills..."+n+"/"+hill_number,True,False
  167. Flip
  168.         hx=Rnd(0,size)
  169.         hy=Rnd(0,size)
  170.         For x=1 To hill_size
  171.                 For xx=1 To 45
  172.                 For yy=1 To 45
  173.                 hxx=hx+xx-1
  174.                 hyy=hy+yy-1
  175.                 If hxx>size Then hxx=hxx-size
  176.                 If hxx<1 Then hxx=hxx+size
  177.                 If hyy>size Then hyy=hyy-size
  178.                 If hyy<1 Then hyy=hyy+size
  179.                 land#(hxx,hyy)=land#(hxx,hyy)+hill#(xx,yy)
  180.                 Next
  181.                 Next
  182.                 hx=hx+Rnd(0,10)-5
  183.                 hy=hy+Rnd(0,10)-5
  184.                 If hx<20 Then hx=size-20
  185.                 If hx>size-20 Then hx=20
  186.                 If hy<20 Then hy=size-20
  187.                 If hy>size-20 Then hy=20
  188.         Next
  189. Next
  190.  
  191.  
  192. ; make landscape - Create Dips
  193. For n=1 To dip_number
  194. printstuff(4)
  195. Text 320,160,"Creating Dips..."+n+"/"+dip_number,True,False
  196. Flip
  197.         hx=Rnd(0,size)
  198.         hy=Rnd(0,size)
  199.         For x=1 To dip_size
  200.                 For xx=1 To 45
  201.                 For yy=1 To 45
  202.                 hxx=hx+xx-1
  203.                 hyy=hy+yy-1
  204.                 If hxx>size Then hxx=hxx-size
  205.                 If hxx<1 Then hxx=hxx+size
  206.                 If hyy>size Then hyy=hyy-size
  207.                 If hyy<1 Then hyy=hyy+size
  208.                 land#(hxx,hyy)=land#(hxx,hyy)-hill#(xx,yy)
  209.                 If land#(hxx,hyy)<-20 Then land#(hxx,hyy)=-20
  210.                 Next
  211.                 Next
  212.                 hx=hx+Rnd(0,10)-5
  213.                 hy=hy+Rnd(0,10)-5
  214.                 If hx<20 Then hx=size-20
  215.                 If hx>size-20 Then hx=20
  216.                 If hy<20 Then hy=size-20
  217.                 If hy>size-20 Then hy=20
  218.         Next
  219. Next
  220.  
  221.  
  222. ; Blur Landscape
  223. printstuff(5)
  224. Text 320,180,"Blurring Landscape pass 1",True,False
  225. Flip
  226. For x=1 To size
  227.         For y=1 To size
  228.                 p=land#(x,y)
  229.                 For bx=-4 To 4
  230.                 For by=-4 To 4
  231.                 xx=x+bx
  232.                 yy=y+by
  233.                 If xx>=1 And xx<=size And yy>=1 And yy<=size Then land2#(xx,yy)=land2#(xx,yy)+(p*blur#(bx+5,by+5))
  234.                 Next
  235.                 Next
  236.         Next
  237. Next
  238. For x=1 To size
  239.         For y=1 To size
  240.         land#(x,y)=land2#(x,y)
  241.         land2#(x,y)=0
  242.         Next
  243. Next
  244.  
  245. ; Normalise Landscape
  246. printstuff(6)
  247. Text 320,200,"Normalising Landscape pass 1",True,False
  248. Flip
  249. high#=-10000
  250. low#=10000
  251. For x=0 To size-1
  252.         For y=0 To size-1
  253.         If land#(x+1,y+1)>high# Then high#=land#(x+1,y+1)
  254.         If land#(x+1,y+1)<low# Then low#=land#(x+1,y+1)
  255.         Next
  256. Next
  257. normal#=high#-low#
  258. For x=0 To size-1
  259.         For y=0 To size-1
  260.         l#=land#(x+1,y+1)
  261.         l#=l#-low#
  262.         l#=(l#/normal#)*255
  263.         land#(x+1,y+1)=Int(l#)
  264.         Next
  265. Next
  266.  
  267. ; Blur Landscape
  268. printstuff(7)
  269. Text 320,220,"Blurring Landscape pass 2",True,False
  270. Flip
  271. For x=1 To size
  272.         For y=1 To size
  273.                 p=land#(x,y)
  274.                 For bx=-4 To 4
  275.                 For by=-4 To 4
  276.                 xx=x+bx
  277.                 yy=y+by
  278.                 If xx>=1 And xx<=size And yy>=1 And yy<=size Then land2#(xx,yy)=land2#(xx,yy)+(p*blur#(bx+5,by+5))
  279.                 Next
  280.                 Next
  281.         Next
  282. Next
  283. For x=1 To size
  284.         For y=1 To size
  285.         land#(x,y)=land2#(x,y)
  286.         land2#(x,y)=0
  287.         Next
  288. Next
  289.  
  290. ; Normalise Landscape
  291. printstuff(8)
  292. Text 320,240,"Normalising Landscape pass 2",True,False
  293. Flip
  294. high#=-10000
  295. low#=10000
  296. For x=0 To size-1
  297.         For y=0 To size-1
  298.         If land#(x+1,y+1)>high# Then high#=land#(x+1,y+1)
  299.         If land#(x+1,y+1)<low# Then low#=land#(x+1,y+1)
  300.         Next
  301. Next
  302. normal#=high#-low#
  303. For x=0 To size-1
  304.         For y=0 To size-1
  305.         l#=land#(x+1,y+1)
  306.         l#=l#-low#
  307.         l#=(l#/normal#)*255
  308.         land#(x+1,y+1)=Int(l#)
  309.         Next
  310. Next
  311.  
  312. ; Render Heightmap and capture image
  313. printstuff(9)
  314. Text 320,260,"Rendering Height Map",True,False
  315. Flip
  316.  
  317. height=CreateImage (size,size)
  318. SetBuffer ImageBuffer(height)
  319. For x=0 To size-1
  320.         For y=0 To size-1
  321.         dot(x,y,land#(x+1,y+1))
  322.         Next
  323. Next
  324.  
  325. SetBuffer BackBuffer()
  326.  
  327. SaveImage height,"heightmap.bmp"
  328.  
  329.  
  330. ; Show 3D view of Landscape
  331. Cls
  332. Graphics3D 640,480,16                   ;Set graphics mode
  333. SetBuffer BackBuffer()
  334.  
  335. terrain = LoadTerrain ("heightmap.bmp")
  336. ScaleEntity terrain, 5, 100, 5
  337. TerrainShading terrain, True
  338. TerrainDetail terrain, 2500
  339. grass=LoadTexture( "grass.bmp" )
  340. ScaleTexture grass,20,20
  341. EntityTexture terrain,grass
  342.  
  343. camera = CreateCamera()
  344. CameraFogRange camera,700,1000
  345. CameraFogMode camera,1
  346. AmbientLight 255,255,255
  347. middle= CreatePivot()
  348. PositionEntity middle,640,0,640
  349. rot#=0.0
  350. While Not KeyHit(1)
  351.  
  352. cx#=640+(Sin(rot#)*400)
  353. cz#=640+(Cos(rot#)*400)
  354. cy#=TerrainY#(terrain,cx#,0.0,cz#)+5
  355. PositionEntity camera,cx#,cy#,cz#
  356.  
  357.  
  358. lx#=640+(Sin(rot#)*400)+(Cos(rot#+90)*20)
  359. lz#=640+(Cos(rot#)*400)+(Sin(rot#+90)*20)
  360.  
  361. ly#=TerrainY#(terrain,lx#,0.0,lz#)+5
  362. PositionEntity middle,lx#,ly#,lz#
  363.  
  364. PointEntity camera,middle
  365. rot#=rot#+.1
  366. If rot#=360 Then rot#=0
  367. UpdateWorld
  368. RenderWorld
  369. Flip
  370. Wend
  371.  
  372.  
  373. End
  374.  
  375. Function dot(x,y,c)
  376. If c>255 Then c=255
  377. If c<0 Then c=0
  378. Color c,c,c
  379. Plot x,y
  380. End Function
  381.  
  382. Function printstuff(prtstf)
  383. Cls
  384. Color 255,255,255
  385. Text 320,0,"Height Map Generator",True,False
  386. Color 255,255,0
  387. Text 320,20,"Rob Farley 2001",True,False
  388. Color 0,128,128
  389. If prtstf<>1 Then Text 320,100,"Drawing Edges...",True,False
  390. If prtstf<>2 Then Text 320,120,"Filling Middle...",True,False
  391. If prtstf<>3 Then Text 320,140,"Creating Hills...",True,False
  392. If prtstf<>4 Then Text 320,160,"Creating Dips...",True,False
  393. If prtstf<>5 Then Text 320,180,"Blurring Landscape pass 1",True,False
  394. If prtstf<>6 Then Text 320,200,"Normalising Landscape pass 1",True,False
  395. If prtstf<>7 Then Text 320,220,"Blurring Landscape pass 2",True,False
  396. If prtstf<>8 Then Text 320,240,"Normalising Landscape pass 2",True,False
  397. If prtstf<>9 Then Text 320,260,"Rendering Height Map",True,False
  398. Color 0,255,255
  399. End Function


Comments :


RiverRatt(Posted 1+ years ago)

 Cool thanks Rob


puki(Posted 1+ years ago)

 Hey "Rob Farley" is da man.  "puki" like him.


CyberHeater(Posted 1+ years ago)

 That's really nice.  Cheers.


Rob Farley(Posted 1+ years ago)

 Oh my god... Don't use this crap! I wrote this 3 years ago and it's total rubbish! I'm sure Sswifts got a perlin noise function in the code arcs somewhere. Even my website is out of date!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal