91
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
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;
}
}
terrHeightUp = ground.calcMinDistance(heightCalcPos, new SimpleVector(0,-1,0), 100); // always 2147483647
terrHeightDown = ground.calcMinDistance(heightCalcPos, new SimpleVector(0,1,0), 100);
public class Terrain {
//private static float[][] terrain = new float[128][128];
//private static Object3D ground = null;
public static Object3D Generate(int X_SIZE, int Z_SIZE, TextureManager tm, String TextureID){
/** Generate ground **/
//int X_SIZE = 128;
//int Z_SIZE = 128;
float[][] terrain = new float[X_SIZE][Z_SIZE];
for (int x = 0; x < X_SIZE; x++) {
for (int z = 0; z < Z_SIZE; z++) {
terrain[x][z] = -20 + (float) Math.random() * 40f; // Very simple....
}
}
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);
for (int x = 0; x < X_SIZE - 1; x++) {
for (int z = 0; z < Z_SIZE - 1; z++) {
/**
* We are now taking the heights calculated above and build the
* actual triangles using this data. The terrain's grid is fixed
* in x and z direction and so are the texture coordinates. The
* only part that varies is the height, represented by the data
* in terrain[][]. jPCT automatically takes care of vertex
* sharing and mesh optimizations (this is why building objects
* this way isn't blazing fast...but it pays out in the
* end...:-)) The Mesh is build with triangle strips in mind
* here. The format for these strips is equal to that of
* OpenGL's triangle strips.
*/
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;
}
}
SimpleVector playerPosition = new SimpleVector();
playerPosition.set(player.getPositionInWorldSpace());
ground.getHeight(playerPosition.x, playerPosition.z);
TextureManager.getInstance().flush();
TextureManager tm = TextureManager.getInstance();
Resources res = Engine.resources;
/** LOAD TEXTURES **/
Texture random = new Texture(Engine.resources.getDrawable(R.raw.random));
tm.addTexture("random", random);
TextureInfo randomTex = new TextureInfo(tm.getTextureID("random"));
button1 = Primitives.getEllipsoid(64, 0.5f);
button1.setTexture(randomTex);
button1.build();
button1.translate(0, -125, 0);
world.addObject(button1);
All this code is located in onSurfaceCreated.