Another DOT 3 Bump map

Blitz3D Forums/Blitz3D Programming/Another DOT 3 Bump map

Extron(Posted 2003) [#1]
This is another methode to the bump mapping.

Bumpdemo

Here is a code :
;
; CREATED BY EXTRON
; FREE TO USE
;
Graphics3D 800,600,32,2
SetBuffer BackBuffer()

; CREATE CAMERA
Camera=CreateCamera()
CameraRange camera,0.1,10000000
PositionEntity camera,0,0,-3

; CREATE EARTH AND LOAD TEXTURE
tex=LoadTexture("earth11.bmp",1+256+512)
sphere=CreateSphere(20)
PositionEntity sphere,0,0,0
EntityTexture sphere,tex

; CREATE A FAKE LIGHT WHITH PIVOT
pivot=CreatePivot()
PositionEntity pivot,0,0,0
fakelight=CreateSphere(2,pivot)
PositionEntity fakelight,0,0,-1.5
ScaleEntity fakelight,0.025,0.025,0.025
AmbientLight 255,255,255


; HERE WE CREATE AN ARRAY FOR THE NORMALS
; REAL NORMAL MAP IS IN THE FILE "earth11bump.tga"
; CALCULATE THE NORMALIZED VECTOR FOR EACH POSITION
Dim vecteurx#(511,255) : Dim vecteury#(511,255) : Dim vecteurz#(511,255)
img=LoadImage("earth11bump.tga")
For y=0 To 255
	For x=0 To 511
		a=ReadPixel(x,y,ImageBuffer(img))
		vecteurx#(x,y)=(a And $ff0000) Sar 16
		vecteury#(x,y)=(a And $ff00) Sar 8
		vecteurz#(x,y)=a And $ff
		TFormNormal vecteurx#(x,y), vecteury#(x,y), vecteurz#(x,y), 0, 0
		vecteurx#(x,y)=TFormedX() : vecteury#(x,y)=TFormedY() : vecteurz#(x,y)=TFormedZ()
	Next
Next
; DON'T NEED ANY MORE (HOP! POUBELLE.)
FreeImage img

; ANOTHER ARRAY FOR THE TEXTURE, THIS SAVE THE DATA, IT'S NECESSARY. 
Dim texsave(512,256)
For y=0 To 255
	For x=0 To 511
		texsave(x,y)=ReadPixel(x,y,TextureBuffer(tex))
	Next
Next
;
;**************************************************************************************
;
; START RENDER
While Not KeyDown(1)

; ROTATE EARTH AND FAKE LIGHT
TurnEntity pivot,0,-1,0
TurnEntity sphere,0,0.1,0

; CALCULATE THE NORMAL BETWEEN THE CENTER OF THE EARTH AND THE CENTER OF THE FAKE LIGHT
vectorx#=EntityX(sphere)+EntityX(fakelight,1)
vectory#=EntityY(sphere)+EntityY(fakelight,1)
vectorz#=EntityZ(sphere)-EntityZ(fakelight,1)
TFormNormal vectorx#, vectory#, vectorz#, 0, 0
vecx#=TFormedX() : vecy#=TFormedY() : vecz#=TFormedZ()

; HERE WE UPDATE THE TEXTURE
SetBuffer TextureBuffer(tex) 
LockBuffer TextureBuffer(tex)
For y=0 To 255
	For x=0 To 511
		; DOT PRODUCT X,Y AND Z BETWEEN THE VECTOR OF THE FAKE LIGHT AND THE PIXEL VECTOR OF THE BUMP IMAGE
		DP3# = (vecteurx#(x,y) * vecx#) + (vecteury#(x,y) * vecy#) + (vecteurz#(x,y) * vecz#)  
		; UPDATE RED, GREEN AND BLUE OF THE TEXTURE WHITH THE COEFFICIENT
		a=texsave(x,y)
		r=(a And $ff0000) Sar 16
		v=(a And $ff00) Sar 8
		b=a And $ff
		r=Int((Float(r)*DP3#)) : v=Int((Float(v)*DP3#)) : b=Int((Float(b)*DP3#))
		; BEWARE THIS CAN BE LESS THAN 0
		If r<0 Then r=0
		If v<0 Then v=0
		If b<0 Then b=0
		a=$ff000000+(r*256*256)+(v*256)+b
		WritePixelFast x,y,a
	Next
Next
UnlockBuffer TextureBuffer(tex)
SetBuffer BackBuffer()

; RENDER THE WORLD AND ENJOY
RenderWorld

Flip 0

Wend

End


Just for the fun.


Bot Builder(Posted 2003) [#2]
um, your linking to "tiledmesh.zip" it apparently has nothing to do with bumpmapping.


Extron(Posted 2003) [#3]
Oups! Sorry! :)
Corrected.


CyBeRGoth(Posted 2003) [#4]
Cool

But you are implying that the sun goes round the earth?!
Thats not true!! The earth goes around the sun :P

Im just kidding (tho not about the sun :)


Extron(Posted 2003) [#5]
LOL!
Poor Gallilé. :)))
It's another point of view, are you sure! :))