- /*dstream<<"p.vertices.size()="<<p.vertices.size()
- <<", p.indices.size()="<<p.indices.size()
- <<std::endl;*/
-
- // Create meshbuffer
-
- // This is a "Standard MeshBuffer",
- // it's a typedeffed CMeshBuffer<video::S3DVertex>
- 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<PreMeshBuffer> m_prebuffers;
-};
-
-void MapBlock::updateMesh()
-{
- /*v3s16 p = getPosRelative();
- std::cout<<"MapBlock("<<p.X<<","<<p.Y<<","<<p.Z<<")"
- <<"::updateMesh(): ";*/
- //<<"::updateMesh()"<<std::endl;
-
- /*
- TODO: Change this to directly generate the mesh (and get rid
- of FastFaces)
- */
-
- core::list<FastFace*> *fastfaces_new = new core::list<FastFace*>;
-
- /*
- 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; y<MAP_BLOCKSIZE; y++){
- //for(s16 y=-1; y<MAP_BLOCKSIZE; y++){
- for(s16 z=0; z<MAP_BLOCKSIZE; z++){
- updateFastFaceRow(v3s16(0,y,z), MAP_BLOCKSIZE,
- v3s16(1,0,0),
- v3s16(0,1,0),
- *fastfaces_new);
- }
- }
- /*
- Go through every x,y and get right faces in rows of z+
- */
- for(s16 x=0; x<MAP_BLOCKSIZE; x++){
- //for(s16 x=-1; x<MAP_BLOCKSIZE; x++){
- for(s16 y=0; y<MAP_BLOCKSIZE; y++){
- updateFastFaceRow(v3s16(x,y,0), MAP_BLOCKSIZE,
- v3s16(0,0,1),
- v3s16(1,0,0),
- *fastfaces_new);
- }
- }
- /*
- Go through every y,z and get back faces in rows of x+
- */
- for(s16 z=0; z<MAP_BLOCKSIZE; z++){
- //for(s16 z=-1; z<MAP_BLOCKSIZE; z++){
- for(s16 y=0; y<MAP_BLOCKSIZE; y++){
- updateFastFaceRow(v3s16(0,y,z), MAP_BLOCKSIZE,
- v3s16(1,0,0),
- v3s16(0,0,1),
- *fastfaces_new);
- }
- }
-
- scene::SMesh *mesh_new = NULL;
-
- mesh_new = new scene::SMesh();
-
- if(fastfaces_new->getSize() > 0)
- {
- MeshCollector collector;
-
- core::list<FastFace*>::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 "<<fastfaces_new->getSize()<<" faces "
- <<"and uses "<<mesh_new->getMeshBufferCount()
- <<" materials (meshbuffers)"<<std::endl;*/
- }
-
- /*
- Clear temporary FastFaces
- */
-
- core::list<FastFace*>::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; z<MAP_BLOCKSIZE; z++)
- for(s16 y=0; y<MAP_BLOCKSIZE; y++)
- for(s16 x=0; x<MAP_BLOCKSIZE; x++)
- {
- v3s16 p(x,y,z);
-
- MapNode &n = getNodeRef(x,y,z);
-
- if(n.d == CONTENT_LIGHT)
- {
- //scene::IMeshBuffer *buf = new scene::SMeshBuffer();
- scene::SMeshBuffer *buf = new scene::SMeshBuffer();
- video::SColor c(255,255,255,255);
-
- video::S3DVertex vertices[4] =
- {
- video::S3DVertex(-BS/2,-BS/2,0, 0,0,0, c, 0,1),
- video::S3DVertex(BS/2,-BS/2,0, 0,0,0, c, 1,1),
- video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
- video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
- };
-
- v3s16 dir = unpackDir(n.dir);
-
- for(s32 i=0; i<4; i++)
- {
- if(dir == v3s16(1,0,0))
- vertices[i].Pos.rotateXZBy(0);
- if(dir == v3s16(-1,0,0))
- vertices[i].Pos.rotateXZBy(180);
- if(dir == v3s16(0,0,1))
- vertices[i].Pos.rotateXZBy(90);
- if(dir == v3s16(0,0,-1))
- vertices[i].Pos.rotateXZBy(-90);
- if(dir == v3s16(0,-1,0))
- vertices[i].Pos.rotateXZBy(45);
- if(dir == v3s16(0,1,0))
- vertices[i].Pos.rotateXZBy(-45);
-
- vertices[i].Pos += intToFloat(p + getPosRelative());
- }
-
- u16 indices[] = {0,1,2,2,3,0};
- buf->append(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; i<c; i++)
- {
- IMeshBuffer *buf = mesh_old->getMeshBuffer(i);
- }*/
- // Drop the mesh
- mesh_old->drop();
- //delete mesh_old;
- }
-
- mesh_mutex.Unlock();
-
- //std::cout<<"added "<<fastfaces.getSize()<<" faces."<<std::endl;