For the terrain lighting I set it up in 3d studio max with the color-per-vertex tool. The tool computes vertex color values based on all the lights in the scene, and even takes into account shadows and radiosity. So I exported my terrain as usual but put vertex colors instead of the normals into my file. I modified my shader to use the colors instead of normals and loaded up the game. It looked terrible! the colors were clearly sitting on the wrong verticies. I checked and rechecked and I was getting the vertex colors with the exact same indexes that I had been getting the normals with. So why were the normals correct and the vertex colors wrong?
Answer: 3d studio uses a whole different vertex channel/buffer for color verticies, just as it does for texture verticies. There is no 1-to-1 ratio of Position&Normal vertecies with color verticies. The solution was to compromise and only take the vertex colors that lie on a texture vertex (being there are more texture verticies than position verticies). So when exporting a mesh,
for each texture vertex in the mesh
{
for each polygon that referances that vertex
{
- get the same polygon from the position buffer and the same polygon index (1,2,or 3) that referanced the texture vertex I am on, and save this position value
- get the same polygon from the color-per-vertex buffer and the same polygon index (1,2,or 3) that referanced the texture vertex I am on, and save this color value
- for however many position values were gathered, I checked to make sure they are all the same value. If not then there are problems problems and the easiest choice will be to break up the mesh polygon by polygon( the correct choice though is to explode all buffers then un-explode them while combining vertex that are the same in all 3 buffers) (so far I have never run into this, I always have more or the same texture vertex than I do position vertex)
- for however many color-per-vertex values were gathered, average them (some values might be lost, but if anything it will look more blended. If I am sharing a texture coordinate between two polies then I might as well share the color.)
- output the texture vertex, 1 of the position vertex (that should all be the same), and the averaged color vertex
then to output the index buffer I just use the texture-polygon index buffer which will now map to each of the 3 buffers.