Created
July 14, 2012 09:29
-
-
Save twyatt/3110188 to your computer and use it in GitHub Desktop.
Simple utility class for reading libGDX's Pixmaps for use as heightmaps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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