]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_mapblock.cpp
Facedir rotation of nodebox textures
[dragonfireclient.git] / src / content_mapblock.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "content_mapblock.h"
21
22 #include "main.h" // For g_settings
23 #include "mapblock_mesh.h" // For MapBlock_LightColor() and MeshCollector
24 #include "settings.h"
25 #include "nodedef.h"
26 #include "tile.h"
27 #include "gamedef.h"
28 #include "util/numeric.h"
29 #include "util/serialize.h"
30 #include "util/directiontables.h"
31
32 // Create a cuboid.
33 //  collector - the MeshCollector for the resulting polygons
34 //  box       - the position and size of the box
35 //  tiles     - the tiles (materials) to use (for all 6 faces)
36 //  tilecount - number of entries in tiles, 1<=tilecount<=6
37 //  c         - vertex colour - used for all
38 //  txc       - texture coordinates - this is a list of texture coordinates
39 //              for the opposite corners of each face - therefore, there
40 //              should be (2+2)*6=24 values in the list. Alternatively, pass
41 //              NULL to use the entire texture for each face. The order of
42 //              the faces in the list is up-down-right-left-back-front
43 //              (compatible with ContentFeatures). If you specified 0,0,1,1
44 //              for each face, that would be the same as passing NULL.
45 void makeCuboid(MeshCollector *collector, const aabb3f &box,
46         const TileSpec *tiles, int tilecount,
47         video::SColor &c, const f32* txc)
48 {
49         assert(tilecount >= 1 && tilecount <= 6);
50
51         v3f min = box.MinEdge;
52         v3f max = box.MaxEdge;
53
54         if(txc == NULL)
55         {
56                 static const f32 txc_default[24] = {
57                         0,0,1,1,
58                         0,0,1,1,
59                         0,0,1,1,
60                         0,0,1,1,
61                         0,0,1,1,
62                         0,0,1,1
63                 };
64                 txc = txc_default;
65         }
66
67         video::S3DVertex vertices[24] =
68         {
69                 // up
70                 video::S3DVertex(min.X,max.Y,max.Z, 0,1,0, c, txc[0],txc[1]),
71                 video::S3DVertex(max.X,max.Y,max.Z, 0,1,0, c, txc[2],txc[1]),
72                 video::S3DVertex(max.X,max.Y,min.Z, 0,1,0, c, txc[2],txc[3]),
73                 video::S3DVertex(min.X,max.Y,min.Z, 0,1,0, c, txc[0],txc[3]),
74                 // down
75                 video::S3DVertex(min.X,min.Y,min.Z, 0,-1,0, c, txc[4],txc[5]),
76                 video::S3DVertex(max.X,min.Y,min.Z, 0,-1,0, c, txc[6],txc[5]),
77                 video::S3DVertex(max.X,min.Y,max.Z, 0,-1,0, c, txc[6],txc[7]),
78                 video::S3DVertex(min.X,min.Y,max.Z, 0,-1,0, c, txc[4],txc[7]),
79                 // right
80                 video::S3DVertex(max.X,max.Y,min.Z, 1,0,0, c, txc[ 8],txc[9]),
81                 video::S3DVertex(max.X,max.Y,max.Z, 1,0,0, c, txc[10],txc[9]),
82                 video::S3DVertex(max.X,min.Y,max.Z, 1,0,0, c, txc[10],txc[11]),
83                 video::S3DVertex(max.X,min.Y,min.Z, 1,0,0, c, txc[ 8],txc[11]),
84                 // left
85                 video::S3DVertex(min.X,max.Y,max.Z, -1,0,0, c, txc[12],txc[13]),
86                 video::S3DVertex(min.X,max.Y,min.Z, -1,0,0, c, txc[14],txc[13]),
87                 video::S3DVertex(min.X,min.Y,min.Z, -1,0,0, c, txc[14],txc[15]),
88                 video::S3DVertex(min.X,min.Y,max.Z, -1,0,0, c, txc[12],txc[15]),
89                 // back
90                 video::S3DVertex(max.X,max.Y,max.Z, 0,0,1, c, txc[16],txc[17]),
91                 video::S3DVertex(min.X,max.Y,max.Z, 0,0,1, c, txc[18],txc[17]),
92                 video::S3DVertex(min.X,min.Y,max.Z, 0,0,1, c, txc[18],txc[19]),
93                 video::S3DVertex(max.X,min.Y,max.Z, 0,0,1, c, txc[16],txc[19]),
94                 // front
95                 video::S3DVertex(min.X,max.Y,min.Z, 0,0,-1, c, txc[20],txc[21]),
96                 video::S3DVertex(max.X,max.Y,min.Z, 0,0,-1, c, txc[22],txc[21]),
97                 video::S3DVertex(max.X,min.Y,min.Z, 0,0,-1, c, txc[22],txc[23]),
98                 video::S3DVertex(min.X,min.Y,min.Z, 0,0,-1, c, txc[20],txc[23]),
99         };
100
101         for(s32 j=0; j<24; j++)
102         {
103                 int tileindex = MYMIN(j/4, tilecount-1);
104                 vertices[j].TCoords *= tiles[tileindex].texture.size;
105                 vertices[j].TCoords += tiles[tileindex].texture.pos;
106         }
107
108         u16 indices[] = {0,1,2,2,3,0};
109
110         // Add to mesh collector
111         for(s32 j=0; j<24; j+=4)
112         {
113                 int tileindex = MYMIN(j/4, tilecount-1);
114                 collector->append(tiles[tileindex],
115                                 vertices+j, 4, indices, 6);
116         }
117 }
118
119 void mapblock_mesh_generate_special(MeshMakeData *data,
120                 MeshCollector &collector)
121 {
122         INodeDefManager *nodedef = data->m_gamedef->ndef();
123
124         // 0ms
125         //TimeTaker timer("mapblock_mesh_generate_special()");
126
127         /*
128                 Some settings
129         */
130         bool new_style_water = g_settings->getBool("new_style_water");
131         
132         float node_liquid_level = 1.0;
133         if(new_style_water)
134                 node_liquid_level = 0.85;
135         
136         v3s16 blockpos_nodes = data->m_blockpos*MAP_BLOCKSIZE;
137
138         for(s16 z=0; z<MAP_BLOCKSIZE; z++)
139         for(s16 y=0; y<MAP_BLOCKSIZE; y++)
140         for(s16 x=0; x<MAP_BLOCKSIZE; x++)
141         {
142                 v3s16 p(x,y,z);
143
144                 MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes+p);
145                 const ContentFeatures &f = nodedef->get(n);
146
147                 // Only solidness=0 stuff is drawn here
148                 if(f.solidness != 0)
149                         continue;
150                 
151                 switch(f.drawtype){
152                 default:
153                         infostream<<"Got "<<f.drawtype<<std::endl;
154                         assert(0);
155                         break;
156                 case NDT_AIRLIKE:
157                         break;
158                 case NDT_LIQUID:
159                 {
160                         /*
161                                 Add water sources to mesh if using new style
162                         */
163                         TileSpec tile_liquid = f.special_tiles[0];
164                         AtlasPointer &pa_liquid = tile_liquid.texture;
165
166                         bool top_is_air = false;
167                         MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
168                         if(n.getContent() == CONTENT_AIR)
169                                 top_is_air = true;
170                         
171                         if(top_is_air == false)
172                                 continue;
173
174                         u16 l = getInteriorLight(n, 0, data);
175                         video::SColor c = MapBlock_LightColor(f.alpha, l);
176                         
177                         video::S3DVertex vertices[4] =
178                         {
179                                 video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
180                                                 pa_liquid.x0(), pa_liquid.y1()),
181                                 video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
182                                                 pa_liquid.x1(), pa_liquid.y1()),
183                                 video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
184                                                 pa_liquid.x1(), pa_liquid.y0()),
185                                 video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
186                                                 pa_liquid.x0(), pa_liquid.y0()),
187                         };
188
189                         v3f offset(p.X, p.Y + (-0.5+node_liquid_level)*BS, p.Z);
190                         for(s32 i=0; i<4; i++)
191                         {
192                                 vertices[i].Pos += offset;
193                         }
194
195                         u16 indices[] = {0,1,2,2,3,0};
196                         // Add to mesh collector
197                         collector.append(tile_liquid, vertices, 4, indices, 6);
198                 break;}
199                 case NDT_FLOWINGLIQUID:
200                 {
201                         /*
202                                 Add flowing liquid to mesh
203                         */
204                         TileSpec tile_liquid = f.special_tiles[0];
205                         TileSpec tile_liquid_bfculled = f.special_tiles[1];
206                         AtlasPointer &pa_liquid = tile_liquid.texture;
207
208                         bool top_is_same_liquid = false;
209                         MapNode ntop = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y+1,z));
210                         content_t c_flowing = nodedef->getId(f.liquid_alternative_flowing);
211                         content_t c_source = nodedef->getId(f.liquid_alternative_source);
212                         if(ntop.getContent() == c_flowing || ntop.getContent() == c_source)
213                                 top_is_same_liquid = true;
214                         
215                         u16 l = 0;
216                         // If this liquid emits light and doesn't contain light, draw
217                         // it at what it emits, for an increased effect
218                         u8 light_source = nodedef->get(n).light_source;
219                         if(light_source != 0){
220                                 //l = decode_light(undiminish_light(light_source));
221                                 l = decode_light(light_source);
222                                 l = l | (l<<8);
223                         }
224                         // Use the light of the node on top if possible
225                         else if(nodedef->get(ntop).param_type == CPT_LIGHT)
226                                 l = getInteriorLight(ntop, 0, data);
227                         // Otherwise use the light of this node (the liquid)
228                         else
229                                 l = getInteriorLight(n, 0, data);
230                         video::SColor c = MapBlock_LightColor(f.alpha, l);
231                         
232                         // Neighbor liquid levels (key = relative position)
233                         // Includes current node
234                         core::map<v3s16, f32> neighbor_levels;
235                         core::map<v3s16, content_t> neighbor_contents;
236                         core::map<v3s16, u8> neighbor_flags;
237                         const u8 neighborflag_top_is_same_liquid = 0x01;
238                         v3s16 neighbor_dirs[9] = {
239                                 v3s16(0,0,0),
240                                 v3s16(0,0,1),
241                                 v3s16(0,0,-1),
242                                 v3s16(1,0,0),
243                                 v3s16(-1,0,0),
244                                 v3s16(1,0,1),
245                                 v3s16(-1,0,-1),
246                                 v3s16(1,0,-1),
247                                 v3s16(-1,0,1),
248                         };
249                         for(u32 i=0; i<9; i++)
250                         {
251                                 content_t content = CONTENT_AIR;
252                                 float level = -0.5 * BS;
253                                 u8 flags = 0;
254                                 // Check neighbor
255                                 v3s16 p2 = p + neighbor_dirs[i];
256                                 MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
257                                 if(n2.getContent() != CONTENT_IGNORE)
258                                 {
259                                         content = n2.getContent();
260
261                                         if(n2.getContent() == c_source)
262                                                 level = (-0.5+node_liquid_level) * BS;
263                                         else if(n2.getContent() == c_flowing)
264                                                 level = (-0.5 + ((float)(n2.param2&LIQUID_LEVEL_MASK)
265                                                                 + 0.5) / 8.0 * node_liquid_level) * BS;
266
267                                         // Check node above neighbor.
268                                         // NOTE: This doesn't get executed if neighbor
269                                         //       doesn't exist
270                                         p2.Y += 1;
271                                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
272                                         if(n2.getContent() == c_source ||
273                                                         n2.getContent() == c_flowing)
274                                                 flags |= neighborflag_top_is_same_liquid;
275                                 }
276                                 
277                                 neighbor_levels.insert(neighbor_dirs[i], level);
278                                 neighbor_contents.insert(neighbor_dirs[i], content);
279                                 neighbor_flags.insert(neighbor_dirs[i], flags);
280                         }
281
282                         // Corner heights (average between four liquids)
283                         f32 corner_levels[4];
284                         
285                         v3s16 halfdirs[4] = {
286                                 v3s16(0,0,0),
287                                 v3s16(1,0,0),
288                                 v3s16(1,0,1),
289                                 v3s16(0,0,1),
290                         };
291                         for(u32 i=0; i<4; i++)
292                         {
293                                 v3s16 cornerdir = halfdirs[i];
294                                 float cornerlevel = 0;
295                                 u32 valid_count = 0;
296                                 u32 air_count = 0;
297                                 for(u32 j=0; j<4; j++)
298                                 {
299                                         v3s16 neighbordir = cornerdir - halfdirs[j];
300                                         content_t content = neighbor_contents[neighbordir];
301                                         // If top is liquid, draw starting from top of node
302                                         if(neighbor_flags[neighbordir] &
303                                                         neighborflag_top_is_same_liquid)
304                                         {
305                                                 cornerlevel = 0.5*BS;
306                                                 valid_count = 1;
307                                                 break;
308                                         }
309                                         // Source is always the same height
310                                         else if(content == c_source)
311                                         {
312                                                 cornerlevel = (-0.5+node_liquid_level)*BS;
313                                                 valid_count = 1;
314                                                 break;
315                                         }
316                                         // Flowing liquid has level information
317                                         else if(content == c_flowing)
318                                         {
319                                                 cornerlevel += neighbor_levels[neighbordir];
320                                                 valid_count++;
321                                         }
322                                         else if(content == CONTENT_AIR)
323                                         {
324                                                 air_count++;
325                                         }
326                                 }
327                                 if(air_count >= 2)
328                                         cornerlevel = -0.5*BS;
329                                 else if(valid_count > 0)
330                                         cornerlevel /= valid_count;
331                                 corner_levels[i] = cornerlevel;
332                         }
333
334                         /*
335                                 Generate sides
336                         */
337
338                         v3s16 side_dirs[4] = {
339                                 v3s16(1,0,0),
340                                 v3s16(-1,0,0),
341                                 v3s16(0,0,1),
342                                 v3s16(0,0,-1),
343                         };
344                         s16 side_corners[4][2] = {
345                                 {1, 2},
346                                 {3, 0},
347                                 {2, 3},
348                                 {0, 1},
349                         };
350                         for(u32 i=0; i<4; i++)
351                         {
352                                 v3s16 dir = side_dirs[i];
353
354                                 /*
355                                         If our topside is liquid and neighbor's topside
356                                         is liquid, don't draw side face
357                                 */
358                                 if(top_is_same_liquid &&
359                                                 neighbor_flags[dir] & neighborflag_top_is_same_liquid)
360                                         continue;
361
362                                 content_t neighbor_content = neighbor_contents[dir];
363                                 const ContentFeatures &n_feat = nodedef->get(neighbor_content);
364                                 
365                                 // Don't draw face if neighbor is blocking the view
366                                 if(n_feat.solidness == 2)
367                                         continue;
368                                 
369                                 bool neighbor_is_same_liquid = (neighbor_content == c_source
370                                                 || neighbor_content == c_flowing);
371                                 
372                                 // Don't draw any faces if neighbor same is liquid and top is
373                                 // same liquid
374                                 if(neighbor_is_same_liquid == true
375                                                 && top_is_same_liquid == false)
376                                         continue;
377
378                                 // Use backface culled material if neighbor doesn't have a
379                                 // solidness of 0
380                                 const TileSpec *current_tile = &tile_liquid;
381                                 if(n_feat.solidness != 0 || n_feat.visual_solidness != 0)
382                                         current_tile = &tile_liquid_bfculled;
383                                 
384                                 video::S3DVertex vertices[4] =
385                                 {
386                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
387                                                         pa_liquid.x0(), pa_liquid.y1()),
388                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
389                                                         pa_liquid.x1(), pa_liquid.y1()),
390                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
391                                                         pa_liquid.x1(), pa_liquid.y0()),
392                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
393                                                         pa_liquid.x0(), pa_liquid.y0()),
394                                 };
395                                 
396                                 /*
397                                         If our topside is liquid, set upper border of face
398                                         at upper border of node
399                                 */
400                                 if(top_is_same_liquid)
401                                 {
402                                         vertices[2].Pos.Y = 0.5*BS;
403                                         vertices[3].Pos.Y = 0.5*BS;
404                                 }
405                                 /*
406                                         Otherwise upper position of face is corner levels
407                                 */
408                                 else
409                                 {
410                                         vertices[2].Pos.Y = corner_levels[side_corners[i][0]];
411                                         vertices[3].Pos.Y = corner_levels[side_corners[i][1]];
412                                 }
413                                 
414                                 /*
415                                         If neighbor is liquid, lower border of face is corner
416                                         liquid levels
417                                 */
418                                 if(neighbor_is_same_liquid)
419                                 {
420                                         vertices[0].Pos.Y = corner_levels[side_corners[i][1]];
421                                         vertices[1].Pos.Y = corner_levels[side_corners[i][0]];
422                                 }
423                                 /*
424                                         If neighbor is not liquid, lower border of face is
425                                         lower border of node
426                                 */
427                                 else
428                                 {
429                                         vertices[0].Pos.Y = -0.5*BS;
430                                         vertices[1].Pos.Y = -0.5*BS;
431                                 }
432                                 
433                                 for(s32 j=0; j<4; j++)
434                                 {
435                                         if(dir == v3s16(0,0,1))
436                                                 vertices[j].Pos.rotateXZBy(0);
437                                         if(dir == v3s16(0,0,-1))
438                                                 vertices[j].Pos.rotateXZBy(180);
439                                         if(dir == v3s16(-1,0,0))
440                                                 vertices[j].Pos.rotateXZBy(90);
441                                         if(dir == v3s16(1,0,-0))
442                                                 vertices[j].Pos.rotateXZBy(-90);
443                                                 
444                                         // Do this to not cause glitches when two liquids are
445                                         // side-by-side
446                                         /*if(neighbor_is_same_liquid == false){
447                                                 vertices[j].Pos.X *= 0.98;
448                                                 vertices[j].Pos.Z *= 0.98;
449                                         }*/
450
451                                         vertices[j].Pos += intToFloat(p, BS);
452                                 }
453
454                                 u16 indices[] = {0,1,2,2,3,0};
455                                 // Add to mesh collector
456                                 collector.append(*current_tile, vertices, 4, indices, 6);
457                         }
458                         
459                         /*
460                                 Generate top side, if appropriate
461                         */
462                         
463                         if(top_is_same_liquid == false)
464                         {
465                                 video::S3DVertex vertices[4] =
466                                 {
467                                         video::S3DVertex(-BS/2,0,BS/2, 0,0,0, c,
468                                                         pa_liquid.x0(), pa_liquid.y1()),
469                                         video::S3DVertex(BS/2,0,BS/2, 0,0,0, c,
470                                                         pa_liquid.x1(), pa_liquid.y1()),
471                                         video::S3DVertex(BS/2,0,-BS/2, 0,0,0, c,
472                                                         pa_liquid.x1(), pa_liquid.y0()),
473                                         video::S3DVertex(-BS/2,0,-BS/2, 0,0,0, c,
474                                                         pa_liquid.x0(), pa_liquid.y0()),
475                                 };
476                                 
477                                 // To get backface culling right, the vertices need to go
478                                 // clockwise around the front of the face. And we happened to
479                                 // calculate corner levels in exact reverse order.
480                                 s32 corner_resolve[4] = {3,2,1,0};
481
482                                 for(s32 i=0; i<4; i++)
483                                 {
484                                         //vertices[i].Pos.Y += liquid_level;
485                                         //vertices[i].Pos.Y += neighbor_levels[v3s16(0,0,0)];
486                                         s32 j = corner_resolve[i];
487                                         vertices[i].Pos.Y += corner_levels[j];
488                                         vertices[i].Pos += intToFloat(p, BS);
489                                 }
490                                 
491                                 // Default downwards-flowing texture animation goes from 
492                                 // -Z towards +Z, thus the direction is +Z.
493                                 // Rotate texture to make animation go in flow direction
494                                 // Positive if liquid moves towards +Z
495                                 int dz = (corner_levels[side_corners[2][0]] +
496                                                 corner_levels[side_corners[2][1]] <
497                                                 corner_levels[side_corners[3][0]] +
498                                                 corner_levels[side_corners[3][1]]);
499                                 // Positive if liquid moves towards +X
500                                 int dx = (corner_levels[side_corners[0][0]] +
501                                                 corner_levels[side_corners[0][1]] <
502                                                 corner_levels[side_corners[1][0]] +
503                                                 corner_levels[side_corners[1][1]]);
504                                 // -X
505                                 if(-dx >= abs(dz))
506                                 {
507                                         v2f t = vertices[0].TCoords;
508                                         vertices[0].TCoords = vertices[1].TCoords;
509                                         vertices[1].TCoords = vertices[2].TCoords;
510                                         vertices[2].TCoords = vertices[3].TCoords;
511                                         vertices[3].TCoords = t;
512                                 }
513                                 // +X
514                                 if(dx >= abs(dz))
515                                 {
516                                         v2f t = vertices[0].TCoords;
517                                         vertices[0].TCoords = vertices[3].TCoords;
518                                         vertices[3].TCoords = vertices[2].TCoords;
519                                         vertices[2].TCoords = vertices[1].TCoords;
520                                         vertices[1].TCoords = t;
521                                 }
522                                 // -Z
523                                 if(-dz >= abs(dx))
524                                 {
525                                         v2f t = vertices[0].TCoords;
526                                         vertices[0].TCoords = vertices[3].TCoords;
527                                         vertices[3].TCoords = vertices[2].TCoords;
528                                         vertices[2].TCoords = vertices[1].TCoords;
529                                         vertices[1].TCoords = t;
530                                         t = vertices[0].TCoords;
531                                         vertices[0].TCoords = vertices[3].TCoords;
532                                         vertices[3].TCoords = vertices[2].TCoords;
533                                         vertices[2].TCoords = vertices[1].TCoords;
534                                         vertices[1].TCoords = t;
535                                 }
536
537                                 u16 indices[] = {0,1,2,2,3,0};
538                                 // Add to mesh collector
539                                 collector.append(tile_liquid, vertices, 4, indices, 6);
540                         }
541                 break;}
542                 case NDT_GLASSLIKE:
543                 {
544                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
545                         AtlasPointer ap = tile.texture;
546
547                         u16 l = getInteriorLight(n, 1, data);
548                         video::SColor c = MapBlock_LightColor(255, l);
549
550                         for(u32 j=0; j<6; j++)
551                         {
552                                 // Check this neighbor
553                                 v3s16 n2p = blockpos_nodes + p + g_6dirs[j];
554                                 MapNode n2 = data->m_vmanip.getNodeNoEx(n2p);
555                                 // Don't make face if neighbor is of same type
556                                 if(n2.getContent() == n.getContent())
557                                         continue;
558
559                                 // The face at Z+
560                                 video::S3DVertex vertices[4] =
561                                 {
562                                         video::S3DVertex(-BS/2,-BS/2,BS/2, 0,0,0, c,
563                                                 ap.x0(), ap.y1()),
564                                         video::S3DVertex(BS/2,-BS/2,BS/2, 0,0,0, c,
565                                                 ap.x1(), ap.y1()),
566                                         video::S3DVertex(BS/2,BS/2,BS/2, 0,0,0, c,
567                                                 ap.x1(), ap.y0()),
568                                         video::S3DVertex(-BS/2,BS/2,BS/2, 0,0,0, c,
569                                                 ap.x0(), ap.y0()),
570                                 };
571                                 
572                                 // Rotations in the g_6dirs format
573                                 if(j == 0) // Z+
574                                         for(u16 i=0; i<4; i++)
575                                                 vertices[i].Pos.rotateXZBy(0);
576                                 else if(j == 1) // Y+
577                                         for(u16 i=0; i<4; i++)
578                                                 vertices[i].Pos.rotateYZBy(-90);
579                                 else if(j == 2) // X+
580                                         for(u16 i=0; i<4; i++)
581                                                 vertices[i].Pos.rotateXZBy(-90);
582                                 else if(j == 3) // Z-
583                                         for(u16 i=0; i<4; i++)
584                                                 vertices[i].Pos.rotateXZBy(180);
585                                 else if(j == 4) // Y-
586                                         for(u16 i=0; i<4; i++)
587                                                 vertices[i].Pos.rotateYZBy(90);
588                                 else if(j == 5) // X-
589                                         for(u16 i=0; i<4; i++)
590                                                 vertices[i].Pos.rotateXZBy(90);
591
592                                 for(u16 i=0; i<4; i++){
593                                         vertices[i].Pos += intToFloat(p, BS);
594                                 }
595
596                                 u16 indices[] = {0,1,2,2,3,0};
597                                 // Add to mesh collector
598                                 collector.append(tile, vertices, 4, indices, 6);
599                         }
600                 break;}
601                 case NDT_ALLFACES:
602                 {
603                         TileSpec tile_leaves = getNodeTile(n, p,
604                                         v3s16(0,0,0), data);
605                         AtlasPointer pa_leaves = tile_leaves.texture;
606
607                         u16 l = getInteriorLight(n, 1, data);
608                         video::SColor c = MapBlock_LightColor(255, l);
609
610                         v3f pos = intToFloat(p, BS);
611                         aabb3f box(-BS/2,-BS/2,-BS/2,BS/2,BS/2,BS/2);
612                         box.MinEdge += pos;
613                         box.MaxEdge += pos;
614                         makeCuboid(&collector, box, &tile_leaves, 1, c, NULL);
615                 break;}
616                 case NDT_ALLFACES_OPTIONAL:
617                         // This is always pre-converted to something else
618                         assert(0);
619                         break;
620                 case NDT_TORCHLIKE:
621                 {
622                         v3s16 dir = n.getWallMountedDir(nodedef);
623                         
624                         u8 tileindex = 0;
625                         if(dir == v3s16(0,-1,0)){
626                                 tileindex = 0; // floor
627                         } else if(dir == v3s16(0,1,0)){
628                                 tileindex = 1; // ceiling
629                         // For backwards compatibility
630                         } else if(dir == v3s16(0,0,0)){
631                                 tileindex = 0; // floor
632                         } else {
633                                 tileindex = 2; // side
634                         }
635
636                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
637                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
638                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
639
640                         AtlasPointer ap = tile.texture;
641
642                         video::SColor c(255,255,255,255);
643
644                         // Wall at X+ of node
645                         video::S3DVertex vertices[4] =
646                         {
647                                 video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c,
648                                                 ap.x0(), ap.y1()),
649                                 video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c,
650                                                 ap.x1(), ap.y1()),
651                                 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c,
652                                                 ap.x1(), ap.y0()),
653                                 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c,
654                                                 ap.x0(), ap.y0()),
655                         };
656
657                         for(s32 i=0; i<4; i++)
658                         {
659                                 if(dir == v3s16(1,0,0))
660                                         vertices[i].Pos.rotateXZBy(0);
661                                 if(dir == v3s16(-1,0,0))
662                                         vertices[i].Pos.rotateXZBy(180);
663                                 if(dir == v3s16(0,0,1))
664                                         vertices[i].Pos.rotateXZBy(90);
665                                 if(dir == v3s16(0,0,-1))
666                                         vertices[i].Pos.rotateXZBy(-90);
667                                 if(dir == v3s16(0,-1,0))
668                                         vertices[i].Pos.rotateXZBy(45);
669                                 if(dir == v3s16(0,1,0))
670                                         vertices[i].Pos.rotateXZBy(-45);
671
672                                 vertices[i].Pos += intToFloat(p, BS);
673                         }
674
675                         u16 indices[] = {0,1,2,2,3,0};
676                         // Add to mesh collector
677                         collector.append(tile, vertices, 4, indices, 6);
678                 break;}
679                 case NDT_SIGNLIKE:
680                 {
681                         TileSpec tile = getNodeTileN(n, p, 0, data);
682                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
683                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
684                         AtlasPointer ap = tile.texture;
685
686                         u16 l = getInteriorLight(n, 0, data);
687                         video::SColor c = MapBlock_LightColor(255, l);
688                                 
689                         float d = (float)BS/16;
690                         // Wall at X+ of node
691                         video::S3DVertex vertices[4] =
692                         {
693                                 video::S3DVertex(BS/2-d,BS/2,BS/2, 0,0,0, c,
694                                                 ap.x0(), ap.y0()),
695                                 video::S3DVertex(BS/2-d,BS/2,-BS/2, 0,0,0, c,
696                                                 ap.x1(), ap.y0()),
697                                 video::S3DVertex(BS/2-d,-BS/2,-BS/2, 0,0,0, c,
698                                                 ap.x1(), ap.y1()),
699                                 video::S3DVertex(BS/2-d,-BS/2,BS/2, 0,0,0, c,
700                                                 ap.x0(), ap.y1()),
701                         };
702
703                         v3s16 dir = n.getWallMountedDir(nodedef);
704
705                         for(s32 i=0; i<4; i++)
706                         {
707                                 if(dir == v3s16(1,0,0))
708                                         vertices[i].Pos.rotateXZBy(0);
709                                 if(dir == v3s16(-1,0,0))
710                                         vertices[i].Pos.rotateXZBy(180);
711                                 if(dir == v3s16(0,0,1))
712                                         vertices[i].Pos.rotateXZBy(90);
713                                 if(dir == v3s16(0,0,-1))
714                                         vertices[i].Pos.rotateXZBy(-90);
715                                 if(dir == v3s16(0,-1,0))
716                                         vertices[i].Pos.rotateXYBy(-90);
717                                 if(dir == v3s16(0,1,0))
718                                         vertices[i].Pos.rotateXYBy(90);
719
720                                 vertices[i].Pos += intToFloat(p, BS);
721                         }
722
723                         u16 indices[] = {0,1,2,2,3,0};
724                         // Add to mesh collector
725                         collector.append(tile, vertices, 4, indices, 6);
726                 break;}
727                 case NDT_PLANTLIKE:
728                 {
729                         TileSpec tile = getNodeTileN(n, p, 0, data);
730                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
731                         AtlasPointer ap = tile.texture;
732                         
733                         u16 l = getInteriorLight(n, 1, data);
734                         video::SColor c = MapBlock_LightColor(255, l);
735
736                         for(u32 j=0; j<4; j++)
737                         {
738                                 video::S3DVertex vertices[4] =
739                                 {
740                                         video::S3DVertex(-BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
741                                                 ap.x0(), ap.y1()),
742                                         video::S3DVertex( BS/2*f.visual_scale,-BS/2,0, 0,0,0, c,
743                                                 ap.x1(), ap.y1()),
744                                         video::S3DVertex( BS/2*f.visual_scale,
745                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
746                                                 ap.x1(), ap.y0()),
747                                         video::S3DVertex(-BS/2*f.visual_scale,
748                                                 -BS/2 + f.visual_scale*BS,0, 0,0,0, c,
749                                                 ap.x0(), ap.y0()),
750                                 };
751
752                                 if(j == 0)
753                                 {
754                                         for(u16 i=0; i<4; i++)
755                                                 vertices[i].Pos.rotateXZBy(45);
756                                 }
757                                 else if(j == 1)
758                                 {
759                                         for(u16 i=0; i<4; i++)
760                                                 vertices[i].Pos.rotateXZBy(-45);
761                                 }
762                                 else if(j == 2)
763                                 {
764                                         for(u16 i=0; i<4; i++)
765                                                 vertices[i].Pos.rotateXZBy(135);
766                                 }
767                                 else if(j == 3)
768                                 {
769                                         for(u16 i=0; i<4; i++)
770                                                 vertices[i].Pos.rotateXZBy(-135);
771                                 }
772
773                                 for(u16 i=0; i<4; i++)
774                                 {
775                                         vertices[i].Pos *= f.visual_scale;
776                                         vertices[i].Pos += intToFloat(p, BS);
777                                 }
778
779                                 u16 indices[] = {0,1,2,2,3,0};
780                                 // Add to mesh collector
781                                 collector.append(tile, vertices, 4, indices, 6);
782                         }
783                 break;}
784                 case NDT_FENCELIKE:
785                 {
786                         TileSpec tile = getNodeTile(n, p, v3s16(0,0,0), data);
787                         TileSpec tile_nocrack = tile;
788                         tile_nocrack.material_flags &= ~MATERIAL_FLAG_CRACK;
789                         
790                         // A hack to put wood the right way around in the posts
791                         ITextureSource *tsrc = data->m_gamedef->tsrc();
792                         TileSpec tile_rot = tile;
793                         tile_rot.texture = tsrc->getTexture(tsrc->getTextureName(
794                                         tile.texture.id) + "^[transformR90");
795                                         
796                         u16 l = getInteriorLight(n, 1, data);
797                         video::SColor c = MapBlock_LightColor(255, l);
798
799                         const f32 post_rad=(f32)BS/8;
800                         const f32 bar_rad=(f32)BS/16;
801                         const f32 bar_len=(f32)(BS/2)-post_rad;
802
803                         v3f pos = intToFloat(p, BS);
804
805                         // The post - always present
806                         aabb3f post(-post_rad,-BS/2,-post_rad,post_rad,BS/2,post_rad);
807                         post.MinEdge += pos;
808                         post.MaxEdge += pos;
809                         f32 postuv[24]={
810                                         6/16.,6/16.,10/16.,10/16.,
811                                         6/16.,6/16.,10/16.,10/16.,
812                                         0/16.,0,4/16.,1,
813                                         4/16.,0,8/16.,1,
814                                         8/16.,0,12/16.,1,
815                                         12/16.,0,16/16.,1};
816                         makeCuboid(&collector, post, &tile_rot, 1, c, postuv);
817
818                         // Now a section of fence, +X, if there's a post there
819                         v3s16 p2 = p;
820                         p2.X++;
821                         MapNode n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
822                         const ContentFeatures *f2 = &nodedef->get(n2);
823                         if(f2->drawtype == NDT_FENCELIKE)
824                         {
825                                 aabb3f bar(-bar_len+BS/2,-bar_rad+BS/4,-bar_rad,
826                                                 bar_len+BS/2,bar_rad+BS/4,bar_rad);
827                                 bar.MinEdge += pos;
828                                 bar.MaxEdge += pos;
829                                 f32 xrailuv[24]={
830                                         0/16.,2/16.,16/16.,4/16.,
831                                         0/16.,4/16.,16/16.,6/16.,
832                                         6/16.,6/16.,8/16.,8/16.,
833                                         10/16.,10/16.,12/16.,12/16.,
834                                         0/16.,8/16.,16/16.,10/16.,
835                                         0/16.,14/16.,16/16.,16/16.};
836                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
837                                                 c, xrailuv);
838                                 bar.MinEdge.Y -= BS/2;
839                                 bar.MaxEdge.Y -= BS/2;
840                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
841                                                 c, xrailuv);
842                         }
843
844                         // Now a section of fence, +Z, if there's a post there
845                         p2 = p;
846                         p2.Z++;
847                         n2 = data->m_vmanip.getNodeNoEx(blockpos_nodes + p2);
848                         f2 = &nodedef->get(n2);
849                         if(f2->drawtype == NDT_FENCELIKE)
850                         {
851                                 aabb3f bar(-bar_rad,-bar_rad+BS/4,-bar_len+BS/2,
852                                                 bar_rad,bar_rad+BS/4,bar_len+BS/2);
853                                 bar.MinEdge += pos;
854                                 bar.MaxEdge += pos;
855                                 f32 zrailuv[24]={
856                                         3/16.,1/16.,5/16.,5/16., // cannot rotate; stretch
857                                         4/16.,1/16.,6/16.,5/16., // for wood texture instead
858                                         0/16.,9/16.,16/16.,11/16.,
859                                         0/16.,6/16.,16/16.,8/16.,
860                                         6/16.,6/16.,8/16.,8/16.,
861                                         10/16.,10/16.,12/16.,12/16.};
862                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
863                                                 c, zrailuv);
864                                 bar.MinEdge.Y -= BS/2;
865                                 bar.MaxEdge.Y -= BS/2;
866                                 makeCuboid(&collector, bar, &tile_nocrack, 1,
867                                                 c, zrailuv);
868                         }
869                 break;}
870                 case NDT_RAILLIKE:
871                 {
872                         bool is_rail_x [] = { false, false };  /* x-1, x+1 */
873                         bool is_rail_z [] = { false, false };  /* z-1, z+1 */
874
875                         bool is_rail_z_minus_y [] = { false, false };  /* z-1, z+1; y-1 */
876                         bool is_rail_x_minus_y [] = { false, false };  /* x-1, z+1; y-1 */
877                         bool is_rail_z_plus_y [] = { false, false };  /* z-1, z+1; y+1 */
878                         bool is_rail_x_plus_y [] = { false, false };  /* x-1, x+1; y+1 */
879
880                         MapNode n_minus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1,y,z));
881                         MapNode n_plus_x = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1,y,z));
882                         MapNode n_minus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z-1));
883                         MapNode n_plus_z = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x,y,z+1));
884                         MapNode n_plus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y+1, z));
885                         MapNode n_plus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x+1, y-1, z));
886                         MapNode n_minus_x_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y+1, z));
887                         MapNode n_minus_x_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x-1, y-1, z));
888                         MapNode n_plus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z+1));
889                         MapNode n_minus_z_plus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y+1, z-1));
890                         MapNode n_plus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z+1));
891                         MapNode n_minus_z_minus_y = data->m_vmanip.getNodeNoEx(blockpos_nodes + v3s16(x, y-1, z-1));
892                         
893                         content_t thiscontent = n.getContent();
894                         if(n_minus_x.getContent() == thiscontent)
895                                 is_rail_x[0] = true;
896                         if (n_minus_x_minus_y.getContent() == thiscontent)
897                                 is_rail_x_minus_y[0] = true;
898                         if(n_minus_x_plus_y.getContent() == thiscontent)
899                                 is_rail_x_plus_y[0] = true;
900
901                         if(n_plus_x.getContent() == thiscontent)
902                                 is_rail_x[1] = true;
903                         if (n_plus_x_minus_y.getContent() == thiscontent)
904                                 is_rail_x_minus_y[1] = true;
905                         if(n_plus_x_plus_y.getContent() == thiscontent)
906                                 is_rail_x_plus_y[1] = true;
907
908                         if(n_minus_z.getContent() == thiscontent)
909                                 is_rail_z[0] = true;
910                         if (n_minus_z_minus_y.getContent() == thiscontent)
911                                 is_rail_z_minus_y[0] = true;
912                         if(n_minus_z_plus_y.getContent() == thiscontent)
913                                 is_rail_z_plus_y[0] = true;
914
915                         if(n_plus_z.getContent() == thiscontent)
916                                 is_rail_z[1] = true;
917                         if (n_plus_z_minus_y.getContent() == thiscontent)
918                                 is_rail_z_minus_y[1] = true;
919                         if(n_plus_z_plus_y.getContent() == thiscontent)
920                                 is_rail_z_plus_y[1] = true;
921
922                         bool is_rail_x_all[] = {false, false};
923                         bool is_rail_z_all[] = {false, false};
924                         is_rail_x_all[0]=is_rail_x[0] || is_rail_x_minus_y[0] || is_rail_x_plus_y[0];
925                         is_rail_x_all[1]=is_rail_x[1] || is_rail_x_minus_y[1] || is_rail_x_plus_y[1];
926                         is_rail_z_all[0]=is_rail_z[0] || is_rail_z_minus_y[0] || is_rail_z_plus_y[0];
927                         is_rail_z_all[1]=is_rail_z[1] || is_rail_z_minus_y[1] || is_rail_z_plus_y[1];
928
929                         // reasonable default, flat straight unrotated rail
930                         bool is_straight = true;
931                         int adjacencies = 0;
932                         int angle = 0;
933                         u8 tileindex = 0;
934
935                         // check for sloped rail
936                         if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1])
937                         {
938                                 adjacencies = 5; //5 means sloped
939                                 is_straight = true; // sloped is always straight
940                         }
941                         else
942                         {
943                                 // is really straight, rails on both sides
944                                 is_straight = (is_rail_x_all[0] && is_rail_x_all[1]) || (is_rail_z_all[0] && is_rail_z_all[1]);
945                                 adjacencies = is_rail_x_all[0] + is_rail_x_all[1] + is_rail_z_all[0] + is_rail_z_all[1];
946                         }
947
948                         switch (adjacencies) {
949                         case 1:
950                                 if(is_rail_x_all[0] || is_rail_x_all[1])
951                                         angle = 90;
952                                 break;
953                         case 2:
954                                 if(!is_straight)
955                                         tileindex = 1; // curved
956                                 if(is_rail_x_all[0] && is_rail_x_all[1])
957                                         angle = 90;
958                                 if(is_rail_z_all[0] && is_rail_z_all[1]){
959                                         if (n_minus_z_plus_y.getContent() == thiscontent) angle = 180;
960                                 }
961                                 else if(is_rail_x_all[0] && is_rail_z_all[0])
962                                         angle = 270;
963                                 else if(is_rail_x_all[0] && is_rail_z_all[1])
964                                         angle = 180;
965                                 else if(is_rail_x_all[1] && is_rail_z_all[1])
966                                         angle = 90;
967                                 break;
968                         case 3:
969                                 // here is where the potential to 'switch' a junction is, but not implemented at present
970                                 tileindex = 2; // t-junction
971                                 if(!is_rail_x_all[1])
972                                         angle=180;
973                                 if(!is_rail_z_all[0])
974                                         angle=90;
975                                 if(!is_rail_z_all[1])
976                                         angle=270;
977                                 break;
978                         case 4:
979                                 tileindex = 3; // crossing
980                                 break;
981                         case 5: //sloped
982                                 if(is_rail_z_plus_y[0])
983                                         angle = 180;
984                                 if(is_rail_x_plus_y[0])
985                                         angle = 90;
986                                 if(is_rail_x_plus_y[1])
987                                         angle = -90;
988                                 break;
989                         default:
990                                 break;
991                         }
992
993                         TileSpec tile = getNodeTileN(n, p, tileindex, data);
994                         tile.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
995                         tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY;
996
997                         AtlasPointer ap = tile.texture;
998                         
999                         u16 l = getInteriorLight(n, 0, data);
1000                         video::SColor c = MapBlock_LightColor(255, l);
1001
1002                         float d = (float)BS/64;
1003                         
1004                         char g=-1;
1005                         if (is_rail_x_plus_y[0] || is_rail_x_plus_y[1] || is_rail_z_plus_y[0] || is_rail_z_plus_y[1])
1006                                 g=1; //Object is at a slope
1007
1008                         video::S3DVertex vertices[4] =
1009                         {
1010                                         video::S3DVertex(-BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1011                                                         ap.x0(), ap.y1()),
1012                                         video::S3DVertex(BS/2,-BS/2+d,-BS/2, 0,0,0, c,
1013                                                         ap.x1(), ap.y1()),
1014                                         video::S3DVertex(BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1015                                                         ap.x1(), ap.y0()),
1016                                         video::S3DVertex(-BS/2,g*BS/2+d,BS/2, 0,0,0, c,
1017                                                         ap.x0(), ap.y0()),
1018                         };
1019
1020                         for(s32 i=0; i<4; i++)
1021                         {
1022                                 if(angle != 0)
1023                                         vertices[i].Pos.rotateXZBy(angle);
1024                                 vertices[i].Pos += intToFloat(p, BS);
1025                         }
1026
1027                         u16 indices[] = {0,1,2,2,3,0};
1028                         collector.append(tile, vertices, 4, indices, 6);
1029                 break;}
1030                 case NDT_NODEBOX:
1031                 {
1032                         TileSpec tiles[6];
1033                         for(int i = 0; i < 6; i++)
1034                         {
1035                                 tiles[i] = getNodeTileN(n, p, i, data);
1036                         }
1037
1038                         // Facedir rotation for textures
1039                         if(f.node_box.type == NODEBOX_FIXED){
1040                                 int facedir = n.getFaceDir(nodedef);
1041                                 if(facedir == 1){ // -90
1042                                         TileSpec old[6];
1043                                         for(int i=0; i<6; i++)
1044                                                 old[i] = tiles[i];
1045                                         // right <- back
1046                                         tiles[2] = old[4];
1047                                         // back <- left
1048                                         tiles[4] = old[3];
1049                                         // left <- front
1050                                         tiles[3] = old[5];
1051                                         // front <- right
1052                                         tiles[5] = old[2];
1053                                 }
1054                                 if(facedir == 2){ // 180
1055                                         TileSpec old[6];
1056                                         for(int i=0; i<6; i++)
1057                                                 old[i] = tiles[i];
1058                                         // right <-> left
1059                                         tiles[2] = old[3];
1060                                         tiles[3] = old[2];
1061                                         // back <-> front
1062                                         tiles[4] = old[5];
1063                                         tiles[5] = old[4];
1064                                 }
1065                                 if(facedir == 3){ // 90
1066                                         TileSpec old[6];
1067                                         for(int i=0; i<6; i++)
1068                                                 old[i] = tiles[i];
1069                                         // right <- front
1070                                         tiles[2] = old[5];
1071                                         // back <- right
1072                                         tiles[4] = old[2];
1073                                         // left <- back
1074                                         tiles[3] = old[4];
1075                                         // front <- left
1076                                         tiles[5] = old[3];
1077                                 }
1078                         }
1079
1080                         u16 l = getInteriorLight(n, 0, data);
1081                         video::SColor c = MapBlock_LightColor(255, l);
1082
1083                         v3f pos = intToFloat(p, BS);
1084
1085                         std::vector<aabb3f> boxes = n.getNodeBoxes(nodedef);
1086                         for(std::vector<aabb3f>::iterator
1087                                         i = boxes.begin();
1088                                         i != boxes.end(); i++)
1089                         {
1090                                 aabb3f box = *i;
1091                                 box.MinEdge += pos;
1092                                 box.MaxEdge += pos;
1093
1094                                 // Compute texture coords
1095                                 f32 tx1 = (i->MinEdge.X/BS)+0.5;
1096                                 f32 ty1 = (i->MinEdge.Y/BS)+0.5;
1097                                 f32 tz1 = (i->MinEdge.Z/BS)+0.5;
1098                                 f32 tx2 = (i->MaxEdge.X/BS)+0.5;
1099                                 f32 ty2 = (i->MaxEdge.Y/BS)+0.5;
1100                                 f32 tz2 = (i->MaxEdge.Z/BS)+0.5;
1101                                 f32 txc[24] = {
1102                                         // up
1103                                         tx1, 1-tz2, tx2, 1-tz1,
1104                                         // down
1105                                         tx1, tz1, tx2, tz2,
1106                                         // right
1107                                         tz1, 1-ty2, tz2, 1-ty1,
1108                                         // left
1109                                         1-tz2, 1-ty2, 1-tz1, 1-ty1,
1110                                         // back
1111                                         1-tx2, 1-ty2, 1-tx1, 1-ty1,
1112                                         // front
1113                                         tx1, 1-ty2, tx2, 1-ty1,
1114                                 };
1115
1116                                 makeCuboid(&collector, box, tiles, 6, c, txc);
1117                         }
1118                 break;}
1119                 }
1120         }
1121 }
1122