Ooops
October 17, 2021, 10:39:19

Author Topic: [bb] Normal Map by TetraHC [ 1+ years ago ]  (Read 552 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Normal Map by TetraHC [ 1+ years ago ]
« on: June 29, 2017, 00:28:38 »
Title : Normal Map
Author : TetraHC
Posted : 1+ years ago

Description : I hope someone finds it usefull...

It loads an image called "heightmap.bmp". This is an image containing height information. Exactly the same as you would use for bumpmapping.
This should be a grayscale image. If you use a color image, then only the blue channel is used.
It's usefull for generating quick simple normal maps from just about any image. There are probably many progs out there that do the same but better. But if your like me and just getting into Dot3 bump mapping, then this little prog can help make you understand the normal map.

(I hope to expand it so that you can load models instead of height maps and use this to generate a high quality and more accurate normal map.)

Please leave feedback if you find it usefull or suggest improvments. Thanx


Code :
Code: BlitzBasic
  1. AppTitle "NormalMap"
  2.  
  3. Graphics 800,600,32,2
  4.  
  5. ; this is a grayscale image. Each pixel is height information.
  6. ; White being the heighest and black being the lowest.
  7. ; you can use a color image but only the blue channel is used
  8. heightmap = LoadImage("heightmap.bmp")
  9.  
  10. Global img_w = ImageWidth(heightmap)-1
  11. Global  img_h = ImageHeight(heightmap)-1
  12. lightmap = CreateImage(img_w+1,img_h+1)
  13.  
  14.  
  15. LockBuffer ImageBuffer(heightmap)
  16. LockBuffer ImageBuffer(lightmap)
  17.  
  18. For y = 0 To img_h
  19.   If y>0 : ym1=y-1 : Else : ym1=0 : EndIf
  20.   If y<img_h : yp1=y+1 : Else : yp1=img_h : EndIf
  21.  
  22.   For x = 0 To img_w
  23.     If x>0 : xm1=x-1 : Else : xm1=0 : EndIf
  24.     If x<img_w : xp1=x+1 : Else : xp1=img_w : EndIf  
  25.    
  26.     ; get the pixel value for height info - up, down,
  27.     ;  left and right of current pixel
  28.     ;  And $0000FF restricts to blue channel
  29.     y_u = ReadPixelFast(x,yp1,ImageBuffer(heightmap)) And $0000FF
  30.     y_d = ReadPixelFast(x,ym1,ImageBuffer(heightmap)) And $0000FF
  31.     x_r = ReadPixelFast(xp1,y,ImageBuffer(heightmap)) And $0000FF
  32.     x_l = ReadPixelFast(xm1,y,ImageBuffer(heightmap)) And $0000FF
  33.  
  34.     ;calculate x and y gradient exactly the same as bumpmapping
  35.     nx = (x_l - x_r)
  36.     ny = (y_u - y_d)
  37.    
  38.     ; nx and ny can potentially fall into the range of -255 to 255,
  39.     ;  so we offest it To get rid of the negative value,
  40.     ;   Then half it To keep the value in the range of 255
  41.     red = (nx + 255)/2
  42.     green = (ny + 255)/2
  43.     blue = 255
  44.    
  45.     ; red is horizontal (x axis)
  46.     ; red = 0 normal points left, red = 255 normal points right
  47.     ;  red = 128 normal points out of screen at you
  48.  
  49.     ;green is vertical (y axis)
  50.     ; green = 0 normal points down, green = 255 normal points up
  51.     ;  green = 128 normal points out of screen at you
  52.    
  53.    
  54.     ; shift the colors into the right place
  55.     cr = (red Shl 16) + (green Shl 8) + (blue)
  56.     WritePixelFast(x,y,cr,ImageBuffer(lightmap))
  57.   Next
  58. Next
  59. UnlockBuffer ImageBuffer(lightmap)
  60. UnlockBuffer ImageBuffer(heightmap)
  61.  
  62. While Not KeyDown(1)
  63.   DrawImage heightmap,0,0
  64.   DrawImage lightmap,0,ImageHeight(heightmap)
  65.   Flip
  66. Wend
  67.  
  68. SaveImage lightmap,"normal_map.bmp"
  69. FreeImage heightmap
  70. FreeImage lightmap
  71. End


Comments :


Norberth(Posted 7 months ago)

 Thanks, it's espectacular code!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal