211
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.
//load map image and grab pixels data
Image map = ClientFactory.getImage("/resources6/test_map2.gif").getScaledInstance(MAP_WIDTH, MAP_HEIGHT, Image.SCALE_SMOOTH);
PixelGrabber pg = new PixelGrabber(map, 0, 0, MAP_WIDTH, MAP_HEIGHT, true);
pg.grabPixels(0);
int[] pixels = (int[]) pg.getPixels();
//create terrain object
ground = new Object3D(pixels.length * 4);
//store map heights
int[][] cols = new int[MAP_HEIGHT][MAP_WIDTH];
for (int i = 0; i < pixels.length; i++) {
int x = i / MAP_WIDTH;
int y = i % MAP_WIDTH;
int col = getCol(i, pixels);
cols[x][y] = col;
}
//preload random textures for map tiles
String[] textures = new String[]{
"/resources6/test_texture.jpg",
"/resources6/test_texture2.jpg",
"/resources6/test_texture3.jpg",
"/resources6/test_texture4.jpg",
"/resources6/test_texture5.jpg"
};
for (int i = 0; i < textures.length; i++) {
ClientFactory.getTexture(textures[i]);
}
//add polygons to Ground
for (int i = 0; i < MAP_HEIGHT - 1; i++) {
for (int j = 0; j < MAP_WIDTH - 1; j++) {
int nw = cols[i][j];
int ne = cols[i][j + 1];
int se = cols[i + 1][j + 1];
int sw = cols[i + 1][j];
int x = j;
int y = i;
int textureId = TextureManager.getInstance().getTextureID(textures[(int) (Math.random() * textures.length)]);
ClientFactory.addPlane(ground,
new SimpleVector(x * CELL_SIZE, y * CELL_SIZE + CELL_SIZE, HEIGHT_PER_UNIT * sw), //sw
new SimpleVector(x * CELL_SIZE, y * CELL_SIZE, HEIGHT_PER_UNIT * nw), //nw
new SimpleVector(x * CELL_SIZE + CELL_SIZE, y * CELL_SIZE, HEIGHT_PER_UNIT * ne), //ne
new SimpleVector(x * CELL_SIZE + CELL_SIZE, y * CELL_SIZE + CELL_SIZE, HEIGHT_PER_UNIT * se), //se
CELL_SIZE,
textureId);
}
}
ground.calcNormals();