2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
7 import java.awt.image.BufferedImage;
8 import java.awt.image.Raster;
10 import java.io.FileNotFoundException;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15 import javax.imageio.ImageIO;
21 public class MapWriter {
23 public void writeMap(Parameters p) {
24 if (!(new File(p.infile).exists())) {
30 fos = new FileOutputStream(new File(p.outfile));
31 } catch (FileNotFoundException ex) {
32 Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
36 double[][] height = getHeightmap(p.infile);
37 double units = 1d * p.pixelsize;
38 double max = p.height;
40 StringBuffer buf = new StringBuffer();
43 buf.append("{\n\"classname\" \"worldspawn\"\n");
45 // wander through grid
46 for (int x = 0; x < height.length - 1; ++x) {
47 for (int y = 0; y < height[0].length - 1; ++y) {
49 boolean skip = height[x][y] < 0 || height[x][y + 1] < 0 || height[x + 1][y] < 0 || height[x + 1][y + 1] < 0;
59 * c +-------+ d + f (e occluded, unused)
67 Vector3D a = new Vector3D(x * units, -y * units, height[x][y] * max);
68 Vector3D b = new Vector3D((x + 1) * units, -y * units, height[x + 1][y] * max);
69 Vector3D c = new Vector3D(x * units, -(y + 1) * units, height[x][y + 1] * max);
70 Vector3D d = new Vector3D((x + 1) * units, -(y + 1) * units, height[x + 1][y + 1] * max);
71 //Vector3D e = new Vector3D(x * units, -y * units, -16.0);
72 Vector3D f = new Vector3D((x + 1) * units, -y * units, -16.0);
73 Vector3D g = new Vector3D(x * units, -(y + 1) * units, -16.0);
74 Vector3D h = new Vector3D((x + 1) * units, -(y + 1) * units, -16.0);
77 buf.append(getMapPlaneString(a, b, d, p.detail, p.texture, p.texturescale));
78 buf.append(getMapPlaneString(d, b, f, p.detail, "common/caulk", p.texturescale));
79 buf.append(getMapPlaneString(f, b, a, p.detail, "common/caulk", p.texturescale));
80 buf.append(getMapPlaneString(a, d, h, p.detail, "common/caulk", p.texturescale));
81 buf.append(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
86 buf.append(getMapPlaneString(d, c, a, p.detail, p.texture, p.texturescale));
87 buf.append(getMapPlaneString(g, c, d, p.detail, "common/caulk", p.texturescale));
88 buf.append(getMapPlaneString(c, g, a, p.detail, "common/caulk", p.texturescale));
89 buf.append(getMapPlaneString(h, d, a, p.detail, "common/caulk", p.texturescale));
90 buf.append(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
99 fos.write(buf.toString().getBytes());
100 } catch (IOException ex) {
101 Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
105 private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, boolean detail, String material, double scale) {
112 return "( " + p1.x + " " + p1.y + " " + p1.z + " ) ( " + p2.x + " " + p2.y + " " + p2.z + " ) ( " + p3.x + " " + p3.y + " " + p3.z + " ) " + material + " 0 0 0 " + scale + " " + scale + " " + flag + " 0 0\n";
115 private class Vector3D {
117 public double x, y, z;
123 public Vector3D(double x, double y, double z) {
129 public Vector3D crossproduct(Vector3D p1) {
130 Vector3D result = new Vector3D();
132 result.x = this.y * p1.z - this.z * p1.y;
133 result.y = this.z * p1.x - this.x * p1.z;
134 result.z = this.x * p1.y - this.y * p1.x;
139 public double dotproduct(Vector3D p1) {
140 return this.x * p1.x + this.y * p1.y + this.z * p1.z;
143 public Vector3D substract(Vector3D p1) {
144 Vector3D result = new Vector3D();
146 result.x = this.x - p1.x;
147 result.y = this.y - p1.y;
148 result.z = this.z - p1.z;
153 public void scale(double factor) {
159 public double length() {
160 return Math.sqrt((x * x) + (y * y) + (z * z));
163 public void normalize() {
172 private double[][] getHeightmap(String file) {
174 BufferedImage bimg = ImageIO.read(new File(file));
175 Raster raster = bimg.getRaster();
176 int x = raster.getWidth();
177 int y = raster.getHeight();
179 double[][] result = new double[x][y];
181 for (int xi = 0; xi < x; ++xi) {
182 for (int yi = 0; yi < y; ++yi) {
183 float[] pixel = raster.getPixel(xi, yi, (float[]) null);
187 if (pixel.length == 3) {
191 } else if (pixel.length == 4) {
195 } else if (pixel.length == 1) {
200 // grayscale with alpha
206 for (int i = 0; i < channels; ++i) {
209 result[xi][yi] = tmp / (channels * 255f);
212 // mark this pixel to be skipped
213 if (pixel[pixel.length - 1] < 64.0) {
214 result[xi][yi] = -1.0;
222 } catch (IOException ex) {
223 Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);