www.jpct.net

jPCT - a 3d engine for Java => Support => Topic started by: Thistle on December 25, 2012, 11:56:27 pm

Title: Terrain Generation not working
Post by: Thistle on December 25, 2012, 11:56:27 pm
So i've been trying to decode Egon's heightmap code and the edits I made to convert it from Android to work on PC were unsuccessful.

Code: [Select]
public class Terrain {

public static float HighestPoint = 0;

static BufferedImage bmp = null;
static String pixel = null;

public static Object3D generate(int X_SIZE, int Z_SIZE, TextureManager tm) throws IOException {

bmp = ImageIO.read(new File("res/map.bmp"));

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++) {

Color c = new Color(bmp.getRGB(x,z));
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();

terrain[x][z] = (float) (((0.299*r) + (0.587*g) + (0.114*b))/100); // Grayscale algorithm

System.out.println(terrain[x][z]);

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("map"); // 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);
}
ground.build();
}

return ground;
}
}


I am not particularly experienced with this library, but from what I've seen the Terrain generation code looks almost identical to this one, but works properly when I fire it in. What seems to be wrong with this code since it doesn't appear to do anything when I run it?
Title: Re: Terrain Generation not working
Post by: EgonOlsen on December 26, 2012, 12:19:07 am
Any log output?
Title: Re: Terrain Generation not working
Post by: Thistle on December 26, 2012, 01:41:31 am
The System.out prints what I expect it to, most of the values being 0 as most of the bitmap is pure black, with a centre square being completely white which outputs 255. Since 0 to 255 seems extreme as the random terrain code generates between -20 to around 20, I took the grayscale value and divided by 10. The console doesn't seem to return any errors, but if you meant something different I haven't a clue.

EDIT: I'd like to rule out that the generation is not visible to where the camera is currently pointing, but every time I generate a random terrain I can barely see it, and can't quite understand how the controls in JPCT work.
Title: Re: Terrain Generation not working
Post by: EgonOlsen on December 26, 2012, 04:00:53 pm
I actually meant the log output of the engine. It would help to see if the object is actually active and visible. Most likely, your camera simply don't point at the terrain...