January 26, 2021, 12:13:00 PM

Author Topic: [bb] Apply bump/normal map by * [ 1+ years ago ]  (Read 648 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Apply bump/normal map by * [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Apply bump/normal map
Author : *
Posted : 1+ years ago

Description : Apply bump/normal map to any surface

Usage: if you have a texture somewhere on the mesh called MyWallTexture.png you would use : ApplyBumpMap( Mesh, "MyWallTexture.png", MyBumpMap )

Frame is incase you have an animated bump/normal map
Lightmapped is to turn on/off lightmapping of that particular texture.


Code :
Code: BlitzBasic
  1. Function ApplyBumpMap( Mesh, SearchTexture$, BumpMap, Frame=0, LightMapped=1 )
  2.         ;this function changes the texture on a mesh so you can apply a bump/normal map
  3.         Local SurfS=CountSurfaces( Mesh )
  4.         Local SurfI=0
  5.         Local BlankBrush = CreateTexture( 64, 64 )
  6.         Local t0, t1, t2, t3
  7.        
  8.         SetBuffer TextureBuffer( BlankBrush )
  9.         ClsColor 255, 255, 255
  10.         Cls
  11.         ClsColor 0, 0, 0
  12.         SetBuffer BackBuffer()
  13.        
  14.         For SurfI=1 To Surfs
  15.                 s=GetSurface( Mesh, SurfI )
  16.                 b=GetSurfaceBrush(s)
  17.                 t0=GetBrushTexture( b, 0 )
  18.                 t1=GetBrushTexture( b, 1 )
  19.                 t2=GetBrushTexture( b, 2 )
  20.                
  21.                 If Upper$( StripPath$( TextureName$( t0 ) ) ) = Upper$( SearchTexture$ )
  22.                         BrushTexture b, BumpMap, Frame, 0
  23.                         BrushTexture b, t0, Frame, 2
  24.                        
  25.                         ;lightmap is always on channel (index) 1
  26.                         If LightMapped=0
  27.                                 BrushTexture b, BlankBrush, 0, 1
  28.                         Else
  29.                                 BrushTexture b, t1, 0, 1
  30.                         EndIf
  31.                        
  32.                         PaintSurface s, b
  33.                 EndIf
  34.                
  35.                 If t0<>0
  36.                         FreeTexture t0
  37.                 EndIf
  38.         Next
  39.        
  40.         FreeTexture BlankBrush
  41. End Function
  42.  
  43. Function StripPath$(file$)
  44.         ;borrowed from Blitz Help file
  45.         If Len(file$)>0
  46.                 For i=Len(file$) To 1 Step -1
  47.                         mi$=Mid$(file$,i,1)
  48.                         If mi$="" Or mi$="/" Then Return name$ Else name$=mi$+name$
  49.                 Next
  50.         EndIf
  51.  
  52.         Return name$
  53. End Function


Comments :


David819(Posted 1+ years ago)

 Could you post an example of this in use please.


skyfire1(Posted 1+ years ago)

 i really want to use it but i have no idea how it works.


chwaga(Posted 1+ years ago)

 yeah, i tried it and it didn't work (Can't visibly see any bump-mapping going on), here's the code:
Code: [Select]
Graphics3D 1280,1024,32,2
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,0,-3
;RotateEntity camera,10,0,0
light=CreateLight()
PositionEntity light,0,10,0

sphere=CreateCube()
PositionEntity sphere,0,0,0
RotateEntity sphere,90,0,0

tex=LoadTexture ("rock.jpg")
EntityTexture sphere,tex

bump=LoadTexture ("rock_bump.jpg")
applybumpmap( sphere,"rock.jpg",bump)

While Not KeyDown(1)

TurnEntity sphere,.01,.02,.03

If KeyDown(200) Then TranslateEntity light,0,1,0
If KeyDown(208) Then TranslateEntity light,0,-1,0

RenderWorld
Text 0,0,"light y = "+EntityY(light)
Flip
Wend
End


Function ApplyBumpMap( Mesh, SearchTexture$, BumpMap, Frame=0, LightMapped=1 )
;this function changes the texture on a mesh so you can apply a bump/normal map
Local SurfS=CountSurfaces( Mesh )
Local SurfI=0
Local BlankBrush = CreateTexture( 64, 64 )
Local t0, t1, t2, t3

SetBuffer TextureBuffer( BlankBrush )
ClsColor 255, 255, 255
Cls
ClsColor 0, 0, 0
SetBuffer BackBuffer()

For SurfI=1 To Surfs
s=GetSurface( Mesh, SurfI )
b=GetSurfaceBrush(s)
t0=GetBrushTexture( b, 0 )
t1=GetBrushTexture( b, 1 )
t2=GetBrushTexture( b, 2 )

If Upper$( StripPath$( TextureName$( t0 ) ) ) = Upper$( SearchTexture$ )
BrushTexture b, BumpMap, Frame, 0
BrushTexture b, t0, Frame, 2

;lightmap is always on channel (index) 1
If LightMapped=0
BrushTexture b, BlankBrush, 0, 1
Else
BrushTexture b, t1, 0, 1
EndIf

PaintSurface s, b
EndIf

If t0<>0
FreeTexture t0
EndIf
Next

FreeTexture BlankBrush
End Function

Function StripPath$(file$)
;borrowed from Blitz Help file
If Len(file$)>0
For i=Len(file$) To 1 Step -1
mi$=Mid$(file$,i,1)
If mi$="" Or mi$="/" Then Return name$ Else name$=mi$+name$
Next
EndIf

Return name$
End Function


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal