]> icculus.org git repositories - divverent/nexuiz.git/blob - tools/ImgToMap/src/imgtomap/MapWriter.java
autogenerate skybox, fill dropped blocks with full-height sky (first step to autogene...
[divverent/nexuiz.git] / tools / ImgToMap / src / imgtomap / MapWriter.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package imgtomap;
6
7 import java.awt.image.BufferedImage;
8 import java.awt.image.Raster;
9 import java.io.File;
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;
16
17 /**
18  *
19  * @author maik
20  */
21 public class MapWriter {
22
23     public int writeMap(Parameters p) {
24         if (!(new File(p.infile).exists())) {
25             return 1;
26         }
27
28         FileOutputStream fos;
29         try {
30             fos = new FileOutputStream(new File(p.outfile));
31         } catch (FileNotFoundException ex) {
32             Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
33             return 1;
34         }
35
36         double[][] height = getHeightmap(p.infile);
37         double units = 1d * p.pixelsize;
38         double max = p.height;
39
40         StringBuffer buf = new StringBuffer();
41
42         // worldspawn start
43         buf.append("{\n\"classname\" \"worldspawn\"\n");
44
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) {
48
49                 boolean skip = height[x][y] < 0 || height[x][y + 1] < 0 || height[x + 1][y] < 0 || height[x + 1][y + 1] < 0;
50
51                 if (!skip) {
52
53                     /*
54                      * 
55                      *      a +-------+ b
56                      *       /       /|
57                      *      /       / |
58                      *     /       /  |
59                      *  c +-------+ d + f   (e occluded, unused)
60                      *    |       |  /
61                      *    |       | /
62                      *    |       |/
63                      *  g +-------+ h
64                      * 
65                      */
66
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);
75
76                     buf.append("{\n");
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));
82                     buf.append("}\n");
83
84
85                     buf.append("{\n");
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));
91                     buf.append("}\n");
92                 } else if (p.skyfill) {
93                     // fill skipped blocks with sky
94                     Vector3D p1 = new Vector3D(x * units, -(y + 1) * units, -32.0);
95                     Vector3D p2 = new Vector3D((x + 1) * units, -y * units, p.skyheight);
96                     
97                     writeBoxBrush(buf, p1, p2, false, p.skytexture, 1.0);
98                 }
99             }
100         }
101
102         if (p.sky) {
103             // generate skybox
104             int x = height.length - 1;
105             int y = height[0].length - 1;
106             
107             // top
108             Vector3D p1 = new Vector3D(0, -y * units, p.skyheight);
109             Vector3D p2 = new Vector3D(x * units, 0, p.skyheight + 32.0);
110             writeBoxBrush(buf, p1, p2, false, p.skytexture, 1.0);
111             
112             // bottom
113             p1 = new Vector3D(0, -y * units, -64.0);
114             p2 = new Vector3D(x * units, 0, -32.0);
115             writeBoxBrush(buf, p1, p2, false, p.skytexture, 1.0);
116             
117             // north
118             p1 = new Vector3D(0, 0, -32.0);
119             p2 = new Vector3D(x * units, 32, p.skyheight);
120             writeBoxBrush(buf, p1, p2, false, p.skytexture, 1.0);
121             
122             // east
123             p1 = new Vector3D(x * units, -y * units, -32.0);
124             p2 = new Vector3D(x * units + 32.0, 0, p.skyheight);
125             writeBoxBrush(buf, p1, p2, false, p.skytexture, 1.0);
126
127             // south
128             p1 = new Vector3D(0, -y * units - 32, -32.0);
129             p2 = new Vector3D(x * units, -y * units, p.skyheight);
130             writeBoxBrush(buf, p1, p2, false, p.skytexture, 1.0);
131             
132             
133             // west
134             p1 = new Vector3D(0 - 32.0, -y * units, -32.0);
135             p2 = new Vector3D(0, 0, p.skyheight);
136             writeBoxBrush(buf, p1, p2, false, p.skytexture, 1.0);
137
138         }
139
140         // worldspawn end
141         buf.append("}\n");
142         try {
143             fos.write(buf.toString().getBytes());
144         } catch (IOException ex) {
145             Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
146             return 1;
147         }
148         return 0;
149     }
150
151     private void writeBoxBrush(StringBuffer buf, Vector3D p1, Vector3D p2, boolean detail, String texture, double scale) {
152         Vector3D a = new Vector3D(p1.x, p2.y, p2.z);
153         Vector3D b = p2;
154         Vector3D c = new Vector3D(p1.x, p1.y, p2.z);
155         Vector3D d = new Vector3D(p2.x, p1.y, p2.z);
156         //Vector3D e unused
157         Vector3D f = new Vector3D(p2.x, p2.y, p1.z);
158         Vector3D g = p1;
159         Vector3D h = new Vector3D(p2.x, p1.y, p1.z);
160
161         buf.append("{\n");
162         buf.append(getMapPlaneString(a, b, d, detail, texture, scale));
163         buf.append(getMapPlaneString(d, b, f, detail, texture, scale));
164         buf.append(getMapPlaneString(c, d, h, detail, texture, scale));
165         buf.append(getMapPlaneString(a, c, g, detail, texture, scale));
166         buf.append(getMapPlaneString(f, b, a, detail, texture, scale));
167         buf.append(getMapPlaneString(g, h, f, detail, texture, scale));
168         buf.append("}\n");
169
170     }
171
172     private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, boolean detail, String material, double scale) {
173         int flag;
174         if (detail) {
175             flag = 134217728;
176         } else {
177             flag = 0;
178         }
179         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";
180     }
181
182     private class Vector3D {
183
184         public  double x,        y,        z;
185
186         public Vector3D() {
187             this(0.0, 0.0, 0.0);
188         }
189
190         public Vector3D(double x, double y, double z) {
191             this.x = x;
192             this.y = y;
193             this.z = z;
194         }
195
196         public Vector3D crossproduct(Vector3D p1) {
197             Vector3D result = new Vector3D();
198
199             result.x = this.y * p1.z - this.z * p1.y;
200             result.y = this.z * p1.x - this.x * p1.z;
201             result.z = this.x * p1.y - this.y * p1.x;
202
203             return result;
204         }
205
206         public double dotproduct(Vector3D p1) {
207             return this.x * p1.x + this.y * p1.y + this.z * p1.z;
208         }
209
210         public Vector3D substract(Vector3D p1) {
211             Vector3D result = new Vector3D();
212
213             result.x = this.x - p1.x;
214             result.y = this.y - p1.y;
215             result.z = this.z - p1.z;
216
217             return result;
218         }
219
220         public void scale(double factor) {
221             x *= factor;
222             y *= factor;
223             z *= factor;
224         }
225
226         public double length() {
227             return Math.sqrt((x * x) + (y * y) + (z * z));
228         }
229
230         public void normalize() {
231             double l = length();
232
233             x /= l;
234             y /= l;
235             z /= l;
236         }
237     }
238
239     private double[][] getHeightmap(String file) {
240         try {
241             BufferedImage bimg = ImageIO.read(new File(file));
242             Raster raster = bimg.getRaster();
243             int x = raster.getWidth();
244             int y = raster.getHeight();
245
246             double[][] result = new double[x][y];
247
248             for (int xi = 0; xi < x; ++xi) {
249                 for (int yi = 0; yi < y; ++yi) {
250                     float[] pixel = raster.getPixel(xi, yi, (float[]) null);
251
252                     int channels;
253                     boolean alpha;
254                     if (pixel.length == 3) {
255                         // RGB
256                         channels = 3;
257                         alpha = false;
258                     } else if (pixel.length == 4) {
259                         // RGBA
260                         channels = 3;
261                         alpha = true;
262                     } else if (pixel.length == 1) {
263                         // grayscale
264                         channels = 1;
265                         alpha = false;
266                     } else {
267                         // grayscale with alpha
268                         channels = 1;
269                         alpha = true;
270                     }
271
272                     float tmp = 0f;
273                     for (int i = 0; i < channels; ++i) {
274                         tmp += pixel[i];
275                     }
276                     result[xi][yi] = tmp / (channels * 255f);
277
278                     if (alpha) {
279                         // mark this pixel to be skipped
280                         if (pixel[pixel.length - 1] < 64.0) {
281                             result[xi][yi] = -1.0;
282                         }
283                     }
284                 }
285             }
286
287
288             return result;
289         } catch (IOException ex) {
290             Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
291         }
292
293         return null;
294     }
295 }