From a18525a14eca664dc93be01e55b0efd1ce08b0fa Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 29 Nov 2010 14:03:40 +0200 Subject: fixed face updating slowness bug --- src/mapblock.cpp | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 116 insertions(+), 4 deletions(-) (limited to 'src/mapblock.cpp') diff --git a/src/mapblock.cpp b/src/mapblock.cpp index 25561008a..5ca874946 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -283,6 +283,98 @@ void MapBlock::updateFastFaceRow(v3s16 startpos, } } +/* + This is used because CMeshBuffer::append() is very slow +*/ +struct PreMeshBuffer +{ + video::SMaterial material; + core::array 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::IMeshBuffer *buf = new scene::SMeshBuffer(); + // Set 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(); @@ -344,7 +436,25 @@ void MapBlock::updateMesh() if(fastfaces_new->getSize() > 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); + } + mesh_new = new scene::SMesh(); + + collector.fillMesh(mesh_new); + +#if 0 scene::IMeshBuffer *buf = NULL; core::list::Iterator i = fastfaces_new->begin(); @@ -377,17 +487,19 @@ void MapBlock::updateMesh() } material_in_use = f->material; } - - u16 indices[] = {0,1,2,2,3,0}; - buf->append(f->vertices, 4, indices, 6); + + u16 new_indices[] = {0,1,2,2,3,0}; + + //buf->append(f->vertices, 4, indices, 6); } +#endif // 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"<