Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Last active December 16, 2016 21:22
Show Gist options
  • Save zacharycarter/d809ecbe45e83971aeb39188ef79c1c5 to your computer and use it in GitHub Desktop.
Save zacharycarter/d809ecbe45e83971aeb39188ef79c1c5 to your computer and use it in GitHub Desktop.
private static Texture generateNormalMapTexture(int width, int height, Pixmap pm) {
pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
texture = new Texture(width, height, Pixmap.Format.RGBA8888);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Vector3 currentVertex = new Vector3(x, getPixelWrap(x, y, width, height, pm), y);
Vector3 pU = new Vector3(x + 1, getPixelWrap(x + 1, y, width, height, pm), y);
Vector3 pV = new Vector3(x, getPixelWrap(x, y + 1, width, height, pm), y + 1);
Vector3 dpU = negate(currentVertex.cpy().sub(pU));
Vector3 dpV = currentVertex.cpy().sub(pV);
Vector3 normal = dpU.cpy().crs(dpV).nor();
Vector3 normalRGB = normal.cpy().scl(0.5f).add(0.5f);
pixmap.drawPixel(x, y, new Color(normalRGB.x, normalRGB.z, normalRGB.y, 1).toIntBits());
}
}
PixmapIO.writePNG(Gdx.files.getFileHandle("normalmap.png", Files.FileType.Local), pixmap);
texture.setWrap(Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge);
texture.draw(pixmap, 0, 0);
return texture;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment