Author Topic: Heightmap Terrain  (Read 4876 times)

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Heightmap Terrain
« on: November 29, 2010, 08:59:16 pm »
managed to get heightmap working, and want to share it with all of you :)

When using it make sure you have a Texture Manager with a TextureID ready, (I used a flat gray 128x128 image for this)

Code: [Select]
public class Terrain {

public static float HighestPoint = 0;


static Bitmap bmp = null;
static String pixel = null;
public static Object3D Generate(int X_SIZE, int Z_SIZE, TextureManager tm, String TextureID){

bmp = BitmapFactory.decodeResource(SpaceGrabber.resources, R.drawable.terrain); // the heightmap texture (128x128 due to memory limitations?)

float[][] terrain = new float[X_SIZE][Z_SIZE];

for (int x = 0; x < X_SIZE-1; x++) {
for (int z = 0; z < Z_SIZE-1; z++) {

pixel = Integer.toString(bmp.getPixel(x, z), 16);
terrain[x][z] = Integer.parseInt(pixel.charAt(1) + "" + pixel.charAt(2), 16);

if(terrain[x][z] > HighestPoint){
HighestPoint = terrain[x][z];
}
}

}
for (int x = 0; x < X_SIZE - 1; x++) {
for (int z = 0; z < Z_SIZE - 1; z++) {
terrain[x][z] = (terrain[x][z] + terrain[x + 1][z] + terrain[x][z + 1] + terrain[x + 1][z + 1]) / 4;
}
}
Object3D ground = new Object3D(X_SIZE * Z_SIZE * 2);

float xSizeF = (float) X_SIZE;
float zSizeF = (float) Z_SIZE;

int id = tm.getTextureID(TextureID); // I used a gray 128x128 texture

for (int x = 0; x < X_SIZE - 1; x++) {
for (int z = 0; z < Z_SIZE - 1; z++) {

TextureInfo ti = new TextureInfo(id, (x / xSizeF), (z / zSizeF), ((x + 1) / xSizeF), (z / zSizeF), (x / xSizeF), ((z + 1) / zSizeF));
ground.addTriangle(new SimpleVector(x * 10, terrain[x][z], z * 10), new SimpleVector((x + 1) * 10, terrain[x + 1][z], z * 10),
new SimpleVector(x * 10, terrain[x][z + 1], (z + 1) * 10), ti);

ti = new TextureInfo(id, (x / xSizeF), ((z + 1) / zSizeF), ((x + 1) / xSizeF), (z / zSizeF), ((x + 1) / xSizeF), ((z + 1) / zSizeF));
ground.addTriangle(new SimpleVector(x * 10, terrain[x][z + 1], (z + 1) * 10), new SimpleVector((x + 1) * 10, terrain[x + 1][z], z * 10), new SimpleVector((x + 1) * 10,
terrain[x + 1][z + 1], (z + 1) * 10), ti);
}
}

return ground;
}
}
« Last Edit: November 29, 2010, 09:12:51 pm by Kaiidyn »
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch

Offline thanacha

  • byte
  • *
  • Posts: 1
    • View Profile
Re: Heightmap Terrain
« Reply #1 on: November 30, 2010, 07:45:34 am »
Thank you for your kindly  :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Heightmap Terrain
« Reply #2 on: November 30, 2010, 08:40:53 am »
The wiki has a code snippet section. Maybe you want to add it there too?

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Re: Heightmap Terrain
« Reply #3 on: November 30, 2010, 12:14:04 pm »
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Heightmap Terrain
« Reply #4 on: November 30, 2010, 12:34:27 pm »
Great, thank you.

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Re: Heightmap Terrain
« Reply #5 on: November 30, 2010, 02:04:39 pm »
Is it possible that you optimize this snippet and add it to a new version of jPCT (and ae) ?

use it like

Terrain terrain = new Terrain();
terrain.Generate(sizex, sizex, maxheight);
and
terrain.fromHeightmap(resource image);
or/and
terrain.fromHeightmap("path/to/image");

returning Object3D

and perhaps a SimpleVector getSize() to return the size of the terrain in a SimpleVector.
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch