MD3 normals (bit calculation stuff)

BlitzMax Forums/BlitzMax Programming/MD3 normals (bit calculation stuff)

JoshK(Posted 2007) [#1]
Please see the section on Normals:
http://www.linux.ucla.edu/~phaethon/q3/formats/md3format.html

Decoding:

(Code in q3tools/q3map/misc_model.c:InsertMD3Model)
lat <- ((normal shift-right 8) binary-and 255) * (2 * 360.0 ) / 255
lng <- (normal binary-and 255) * (2 * 360.0) / 255
x <- cos ( lat ) * sin ( lng )
y <- sin ( lat ) * sin ( lng )
z <- cos ( lng )


Here is my code:
      normal=ReadShort(f)
	lat# =  (Float(( normal Shr 8 ) & 255 ) * (2.0 * Pi ) / 255.0)
	lng# = (Float( normal & 255 ) * (2.0 * Pi) / 255.0)
	nx# = Cos ( lat ) * Sin ( lng )
	ny# = Sin ( lat ) * Sin ( lng )
	nz# = Cos ( lng )


Am I doing this right?


Floyd(Posted 2007) [#2]
Looks reasonable except that you should change 2.0 * Pi (radians) to 360.0 (degrees).


JoshK(Posted 2007) [#3]
Here's the data from a cube, positions first then normals. It doesn't look right:
-99.9843750, -99.9843750, 100.000000
0.0246207807, -0.000910395698, 0.999696434

100.000000, -99.9843750, 100.000000
0.0246207807, -0.000910395698, 0.999696434

-99.9843750, 100.000000, 100.000000
0.0246207807, -0.000910395698, 0.999696434

100.000000, 100.000000, 100.000000
0.0246207807, -0.000910395698, 0.999696434

-99.9843750, -99.9843750, -99.9843750
0.0246207807, -0.000910395698, 0.999696434

100.000000, -99.9843750, -99.9843750
0.0246207807, -0.000910395698, 0.999696434

-99.9843750, 100.000000, -99.9843750
0.0246207807, -0.000910395698, 0.999696434

100.000000, 100.000000, -99.9843750
0.0246207807, -0.000910395698, 0.999696434


I guess gmax doesn't export normals.


Floyd(Posted 2007) [#4]
On second thought I don't trust the code at http://www.linux.ucla.edu/~phaethon/q3/formats/md3format.html

In that code both angles have a range of 2*Pi, or 360 degrees. But latitude should have a range of only 180 degrees.

I would also have expected to see division by 256 rather than 255. There are 256 possible 8-bit values for an angle. With division by 255 the angle 0 becomes 0 degrees and 255 becomes 360 degrees, the same as 0.


JoshK(Posted 2007) [#5]
I found a fixed md3 exporter that claims to work.

I think got it working using this code:

lng# = Float( f.ReadByte() )/256.0*360.0
lat# = Float( f.ReadByte() )/256.0*360.0
x = Cos ( lat ) * Sin ( lng )
y = Sin ( lat ) * Sin ( lng )
z = Cos ( lng )
Print x+", "+y+", "+z