/* (c) 2010 Perttu Ahola */ #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(u8 material, 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 == MATERIAL_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->material = material; 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(); // 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); return light; } catch(InvalidPositionException &e) { return 0; } } /* Gets node material from any place relative to block. Returns MATERIAL_AIR if doesn't exist. */ u8 MapBlock::getNodeMaterial(v3s16 p) { try{ MapNode n = getNodeParent(p); return n.d; } catch(InvalidPositionException &e) { return MATERIAL_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; /* The light in the air lights the surface is taken from the node that is air. */ u8 light = getFaceLight(p, face_dir); u16 continuous_materials_count = 0; u8 material0 = getNodeMaterial(p); u8 material1 = getNodeMaterial(p + face_dir); for(u16 j=0; j *fastfaces_new = new core::list; //TimeTaker timer1("updateMesh1", g_device); /* 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) { mesh_new = new scene::SMesh(); scene::IMeshBuffer *buf = NULL; /* Buffer for lesser calls to mesh_new->getMeshBuffer(g_materials[f->material]), which is slow. key = material id, value = meshbuffer of that material */ core::map bufs; core::list::Iterator i = fastfaces_new->begin(); // MATERIAL_AIR shouldn't be used by any face u8 material_in_use = MATERIAL_AIR; for(; i != fastfaces_new->end(); i++) { FastFace *f = *i; if(f->material != material_in_use || buf == NULL) { // Try to get a meshbuffer associated with the material core::map::Node* n = bufs.find(f->material); if(n != NULL) { buf = n->getValue(); } else { buf = mesh_new->getMeshBuffer(g_materials[f->material]); // If not found, create one if(buf == NULL) { // This is a "Standard MeshBuffer", // it's a typedeffed CMeshBuffer buf = new scene::SMeshBuffer(); bufs[f->material] = buf; // Set material ((scene::SMeshBuffer*)buf)->Material = g_materials[f->material]; // Use VBO //buf->setHardwareMappingHint(scene::EHM_STATIC); // Add to mesh mesh_new->addMeshBuffer(buf); // Mesh grabbed it buf->drop(); } } material_in_use = f->material; } u16 indices[] = {0,1,2,2,3,0}; //TimeTaker timer("", g_device); buf->append(f->vertices, 4, indices, 6); //appendtime += timer.stop(true); } // 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"< & 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(buflen); dest[0] = is_underground; for(u32 i=0; i materialdata(nodecount); for(u32 i=0; i paramdata(nodecount); 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); } } // All other versions else { 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