Hi.
Im trying to create normal maps from height maps myself. I have one height map for wich i created a normal map in a third party tool (a nvidia plugin for photoshop). That normal map looks like this.
when i use this normal map for rendering the result is this.
all good.
now I try to generate the normal map myself. The best i get is this
and the rendering result is this...
I try to generate the normal map by calculating and avaraging the 6 surrounding triangles normals for every vertex. like this.
[source]
private void testCalcNormals() {
StaticTile[][] tiles = world.staticTerrain.tiles;
StaticTile tile = tiles[10][5];//one StaticTile contains a 1024x1024 heightmap and normal map among other things.
for (int x = 0; x < 1024; x++)
{
for (int y = 0; y < 1024; y++)
{
StaticTile[][] tiles = world.staticTerrain.tiles;
StaticTile tile = tiles[10][5];//one StaticTile contains a 1024x1024 heightmap and normal map among other things.
for (int x = 0; x < 1024; x++)
{
for (int y = 0; y < 1024; y++)
{
float a1 = tile.getHeight(x-1, y-1);
float a2 = tile.getHeight(x, y-1);
float a3 = tile.getHeight(x+1, y-1);
float b1 = tile.getHeight(x-1, y);
float b2 = tile.getHeight(x, y);
float b3 = tile.getHeight(x+1, y);
float c1 = tile.getHeight(x-1, y+1);
float c2 = tile.getHeight(x, y+1);
float c3 = tile.getHeight(x+1, y+1);
float w = 100f;
Vector3 A1 = v(-w,a1,-w);
Vector3 A2 = v(0,a2,-w);
Vector3 A3 = v(w,a3,-w);
Vector3 B1 = v(-w,b1,0);
Vector3 B2 = v(0,b2,0);
Vector3 B3 = v(w,b3,0);
Vector3 C1 = v(-w,c1,w);
Vector3 C2 = v(0,c2,w);
Vector3 C3 = v(w,c3,w);
Vector3 t1 = surfaceNormal(B2, A2, B3);
Vector3 t2 = surfaceNormal(B2, B3, C3);
Vector3 t3 = surfaceNormal(B2, C3, C2);
Vector3 t4 = surfaceNormal(B2, C2, B1);
Vector3 t5 = surfaceNormal(B2, B1, A1);
Vector3 t6 = surfaceNormal(B2, A1, A2);
Vector3 normal = t1.add(t2).add(t3).add(t4).add(t5).add(t6).divide(6);
normal.normalize();
tile.setNormal(x, y, normal);
}
}
}
private static Vector3 surfaceNormal(Vector3 c1, Vector3 c2, Vector3 c3)
{
Vector3 edge1 = new Vector3(c2.x - c1.x, c2.y - c1.y, c2.z - c1.z);
Vector3 edge2 = new Vector3(c3.x - c1.x, c3.y - c1.y, c3.z - c1.z);
Vector3 normal = edge1.cross(edge2);
normal.normalize();
return normal;
}[/source]
tile.setNormal(x, y, normal);
}
}
}
private static Vector3 surfaceNormal(Vector3 c1, Vector3 c2, Vector3 c3)
{
Vector3 edge1 = new Vector3(c2.x - c1.x, c2.y - c1.y, c2.z - c1.z);
Vector3 edge2 = new Vector3(c3.x - c1.x, c3.y - c1.y, c3.z - c1.z);
Vector3 normal = edge1.cross(edge2);
normal.normalize();
return normal;
}[/source]
What am I doing wrong here?
Thnx in Advance!