Cpp11 initializers: last src root changeset (#6022)
[oweals/minetest.git] / src / wieldmesh.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2014 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 "settings.h"
21 #include "wieldmesh.h"
22 #include "inventory.h"
23 #include "client.h"
24 #include "itemdef.h"
25 #include "nodedef.h"
26 #include "mesh.h"
27 #include "mapblock_mesh.h"
28 #include "client/tile.h"
29 #include "log.h"
30 #include "util/numeric.h"
31 #include <map>
32 #include <IMeshManipulator.h>
33
34 #define WIELD_SCALE_FACTOR 30.0
35 #define WIELD_SCALE_FACTOR_EXTRUDED 40.0
36
37 #define MIN_EXTRUSION_MESH_RESOLUTION 16
38 #define MAX_EXTRUSION_MESH_RESOLUTION 512
39
40 static scene::IMesh *createExtrusionMesh(int resolution_x, int resolution_y)
41 {
42         const f32 r = 0.5;
43
44         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
45         video::SColor c(255,255,255,255);
46         v3f scale(1.0, 1.0, 0.1);
47
48         // Front and back
49         {
50                 video::S3DVertex vertices[8] = {
51                         // z-
52                         video::S3DVertex(-r,+r,-r, 0,0,-1, c, 0,0),
53                         video::S3DVertex(+r,+r,-r, 0,0,-1, c, 1,0),
54                         video::S3DVertex(+r,-r,-r, 0,0,-1, c, 1,1),
55                         video::S3DVertex(-r,-r,-r, 0,0,-1, c, 0,1),
56                         // z+
57                         video::S3DVertex(-r,+r,+r, 0,0,+1, c, 0,0),
58                         video::S3DVertex(-r,-r,+r, 0,0,+1, c, 0,1),
59                         video::S3DVertex(+r,-r,+r, 0,0,+1, c, 1,1),
60                         video::S3DVertex(+r,+r,+r, 0,0,+1, c, 1,0),
61                 };
62                 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
63                 buf->append(vertices, 8, indices, 12);
64         }
65
66         f32 pixelsize_x = 1 / (f32) resolution_x;
67         f32 pixelsize_y = 1 / (f32) resolution_y;
68
69         for (int i = 0; i < resolution_x; ++i) {
70                 f32 pixelpos_x = i * pixelsize_x - 0.5;
71                 f32 x0 = pixelpos_x;
72                 f32 x1 = pixelpos_x + pixelsize_x;
73                 f32 tex0 = (i + 0.1) * pixelsize_x;
74                 f32 tex1 = (i + 0.9) * pixelsize_x;
75                 video::S3DVertex vertices[8] = {
76                         // x-
77                         video::S3DVertex(x0,-r,-r, -1,0,0, c, tex0,1),
78                         video::S3DVertex(x0,-r,+r, -1,0,0, c, tex1,1),
79                         video::S3DVertex(x0,+r,+r, -1,0,0, c, tex1,0),
80                         video::S3DVertex(x0,+r,-r, -1,0,0, c, tex0,0),
81                         // x+
82                         video::S3DVertex(x1,-r,-r, +1,0,0, c, tex0,1),
83                         video::S3DVertex(x1,+r,-r, +1,0,0, c, tex0,0),
84                         video::S3DVertex(x1,+r,+r, +1,0,0, c, tex1,0),
85                         video::S3DVertex(x1,-r,+r, +1,0,0, c, tex1,1),
86                 };
87                 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
88                 buf->append(vertices, 8, indices, 12);
89         }
90         for (int i = 0; i < resolution_y; ++i) {
91                 f32 pixelpos_y = i * pixelsize_y - 0.5;
92                 f32 y0 = -pixelpos_y - pixelsize_y;
93                 f32 y1 = -pixelpos_y;
94                 f32 tex0 = (i + 0.1) * pixelsize_y;
95                 f32 tex1 = (i + 0.9) * pixelsize_y;
96                 video::S3DVertex vertices[8] = {
97                         // y-
98                         video::S3DVertex(-r,y0,-r, 0,-1,0, c, 0,tex0),
99                         video::S3DVertex(+r,y0,-r, 0,-1,0, c, 1,tex0),
100                         video::S3DVertex(+r,y0,+r, 0,-1,0, c, 1,tex1),
101                         video::S3DVertex(-r,y0,+r, 0,-1,0, c, 0,tex1),
102                         // y+
103                         video::S3DVertex(-r,y1,-r, 0,+1,0, c, 0,tex0),
104                         video::S3DVertex(-r,y1,+r, 0,+1,0, c, 0,tex1),
105                         video::S3DVertex(+r,y1,+r, 0,+1,0, c, 1,tex1),
106                         video::S3DVertex(+r,y1,-r, 0,+1,0, c, 1,tex0),
107                 };
108                 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
109                 buf->append(vertices, 8, indices, 12);
110         }
111
112         // Create mesh object
113         scene::SMesh *mesh = new scene::SMesh();
114         mesh->addMeshBuffer(buf);
115         buf->drop();
116         scaleMesh(mesh, scale);  // also recalculates bounding box
117         return mesh;
118 }
119
120 /*
121         Caches extrusion meshes so that only one of them per resolution
122         is needed. Also caches one cube (for convenience).
123
124         E.g. there is a single extrusion mesh that is used for all
125         16x16 px images, another for all 256x256 px images, and so on.
126
127         WARNING: Not thread safe. This should not be a problem since
128         rendering related classes (such as WieldMeshSceneNode) will be
129         used from the rendering thread only.
130 */
131 class ExtrusionMeshCache: public IReferenceCounted
132 {
133 public:
134         // Constructor
135         ExtrusionMeshCache()
136         {
137                 for (int resolution = MIN_EXTRUSION_MESH_RESOLUTION;
138                                 resolution <= MAX_EXTRUSION_MESH_RESOLUTION;
139                                 resolution *= 2) {
140                         m_extrusion_meshes[resolution] =
141                                 createExtrusionMesh(resolution, resolution);
142                 }
143                 m_cube = createCubeMesh(v3f(1.0, 1.0, 1.0));
144         }
145         // Destructor
146         virtual ~ExtrusionMeshCache()
147         {
148                 for (std::map<int, scene::IMesh*>::iterator
149                                 it = m_extrusion_meshes.begin();
150                                 it != m_extrusion_meshes.end(); ++it) {
151                         it->second->drop();
152                 }
153                 m_cube->drop();
154         }
155         // Get closest extrusion mesh for given image dimensions
156         // Caller must drop the returned pointer
157         scene::IMesh* create(core::dimension2d<u32> dim)
158         {
159                 // handle non-power of two textures inefficiently without cache
160                 if (!is_power_of_two(dim.Width) || !is_power_of_two(dim.Height)) {
161                         return createExtrusionMesh(dim.Width, dim.Height);
162                 }
163
164                 int maxdim = MYMAX(dim.Width, dim.Height);
165
166                 std::map<int, scene::IMesh*>::iterator
167                         it = m_extrusion_meshes.lower_bound(maxdim);
168
169                 if (it == m_extrusion_meshes.end()) {
170                         // no viable resolution found; use largest one
171                         it = m_extrusion_meshes.find(MAX_EXTRUSION_MESH_RESOLUTION);
172                         sanity_check(it != m_extrusion_meshes.end());
173                 }
174
175                 scene::IMesh *mesh = it->second;
176                 mesh->grab();
177                 return mesh;
178         }
179         // Returns a 1x1x1 cube mesh with one meshbuffer (material) per face
180         // Caller must drop the returned pointer
181         scene::IMesh* createCube()
182         {
183                 m_cube->grab();
184                 return m_cube;
185         }
186
187 private:
188         std::map<int, scene::IMesh*> m_extrusion_meshes;
189         scene::IMesh *m_cube;
190 };
191
192 ExtrusionMeshCache *g_extrusion_mesh_cache = NULL;
193
194
195 WieldMeshSceneNode::WieldMeshSceneNode(
196                 scene::ISceneNode *parent,
197                 scene::ISceneManager *mgr,
198                 s32 id,
199                 bool lighting
200 ):
201         scene::ISceneNode(parent, mgr, id),
202         m_material_type(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF),
203         m_lighting(lighting)
204 {
205         m_enable_shaders = g_settings->getBool("enable_shaders");
206         m_anisotropic_filter = g_settings->getBool("anisotropic_filter");
207         m_bilinear_filter = g_settings->getBool("bilinear_filter");
208         m_trilinear_filter = g_settings->getBool("trilinear_filter");
209
210         // If this is the first wield mesh scene node, create a cache
211         // for extrusion meshes (and a cube mesh), otherwise reuse it
212         if (!g_extrusion_mesh_cache)
213                 g_extrusion_mesh_cache = new ExtrusionMeshCache();
214         else
215                 g_extrusion_mesh_cache->grab();
216
217         // Disable bounding box culling for this scene node
218         // since we won't calculate the bounding box.
219         setAutomaticCulling(scene::EAC_OFF);
220
221         // Create the child scene node
222         scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
223         m_meshnode = SceneManager->addMeshSceneNode(dummymesh, this, -1);
224         m_meshnode->setReadOnlyMaterials(false);
225         m_meshnode->setVisible(false);
226         dummymesh->drop(); // m_meshnode grabbed it
227 }
228
229 WieldMeshSceneNode::~WieldMeshSceneNode()
230 {
231         sanity_check(g_extrusion_mesh_cache);
232         if (g_extrusion_mesh_cache->drop())
233                 g_extrusion_mesh_cache = nullptr;
234 }
235
236 void WieldMeshSceneNode::setCube(const ContentFeatures &f,
237                         v3f wield_scale)
238 {
239         scene::IMesh *cubemesh = g_extrusion_mesh_cache->createCube();
240         scene::SMesh *copy = cloneMesh(cubemesh);
241         cubemesh->drop();
242         postProcessNodeMesh(copy, f, false, true, &m_material_type, &m_colors);
243         changeToMesh(copy);
244         copy->drop();
245         m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR);
246 }
247
248 void WieldMeshSceneNode::setExtruded(const std::string &imagename,
249                 v3f wield_scale, ITextureSource *tsrc, u8 num_frames)
250 {
251         video::ITexture *texture = tsrc->getTexture(imagename);
252         if (!texture) {
253                 changeToMesh(nullptr);
254                 return;
255         }
256
257         core::dimension2d<u32> dim = texture->getSize();
258         // Detect animation texture and pull off top frame instead of using entire thing
259         if (num_frames > 1) {
260                 u32 frame_height = dim.Height / num_frames;
261                 dim = core::dimension2d<u32>(dim.Width, frame_height);
262         }
263         scene::IMesh *mesh = g_extrusion_mesh_cache->create(dim);
264         scene::SMesh *copy = cloneMesh(mesh);
265         mesh->drop();
266         changeToMesh(copy);
267         copy->drop();
268
269         m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR_EXTRUDED);
270
271         // Customize material
272         video::SMaterial &material = m_meshnode->getMaterial(0);
273         material.setTexture(0, tsrc->getTextureForMesh(imagename));
274         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
275         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
276         material.MaterialType = m_material_type;
277         material.setFlag(video::EMF_BACK_FACE_CULLING, true);
278         // Enable bi/trilinear filtering only for high resolution textures
279         if (dim.Width > 32) {
280                 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
281                 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
282         } else {
283                 material.setFlag(video::EMF_BILINEAR_FILTER, false);
284                 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
285         }
286         material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter);
287         // mipmaps cause "thin black line" artifacts
288 #if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2
289         material.setFlag(video::EMF_USE_MIP_MAPS, false);
290 #endif
291         if (m_enable_shaders) {
292                 material.setTexture(2, tsrc->getShaderFlagsTexture(false));
293         }
294 }
295
296 void WieldMeshSceneNode::setItem(const ItemStack &item, Client *client)
297 {
298         ITextureSource *tsrc = client->getTextureSource();
299         IItemDefManager *idef = client->getItemDefManager();
300         IShaderSource *shdrsrc = client->getShaderSource();
301         INodeDefManager *ndef = client->getNodeDefManager();
302         const ItemDefinition &def = item.getDefinition(idef);
303         const ContentFeatures &f = ndef->get(def.name);
304         content_t id = ndef->getId(def.name);
305
306         if (m_enable_shaders) {
307                 u32 shader_id = shdrsrc->getShader("wielded_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
308                 m_material_type = shdrsrc->getShaderInfo(shader_id).material;
309         }
310
311         // Color-related
312         m_colors.clear();
313         m_base_color = idef->getItemstackColor(item, client);
314
315         // If wield_image is defined, it overrides everything else
316         if (def.wield_image != "") {
317                 setExtruded(def.wield_image, def.wield_scale, tsrc, 1);
318                 m_colors.push_back(ItemPartColor());
319                 return;
320         }
321         // Handle nodes
322         // See also CItemDefManager::createClientCached()
323         else if (def.type == ITEM_NODE) {
324                 if (f.mesh_ptr[0]) {
325                         // e.g. mesh nodes and nodeboxes
326                         scene::SMesh *mesh = cloneMesh(f.mesh_ptr[0]);
327                         postProcessNodeMesh(mesh, f, m_enable_shaders, true,
328                                 &m_material_type, &m_colors);
329                         changeToMesh(mesh);
330                         mesh->drop();
331                         // mesh is pre-scaled by BS * f->visual_scale
332                         m_meshnode->setScale(
333                                         def.wield_scale * WIELD_SCALE_FACTOR
334                                         / (BS * f.visual_scale));
335                 } else if (f.drawtype == NDT_AIRLIKE) {
336                         changeToMesh(nullptr);
337                 } else if (f.drawtype == NDT_PLANTLIKE) {
338                         setExtruded(tsrc->getTextureName(f.tiles[0].layers[0].texture_id),
339                                 def.wield_scale, tsrc,
340                                 f.tiles[0].layers[0].animation_frame_count);
341                 } else if (f.drawtype == NDT_NORMAL || f.drawtype == NDT_ALLFACES) {
342                         setCube(f, def.wield_scale);
343                 } else {
344                         MeshMakeData mesh_make_data(client, false);
345                         MapNode mesh_make_node(id, 255, 0);
346                         mesh_make_data.fillSingleNode(&mesh_make_node);
347                         MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0));
348                         scene::SMesh *mesh = cloneMesh(mapblock_mesh.getMesh());
349                         translateMesh(mesh, v3f(-BS, -BS, -BS));
350                         postProcessNodeMesh(mesh, f, m_enable_shaders, true,
351                                 &m_material_type, &m_colors);
352                         changeToMesh(mesh);
353                         mesh->drop();
354                         m_meshnode->setScale(
355                                         def.wield_scale * WIELD_SCALE_FACTOR
356                                         / (BS * f.visual_scale));
357                 }
358                 u32 material_count = m_meshnode->getMaterialCount();
359                 for (u32 i = 0; i < material_count; ++i) {
360                         video::SMaterial &material = m_meshnode->getMaterial(i);
361                         material.setFlag(video::EMF_BACK_FACE_CULLING, true);
362                         material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
363                         material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
364                 }
365                 return;
366         }
367         else if (def.inventory_image != "") {
368                 setExtruded(def.inventory_image, def.wield_scale, tsrc, 1);
369                 m_colors.push_back(ItemPartColor());
370                 return;
371         }
372
373         // no wield mesh found
374         changeToMesh(nullptr);
375 }
376
377 void WieldMeshSceneNode::setColor(video::SColor c)
378 {
379         assert(!m_lighting);
380         scene::IMesh *mesh = m_meshnode->getMesh();
381         if (!mesh)
382                 return;
383
384         u8 red = c.getRed();
385         u8 green = c.getGreen();
386         u8 blue = c.getBlue();
387         u32 mc = mesh->getMeshBufferCount();
388         for (u32 j = 0; j < mc; j++) {
389                 video::SColor bc(m_base_color);
390                 if ((m_colors.size() > j) && (m_colors[j].override_base))
391                         bc = m_colors[j].color;
392                 video::SColor buffercolor(255,
393                         bc.getRed() * red / 255,
394                         bc.getGreen() * green / 255,
395                         bc.getBlue() * blue / 255);
396                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
397                 colorizeMeshBuffer(buf, &buffercolor);
398         }
399 }
400
401 void WieldMeshSceneNode::render()
402 {
403         // note: if this method is changed to actually do something,
404         // you probably should implement OnRegisterSceneNode as well
405 }
406
407 void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
408 {
409         if (!mesh) {
410                 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
411                 m_meshnode->setVisible(false);
412                 m_meshnode->setMesh(dummymesh);
413                 dummymesh->drop();  // m_meshnode grabbed it
414         } else {
415                 m_meshnode->setMesh(mesh);
416         }
417
418         m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
419         // need to normalize normals when lighting is enabled (because of setScale())
420         m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
421         m_meshnode->setVisible(true);
422 }
423
424 void getItemMesh(Client *client, const ItemStack &item, ItemMesh *result)
425 {
426         ITextureSource *tsrc = client->getTextureSource();
427         IItemDefManager *idef = client->getItemDefManager();
428         INodeDefManager *ndef = client->getNodeDefManager();
429         const ItemDefinition &def = item.getDefinition(idef);
430         const ContentFeatures &f = ndef->get(def.name);
431         content_t id = ndef->getId(def.name);
432
433         if (!g_extrusion_mesh_cache) {
434                 g_extrusion_mesh_cache = new ExtrusionMeshCache();
435         } else {
436                 g_extrusion_mesh_cache->grab();
437         }
438
439         scene::SMesh *mesh = nullptr;
440
441         // Shading is on by default
442         result->needs_shading = true;
443
444         // If inventory_image is defined, it overrides everything else
445         if (def.inventory_image != "") {
446                 mesh = getExtrudedMesh(tsrc, def.inventory_image);
447                 result->buffer_colors.push_back(ItemPartColor());
448                 // Items with inventory images do not need shading
449                 result->needs_shading = false;
450         } else if (def.type == ITEM_NODE) {
451                 if (f.mesh_ptr[0]) {
452                         mesh = cloneMesh(f.mesh_ptr[0]);
453                         scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
454                 } else if (f.drawtype == NDT_PLANTLIKE) {
455                         mesh = getExtrudedMesh(tsrc,
456                                 tsrc->getTextureName(f.tiles[0].layers[0].texture_id));
457                 } else if (f.drawtype == NDT_NORMAL || f.drawtype == NDT_ALLFACES
458                         || f.drawtype == NDT_LIQUID || f.drawtype == NDT_FLOWINGLIQUID) {
459                         scene::IMesh *cube = g_extrusion_mesh_cache->createCube();
460                         mesh = cloneMesh(cube);
461                         cube->drop();
462                         scaleMesh(mesh, v3f(1.2, 1.2, 1.2));
463                 } else {
464                         MeshMakeData mesh_make_data(client, false);
465                         MapNode mesh_make_node(id, 255, 0);
466                         mesh_make_data.fillSingleNode(&mesh_make_node);
467                         MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0));
468                         mesh = cloneMesh(mapblock_mesh.getMesh());
469                         translateMesh(mesh, v3f(-BS, -BS, -BS));
470                         scaleMesh(mesh, v3f(0.12, 0.12, 0.12));
471
472                         u32 mc = mesh->getMeshBufferCount();
473                         for (u32 i = 0; i < mc; ++i) {
474                                 video::SMaterial &material1 =
475                                         mesh->getMeshBuffer(i)->getMaterial();
476                                 video::SMaterial &material2 =
477                                         mapblock_mesh.getMesh()->getMeshBuffer(i)->getMaterial();
478                                 material1.setTexture(0, material2.getTexture(0));
479                                 material1.setTexture(1, material2.getTexture(1));
480                                 material1.setTexture(2, material2.getTexture(2));
481                                 material1.setTexture(3, material2.getTexture(3));
482                                 material1.MaterialType = material2.MaterialType;
483                         }
484                 }
485
486                 u32 mc = mesh->getMeshBufferCount();
487                 for (u32 i = 0; i < mc; ++i) {
488                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
489                         video::SMaterial &material = buf->getMaterial();
490                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
491                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
492                         material.setFlag(video::EMF_TRILINEAR_FILTER, false);
493                         material.setFlag(video::EMF_BACK_FACE_CULLING, true);
494                         material.setFlag(video::EMF_LIGHTING, false);
495                 }
496
497                 rotateMeshXZby(mesh, -45);
498                 rotateMeshYZby(mesh, -30);
499
500                 postProcessNodeMesh(mesh, f, false, false, nullptr, &result->buffer_colors);
501         }
502         result->mesh = mesh;
503 }
504
505
506
507 scene::SMesh *getExtrudedMesh(ITextureSource *tsrc, const std::string &imagename)
508 {
509         video::ITexture *texture = tsrc->getTextureForMesh(imagename);
510         if (!texture) {
511                 return nullptr;
512         }
513
514         core::dimension2d<u32> dim = texture->getSize();
515         scene::IMesh *original = g_extrusion_mesh_cache->create(dim);
516         scene::SMesh *mesh = cloneMesh(original);
517         original->drop();
518
519         // Customize material
520         video::SMaterial &material = mesh->getMeshBuffer(0)->getMaterial();
521         material.setTexture(0, tsrc->getTexture(imagename));
522         material.TextureLayer[0].TextureWrapU = video::ETC_CLAMP_TO_EDGE;
523         material.TextureLayer[0].TextureWrapV = video::ETC_CLAMP_TO_EDGE;
524         material.setFlag(video::EMF_BILINEAR_FILTER, false);
525         material.setFlag(video::EMF_TRILINEAR_FILTER, false);
526         material.setFlag(video::EMF_BACK_FACE_CULLING, true);
527         material.setFlag(video::EMF_LIGHTING, false);
528         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
529         scaleMesh(mesh, v3f(2.0, 2.0, 2.0));
530
531         return mesh;
532 }
533
534 void postProcessNodeMesh(scene::SMesh *mesh, const ContentFeatures &f,
535         bool use_shaders, bool set_material, video::E_MATERIAL_TYPE *mattype,
536         std::vector<ItemPartColor> *colors)
537 {
538         u32 mc = mesh->getMeshBufferCount();
539         // Allocate colors for existing buffers
540         colors->clear();
541         for (u32 i = 0; i < mc; ++i)
542                 colors->push_back(ItemPartColor());
543
544         for (u32 i = 0; i < mc; ++i) {
545                 const TileSpec *tile = &(f.tiles[i]);
546                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
547                 for (int layernum = 0; layernum < MAX_TILE_LAYERS; layernum++) {
548                         const TileLayer *layer = &tile->layers[layernum];
549                         if (layer->texture_id == 0)
550                                 continue;
551                         if (layernum != 0) {
552                                 scene::IMeshBuffer *copy = cloneMeshBuffer(buf);
553                                 copy->getMaterial() = buf->getMaterial();
554                                 mesh->addMeshBuffer(copy);
555                                 copy->drop();
556                                 buf = copy;
557                                 colors->push_back(
558                                         ItemPartColor(layer->has_color, layer->color));
559                         } else {
560                                 (*colors)[i] = ItemPartColor(layer->has_color, layer->color);
561                         }
562                         video::SMaterial &material = buf->getMaterial();
563                         if (set_material)
564                                 layer->applyMaterialOptions(material);
565                         if (mattype) {
566                                 material.MaterialType = *mattype;
567                         }
568                         if (layer->animation_frame_count > 1) {
569                                 FrameSpec animation_frame = layer->frames[0];
570                                 material.setTexture(0, animation_frame.texture);
571                         } else {
572                                 material.setTexture(0, layer->texture);
573                         }
574                         if (use_shaders) {
575                                 if (layer->normal_texture) {
576                                         if (layer->animation_frame_count > 1) {
577                                                 FrameSpec animation_frame = layer->frames[0];
578                                                 material.setTexture(1, animation_frame.normal_texture);
579                                         } else
580                                                 material.setTexture(1, layer->normal_texture);
581                                 }
582                                 material.setTexture(2, layer->flags_texture);
583                         }
584                 }
585         }
586 }