|
Here's my code that reads specific "blocks" of data
from a RAW bitmap (24bits,8 R, 8 G, 8 B). It's called with upper left
hand location coordinates, and returns the data as a delimited string of
height values (ugly, I know).
Because this raw file is accessed throughout a
player's session, the file is opened in the init() method, and remains open (I
seem to get a little better performance this way??). The heightmap.raw
file is about 12MB.
Any comments on my crappy code is
welcome...
Scott
Virtopia 2 3D Project
public final class MapReader{
private DataInputStream in; private DataOutputStream out; private RandomAccessFile is; public MapReader(){ System.out.println("Initializing MapReader."); init(); } public void init(){
try { is = new RandomAccessFile("heightmap.raw", "r"); } catch (IOException e) { System.out.println("File error: " + e); } } public String getTerrainData(int tx, int tz, int blockSize){ String outData=""; int i,j; float scaleFactor=1.0f; float heightOffset=0.0f; float data[][] = new float[5][5]; try{ for (j=0;j<blockSize;j++){ is.seek(tx*3 + (2048*3 * (tz+j) )) ; for (i = 0; i < blockSize; i++) { int data1 = (0xff) & is.readByte(); int data2 = (0xff) & is.readByte(); int data3 = (0xff) & is.readByte(); data[i][j] = (data1+data2+data3)/3.0f; } } } catch(IOException e){ System.out.println("Error reading map: " + e); } DecimalFormat format = new DecimalFormat("0.00"); for (j = 0;j<blockSize; j++)
{
for (i = 0;i<blockSize; i++) { outData = outData + format.format(((data[i][j])/scaleFactor+heightOffset)) + ","; } } return outData;
} public void
destroy(){
try{ is.close(); } catch (IOException e){ System.out.println("Error closing map file: " + e); } is=null; } }
|
- [JAVA3D] Terragen clifton.zama
- [JAVA3D] Terragen clifton.zama
- Re: [JAVA3D] Terragen Scott
- Re: [JAVA3D] Terragen clifton.zama
- Scott
