Code archives/3D Graphics - Effects/Apply bump/normal map

This code has been declared by its author to be Public Domain code.

Download source code

Apply bump/normal map by *2004
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.
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

Comments

David8192005
Could you post an example of this in use please.


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


chwaga2007
yeah, i tried it and it didn't work (Can't visibly see any bump-mapping going on), here's the code:
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



Code Archives Forum