The problem I'm having is converting the code(note that it's not my code) on this page to Irrlicht in such a way that it'll yield good performance: http://dave.uesp.net/wiki/Block_Land_5
This is the solution I came up with:
Code: Select all
u32 NumVertices = 0;
void chunk::CreateMeshChunk()
{
SMeshBuffer *buf = nullptr;
buf = new SMeshBuffer();
Mesh->addMeshBuffer(buf);
buf->drop();
u32 Block;
u32 MaxSize = 256;
if(NumVertices) buf->Vertices.reallocate(NumVertices);
for (u32 BlockType = 1; BlockType <= 1; ++BlockType )
{
NumVertices = 0;
for (u32 z = MinZ; z < CHUNK_SIZE + MinZ; ++z)
{
for (u32 y = MinY; y < CHUNK_SIZE + MinY; ++y)
{
for (u32 x = MinX; x < CHUNK_SIZE + MinZ; ++x)
{
if(GetBlock(x,y,z) != BlockType) continue;
Block = BlockType;
if (x > 0) Block = GetBlock(x-1,y,z);
if (Block == 0)
{
buf->Vertices.set_used(NumVertices+4);
buf->Vertices[NumVertices] = S3DVertex(vector3df(x, y, z+1), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x, y+1, z+1), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x, y+1, z), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z), vector3df(-1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
buf->Indices.push_back(NumVertices);
buf->Indices.push_back(NumVertices+1);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+3);
buf->Indices.push_back(NumVertices);
NumVertices += 4;
}
//x+1
Block = BlockType;
if (x < 0 + MaxSize - 1) Block = GetBlock(x+1,y,z);
if (Block == 0)
{
buf->Vertices.set_used(NumVertices+4);
buf->Vertices[NumVertices] = S3DVertex(vector3df(x+1, y, z), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(0, 1));
buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y+1, z), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(1, 1));
buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y+1, z+1), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(1, 0));
buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x+1, y, z+1), vector3df(1,0,0), SColor(255, 255,255,255), vector2df(0, 0));
buf->Indices.push_back(NumVertices);
buf->Indices.push_back(NumVertices+1);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+3);
buf->Indices.push_back(NumVertices);
NumVertices += 4;
}
//y-1
Block = BlockType;
if (y > 0) Block = GetBlock(x,y-1,z);
if (Block == 0)
{
buf->Vertices.set_used(NumVertices+4);
buf->Vertices[NumVertices+0] = S3DVertex(vector3df(x, y, z), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(0, 1));
buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y, z), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(1, 1));
buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y, z+1), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(1, 0));
buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z+1), vector3df(0,-1,0), SColor(255, 255,255,255), vector2df(0, 0));
buf->Indices.push_back(NumVertices);
buf->Indices.push_back(NumVertices+1);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+3);
buf->Indices.push_back(NumVertices);
NumVertices += 4;
}
//y+1
Block = BlockType;
if (y < 0 + MaxSize - 1) Block = GetBlock(x,y+1,z);
if (Block == 0)
{
buf->Vertices.set_used(NumVertices+4);
buf->Vertices[NumVertices+0] = S3DVertex(vector3df(x, y+1, z+1), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(0, 1));
buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y+1, z+1), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(1, 1));
buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y+1, z), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(1, 0));
buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y+1, z), vector3df(0,1,0), SColor(255, 255,255,255), vector2df(0, 0));
buf->Indices.push_back(NumVertices);
buf->Indices.push_back(NumVertices+1);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+3);
buf->Indices.push_back(NumVertices);
NumVertices += 4;
}
//z-1
Block = BlockType;
if (z > 0) Block = GetBlock(x,y,z-1);
if (Block == 0)
{
buf->Vertices.set_used(NumVertices+4);
buf->Vertices[NumVertices+0] = S3DVertex(vector3df(x, y+1, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(0, 1));
buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y+1, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(1, 1));
buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(1, 0));
buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y, z), vector3df(0,0,-1), SColor(255, 255,255,255), vector2df(0, 0));
buf->Indices.push_back(NumVertices);
buf->Indices.push_back(NumVertices+1);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+3);
buf->Indices.push_back(NumVertices);
NumVertices += 4;
}
//z+1
Block = BlockType;
if (z < 0 + MaxSize - 1) Block = GetBlock(x,y,z+1);
if (Block == 0)
{
buf->Vertices.set_used(NumVertices+4);
buf->Vertices[NumVertices] = S3DVertex(vector3df(x, y, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(0, 1));
buf->Vertices[NumVertices+1] = S3DVertex(vector3df(x+1, y, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(1, 1));
buf->Vertices[NumVertices+2] = S3DVertex(vector3df(x+1, y+1, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(1, 0));
buf->Vertices[NumVertices+3] = S3DVertex(vector3df(x, y+1, z+1), vector3df(0,0,1), SColor(255, 255,255,255), vector2df(0, 0));
buf->Indices.push_back(NumVertices);
buf->Indices.push_back(NumVertices+1);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+2);
buf->Indices.push_back(NumVertices+3);
buf->Indices.push_back(NumVertices);
NumVertices += 4;
}
}
}
}
}
Mesh->recalculateBoundingBox();
}
The second problem is that I get poor FPS. 30 FPS when I've rendered only 3100 out of 4096 chunks and the second thread is sleeped for 100 milliseconds constantly in a while loop. In Ogre I got around 150 FPS. I assume that Ogre automatically took care of some kind of culling or hardware settings that I should have done myself in Irrlicht. Any ideas? Here's the function that renders the meshes:
Code: Select all
void Main::RenderChunk(SMesh* Mesh)
{
IMeshSceneNode* MeshNode = IrrScnMgr->addMeshSceneNode(Mesh);
MeshNode->setMaterialFlag(video::EMF_LIGHTING, false);
MeshNode->setMaterialType(video::EMT_SOLID);
MeshNode->setAutomaticCulling(EAC_FRUSTUM_SPHERE);
MeshNode->setMaterialTexture(0, IrrDriver->getTexture("C:/Users/Zodiac/Desktop/Irrlicht/Irrlicht Engine/media/grass_1024.jpg"));
}
I would be very grateful if someone could help me. Thanks!