January 15, 2021, 05:40:03 PM

Author Topic: [bb] Batch Height to Normal Converter by bingman [ 1+ years ago ]  (Read 444 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Batch Height to Normal Converter
Author : bingman
Posted : 1+ years ago

Description : This will batch process GreyScale BMPs to Normal Maps.

PLEASE READ THE DIRECTIONS COMMENTED IN THE CODE CAREFULLY!


Code :
Code: BlitzBasic
  1. AppTitle "BATCH HEIGHTMAP TO NORMAL MAP CODE"
  2.  
  3. Graphics 800,600,32,0
  4.  
  5.  
  6. ;IMPORTANT!
  7. ;1. Files should be 8 bit greyscale .bmps
  8. ;1. The images you want To convert must be in a folder called 'BMP' which is subfolder of where you run the program.
  9. ;2. A Second folder named 'Normals' must be in the 'Bmp' folder.
  10.  
  11. Global Dir$ = CurrentDir$()+"Bmp"
  12. Print Dir$
  13. Global DirSave$ = Dir$ + "Normals"
  14. MyDir = ReadDir(Dir$)
  15. Repeat
  16. Map$ = NextFile$(MyDir)
  17. If Map$ = "" Then Exit
  18. If FileType(Dir$+""+Map$) = 2 Then Goto NoFile
  19. Gosub MAKEMAP
  20. .NoFile
  21. Forever
  22. CloseDir MyDir
  23. End
  24.  
  25. .MAKEMAP
  26. Global MapLength = (Len(Map$) -4)
  27. Global Map2$ = LSet$(Map$,MapLength)
  28. Global heightmap = LoadImage(Map$)
  29. Global img_w = ImageWidth(heightmap)-1
  30. Global  img_h = ImageHeight(heightmap)-1
  31. Global lightmap = CreateImage(img_w+1,img_h+1)
  32.  
  33. LockBuffer ImageBuffer(heightmap)
  34. LockBuffer ImageBuffer(lightmap)
  35.  
  36. For y = 0 To img_h
  37.   If y>0 : ym1=y-1 : Else : ym1=0 : EndIf
  38.   If y<img_h : yp1=y+1 : Else : yp1=img_h : EndIf
  39.  
  40.   For x = 0 To img_w
  41.     If x>0 : xm1=x-1 : Else : xm1=0 : EndIf
  42.     If x<img_w : xp1=x+1 : Else : xp1=img_w : EndIf  
  43.    
  44.     ; get the pixel value for height info - up, down,
  45.     ;  left and right of current pixel
  46.     ;  And $0000FF restricts to blue channel
  47.     y_u = ReadPixelFast(x,yp1,ImageBuffer(heightmap)) And $0000FF
  48.     y_d = ReadPixelFast(x,ym1,ImageBuffer(heightmap)) And $0000FF
  49.     x_r = ReadPixelFast(xp1,y,ImageBuffer(heightmap)) And $0000FF
  50.     x_l = ReadPixelFast(xm1,y,ImageBuffer(heightmap)) And $0000FF
  51.  
  52.     ;calculate x and y gradient exactly the same as bumpmapping
  53.     nx = (x_l - x_r)
  54.     ny = (y_u - y_d)
  55.    
  56.     ; nx and ny can potentially fall into the range of -255 to 255,
  57.     ;  so we offest it To get rid of the negative value,
  58.     ;   Then half it To keep the value in the range of 255
  59.     red = (nx + 255)/2
  60.     green = (ny + 255)/2
  61.     blue = 255
  62.    
  63.     ; red is horizontal (x axis)
  64.     ; red = 0 normal points left, red = 255 normal points right
  65.     ;  red = 128 normal points out of screen at you
  66.  
  67.     ;green is vertical (y axis)
  68.     ; green = 0 normal points down, green = 255 normal points up
  69.     ;  green = 128 normal points out of screen at you
  70.    
  71.    
  72.     ; shift the colors into the right place
  73.     cr = (red Shl 16) + (green Shl 8) + (blue)
  74.     WritePixelFast(x,y,cr,ImageBuffer(lightmap))
  75.   Next
  76. Next
  77. UnlockBuffer ImageBuffer(lightmap)
  78. UnlockBuffer ImageBuffer(heightmap)
  79. ChangeDir(DirSave$)
  80. SaveImage lightmap,Map2$ + "_N.bmp"
  81. ChangeDir(Dir$)
  82. FreeImage heightmap
  83. FreeImage lightmap
  84. Return


Comments :


markcw(Posted 1+ years ago)

 This is cool.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal