Skip to content

Instantly share code, notes, and snippets.

@twyatt
Created July 14, 2012 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save twyatt/3110188 to your computer and use it in GitHub Desktop.
Save twyatt/3110188 to your computer and use it in GitHub Desktop.
Simple utility class for reading libGDX's Pixmaps for use as heightmaps.
package com.traviswyatt.example.helpers.terrain;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.traviswyatt.example.models.Heightmap;
public class HeightmapUtils {
public static Heightmap load(FileHandle file) {
Pixmap pixmap = new Pixmap(file);
return load(pixmap);
}
public static Heightmap load(Pixmap pixmap) {
int width = pixmap.getWidth();
int height = pixmap.getHeight();
float[] data = new float[width * height];
Color color = new Color();
int i = 0;
for (int y = 0; y < width; y++) {
for (int x = 0; x < height; x++) {
int pixel = pixmap.getPixel(x, y);
Color.rgba8888ToColor(color, pixel);
data[i] = color.r;
i++;
}
}
boolean flipY = true;
return new Heightmap(width, height, data, flipY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment