3 Copyright (C) 2010-2014 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "wieldmesh.h"
23 #include "inventory.h"
28 #include "mapblock_mesh.h"
31 #include "util/numeric.h"
33 #include <IMeshManipulator.h>
35 #define WIELD_SCALE_FACTOR 30.0
36 #define WIELD_SCALE_FACTOR_EXTRUDED 40.0
38 #define MIN_EXTRUSION_MESH_RESOLUTION 32 // not 16: causes too many "holes"
39 #define MAX_EXTRUSION_MESH_RESOLUTION 512
41 static scene::IMesh* createExtrusionMesh(int resolution_x, int resolution_y)
45 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
46 video::SColor c(255,255,255,255);
47 v3f scale(1.0, 1.0, 0.1);
51 video::S3DVertex vertices[8] = {
53 video::S3DVertex(-r,+r,-r, 0,0,-1, c, 0,0),
54 video::S3DVertex(+r,+r,-r, 0,0,-1, c, 1,0),
55 video::S3DVertex(+r,-r,-r, 0,0,-1, c, 1,1),
56 video::S3DVertex(-r,-r,-r, 0,0,-1, c, 0,1),
58 video::S3DVertex(-r,+r,+r, 0,0,+1, c, 0,0),
59 video::S3DVertex(-r,-r,+r, 0,0,+1, c, 0,1),
60 video::S3DVertex(+r,-r,+r, 0,0,+1, c, 1,1),
61 video::S3DVertex(+r,+r,+r, 0,0,+1, c, 1,0),
63 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
64 buf->append(vertices, 8, indices, 12);
67 f32 pixelsize_x = 1 / (f32) resolution_x;
68 f32 pixelsize_y = 1 / (f32) resolution_y;
70 for (int i = 0; i < resolution_x; ++i) {
71 f32 pixelpos_x = i * pixelsize_x - 0.5;
73 f32 x1 = pixelpos_x + pixelsize_x;
74 f32 tex0 = (i + 0.1) * pixelsize_x;
75 f32 tex1 = (i + 0.9) * pixelsize_x;
76 video::S3DVertex vertices[8] = {
78 video::S3DVertex(x0,-r,-r, -1,0,0, c, tex0,1),
79 video::S3DVertex(x0,-r,+r, -1,0,0, c, tex1,1),
80 video::S3DVertex(x0,+r,+r, -1,0,0, c, tex1,0),
81 video::S3DVertex(x0,+r,-r, -1,0,0, c, tex0,0),
83 video::S3DVertex(x1,-r,-r, +1,0,0, c, tex0,1),
84 video::S3DVertex(x1,+r,-r, +1,0,0, c, tex0,0),
85 video::S3DVertex(x1,+r,+r, +1,0,0, c, tex1,0),
86 video::S3DVertex(x1,-r,+r, +1,0,0, c, tex1,1),
88 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
89 buf->append(vertices, 8, indices, 12);
91 for (int i = 0; i < resolution_y; ++i) {
92 f32 pixelpos_y = i * pixelsize_y - 0.5;
93 f32 y0 = -pixelpos_y - pixelsize_y;
95 f32 tex0 = (i + 0.1) * pixelsize_y;
96 f32 tex1 = (i + 0.9) * pixelsize_y;
97 video::S3DVertex vertices[8] = {
99 video::S3DVertex(-r,y0,-r, 0,-1,0, c, 0,tex0),
100 video::S3DVertex(+r,y0,-r, 0,-1,0, c, 1,tex0),
101 video::S3DVertex(+r,y0,+r, 0,-1,0, c, 1,tex1),
102 video::S3DVertex(-r,y0,+r, 0,-1,0, c, 0,tex1),
104 video::S3DVertex(-r,y1,-r, 0,+1,0, c, 0,tex0),
105 video::S3DVertex(-r,y1,+r, 0,+1,0, c, 0,tex1),
106 video::S3DVertex(+r,y1,+r, 0,+1,0, c, 1,tex1),
107 video::S3DVertex(+r,y1,-r, 0,+1,0, c, 1,tex0),
109 u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4};
110 buf->append(vertices, 8, indices, 12);
113 // Create mesh object
114 scene::SMesh *mesh = new scene::SMesh();
115 mesh->addMeshBuffer(buf);
117 scaleMesh(mesh, scale); // also recalculates bounding box
122 Caches extrusion meshes so that only one of them per resolution
123 is needed. Also caches one cube (for convenience).
125 E.g. there is a single extrusion mesh that is used for all
126 16x16 px images, another for all 256x256 px images, and so on.
128 WARNING: Not thread safe. This should not be a problem since
129 rendering related classes (such as WieldMeshSceneNode) will be
130 used from the rendering thread only.
132 class ExtrusionMeshCache: public IReferenceCounted
138 for (int resolution = MIN_EXTRUSION_MESH_RESOLUTION;
139 resolution <= MAX_EXTRUSION_MESH_RESOLUTION;
141 m_extrusion_meshes[resolution] =
142 createExtrusionMesh(resolution, resolution);
144 m_cube = createCubeMesh(v3f(1.0, 1.0, 1.0));
147 virtual ~ExtrusionMeshCache()
149 for (std::map<int, scene::IMesh*>::iterator
150 it = m_extrusion_meshes.begin();
151 it != m_extrusion_meshes.end(); ++it) {
156 // Get closest extrusion mesh for given image dimensions
157 // Caller must drop the returned pointer
158 scene::IMesh* create(core::dimension2d<u32> dim)
160 // handle non-power of two textures inefficiently without cache
161 if (!is_power_of_two(dim.Width) || !is_power_of_two(dim.Height)) {
162 return createExtrusionMesh(dim.Width, dim.Height);
165 int maxdim = MYMAX(dim.Width, dim.Height);
167 std::map<int, scene::IMesh*>::iterator
168 it = m_extrusion_meshes.lower_bound(maxdim);
170 if (it == m_extrusion_meshes.end()) {
171 // no viable resolution found; use largest one
172 it = m_extrusion_meshes.find(MAX_EXTRUSION_MESH_RESOLUTION);
173 assert(it != m_extrusion_meshes.end());
176 scene::IMesh *mesh = it->second;
180 // Returns a 1x1x1 cube mesh with one meshbuffer (material) per face
181 // Caller must drop the returned pointer
182 scene::IMesh* createCube()
189 std::map<int, scene::IMesh*> m_extrusion_meshes;
190 scene::IMesh *m_cube;
193 ExtrusionMeshCache *g_extrusion_mesh_cache = NULL;
196 WieldMeshSceneNode::WieldMeshSceneNode(
197 scene::ISceneNode *parent,
198 scene::ISceneManager *mgr,
202 scene::ISceneNode(parent, mgr, id),
204 m_material_type(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF),
205 m_lighting(lighting),
206 m_bounding_box(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
208 m_enable_shaders = g_settings->getBool("enable_shaders");
209 m_anisotropic_filter = g_settings->getBool("anisotropic_filter");
210 m_bilinear_filter = g_settings->getBool("bilinear_filter");
211 m_trilinear_filter = g_settings->getBool("trilinear_filter");
213 // If this is the first wield mesh scene node, create a cache
214 // for extrusion meshes (and a cube mesh), otherwise reuse it
215 if (g_extrusion_mesh_cache == NULL)
216 g_extrusion_mesh_cache = new ExtrusionMeshCache();
218 g_extrusion_mesh_cache->grab();
220 // Disable bounding box culling for this scene node
221 // since we won't calculate the bounding box.
222 setAutomaticCulling(scene::EAC_OFF);
224 // Create the child scene node
225 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
226 m_meshnode = SceneManager->addMeshSceneNode(dummymesh, this, -1);
227 m_meshnode->setReadOnlyMaterials(false);
228 m_meshnode->setVisible(false);
229 dummymesh->drop(); // m_meshnode grabbed it
232 WieldMeshSceneNode::~WieldMeshSceneNode()
234 assert(g_extrusion_mesh_cache);
235 if (g_extrusion_mesh_cache->drop())
236 g_extrusion_mesh_cache = NULL;
239 void WieldMeshSceneNode::setCube(const TileSpec tiles[6],
240 v3f wield_scale, ITextureSource *tsrc)
242 scene::IMesh *cubemesh = g_extrusion_mesh_cache->createCube();
243 changeToMesh(cubemesh);
246 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR);
248 // Customize materials
249 for (u32 i = 0; i < m_meshnode->getMaterialCount(); ++i) {
251 video::SMaterial &material = m_meshnode->getMaterial(i);
252 if (tiles[i].animation_frame_count == 1) {
253 material.setTexture(0, tiles[i].texture);
255 FrameSpec animation_frame = tiles[i].frames.find(0)->second;
256 material.setTexture(0, animation_frame.texture);
258 tiles[i].applyMaterialOptions(material);
262 void WieldMeshSceneNode::setExtruded(const std::string &imagename,
263 v3f wield_scale, ITextureSource *tsrc)
265 video::ITexture *texture = tsrc->getTexture(imagename);
270 core::dimension2d<u32> dim = texture->getSize();
271 scene::IMesh *mesh = g_extrusion_mesh_cache->create(dim);
275 m_meshnode->setScale(wield_scale * WIELD_SCALE_FACTOR_EXTRUDED);
277 // Customize material
278 video::SMaterial &material = m_meshnode->getMaterial(0);
279 material.setTexture(0, texture);
280 material.MaterialType = m_material_type;
281 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
282 // Enable filtering only for high resolution texures
283 if (dim.Width > 32) {
284 material.setFlag(video::EMF_ANISOTROPIC_FILTER, m_anisotropic_filter);
285 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
286 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
288 material.setFlag(video::EMF_ANISOTROPIC_FILTER, false);
289 material.setFlag(video::EMF_BILINEAR_FILTER, false);
290 material.setFlag(video::EMF_TRILINEAR_FILTER, false);
292 // mipmaps cause "thin black line" artifacts
293 material.setFlag(video::EMF_USE_MIP_MAPS, false);
294 if (m_enable_shaders)
295 material.setTexture(2, tsrc->getTexture("disable_img.png"));
298 void WieldMeshSceneNode::setItem(const ItemStack &item, IGameDef *gamedef)
300 ITextureSource *tsrc = gamedef->getTextureSource();
301 IItemDefManager *idef = gamedef->getItemDefManager();
302 IShaderSource *shdrsrc = gamedef->getShaderSource();
303 INodeDefManager *ndef = gamedef->getNodeDefManager();
304 const ItemDefinition &def = item.getDefinition(idef);
305 const ContentFeatures &f = ndef->get(def.name);
306 content_t id = ndef->getId(def.name);
308 if (m_enable_shaders) {
309 u32 shader_id = shdrsrc->getShader("nodes_shader", TILE_MATERIAL_BASIC, NDT_NORMAL);
310 m_material_type = shdrsrc->getShaderInfo(shader_id).material;
313 // If wield_image is defined, it overrides everything else
314 if (def.wield_image != "") {
315 setExtruded(def.wield_image, def.wield_scale, tsrc);
319 // See also CItemDefManager::createClientCached()
320 else if (def.type == ITEM_NODE) {
322 // e.g. mesh nodes and nodeboxes
323 changeToMesh(f.mesh_ptr[0]);
324 // mesh_ptr[0] is pre-scaled by BS * f->visual_scale
325 m_meshnode->setScale(
326 def.wield_scale * WIELD_SCALE_FACTOR
327 / (BS * f.visual_scale));
328 } else if (f.drawtype == NDT_AIRLIKE) {
330 } else if (f.drawtype == NDT_PLANTLIKE) {
331 setExtruded(tsrc->getTextureName(f.tiles[0].texture_id), def.wield_scale, tsrc);
332 } else if (f.drawtype == NDT_NORMAL || f.drawtype == NDT_ALLFACES) {
333 setCube(f.tiles, def.wield_scale, tsrc);
335 MeshMakeData mesh_make_data(gamedef);
336 MapNode mesh_make_node(id, 255, 0);
337 mesh_make_data.fillSingleNode(&mesh_make_node);
338 MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0));
339 changeToMesh(mapblock_mesh.getMesh());
340 translateMesh(m_meshnode->getMesh(), v3f(-BS, -BS, -BS));
341 m_meshnode->setScale(
342 def.wield_scale * WIELD_SCALE_FACTOR
343 / (BS * f.visual_scale));
345 for (u32 i = 0; i < m_meshnode->getMaterialCount(); ++i) {
347 video::SMaterial &material = m_meshnode->getMaterial(i);
348 material.setFlag(video::EMF_BACK_FACE_CULLING, true);
349 material.setFlag(video::EMF_BILINEAR_FILTER, m_bilinear_filter);
350 material.setFlag(video::EMF_TRILINEAR_FILTER, m_trilinear_filter);
351 bool animated = (f.tiles[i].animation_frame_count > 1);
353 FrameSpec animation_frame = f.tiles[i].frames.find(0)->second;
354 material.setTexture(0, animation_frame.texture);
356 material.setTexture(0, f.tiles[i].texture);
358 material.MaterialType = m_material_type;
359 if (m_enable_shaders) {
360 if (f.tiles[i].normal_texture) {
362 FrameSpec animation_frame = f.tiles[i].frames.find(0)->second;
363 material.setTexture(1, animation_frame.normal_texture);
365 material.setTexture(1, f.tiles[i].normal_texture);
367 material.setTexture(2, tsrc->getTexture("enable_img.png"));
369 material.setTexture(2, tsrc->getTexture("disable_img.png"));
375 else if (def.inventory_image != "") {
376 setExtruded(def.inventory_image, def.wield_scale, tsrc);
380 // no wield mesh found
384 void WieldMeshSceneNode::setColor(video::SColor color)
387 setMeshColor(m_meshnode->getMesh(), color);
390 void WieldMeshSceneNode::render()
392 // note: if this method is changed to actually do something,
393 // you probably should implement OnRegisterSceneNode as well
396 void WieldMeshSceneNode::changeToMesh(scene::IMesh *mesh)
399 scene::IMesh *dummymesh = g_extrusion_mesh_cache->createCube();
400 m_meshnode->setVisible(false);
401 m_meshnode->setMesh(dummymesh);
402 dummymesh->drop(); // m_meshnode grabbed it
406 m_meshnode->setMesh(mesh);
409 Lighting is disabled, this means the caller can (and probably will)
410 call setColor later. We therefore need to clone the mesh so that
411 setColor will only modify this scene node's mesh, not others'.
413 scene::IMeshManipulator *meshmanip = SceneManager->getMeshManipulator();
414 scene::IMesh *new_mesh = meshmanip->createMeshCopy(mesh);
415 m_meshnode->setMesh(new_mesh);
416 new_mesh->drop(); // m_meshnode grabbed it
419 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, m_lighting);
420 // need to normalize normals when lighting is enabled (because of setScale())
421 m_meshnode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, m_lighting);
422 m_meshnode->setVisible(true);