]> icculus.org git repositories - divverent/nexuiz.git/blob - tools/ImgToMap/src/imgtomap/MapWriter.java
grow vis-blockers in both x and y seperately. This allows for non-square visblockers...
[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                     double x = b.x * units;
168                     double y = (b.y + b.ydim) * units;
169                     x = x > xmax ? xmax : x;
170                     y = y > ymax ? ymax : y;
171                     Vector3D p1 = new Vector3D(x, -y, -32.0);
172
173                     x = (b.x + b.xdim) * units;
174                     y = b.y * units;
175                     x = x > xmax ? xmax : x;
176                     y = y > ymax ? ymax : y;
177                     Vector3D p2 = new Vector3D(x, -y, z);
178
179                     writeBoxBrush(pw, p1, p2, false, "common/caulk", 1.0);
180                 }
181
182             }
183         }
184
185         // worldspawn end
186         pw.print("}\n");
187         pw.close();
188         return 0;
189     }
190
191     private void writeBoxBrush(PrintWriter pw, Vector3D p1, Vector3D p2, boolean detail, String texture, double scale) {
192         Vector3D a = new Vector3D(p1.x, p2.y, p2.z);
193         Vector3D b = p2;
194         Vector3D c = new Vector3D(p1.x, p1.y, p2.z);
195         Vector3D d = new Vector3D(p2.x, p1.y, p2.z);
196         //Vector3D e unused
197         Vector3D f = new Vector3D(p2.x, p2.y, p1.z);
198         Vector3D g = p1;
199         Vector3D h = new Vector3D(p2.x, p1.y, p1.z);
200
201         pw.print("{\n");
202         pw.print(getMapPlaneString(a, b, d, detail, texture, scale));
203         pw.print(getMapPlaneString(d, b, f, detail, texture, scale));
204         pw.print(getMapPlaneString(c, d, h, detail, texture, scale));
205         pw.print(getMapPlaneString(a, c, g, detail, texture, scale));
206         pw.print(getMapPlaneString(f, b, a, detail, texture, scale));
207         pw.print(getMapPlaneString(g, h, f, detail, texture, scale));
208         pw.print("}\n");
209
210     }
211
212     private String getMapPlaneString(Vector3D p1, Vector3D p2, Vector3D p3, boolean detail, String material, double scale) {
213         int flag;
214         if (detail) {
215             flag = 134217728;
216         } else {
217             flag = 0;
218         }
219         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";
220     }
221
222     private double[][] getHeightmap(String file) {
223         try {
224             BufferedImage bimg = ImageIO.read(new File(file));
225             Raster raster = bimg.getRaster();
226             int x = raster.getWidth();
227             int y = raster.getHeight();
228
229             double[][] result = new double[x][y];
230
231             for (int xi = 0; xi < x; ++xi) {
232                 for (int yi = 0; yi < y; ++yi) {
233                     float[] pixel = raster.getPixel(xi, yi, (float[]) null);
234
235                     int channels;
236                     boolean alpha;
237                     if (pixel.length == 3) {
238                         // RGB
239                         channels = 3;
240                         alpha = false;
241                     } else if (pixel.length == 4) {
242                         // RGBA
243                         channels = 3;
244                         alpha = true;
245                     } else if (pixel.length == 1) {
246                         // grayscale
247                         channels = 1;
248                         alpha = false;
249                     } else {
250                         // grayscale with alpha
251                         channels = 1;
252                         alpha = true;
253                     }
254
255                     float tmp = 0f;
256                     for (int i = 0; i < channels; ++i) {
257                         tmp += pixel[i];
258                     }
259                     result[xi][yi] = tmp / (channels * 255f);
260
261                     if (alpha) {
262                         // mark this pixel to be skipped
263                         if (pixel[pixel.length - 1] < 64.0) {
264                             result[xi][yi] = -1.0;
265                         }
266                     }
267                 }
268             }
269
270
271             return result;
272         } catch (IOException ex) {
273             Logger.getLogger(MapWriter.class.getName()).log(Level.SEVERE, null, ex);
274         }
275
276         return null;
277     }
278
279     private double[][] getColumns(double[][] heights) {
280         double[][] result = new double[heights.length][heights[0].length];
281
282         for (int x = 0; x < heights.length; ++x) {
283             for (int y = 0; y < heights[0].length; ++y) {
284                 result[x][y] = getMinMaxForRegion(heights, x, y, 2)[0];
285             }
286         }
287
288         return result;
289     }
290
291     private double[] getMinMaxForRegion(double[][] field, int x, int y, int dim) {
292         return getMinMaxForRegion(field, x, y, dim, dim);
293     }
294
295     private double[] getMinMaxForRegion(double[][] field, int x, int y, int xdim, int ydim) {
296         double max = -100d;
297         double min = 100d;
298
299         for (int i = x; i < x + xdim; ++i) {
300             for (int j = y; j < y + ydim; ++j) {
301                 if (i >= 0 && j >= 0 && i < field.length && j < field[0].length) {
302                     min = field[i][j] < min ? field[i][j] : min;
303                     max = field[i][j] > max ? field[i][j] : max;
304                 }
305             }
306         }
307
308         double[] result = {min, max};
309         return result;
310     }
311
312     public List<VisBlocker> genBlockers(double[][] columns, double delta) {
313
314         VisBlocker[][] blockers = new VisBlocker[columns.length][columns[0].length];
315         LinkedList<VisBlocker> result = new LinkedList<VisBlocker>();
316
317         for (int x = 0; x < columns.length; ++x) {
318             for (int y = 0; y < columns[0].length; ++y) {
319                 if (blockers[x][y] == null && columns[x][y] >= 0) {
320                     // this pixel isn't covered by a blocker yet... so let's create one!
321                     VisBlocker b = new VisBlocker();
322                     result.add(b);
323                     b.x = x;
324                     b.y = y;
325                     b.minheight = b.origheight = columns[x][y];
326
327                     // grow till the delta hits
328                     int xdim = 1;
329                     int ydim = 1;
330                     boolean xgrow = true;
331                     boolean ygrow = true;
332                     double min = b.minheight;
333                     for (; xdim < columns.length && ydim < columns[0].length;) {
334                         double[] minmax = getMinMaxForRegion(columns, x, y, xdim + 1, ydim);
335                         if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
336                             xgrow = false;
337                         }
338
339                         minmax = getMinMaxForRegion(columns, x, y, xdim, ydim + 1);
340                         if (Math.abs(b.origheight - minmax[0]) > delta || Math.abs(b.origheight - minmax[1]) > delta) {
341                             ygrow = false;
342                         }
343
344                         min = minmax[0];
345
346                         if (xgrow) {
347                             ++xdim;
348                         }
349                         if (ygrow) {
350                             ++ydim;
351                         }
352
353                         minmax = getMinMaxForRegion(columns, x, y, xdim, ydim);
354                         min = minmax[0];
355
356                         if (!(xgrow || ygrow)) {
357                             break;
358                         }
359                     }
360
361                     b.xdim = xdim;
362                     b.ydim = ydim;
363                     b.minheight = min;
364
365                     for (int i = x; i < x + b.xdim; ++i) {
366                         for (int j = y; j < y + b.ydim; ++j) {
367                             if (i >= 0 && j >= 0 && i < blockers.length && j < blockers[0].length) {
368                                 blockers[i][j] = b;
369                                 columns[i][j] = -1337.0;
370                             }
371                         }
372                     }
373
374                 }
375             }
376         }
377         return result;
378     }
379
380     private class Vector3D {
381
382         public  double x,    y,    z;
383
384         public Vector3D() {
385             this(0.0, 0.0, 0.0);
386         }
387
388         public Vector3D(double x, double y, double z) {
389             this.x = x;
390             this.y = y;
391             this.z = z;
392         }
393
394         public Vector3D crossproduct(Vector3D p1) {
395             Vector3D result = new Vector3D();
396
397             result.x = this.y * p1.z - this.z * p1.y;
398             result.y = this.z * p1.x - this.x * p1.z;
399             result.z = this.x * p1.y - this.y * p1.x;
400
401             return result;
402         }
403
404         public double dotproduct(Vector3D p1) {
405             return this.x * p1.x + this.y * p1.y + this.z * p1.z;
406         }
407
408         public Vector3D substract(Vector3D p1) {
409             Vector3D result = new Vector3D();
410
411             result.x = this.x - p1.x;
412             result.y = this.y - p1.y;
413             result.z = this.z - p1.z;
414
415             return result;
416         }
417
418         public void scale(double factor) {
419             x *= factor;
420             y *= factor;
421             z *= factor;
422         }
423
424         public double length() {
425             return Math.sqrt((x * x) + (y * y) + (z * z));
426         }
427
428         public void normalize() {
429             double l = length();
430
431             x /= l;
432             y /= l;
433             z /= l;
434         }
435     }
436
437     private class VisBlocker {
438
439         public  int x,    y,    xdim,   ydim;
440         public  double origheight,    minheight;
441     }
442 }