]> icculus.org git repositories - divverent/nexuiz.git/blob - tools/ImgToMap/src/imgtomap/MapWriter.java
put terrain brushes into a func_group
[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         // worldspawn start
45         pw.print("{\n\"classname\" \"worldspawn\"\n");
46
47         double xmax = (columns.length - 1) * units;
48         double ymax = (columns[0].length - 1) * units;
49
50         if (p.skyfill) {
51             List<Block> fillers = genSkyFillers(columns);
52             for (Block b : fillers) {
53                 double x = b.x * units;
54                 double y = (b.y + b.ydim) * units;
55                 x = x > xmax ? xmax : x;
56                 y = y > ymax ? ymax : y;
57                 Vector3D p1 = new Vector3D(x, -y, -32.0);
58
59                 x = (b.x + b.xdim) * units;
60                 y = b.y * units;
61                 x = x > xmax ? xmax : x;
62                 y = y > ymax ? ymax : y;
63                 Vector3D p2 = new Vector3D(x, -y, p.skyheight);
64
65                 writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
66             }
67         }
68
69         if (p.sky) {
70             // generate skybox
71             int x = height.length - 1;
72             int y = height[0].length - 1;
73
74             // top
75             Vector3D p1 = new Vector3D(0, -y * units, p.skyheight);
76             Vector3D p2 = new Vector3D(x * units, 0, p.skyheight + 32.0);
77             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
78
79             // bottom
80             p1 = new Vector3D(0, -y * units, -64.0);
81             p2 = new Vector3D(x * units, 0, -32.0);
82             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
83
84             // north
85             p1 = new Vector3D(0, 0, -32.0);
86             p2 = new Vector3D(x * units, 32, p.skyheight);
87             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
88
89             // east
90             p1 = new Vector3D(x * units, -y * units, -32.0);
91             p2 = new Vector3D(x * units + 32.0, 0, p.skyheight);
92             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
93
94             // south
95             p1 = new Vector3D(0, -y * units - 32, -32.0);
96             p2 = new Vector3D(x * units, -y * units, p.skyheight);
97             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
98
99
100             // west
101             p1 = new Vector3D(0 - 32.0, -y * units, -32.0);
102             p2 = new Vector3D(0, 0, p.skyheight);
103             writeBoxBrush(pw, p1, p2, false, p.skytexture, 1.0);
104
105         }
106
107         // genBlockers screws the columns array!
108         // this should be the last step!
109         if (p.visblockers) {
110             List<Block> blockers = genBlockers(columns, 0.15);
111             for (Block b : blockers) {
112                 double z = b.minheight * p.height - 1;
113                 z = Math.floor(z / 16);
114                 z = z * 16;
115
116                 if (z > 0) {
117                     double x = b.x * units;
118                     double y = (b.y + b.ydim) * units;
119                     x = x > xmax ? xmax : x;
120                     y = y > ymax ? ymax : y;
121                     Vector3D p1 = new Vector3D(x, -y, -32.0);
122
123                     x = (b.x + b.xdim) * units;
124                     y = b.y * units;
125                     x = x > xmax ? xmax : x;
126                     y = y > ymax ? ymax : y;
127                     Vector3D p2 = new Vector3D(x, -y, z);
128
129                     writeBoxBrush(pw, p1, p2, false, "common/caulk", 1.0);
130                 }
131
132             }
133         }
134
135         // worldspawn end
136         pw.print("}\n");
137
138         // func_group start
139         pw.print("{\n\"classname\" \"func_group\"\n");
140         // wander through grid
141         for (int x = 0; x < height.length - 1; ++x) {
142             for (int y = 0; y < height[0].length - 1; ++y) {
143
144                 boolean skip = getMinMaxForRegion(height, x, y, 2)[0] < 0;
145
146                 if (!skip) {
147
148                     /*
149                      * 
150                      *      a +-------+ b
151                      *       /       /|
152                      *      /       / |
153                      *     /       /  |
154                      *  c +-------+ d + f   (e occluded, unused)
155                      *    |       |  /
156                      *    |       | /
157                      *    |       |/
158                      *  g +-------+ h
159                      * 
160                      */
161
162                     Vector3D a = new Vector3D(x * units, -y * units, height[x][y] * max);
163                     Vector3D b = new Vector3D((x + 1) * units, -y * units, height[x + 1][y] * max);
164                     Vector3D c = new Vector3D(x * units, -(y + 1) * units, height[x][y + 1] * max);
165                     Vector3D d = new Vector3D((x + 1) * units, -(y + 1) * units, height[x + 1][y + 1] * max);
166                     //Vector3D e = new Vector3D(x * units, -y * units, -16.0);
167                     Vector3D f = new Vector3D((x + 1) * units, -y * units, -16.0);
168                     Vector3D g = new Vector3D(x * units, -(y + 1) * units, -16.0);
169                     Vector3D h = new Vector3D((x + 1) * units, -(y + 1) * units, -16.0);
170
171                     pw.print("{\n");
172                     pw.print(getMapPlaneString(a, b, d, p.detail, p.texture, p.texturescale));
173                     pw.print(getMapPlaneString(d, b, f, p.detail, "common/caulk", p.texturescale));
174                     pw.print(getMapPlaneString(f, b, a, p.detail, "common/caulk", p.texturescale));
175                     pw.print(getMapPlaneString(a, d, h, p.detail, "common/caulk", p.texturescale));
176                     pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
177                     pw.print("}\n");
178
179
180                     pw.print("{\n");
181                     pw.print(getMapPlaneString(d, c, a, p.detail, p.texture, p.texturescale));
182                     pw.print(getMapPlaneString(g, c, d, p.detail, "common/caulk", p.texturescale));
183                     pw.print(getMapPlaneString(c, g, a, p.detail, "common/caulk", p.texturescale));
184                     pw.print(getMapPlaneString(h, d, a, p.detail, "common/caulk", p.texturescale));
185                     pw.print(getMapPlaneString(g, h, f, p.detail, "common/caulk", p.texturescale));
186                     pw.print("}\n");
187                 }
188             }
189         }
190         // func_group end
191         pw.print("}\n");
192
193         pw.close();
194         return 0;
195     }
196
197     private void writeBoxBrush(PrintWriter pw, Vector3D p1, Vector3D p2, boolean detail, String texture, double scale) {
198         Vector3D a = new Vector3D(p1.x, p2.y, p2.z);
199         Vector3D b = p2;
200         Vector3D c = new Vector3D(p1.x, p1.y, p2.z);
201         Vector3D d = new Vector3D(p2.x, p1.y, p2.z);
202         //Vector3D e unused
203         Vector3D f = new Vector3D(p2.x, p2.y, p1.z);
204         Vector3D g = p1;
205         Vector3D h = new Vector3D(p2.x, p1.y, p1.z);
206
207         pw.print("{\n");
208         pw.print(getMapPlaneString(a, b, d, detail, texture, scale));
209         pw.print(getMapPlaneString(d, b, f, detail, texture, scale));
210         pw.print(getMapPlaneString(c, d, h, detail, texture, scale));
211         pw.print(getMapPlaneString(a, c, g, detail, texture, scale));
212         pw.print(getMapPlaneString(f, b, a, detail, texture, scale));
213         pw.print(getMapPlaneString(g, h, f, detail, texture, scale));
214         pw.print("}\n");
215
216     }
217
218     private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, boolean detail, String material, double scale) {
219         int flag;
220         if (detail) {
221             flag = 134217728;
222         } else {
223             flag = 0;
224         }
225         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";
226     }
227
228     private double[][] getHeightmap(String file) {
229         try {
230             BufferedImage bimg = ImageIO.read(new File(file));
231             Raster raster = bimg.getRaster();
232             int x = raster.getWidth();
233             int y = raster.getHeight();
234
235             double[][] result = new double[x][y];
236
237             for (int xi = 0; xi < x; ++xi) {
238                 for (int yi = 0; yi < y; ++yi) {
239                     float[] pixel = raster.getPixel(xi, yi, (float[]) null);
240
241                     int channels;
242                     boolean alpha;
243                     if (pixel.length == 3) {
244                         // RGB
245                         channels = 3;
246                         alpha = false;
247                     } else if (pixel.length == 4) {
248                         // RGBA
249                         channels = 3;
250                         alpha = true;
251                     } else if (pixel.length == 1) {
252                         // grayscale
253                         channels = 1;
254                         alpha = false;
255                     } else {
256                         // grayscale with alpha
257                         channels = 1;
258                         alpha = true;
259                     }
260
261                     float tmp = 0f;
262                     for (int i = 0; i < channels; ++i) {
263                         tmp += pixel[i];
264                     }
265                     result[xi][yi] = tmp / (channels * 255f);
266
267                     if (alpha) {
268                         // mark this pixel to be skipped
269                         if (pixel[pixel.length - 1] < 64.0) {
270                             result[xi][yi] = -1.0;
271                         }
272                     }
273                 }
274             }
275
276
277             return result;
278         } catch (IOException ex) {
279             Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
280         }
281
282         return null;
283     }
284
285     private double[][] getColumns(double[][] heights) {
286         double[][] result = new double[heights.length][heights[0].length];
287
288         for (int x = 0; x < heights.length; ++x) {
289             for (int y = 0; y < heights[0].length; ++y) {
290                 result[x][y] = getMinMaxForRegion(heights, x, y, 2)[0];
291             }
292         }
293
294         return result;
295     }
296
297     private double[] getMinMaxForRegion(double[][] field, int x, int y, int dim) {
298         return getMinMaxForRegion(field, x, y, dim, dim);
299     }
300
301     private double[] getMinMaxForRegion(double[][] field, int x, int y, int xdim, int ydim) {
302         double max = -100d;
303         double min = 100d;
304
305         for (int i = x; i < x + xdim; ++i) {
306             for (int j = y; j < y + ydim; ++j) {
307                 if (i >= 0 && j >= 0 && i < field.length && j < field[0].length) {
308                     min = field[i][j] < min ? field[i][j] : min;
309                     max = field[i][j] > max ? field[i][j] : max;
310                 }
311             }
312         }
313
314         double[] result = {min, max};
315         return result;
316     }
317
318     private List<Block> genBlockers(double[][] columns, double delta) {
319
320         Block[][] blockers = new Block[columns.length][columns[0].length];
321         LinkedList<Block> result = new LinkedList<Block>();
322
323         for (int x = 0; x < columns.length; ++x) {
324             for (int y = 0; y < columns[0].length; ++y) {
325                 if (blockers[x][y] == null && columns[x][y] >= 0) {
326                     // this pixel isn't covered by a blocker yet... so let's create one!
327                     Block b = new Block();
328                     result.add(b);
329                     b.x = x;
330                     b.y = y;
331                     b.minheight = b.origheight = columns[x][y];
332
333                     // grow till the delta hits
334                     int xdim = 1;
335                     int ydim = 1;
336                     boolean xgrow = true;
337                     boolean ygrow = true;
338                     double min = b.minheight;
339                     for (; xdim < columns.length && ydim < columns[0].length;) {
340                         double[] minmax = getMinMaxForRegion(columns, x, y, xdim + 1, ydim);
341                         if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
342                             xgrow = false;
343                         }
344
345                         minmax = getMinMaxForRegion(columns, x, y, xdim, ydim + 1);
346                         if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
347                             ygrow = false;
348                         }
349
350                         min = minmax[0];
351
352                         if (xgrow) {
353                             ++xdim;
354                         }
355                         if (ygrow) {
356                             ++ydim;
357                         }
358
359                         minmax = getMinMaxForRegion(columns, x, y, xdim, ydim);
360                         min = minmax[0];
361
362                         if (!(xgrow || ygrow)) {
363                             break;
364                         }
365                     }
366
367                     b.xdim = xdim;
368                     b.ydim = ydim;
369                     b.minheight = min;
370
371                     for (int i = x; i < x + b.xdim; ++i) {
372                         for (int j = y; j < y + b.ydim; ++j) {
373                             if (i >= 0 && j >= 0 && i < blockers.length && j < blockers[0].length) {
374                                 blockers[i][j] = b;
375                                 columns[i][j] = -1337.0;
376                             }
377                         }
378                     }
379
380                 }
381             }
382         }
383         return result;
384     }
385
386     private List<Block> genSkyFillers(double[][] columns) {
387
388         double delta = 0;
389
390         for (int x = 0; x < columns.length; ++x) {
391             for (int y = 0; y < columns[0].length; ++y) {
392                 if (columns[x][y] < 0) {
393                     // this is a skipped block, see if it neighbours a
394                     // relevant block
395                     if (getMinMaxForRegion(columns, x - 1, y - 1, 3)[1] >= 0) {
396                         columns[x][y] = -100d;
397                     }
398                 }
399             }
400         }
401
402
403         Block[][] fillers = new Block[columns.length][columns[0].length];
404         LinkedList<Block> result = new LinkedList<Block>();
405
406         for (int x = 0; x < columns.length; ++x) {
407             for (int y = 0; y < columns[0].length; ++y) {
408                 if (fillers[x][y] == null && columns[x][y] == -100d) {
409                     // this pixel is marked to be skyfill
410                     Block b = new Block();
411                     result.add(b);
412                     b.x = x;
413                     b.y = y;
414                     b.minheight = b.origheight = columns[x][y];
415
416                     // grow till the delta hits
417                     int xdim = 1;
418                     int ydim = 1;
419                     boolean xgrow = true;
420                     boolean ygrow = true;
421                     double min = b.minheight;
422                     for (; xdim < columns.length && ydim < columns[0].length;) {
423                         double[] minmax = getMinMaxForRegion(columns, x, y, xdim + 1, ydim);
424                         if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
425                             xgrow = false;
426                         }
427
428                         minmax = getMinMaxForRegion(columns, x, y, xdim, ydim + 1);
429                         if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
430                             ygrow = false;
431                         }
432
433                         min = minmax[0];
434
435                         if (xgrow) {
436                             ++xdim;
437                         }
438                         if (ygrow) {
439                             ++ydim;
440                         }
441
442                         minmax = getMinMaxForRegion(columns, x, y, xdim, ydim);
443                         min = minmax[0];
444
445                         if (!(xgrow || ygrow)) {
446                             break;
447                         }
448                     }
449
450                     b.xdim = xdim;
451                     b.ydim = ydim;
452                     b.minheight = min;
453
454                     for (int i = x; i < x + b.xdim; ++i) {
455                         for (int j = y; j < y + b.ydim; ++j) {
456                             if (i >= 0 && j >= 0 && i < fillers.length && j < fillers[0].length) {
457                                 fillers[i][j] = b;
458                                 columns[i][j] = -1337.0;
459                             }
460                         }
461                     }
462
463                 }
464             }
465         }
466         return result;
467     }
468
469     private class Vector3D {
470
471         public double x,  y,  z;
472
473         public Vector3D() {
474             this(0.0, 0.0, 0.0);
475         }
476
477         public Vector3D(double x, double y, double z) {
478             this.x = x;
479             this.y = y;
480             this.z = z;
481         }
482
483         public Vector3D crossproduct(Vector3D p1) {
484             Vector3D result = new Vector3D();
485
486             result.x = this.y * p1.z - this.z * p1.y;
487             result.y = this.z * p1.x - this.x * p1.z;
488             result.z = this.x * p1.y - this.y * p1.x;
489
490             return result;
491         }
492
493         public double dotproduct(Vector3D p1) {
494             return this.x * p1.x + this.y * p1.y + this.z * p1.z;
495         }
496
497         public Vector3D substract(Vector3D p1) {
498             Vector3D result = new Vector3D();
499
500             result.x = this.x - p1.x;
501             result.y = this.y - p1.y;
502             result.z = this.z - p1.z;
503
504             return result;
505         }
506
507         public void scale(double factor) {
508             x *= factor;
509             y *= factor;
510             z *= factor;
511         }
512
513         public double length() {
514             return Math.sqrt((x * x) + (y * y) + (z * z));
515         }
516
517         public void normalize() {
518             double l = length();
519
520             x /= l;
521             y /= l;
522             z /= l;
523         }
524     }
525
526     private class Block {
527
528         public int x,  y,  xdim,  ydim;
529         public double origheight,  minheight;
530     }
531 }