Terrain loading problems (HeightMapGrid)

heres the code, ive double checked the locaiton of the heightmap images, yet still it complains:

“08-Jun-2011 18:02:07 TerrainGenerator getHeightMapAt

INFO: File: terrain_128_128.png not found, loading zero heightmap instead”

my terrain images increment every 64 in x and z so i had to reimplement ImageBasedHeightMapGrid so it did 64



[java] public TerrainGenerator(String textureBase, String textureExt, AssetManager assetManager) {

this.textureBase = textureBase;

this.textureExt = textureExt;

this.assetManager = assetManager;

this.assetManager.registerLocator(“C:/Users/PTP/workspace/Game/assets/”, FileLocator.class);

try{

for(int i=0; i<500; i=i+64){

for(int j=0; j<500; j=j+64){

this.assetManager.loadTexture(“Terrain/terrain_”+i+"_"+j+".PNG");

}

}

}catch(Exception e){

System.out.println(“Couldnt load Terrain”);

System.exit(0);

}

}



@Override

public HeightMap getHeightMapAt(Vector3f location) {

// TODO Auto-generated method stub

int x = (int) location.x
64;

int z = (int) location.z*64;

System.out.println(“X; “+x+” Z:”+z);

AbstractHeightMap heightmap = null;

try {

final InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(textureBase + “" + x + "” + z + “.” + textureExt);

BufferedImage im = null;

if (stream != null) {

im = ImageIO.read(stream);

} else {

im = new BufferedImage(size, size, BufferedImage.TYPE_USHORT_GRAY);

Logger.getLogger(ImageBasedHeightMapGrid.class.getCanonicalName()).log(Level.INFO, “File: " + textureBase + “" + x + "” + z + “.” + textureExt + " not found, loading zero heightmap instead”);

}

// CREATE HEIGHTMAP

heightmap = new Grayscale16BitHeightMap(im);

heightmap.setHeightScale(256);

heightmap.load();

} catch (IOException e) {

} catch (AssetNotFoundException e) {

}

return heightmap;

}

[/java]



the only thing different is the way x and z are calculated to get filenames, and the results are correct.

The problem is your input stream. It isn’t using the asset manager and is pulling from the classpath instead.

Try:

[java]

this.assetManager.loadAsset(new TextureKey(textureBase + “" + x + "” + z + “.” + textureExt)

[/java]

yes I have changed the logic since the first commit of terraingrid, so the heightmaptiles are recognized by their cell id, and not their coordinates. ( so the filenames won’t go crazy when using 500x500 pieces of 512x512 terrain tiles.)



but this in the constructor



[java]this.assetManager.loadTexture(“Terrain/terrain_”+i+""+j+".PNG");[/java]



and this in the loading method:



[java]final InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(textureBase + "
" + x + “_” + z + “.” + textureExt);[/java]



seems not consistent. why don’t you do the check in the constructor with the base and ext instead of a constant? And @Sploreg is right, if you check with the assetManager and it succeeds then you should use it for loading as well, or check with the classloader in the constructor :wink:

A short explanation:



I used the classloader instead of the assetManager, because it seems that the texture loader cannot load 16 bit grayscale images. I now, there’s a parameter to some converter code, but I double checked the loading mechanism, and it seems to start with a conversion from any BufferedImage.Type into BufferedImage.Byte_ARGB where the whole 16 bit grayscale gets converted to 1 byte. I created my own Grayscale16BitHeightMap class, that takes the bufferedImage itself, and loads correctly.

thanks for the help guys but those methods didn’t work for some reason, it removed the error but terrain was still flat, but i found this :

[java] final Texture heightMapImage =

assetManager.loadTexture(“Terrain/terrain_” + x + “_” + z + “.” + textureExt);

final AbstractHeightMap heightmap2 =

new ImageBasedHeightMap(

ImageToAwt.convert(

heightMapImage.getImage(), false, true, 0));

heightmap2.load();

return heightmap2;[/java]



worked in loading the terrain but of course where would life be if there wasn’t another problem in my path :stuck_out_tongue:

http://i51.tinypic.com/2mhsi2t.jpg



i think i might leave terrain for now and do some other stuff, giving me too many headaches

just a fast help: in other topics I read that heightmaps as textures are read flipped, so you must use a textureKey and set the flip to true. It might help you with that. :slight_smile:

yes that does help a bit, still not connecting them together, its ok im going to move on to some basic concepts like day and night cycles and GUI