Page 1 of 1

Creating vertex normals from TN map

PostPosted: Fri May 13, 2011 4:44 pm
by miko
Hiya there,
I'm currently trying to retrieve a pixel from the terrains TN map (normal map) and create vertex normals from its value for my exporter.
Like so:
Code: Select all
ZMAP nmMap = zproj_GetMap("TN");
float fPixel;
zmap_GetPixel(nmMap, i, j, &fPixel);
// Now, I would somehow need to convert fPixel value to a rgb value (preferably of ColourPixel type),
// so I could calculate my own normal vector from that.

Not really sure how to convert the fPixel variable.
Or - as it is 32bit (I reckon), just pass in an UINT and trim to 24bit (color value)?

You see, I'm a bit confused :mrgreen: Any help appreciated...

Re: Creating vertex normals from TN map

PostPosted: Sun May 15, 2011 4:13 am
by Aaron
Hi Miko,

The pixel type for a TN map is declared in the header file thusly:

Code: Select all
typedef struct { signed char x, y, z; } vector3c;


Each vector component ranges from -128 to 127. To convert to RGB, simply add 128 to each value, i.e:

Code: Select all
vector3c v; // your terrain normal vector
zmap_GetPixel(nmMap, i, j, &v);

ColourPixel col; // your colour pixel
col.r = v.x + 128;
col.g = v.y + 128;
col.b = v.z + 128;

zmap_SetPixel(colMap, i, j, &col);


Please note however that since the TN map is usually bump-mapped, the value at a particular pixel may not be very representative of the underlying geometry. This is why the mesh browser window used by the mesh decimator generates its own vertex normals from the mesh itself. If you like, I can fairly easily modify the mesh decimator to generate the vertex normals so that you can access them too.

Cheers,
Aaron.

Re: Creating vertex normals from TN map

PostPosted: Sun May 15, 2011 11:39 am
by miko
Aha! Think I understand the concept now. It is all written in Zeolite_defines.h: typedef vector3c TerrainNormalPixel;
Got a bit mixed up with that void* that needs to be passed into zmap_GetPixel :) Thanks for the enlightenment, Aaron.

Actually, I have an own routine in place that would calculate vertex normals from face normals. For border tiles at mosaic export, I noticed it created some slight edges though (not considering faces of adjacent mosaic tiles). So, I thought of another "nifty" way to retrieve normal values. Did not consider the bump-mapped issue you pointed to, though.

Having the mesh decimator return vertex normals, too, would certainly be useful (along with the possibility to call it "ui-less", as discussed on the other thread :mrgreen: ).

Again, thanks.