I've translated ATIs NormalMapGen to FreeBASIC, but there are some problems:
My test-heightmap
Result of my Program
Result of ATIs Program
http://28398labs.28.funpic.de/stuff/DOT3.bas
sourcecode
http://28398labs.28.funpic.de/stuff/TGAtoDOT3.cpp
ATIs sourcecode
And, the main loop (altough included in the sourcecode, but i post it here :D )
Code: Select all
For y = 0 To gHeight
For x = 0 To gWidth
' Do Y Sobel filter
pix = srcImage((x-1+gWidth) Mod gWidth, (y+1) Mod gHeight)
dY = Cast(Double, pix.red / 255.0 * -1.0)
pix = srcImage(x Mod gWidth, (y+1) Mod gHeight)
dY += Cast(Double, pix.red / 255.0 * -2.0)
pix = srcImage((x+1) Mod gWidth, (y+1) Mod gHeight)
dY += Cast(Double, pix.red / 255.0 * -1.0)
pix = srcImage((x-1+gWidth) Mod gWidth, (y-1+gHeight) Mod gHeight)
dY += Cast(Double, pix.red / 255.0 * 1.0)
pix = srcImage(x Mod gWidth, (y-1+gHeight) Mod gHeight)
dY += Cast(Double, pix.red / 255.0 * 2.0)
pix = srcImage((x+1) Mod gWidth, (y-1+gHeight) Mod gHeight)
dY += Cast(Double, pix.red / 255.0 * 1.0)
' Do X Sobel filter
pix = srcImage((x-1+gWidth) Mod gWidth, (y-1+gHeight) Mod gHeight)
dX = Cast(Double, pix.red / 255.0 * -1.0)
pix = srcImage((x-1+gWidth) Mod gWidth, y Mod gHeight)
dX += Cast(Double, pix.red / 255.0 * -2.0)
pix = srcImage((x-1+gWidth) Mod gWidth, (y+1) Mod gHeight)
dX += Cast(Double, pix.red / 255.0 * -1.0)
pix = srcImage((x+1) Mod gWidth, (y-1+gHeight) Mod gHeight)
dX += Cast(Double, pix.red / 255.0 * 1.0)
pix = srcImage((x+1) Mod gWidth, y Mod gHeight)
dX += Cast(Double, pix.red / 255.0 * 2.0)
pix = srcImage((x+1) Mod gWidth, (y+1) Mod gHeight)
dX += Cast(Double, pix.red / 255.0 * 1.0)
' Cross Product of components of gradient reduces to
nX = -dX
nY = -dY
nZ = 1
' Normalize
oolen = 1.0/Cast(Double, Sqr(nX^2 + nY^2 + nZ^2))
nX *= oolen ' nX *= oolen; ??
nY *= oolen ' nY *= oolen; ??
nZ *= oolen ' nZ *= oolen; ??
pix.red = PackFloatInByte(nX)
pix.green = PackFloatInByte(nY)
pix.blue = PackFloatInByte(nZ)
dstImage(x, y) = pix
Next x
Next y
Can anyone help me?
(by the way: Yes i changed the stupid pointer and so on to an array, but both translations give wrong results... :( )