/**
* Copyright (c) 2008-2010 Ardor Labs, Inc.
*
* This file is part of Ardor3D.
*
* Ardor3D is free software: you can redistribute it and/or modify it
* under the terms of its license which may be found in the accompanying
* LICENSE file or at <http://www.ardor3d.com/LICENSE>.
*/
package net.anzix.fsz.proceduralterrain;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import com.ardor3d.extension.terrain.client.TerrainConfiguration;
import com.ardor3d.extension.terrain.client.TerrainSource;
import com.ardor3d.extension.terrain.util.Tile;
import com.ardor3d.math.Vector3;
import com.ardor3d.math.type.ReadOnlyVector3;
public class ProceduralTerrainSource implements TerrainSource {
private final NominalHeightGenerator nominalHeightGenerator;
private final LayerStructure layerStructure;
private final ReadOnlyVector3 scale = new Vector3(1, 1, 1);;
private final float verticalScale = 100.0f;
private static final int tileSize = 128;
private static final int availableClipmapLevels = 8;
private final ReentrantLock terrainLock = new ReentrantLock();
private final ThreadLocal<float[]> tileDataPool = new ThreadLocal<float[]>() {
@Override
protected float[] initialValue() {
return new float[tileSize * tileSize];
}
};
public ProceduralTerrainSource(final NominalHeightGenerator mominalHeightGenerator, LayerStructure layerStructure) {
this.nominalHeightGenerator = mominalHeightGenerator;
this.layerStructure = layerStructure;
}
@Override
public TerrainConfiguration getConfiguration() throws Exception {
return new TerrainConfiguration(availableClipmapLevels, tileSize, scale, -verticalScale*10, verticalScale*10, false);
}
@Override
public Set<Tile> getValidTiles(final int clipmapLevel, final int tileX, final int tileY, final int numTilesX,
final int numTilesY) throws Exception {
return null;
}
@Override
public Set<Tile> getInvalidTiles(final int clipmapLevel, final int tileX, final int tileY, final int numTilesX,
final int numTilesY) throws Exception {
return null;
}
@Override
public int getContributorId(final int clipmapLevel, final Tile tile) {
return 0;
}
@Override
public float[] getTile(final int clipmapLevel, final Tile tile) throws Exception {
final float[] data = tileDataPool.get();
final int tileX = tile.getX();
final int tileY = tile.getY();
final int baseClipmapLevel = availableClipmapLevels - clipmapLevel - 1;
terrainLock.lock();
try {
for (int y = 0; y < tileSize; y++) {
for (int x = 0; x < tileSize; x++) {
if (Thread.interrupted()) {
return null;
}
final int heightX = tileX * tileSize + x;
final int heightY = tileY * tileSize + y;
final int index = x + y * tileSize;
float nominalHeight = nominalHeightGenerator.getHeight(heightX << baseClipmapLevel, heightY << baseClipmapLevel);
PositionInfo positionInfo = layerStructure.getPositionInfo(nominalHeight, heightX << baseClipmapLevel, heightY << baseClipmapLevel);
data[index] = positionInfo.getRealHeight() * verticalScale;
// data[index] = (float) function.eval(heightX << baseClipmapLevel, heightY << baseClipmapLevel, 0);
}
}
} finally {
terrainLock.unlock();
}
return data;
}
}