Hey all,
Im trying to get a normal map from a heightmap through this calculation:
public TextureImage filter( HeightMap map ) {
TextureImage image = new TextureImage( map.getDimension(), map.getDimension(), ImageFormat.RGB, new int[] { map.getDimension()
* map.getDimension() * 3 * 4 }, ByteBuffer.allocateDirect( map.getDimension() * map.getDimension() * 3 * 4 ).order( ByteOrder.nativeOrder() ));
FloatBuffer buffer = image.getData().asFloatBuffer();
for ( int i = 0; i < map.getDimension(); i++ ) {
for ( int j = 0; j < map.getDimension(); j++ ) {
Vec3f currentVertex = new Vec3f( i, map.getPixelWrap( i, j ), j );
Vec3f pU = new Vec3f( i + 1, map.getPixelWrap( i + 1, j ), j );
Vec3f pV = new Vec3f( i, map.getPixelWrap( i, j + 1 ), j + 1 );
Vec3f dpU = Vec3f.subtract( currentVertex, pU ).negate();
Vec3f dpV = Vec3f.subtract( currentVertex, pV );
Vec3f normal = Vec3f.cross( dpU, dpV ).normalise();
normal.multiply( 0.5f ).add( 0.5f );
buffer.put( normal.x ).put( normal.y ).put( normal.z );
}
}
buffer.rewind();
return image;
}
If I was to loop through the created buffer in a J2D application, I would get a greenish rectangle (indicating normals pointing upwards) which is what I expected. But if I was to use OpenGL…I get an entirely different image:
http://img134.imageshack.us/img134/8868/normalmapwronglm3.png
Any ideas on whats going on?
DP 