/* Minetest Copyright (C) 2010-2013 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "mesh.h" #include "log.h" #include #include #include #include #include // In Irrlicht 1.8 the signature of ITexture::lock was changed from // (bool, u32) to (E_TEXTURE_LOCK_MODE, u32). #if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR <= 7 #define MY_ETLM_READ_ONLY true #else #define MY_ETLM_READ_ONLY video::ETLM_READ_ONLY #endif scene::IAnimatedMesh* createCubeMesh(v3f scale) { video::SColor c(255,255,255,255); video::S3DVertex vertices[24] = { // Up video::S3DVertex(-0.5,+0.5,-0.5, 0,1,0, c, 0,1), video::S3DVertex(-0.5,+0.5,+0.5, 0,1,0, c, 0,0), video::S3DVertex(+0.5,+0.5,+0.5, 0,1,0, c, 1,0), video::S3DVertex(+0.5,+0.5,-0.5, 0,1,0, c, 1,1), // Down video::S3DVertex(-0.5,-0.5,-0.5, 0,-1,0, c, 0,0), video::S3DVertex(+0.5,-0.5,-0.5, 0,-1,0, c, 1,0), video::S3DVertex(+0.5,-0.5,+0.5, 0,-1,0, c, 1,1), video::S3DVertex(-0.5,-0.5,+0.5, 0,-1,0, c, 0,1), // Right video::S3DVertex(+0.5,-0.5,-0.5, 1,0,0, c, 0,1), video::S3DVertex(+0.5,+0.5,-0.5, 1,0,0, c, 0,0), video::S3DVertex(+0.5,+0.5,+0.5, 1,0,0, c, 1,0), video::S3DVertex(+0.5,-0.5,+0.5, 1,0,0, c, 1,1), // Left video::S3DVertex(-0.5,-0.5,-0.5, -1,0,0, c, 1,1), video::S3DVertex(-0.5,-0.5,+0.5, -1,0,0, c, 0,1), video::S3DVertex(-0.5,+0.5,+0.5, -1,0,0, c, 0,0), video::S3DVertex(-0.5,+0.5,-0.5, -1,0,0, c, 1,0), // Back video::S3DVertex(-0.5,-0.5,+0.5, 0,0,1, c, 1,1), video::S3DVertex(+0.5,-0.5,+0.5, 0,0,1, c, 0,1), video::S3DVertex(+0.5,+0.5,+0.5, 0,0,1, c, 0,0), video::S3DVertex(-0.5,+0.5,+0.5, 0,0,1, c, 1,0), // Front video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1), video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0), video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0), video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1), }; u16 indices[6] = {0,1,2,2,3,0}; scene::SMesh *mesh = new scene::SMesh(); for (u32 i=0; i<6; ++i) { scene::IMeshBuffer *buf = new scene::SMeshBuffer(); buf->append(vertices + 4 * i, 4, indices, 6); // Set default material buf->getMaterial().setFlag(video::EMF_LIGHTING, false); buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; // Add mesh buffer to mesh mesh->addMeshBuffer(buf); buf->drop(); } scene::SAnimatedMesh *anim_mesh = new scene::SAnimatedMesh(mesh); mesh->drop(); scaleMesh(anim_mesh, scale); // also recalculates bounding box return anim_mesh; } static scene::IAnimatedMesh* extrudeARGB(u32 twidth, u32 theight, u8 *data) { const s32 argb_wstep = 4 * twidth; const s32 alpha_threshold = 1; scene::IMeshBuffer *buf = new scene::SMeshBuffer(); video::SColor c(255,255,255,255); // Front and back { video::S3DVertex vertices[8] = { video::S3DVertex(-0.5,-0.5,-0.5, 0,0,-1, c, 0,1), video::S3DVertex(-0.5,+0.5,-0.5, 0,0,-1, c, 0,0), video::S3DVertex(+0.5,+0.5,-0.5, 0,0,-1, c, 1,0), video::S3DVertex(+0.5,-0.5,-0.5, 0,0,-1, c, 1,1), video::S3DVertex(+0.5,-0.5,+0.5, 0,0,+1, c, 1,1), video::S3DVertex(+0.5,+0.5,+0.5, 0,0,+1, c, 1,0), video::S3DVertex(-0.5,+0.5,+0.5, 0,0,+1, c, 0,0), video::S3DVertex(-0.5,-0.5,+0.5, 0,0,+1, c, 0,1), }; u16 indices[12] = {0,1,2,2,3,0,4,5,6,6,7,4}; buf->append(vertices, 8, indices, 12); } // "Interior" // (add faces where a solid pixel is next to a transparent one) u8 *solidity = new u8[(twidth+2) * (theight+2)]; u32 wstep = twidth + 2; for (u32 y = 0; y < theight + 2; ++y) { u8 *scanline = solidity + y * wstep; if (y == 0 || y == theight + 1) { for (u32 x = 0; x < twidth + 2; ++x) scanline[x] = 0; } else { scanline[0] = 0; u8 *argb_scanline = data + (y - 1) * argb_wstep; for (u32 x = 0; x < twidth; ++x) scanline[x+1] = (argb_scanline[x*4+3] >= alpha_threshold); scanline[twidth + 1] = 0; } } // without this, there would be occasional "holes" in the mesh f32 eps = 0.01; for (u32 y = 0; y <= theight; ++y) { u8 *scanline = solidity + y * wstep + 1; for (u32 x = 0; x <= twidth; ++x) { if (scanline[x] && !scanline[x + wstep]) { u32 xx = x + 1; while (scanline[xx] && !scanline[xx + wstep]) ++xx; f32 vx1 = (x - eps) / (f32) twidth - 0.5; f32 vx2 = (xx + eps) / (f32) twidth - 0.5; f32 vy = 0.5 - (y - eps) / (f32) theight; f32 tx1 = x / (f32) twidth; f32 tx2 = xx / (f32) twidth; f32 ty = (y - 0.5) / (f32) theight; video::S3DVertex vertices[8] = { video::S3DVertex(vx1,vy,-0.5, 0,-1,0, c, tx1,ty), video::S3DVertex(vx2,vy,-0.5, 0,-1,0, c, tx2,ty), video::S3DVertex(vx2,vy,+0.5, 0,-1,0, c, tx2,ty), video::S3DVertex(vx1,vy,+0.5, 0,-1,0, c, tx1,ty), }; u16 indices[6] = {0,1,2,2,3,0}; buf->append(vertices, 4, indices, 6); x = xx - 1; } if (!scanline[x] && scanline[x + wstep]) { u32 xx = x + 1; while (!scanline[xx] && scanline[xx + wstep]) ++xx; f32 vx1 = (x - eps) / (f32) twidth - 0.5; f32 vx2 = (xx + eps) / (f32) twidth - 0.5; f32 vy = 0.5 - (y + eps) / (f32) theight; f32 tx1 = x / (f32) twidth; f32 tx2 = xx / (f32) twidth; f32 ty = (y + 0.5) / (f32) theight; video::S3DVertex vertices[8] = { video::S3DVertex(vx1,vy,-0.5, 0,1,0, c, tx1,ty), video::S3DVertex(vx1,vy,+0.5, 0,1,0, c, tx1,ty), video::S3DVertex(vx2,vy,+0.5, 0,1,0, c, tx2,ty), video::S3DVertex(vx2,vy,-0.5, 0,1,0, c, tx2,ty), }; u16 indices[6] = {0,1,2,2,3,0}; buf->append(vertices, 4, indices, 6); x = xx - 1; } } } for (u32 x = 0; x <= twidth; ++x) { u8 *scancol = solidity + x + wstep; for (u32 y = 0; /* Minetest Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifndef L_NODEMETA_H_ #define L_NODEMETA_H_ #include "lua_api/l_base.h" #include "irrlichttypes_bloated.h" class ServerEnvironment; class NodeMetadata; /* NodeMetaRef */ class NodeMetaRef : public ModApiBase { private: v3s16 m_p; ServerEnvironment *m_env; static const char className[]; static const luaL_reg methods[]; static NodeMetaRef *checkobject(lua_State *L, int narg); /** * Retrieve metadata for a node. * If @p auto_create is set and the specified node has no metadata information * associated with it yet, the method attempts to attach a new metadata object * to the node and returns a pointer to the metadata when successful. * * However, it is NOT guaranteed that the method will return a pointer, * and @c NULL may be returned in case of an error regardless of @p auto_create. * * @param ref specifies the node for which the associated metadata is retrieved. * @param auto_create when true, try to create metadata information for the node if it has none. * @return pointer to a @c NodeMetadata object or @c NULL in case of error. */ static NodeMetadata* getmeta(NodeMetaRef *ref, bool auto_create); static void reportMetadataChange(NodeMetaRef *ref); // Exported functions // garbage collector static int gc_object(lua_State *L); // get_string(self, name) static int l_get_string(lua_State *L); // set_string(self, name, var) static int l_set_string(lua_State *L); // get_int(self, name) static int l_get_int(lua_State *L); // set_int(self, name, var) static int l_set_int(lua_State *L); // get_float(self, name) static int l_get_float(lua_State *L); // set_float(self, name, var) static int l_set_float(lua_State *L); // get_inventory(self) static int l_get_inventory(lua_State *L); // to_table(self) static int l_to_table(lua_State *L); // from_table(self, table) static int l_from_table(lua_State *L); public: NodeMetaRef(v3s16 p, ServerEnvironment *env); ~NodeMetaRef(); // Creates an NodeMetaRef and leaves it on top of stack // Not callable from Lua; all references are created on the C side. static void create(lua_State *L, v3s16 p, ServerEnvironment *env); static void Register(lua_State *L); }; #endif