]> icculus.org git repositories - divverent/nexuiz.git/blob - tools/ImgToMap/src/imgtomap/MapWriter.java
uh, actually generate terrain
[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.io.PrintWriter;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
18 import javax.imageio.ImageIO;
19
20 /**
21  *
22  * @author maik
23  */
24 public class MapWriter {
25
26     public int writeMap(Parameters p) {
27         if (!(new File(p.infile).exists())) {
28             return 1;
29         }
30
31         double[][] height = getHeightmap(p.infile);
32         double[][] columns = getColumns(height);
33         double units = 1d * p.pixelsize;
34         double max = p.height;
35
36         PrintWriter pw = null;
37         try {
38             pw = new PrintWriter(new FileOutputStream(new File(p.outfile)));
39         } catch (FileNotFoundException ex) {
40             Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
41             return 1;
42         }
43
44
45         // worldspawn start
46         pw.print("{\n\"classname\" \"worldspawn\"\n");
47
48         // wander through grid
49         for (int x = 0; x < height.length - 1; ++x) {
50             for (int y = 0; y < height[0].length - 1; ++y) {
51
52                 boolean skip = getMinMaxForRegion(height, x, y, 2)[0] < 0;
53
54                 if (!skip) {
55
56                     /*
57                      * 
58                      *      a +-------+ b
59                      *       /       /|
60                      *      /       / |
61                      *     /       /  |
62                      *  c +-------+ d + f   (e occluded, unused)
63                      *    |       |  /
64                      *    |       | /
65                      *    |       |/
66                      *  g +-------+ h
67                      * 
68                      */
69
70                     Vector3D a = new Vector3D(x * units, -y * units, height[x][y] * max);
71                     Vector3D b = new Vector3D((x + 1) * units, -y * units, height[x + 1][y] * max);
72                     Vector3D c = new Vector3D(x * units, -(y + 1) * units, height[x][y + 1] * max);
73                     Vector3D d = new Vector3D((x + 1) * units, -(y + 1) * units, height[x + 1][y + 1] * max);
74                     //Vector3D e = new Vector3D(x * units, -y * units, -16.0);
75                     Vector3D f = new Vector3D((x + 1) * units, -y * units, -16.0);
76                     Vector3D g = new Vector3D(x * units, -(y + 1) * units, -16.0);
77                     Vector3D h = new Vector3D((x + 1) * units, -(y + 1) * units, -16.0);
78
79                     pw.print("{\n");
80                     pw.print(getMapPlaneString(a, b, d, p.detail, p.texture, p.texturescale));
81                     pw.print(getMapPlaneString(d, b, f, p.detail, "common/caulk", p.texturescale));
82                     pw.print(getMapPlaneString(f, b, a, p.detail, "common/caulk", p.texturescale));
83                     pw.print(getMapPlaneString(a, d, h, p.detail, "common/caulk", p.texturescale));
84                     pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
85                     pw.print("}\n");
86
87
88                     pw.print("{\n");
89                     pw.print(getMapPlaneString(d, c, a, p.detail, p.texture, p.texturescale));
90                     pw.print(getMapPlaneString(g, c, d, p.detail, "common/caulk", p.texturescale));
91                     pw.print(getMapPlaneString(c, g, a, p.detail, "common/caulk", p.texturescale));
92                     pw.print(getMapPlaneString(h, d, a, p.detail, "common/caulk", p.texturescale));
93                     pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
94                     pw.print("}\n");
95                 }
96             }
97         }
98
99         if (p.skyfill) {
100             for (int x = 0; x < columns.length; ++x) {
101                 for (int y = 0; y < columns[0].length; ++y) {
102                     if (columns[x][y] < 0) {
103                         // this is a skipped block, see if it neighbours a
104                         // relevant block
105                         double tmp = getMinMaxForRegion(columns, x - 1, y - 1, 3)[1];
106                         if (tmp >= 0) {
107                             Vector3D p1 = new Vector3D(x * units, -(y + 1) * units, -32.0);
108                             Vector3D p2 = new Vector3D((x + 1) * units, -y * units, p.skyheight);
109
110                             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
111                         }
112                     }
113                 }
114             }
115         }
116
117         if (p.sky) {
118             // generate skybox
119             int x = height.length - 1;
120             int y = height[0].length - 1;
121
122             // top
123             Vector3D p1 = new Vector3D(0, -y * units, p.skyheight);
124             Vector3D p2 = new Vector3D(x * units, 0, p.skyheight + 32.0);
125             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
126
127             // bottom
128             p1 = new Vector3D(0, -y * units, -64.0);
129             p2 = new Vector3D(x * units, 0, -32.0);
130             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
131
132             // north
133             p1 = new Vector3D(0, 0, -32.0);
134             p2 = new Vector3D(x * units, 32, p.skyheight);
135             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
136
137             // east
138             p1 = new Vector3D(x * units, -y * units, -32.0);
139             p2 = new Vector3D(x * units + 32.0, 0, p.skyheight);
140             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
141
142             // south
143             p1 = new Vector3D(0, -y * units - 32, -32.0);
144             p2 = new Vector3D(x * units, -y * units, p.skyheight);
145             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
146
147
148             // west
149             p1 = new Vector3D(0 - 32.0, -y * units, -32.0);
150             p2 = new Vector3D(0, 0, p.skyheight);
151             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
152
153         }
154
155         // genBlockers screws the columns array!
156         // this should be the last step!
157         if (p.visblockers) {
158             List<VisBlocker> blockers = genBlockers(columns, 0.15);
159             double xmax = (columns.length - 1) * units;
160             double ymax = (columns[0].length - 1) * units;
161             for (VisBlocker b : blockers) {
162                 double z = b.minheight * p.height;
163                 z = Math.floor(z / 16);
164                 z = z * 16;
165
166                 if (z > 0) {
167                     int dim = b.dim;
168
169                     double x = b.x * units;
170                     double y = (b.y + dim) * units;
171                     x = x > xmax ? xmax : x;
172                     y = y > ymax ? ymax : y;
173                     Vector3D p1 = new Vector3D(x, -y, -32.0);
174
175                     x = (b.x + dim) * units;
176                     y = b.y * units;
177                     x = x > xmax ? xmax : x;
178                     y = y > ymax ? ymax : y;
179                     Vector3D p2 = new Vector3D(x, -y, z);
180
181                     writeBoxBrush(pw, p1, p2, false, "common/caulk", 1.0);
182                 }
183
184             }
185         }
186
187         // worldspawn end
188         pw.print("}\n");
189         pw.close();
190         return 0;
191     }
192
193     private void writeBoxBrush(PrintWriter pw, Vector3D p1, Vector3D p2, boolean detail, String texture, double scale) {
194         Vector3D a = new Vector3D(p1.x, p2.y, p2.z);
195         Vector3D b = p2;
196         Vector3D c = new Vector3D(p1.x, p1.y, p2.z);
197         Vector3D d = new Vector3D(p2.x, p1.y, p2.z);
198         //Vector3D e unused
199         Vector3D f = new Vector3D(p2.x, p2.y, p1.z);
200         Vector3D g = p1;
201         Vector3D h = new Vector3D(p2.x, p1.y, p1.z);
202
203         pw.print("{\n");
204         pw.print(getMapPlaneString(a, b, d, detail, texture, scale));
205         pw.print(getMapPlaneString(d, b, f, detail, texture, scale));
206         pw.print(getMapPlaneString(c, d, h, detail, texture, scale));
207         pw.print(getMapPlaneString(a, c, g, detail, texture, scale));
208         pw.print(getMapPlaneString(f, b, a, detail, texture, scale));
209         pw.print(getMapPlaneString(g, h, f, detail, texture, scale));
210         pw.print("}\n");
211
212     }
213
214     private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, boolean detail, String material, double scale) {
215         int flag;
216         if (detail) {
217             flag = 134217728;
218         } else {
219             flag = 0;
220         }
221         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";
222     }
223
224     private double[][] getHeightmap(String file) {
225         try {
226             BufferedImage bimg = ImageIO.read(new File(file));
227             Raster raster = bimg.getRaster();
228             int x = raster.getWidth();
229             int y = raster.getHeight();
230
231             double[][] result = new double[x][y];
232
233             for (int xi = 0; xi < x; ++xi) {
234                 for (int yi = 0; yi < y; ++yi) {
235                     float[] pixel = raster.getPixel(xi, yi, (float[]) null);
236
237                     int channels;
238                     boolean alpha;
239                     if (pixel.length == 3) {
240                         // RGB
241                         channels = 3;
242                         alpha = false;
243                     } else if (pixel.length == 4) {
244                         // RGBA
245                         channels = 3;
246                         alpha = true;
247                     } else if (pixel.length == 1) {
248                         // grayscale
249                         channels = 1;
250                         alpha = false;
251                     } else {
252                         // grayscale with alpha
253                         channels = 1;
254                         alpha = true;
255                     }
256
257                     float tmp = 0f;
258                     for (int i = 0; i < channels; ++i) {
259                         tmp += pixel[i];
260                     }
261                     result[xi][yi] = tmp / (channels * 255f);
262
263                     if (alpha) {
264                         // mark this pixel to be skipped
265                         if (pixel[pixel.length - 1] < 64.0) {
266                             result[xi][yi] = -1.0;
267                         }
268                     }
269                 }
270             }
271
272
273             return result;
274         } catch (IOException ex) {
275             Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
276         }
277
278         return null;
279     }
280
281     private double[][] getColumns(double[][] heights) {
282         double[][] result = new double[heights.length][heights[0].length];
283
284         for (int x = 0; x < heights.length; ++x) {
285             for (int y = 0; y < heights[0].length; ++y) {
286                 result[x][y] = getMinMaxForRegion(heights, x, y, 2)[0];
287             }
288         }
289
290         return result;
291     }
292
293     private double[] getMinMaxForRegion(double[][] field, int x, int y, int dim) {
294         double max = -100d;
295         double min = 100d;
296
297         for (int i = x; i < x + dim; ++i) {
298             for (int j = y; j < y + dim; ++j) {
299                 if (i >= 0 && j >= 0 && i < field.length && j < field[0].length) {
300                     min = field[i][j] < min ? field[i][j] : min;
301                     max = field[i][j] > max ? field[i][j] : max;
302                 }
303             }
304         }
305
306         double[] result = {min, max};
307         return result;
308     }
309
310     public List<VisBlocker> genBlockers(double[][] columns, double delta) {
311
312         VisBlocker[][] blockers = new VisBlocker[columns.length][columns[0].length];
313         LinkedList<VisBlocker> result = new LinkedList<VisBlocker>();
314
315         for (int x = 0; x < columns.length; ++x) {
316             for (int y = 0; y < columns[0].length; ++y) {
317                 if (blockers[x][y] == null && columns[x][y] >= 0) {
318                     // this pixel isn't covered by a blocker yet... so let's create one!
319                     VisBlocker b = new VisBlocker();
320                     result.add(b);
321                     b.x = x;
322                     b.y = y;
323                     b.minheight = b.origheight = columns[x][y];
324
325                     // grow till the delta hits
326                     int dim;
327                     double min = b.minheight;
328                     for (dim = 1; dim < columns.length; ++dim) {
329                         double[] minmax = getMinMaxForRegion(columns, x, y, dim);
330                         if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
331                             break;
332                         }
333                         min = minmax[0];
334
335                     }
336
337                     b.dim = dim - 1;
338                     b.minheight = min;
339
340                     for (int i = x; i < x + b.dim; ++i) {
341                         for (int j = y; j < y + b.dim; ++j) {
342                             if (i >= 0 && j >= 0 && i < blockers.length && j < blockers[0].length) {
343                                 blockers[i][j] = b;
344                                 columns[i][j] = -1337.0;
345                             }
346                         }
347                     }
348
349                 }
350             }
351         }
352         return result;
353     }
354
355     private class Vector3D {
356
357         public double x,  y,  z;
358
359         public Vector3D() {
360             this(0.0, 0.0, 0.0);
361         }
362
363         public Vector3D(double x, double y, double z) {
364             this.x = x;
365             this.y = y;
366             this.z = z;
367         }
368
369         public Vector3D crossproduct(Vector3D p1) {
370             Vector3D result = new Vector3D();
371
372             result.x = this.y * p1.z - this.z * p1.y;
373             result.y = this.z * p1.x - this.x * p1.z;
374             result.z = this.x * p1.y - this.y * p1.x;
375
376             return result;
377         }
378
379         public double dotproduct(Vector3D p1) {
380             return this.x * p1.x + this.y * p1.y + this.z * p1.z;
381         }
382
383         public Vector3D substract(Vector3D p1) {
384             Vector3D result = new Vector3D();
385
386             result.x = this.x - p1.x;
387             result.y = this.y - p1.y;
388             result.z = this.z - p1.z;
389
390             return result;
391         }
392
393         public void scale(double factor) {
394             x *= factor;
395             y *= factor;
396             z *= factor;
397         }
398
399         public double length() {
400             return Math.sqrt((x * x) + (y * y) + (z * z));
401         }
402
403         public void normalize() {
404             double l = length();
405
406             x /= l;
407             y /= l;
408             z /= l;
409         }
410     }
411
412     private class VisBlocker {
413
414         public int x,  y,  dim;
415         public double origheight,  minheight;
416     }
417 }