/* Minetest-c55 Copyright (C) 2010 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 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 General Public License for more details. You should have received a copy of the GNU 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 "mapblock.h" #include "map.h" // For g_materials #include "main.h" #include "light.h" #include /* MapBlock */ bool MapBlock::isValidPositionParent(v3s16 p) { if(isValidPosition(p)) { return true; } else{ return m_parent->isValidPosition(getPosRelative() + p); } } MapNode MapBlock::getNodeParent(v3s16 p) { if(isValidPosition(p) == false) { return m_parent->getNode(getPosRelative() + p); } else { if(data == NULL) throw InvalidPositionException(); return data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X]; } } void MapBlock::setNodeParent(v3s16 p, MapNode & n) { if(isValidPosition(p) == false) { m_parent->setNode(getPosRelative() + p, n); } else { if(data == NULL) throw InvalidPositionException(); data[p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X] = n; } } FastFace * MapBlock::makeFastFace(u16 tile, u8 light, v3f p, v3f dir, v3f scale, v3f posRelative_f) { FastFace *f = new FastFace; // Position is at the center of the cube. v3f pos = p * BS; posRelative_f *= BS; v3f vertex_pos[4]; // If looking towards z+, this is the face that is behind // the center point, facing towards z+. vertex_pos[0] = v3f( BS/2,-BS/2,BS/2); vertex_pos[1] = v3f(-BS/2,-BS/2,BS/2); vertex_pos[2] = v3f(-BS/2, BS/2,BS/2); vertex_pos[3] = v3f( BS/2, BS/2,BS/2); /* TODO: Rotate it the right way (one side comes upside down) */ core::CMatrix4 m; m.buildRotateFromTo(v3f(0,0,1), dir); for(u16 i=0; i<4; i++){ m.rotateVect(vertex_pos[i]); vertex_pos[i].X *= scale.X; vertex_pos[i].Y *= scale.Y; vertex_pos[i].Z *= scale.Z; vertex_pos[i] += pos + posRelative_f; } f32 abs_scale = 1.; if (scale.X < 0.999 || scale.X > 1.001) abs_scale = scale.X; else if(scale.Y < 0.999 || scale.Y > 1.001) abs_scale = scale.Y; else if(scale.Z < 0.999 || scale.Z > 1.001) abs_scale = scale.Z; v3f zerovector = v3f(0,0,0); u8 li = decode_light(light); //u8 li = 150; u8 alpha = 255; //if(material == CONTENT_WATER || material == CONTENT_OCEAN) if(tile == CONTENT_WATER || tile == CONTENT_OCEAN) //if(tile == TILE_WATER) { alpha = 128; } video::SColor c = video::SColor(alpha,li,li,li); /*f->vertices[0] = video::S3DVertex(vertex_pos[0], zerovector, c, core::vector2d(0,1)); f->vertices[1] = video::S3DVertex(vertex_pos[1], zerovector, c, core::vector2d(abs_scale,1)); f->vertices[2] = video::S3DVertex(vertex_pos[2], zerovector, c, core::vector2d(abs_scale,0)); f->vertices[3] = video::S3DVertex(vertex_pos[3], zerovector, c, core::vector2d(0,0));*/ f->vertices[0] = video::S3DVertex(vertex_pos[0], zerovector, c, core::vector2d(0,1)); f->vertices[1] = video::S3DVertex(vertex_pos[1], zerovector, c, core::vector2d(abs_scale,1)); f->vertices[2] = video::S3DVertex(vertex_pos[2], zerovector, c, core::vector2d(abs_scale,0)); f->vertices[3] = video::S3DVertex(vertex_pos[3], zerovector, c, core::vector2d(0,0)); f->tile = tile; //DEBUG //f->tile = TILE_GRASS; return f; } /* Parameters must consist of air and !air. Order doesn't matter. If either of the nodes doesn't exist, light is 0. */ u8 MapBlock::getFaceLight(v3s16 p, v3s16 face_dir) { try{ MapNode n = getNodeParent(p); MapNode n2 = getNodeParent(p + face_dir); u8 light; /*if(n.solidness() < n2.solidness()) light = n.getLight(); else light = n2.getLight();*/ if(n.getLight() > n2.getLight()) light = n.getLight(); else light = n2.getLight(); // Make some nice difference to different sides /*if(face_dir.X == 1 || face_dir.Z == 1 || face_dir.Y == -1) light = diminish_light(diminish_light(light)); else if(face_dir.X == -1 || face_dir.Z == -1) light = diminish_light(light);*/ if(face_dir.X == 1 || face_dir.X == -1 || face_dir.Y == -1) light = diminish_light(diminish_light(light)); else if(face_dir.Z == 1 || face_dir.Z == -1) light = diminish_light(light); return light; } catch(InvalidPositionException &e) { return 0; } } /* Gets node tile from any place relative to block. Returns CONTENT_IGNORE if doesn't exist or should not be drawn. */ u16 MapBlock::getNodeTile(v3s16 p, v3s16 face_dir) { try{ MapNode n = getNodeParent(p); //return content_tile(n.d); return n.getTile(face_dir); } catch(InvalidPositionException &e) { //return CONTENT_IGNORE; return TILE_NONE; } } u8 MapBlock::getNodeContent(v3s16 p) { try{ MapNode n = getNodeParent(p); return n.d; } catch(InvalidPositionException &e) { return CONTENT_IGNORE; } } /* startpos: translate_dir: unit vector with only one of x, y or z face_dir: unit vector with only one of x, y or z */ void MapBlock::updateFastFaceRow(v3s16 startpos, u16 length, v3s16 translate_dir, v3s16 face_dir, core::list &dest) { /* Precalculate some variables */ v3f translate_dir_f(translate_dir.X, translate_dir.Y, translate_dir.Z); // floating point conversion v3f face_dir_f(face_dir.X, face_dir.Y, face_dir.Z); // floating point conversion v3f posRelative_f(getPosRelative().X, getPosRelative().Y, getPosRelative().Z); // floating point conversion v3s16 p = startpos; /* Get face light at starting position */ u8 light = getFaceLight(p, face_dir); u16 continuous_tiles_count = 0; u8 tile0 = getNodeTile(p, face_dir); u8 tile1 = getNodeTile(p + face_dir, -face_dir); for(u16 j=0; j indices; core::array vertices; }; class MeshCollector { public: void append( video::SMaterial material, const video::S3DVertex* const vertices, u32 numVertices, const u16* const indices, u32 numIndices ) { PreMeshBuffer *p = NULL; for(u32 i=0; ivertices.size(); for(u32 i=0; i 65535) { dstream<<"FIXME: Meshbuffer ran out of indices"<indices.push_back(j); } for(u32 i=0; ivertices.push_back(vertices[i]); } } void fillMesh(scene::SMesh *mesh) { /*dstream<<"Filling mesh with "< scene::SMeshBuffer *buf = new scene::SMeshBuffer(); // Set material buf->Material = p.material; //((scene::SMeshBuffer*)buf)->Material = p.material; // Use VBO //buf->setHardwareMappingHint(scene::EHM_STATIC); // Add to mesh mesh->addMeshBuffer(buf); // Mesh grabbed it buf->drop(); buf->append(p.vertices.pointer(), p.vertices.size(), p.indices.pointer(), p.indices.size()); } } private: core::array m_prebuffers; }; void MapBlock::updateMesh() { /*v3s16 p = getPosRelative(); std::cout<<"MapBlock("< *fastfaces_new = new core::list; /* We are including the faces of the trailing edges of the block. This means that when something changes, the caller must also update the meshes of the blocks at the leading edges. */ /* Go through every y,z and get top faces in rows of x+ */ for(s16 y=0; ygetSize() > 0) { MeshCollector collector; core::list::Iterator i = fastfaces_new->begin(); for(; i != fastfaces_new->end(); i++) { FastFace *f = *i; const u16 indices[] = {0,1,2,2,3,0}; /*collector.append(g_materials[f->material], f->vertices, 4, indices, 6);*/ /*collector.append(g_materials[f->tile], f->vertices, 4, indices, 6);*/ collector.append(g_tile_materials[f->tile], f->vertices, 4, indices, 6); } collector.fillMesh(mesh_new); // Use VBO for mesh (this just would set this for ever buffer) //mesh_new->setHardwareMappingHint(scene::EHM_STATIC); /*std::cout<<"MapBlock has "<getSize()<<" faces " <<"and uses "<getMeshBufferCount() <<" materials (meshbuffers)"<::Iterator i; i = fastfaces_new->begin(); for(; i != fastfaces_new->end(); i++) { delete *i; } fastfaces_new->clear(); delete fastfaces_new; /* Add special graphics: - torches TODO: Optimize by using same meshbuffer for same textures */ /*scene::ISceneManager *smgr = NULL; video::IVideoDriver* driver = NULL; if(g_device) { smgr = g_device->getSceneManager(); driver = smgr->getVideoDriver(); }*/ for(s16 z=0; zappend(vertices, 4, indices, 6); // Set material buf->getMaterial().setFlag(video::EMF_LIGHTING, false); buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false); buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; if(dir == v3s16(0,-1,0)) buf->getMaterial().setTexture(0, g_texturecache.get("torch_on_floor")); else if(dir == v3s16(0,1,0)) buf->getMaterial().setTexture(0, g_texturecache.get("torch_on_ceiling")); // For backwards compatibility else if(dir == v3s16(0,0,0)) buf->getMaterial().setTexture(0, g_texturecache.get("torch_on_floor")); else buf->getMaterial().setTexture(0, g_texturecache.get("torch")); // Add to mesh mesh_new->addMeshBuffer(buf); buf->drop(); } } /* Do some stuff to the mesh */ mesh_new->recalculateBoundingBox(); /* Delete new mesh if it is empty */ if(mesh_new->getMeshBufferCount() == 0) { mesh_new->drop(); mesh_new = NULL; } /* Replace the mesh */ mesh_mutex.Lock(); scene::SMesh *mesh_old = mesh; mesh = mesh_new; if(mesh_old != NULL) { // Remove hardware buffers of meshbuffers of mesh // NOTE: No way, this runs in a different thread and everything /*u32 c = mesh_old->getMeshBufferCount(); for(u32 i=0; igetMeshBuffer(i); }*/ // Drop the mesh mesh_old->drop(); //delete mesh_old; } mesh_mutex.Unlock(); //std::cout<<"added "< & light_sources) { // Whether the sunlight at the top of the bottom block is valid bool block_below_is_valid = true; v3s16 pos_relative = getPosRelative(); for(s16 x=0; x &dest) { }*/ /* Serialization */ void MapBlock::serialize(std::ostream &os, u8 version) { if(!ser_ver_supported(version)) throw VersionMismatchException("ERROR: MapBlock format not supported"); if(data == NULL) { throw SerializationError("ERROR: Not writing dummy block."); } // These have no compression if(version <= 3 || version == 5 || version == 6) { u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; u32 buflen = 1 + nodecount * MapNode::serializedLength(version); SharedBuffer dest(buflen); dest[0] = is_underground; for(u32 i=0; i materialdata(nodecount); for(u32 i=0; i lightdata(nodecount); for(u32 i=0; i= 10) { // Get and compress pressure SharedBuffer pressuredata(nodecount); for(u32 i=0; i databuf(nodecount*3); // Get contents for(u32 i=0; i d(len); is.read((char*)*d, len); if(is.gcount() != len) throw SerializationError ("MapBlock::deSerialize: no enough input data"); data[i].deSerialize(*d, version); } } else if(version <= 10) { u32 nodecount = MAP_BLOCKSIZE*MAP_BLOCKSIZE*MAP_BLOCKSIZE; u8 t8; is.read((char*)&t8, 1); is_underground = t8; { // Uncompress and set material data std::ostringstream os(std::ios_base::binary); decompress(is, os, version); std::string s = os.str(); if(s.size() != nodecount) throw SerializationError ("MapBlock::deSerialize: invalid format"); for(u32 i=0; i= 10) { // Uncompress and set pressure data std::ostringstream os(std::ios_base::binary); decompress(is, os, version); std::string s = os.str(); if(s.size() != nodecount) throw SerializationError ("MapBlock::deSerialize: invalid format"); for(u32 i=0; i