I'm attempting to write simple voxelized terrain engine using heightmaps and chunked approach. I wanted to construct separate mesh for each chunk and load the meshes into separate VBOs. I've prepared all the logic, but I don't seem to get VBO rendering to work properly. I prepared very simple heightmap for testing which should render only 2 lines of voxels 16x16x2 = 512 cubes 12 triangles each overall. The problem is my result looks nothing like it should - here's what I mean:
Click
It looks very similar no matter how far I zoom in or out or twist the camera, the lines just move a bit.
Here's how I'm trying to do this:
Constructing buffers for paricular mesh represented by CMesh class:
void CMesh::generateVertexBufferData()
{
this->vertexBuffer = new GLuint();
glGenBuffers(1,vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, *this->vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*meshVertexData->size(),&meshVertexData[0],GL_STATIC_DRAW);
this->vertexCount = meshVertexData->size();
}
void CMesh::generateIndexBufferData()
{
this->indexBuffer = new GLuint();
glGenBuffers(1,this->indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *this->indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*meshIndexData->size(),&meshIndexData[0],GL_STATIC_DRAW);
this->indexCount = meshIndexData->size();
}
Rendering done by GeometryManager class:
glPushMatrix();
pRenderer->ImmediateColorAlpha(1.0f,1.0f,1.0f,1.0f);
pRenderer->SetRenderMode(GL_LINE);
pRenderer->TranslateWorldMatrix(geometryPosition.x*Chunk::CHUNK_SIZE,
geometryPosition.y*Chunk::CHUNK_SIZE,
geometryPosition.z*Chunk::CHUNK_SIZE);
glBindBuffer(GL_ARRAY_BUFFER,*meshArray->at(geometryID)->getBuffer(VERTEX_BUFFER));
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,*meshArray->at(geometryID)->getBuffer(INDEX_BUFFER));
glDrawElements(GL_TRIANGLES,meshArray->at(geometryID)->indexCount,GL_UNSIGNED_INT,BUFFER_OFFSET(0));
glPopMatrix();
I have checked weather I'm calculating vertex positions correctly by rendering them out as points straight from the std::vector* meshVertexData using simple immediate mode like this:
for(int i=0; i < meshArray->at(geometryID)->meshVertexData->size(); i+=3)
{
glBegin(GL_POINTS);
glVertex3f(meshArray->at(geometryID)->meshVertexData->at(i),
meshArray->at(geometryID)->meshVertexData->at(i+1),
meshArray->at(geometryID)->meshVertexData->at(i+2));
glEnd();
}
And the result seems to be correct.
I'm completely stumped about this, I'm not sure what happens between populating VBO with data and rendering that makes it look like it does. I humbly ask for help in this matter, I don't really see what am I doing wrong. Is my approach completely flawed or I'm missing something simple?
If anyone is seriously interested in helping me with solving the problem, but needs more information I'm willing to provide anything you ask me for including full CMesh and GeometryManager code on pastebin.