0
votes

I am trying to render terrain from a heightmap. I currently load the height map and can render it using vertex arrays with glBegin/glEnd but I need to do it with the use of VBOs. Since I'm using Qt the suggested method is QGLBuffer.

What am I doing wrong? vboV is a QGLBuffer which I initialize inside the initializeGL() function.

void GLWidget::initializeGL()
{
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glClearColor(0,0,0,0);

vboV=new QGLBuffer(QGLBuffer::VertexBuffer);
vboV->create();
vboV->bind();
vboV->setUsagePattern(QGLBuffer::StaticDraw);
vboV->allocate(img->height()*img->width()*18);
vboV->release();
}

void GLWidget::paintGL()
{

//the commented section is using vertex array which works fine
//    glEnableClientState(GL_VERTEX_ARRAY);
//    glEnableClientState(GL_COLOR_ARRAY);

//    glVertexPointer(3,GL_FLOAT,0,f);
//    glColorPointer(3,GL_FLOAT,0,colorf);
//    glDrawArrays(GL_TRIANGLES,0,img->height()*img->width()*6);

//    glDisableClientState(GL_COLOR_ARRAY);
//    glDisableClientState(GL_VERTEX_ARRAY);

vboV->bind();
glEnableClientState( GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,f);
glDrawArrays(GL_TRIANGLES,0,img->height()*img->width());
glDisableClientState(GL_VERTEX_ARRAY);
vboV->release();
}

I'm not sure what to change to make it work. The program runs, but the terrain does not render (it's just black).

f is an array of floats which contains {x1, y1, z1, x2, y2, z2...}; the positions of the vertices, which are set counter-clockwise. I don't know if the memory is allocated correctly, nor am I sure of glDrawArrays.

I'm using Qt 4.8.1 and my OS is Windows 7.

1
Are you setting the vertex colours properly? - Chris
Where do you load the VBO with data? Somewhere hopefully there's a call to glBufferData or similar. - radical7
I'm not setting vertex colours. And I don't know how to load data to that buffer, there is no function glBufferData with QGLBuffer. - Emil Smęt

1 Answers

0
votes

You have to add

vboV->write(0, img->data(), img->width() * img->height() * 18);

Or you simply use the overloaded allocate method:

vboV->allocate(img->data(), img->height() * img->width() * 18);

Of course, you need to change the img->data() in dependence of your datatype of img.