PDA

View Full Version : Problem with Vertex Buffer/Index Buffer Combination [Qt 4.8, C++]



Christian Lohr
04-26-2012, 02:42 PM
Hello,
I have the problem, that when I use Vertex Buffer Array in combination with Index Buffer Arrays, nothing appears on the screen. If I'm using only a Vertex Buffer Array, that works fine. I uploaded the project: example_VertexAttrib.zip (works with Qt 4.8 and above)

My Header includes both arrays:


QVector<float> vertices_;
QVector<short> indices_;


I'm initializing both arrays in the Constructor:


/* Push back all vertices */
vertices_.reserve(16);
vertices_
<< -1 << -1 << -1 // a 0
<< 1 << -1 << -1 // b 1
<< 1 << 1 << -1 // c 2
<< -1 << 1 << -1 // d 3

<< -1 << -1 << 1 // e 4
<< 1 << -1 << 1 // f 5
<< 1 << 1 << 1 // g 6
<< -1 << 1 << 1; // h 7

/* Push back all indices */
indices_.reserve(16);
indices_
<< 0 << 1 << 2 << 3 // front
<< 4 << 5 << 6 << 7 // rear
<< 1 << 5 << 6 << 2 // right
<< 4 << 0 << 3 << 7; // left


Buffer Objects are prepared inside QGLWidget::initializeGL()


static const int VERTICES = 0;
static const int INDICES = 1;

// -- Buffer Objects --
GLuint bufferObjectVertices;
GLuint bufferObjectIndices;

QGLFunctions::glGenBuffers(1, &bufferObjectVertices);
QGLFunctions::glGenBuffers(1, &bufferObjectIndices);

QGLFunctions::glBindBuffer(GL_ARRAY_BUFFER, bufferObjectVertices);
QGLFunctions::glBufferData(GL_ARRAY_BUFFER, vertices_.size() * sizeof(float), &vertices_.front(), GL_STATIC_DRAW);

QGLFunctions::glBindBuffer(GL_ELEMENT_ARRAY_BUFFER , bufferObjectIndices);
QGLFunctions::glBufferData(GL_ELEMENT_ARRAY_BUFFER , indices_.size() * sizeof(short), &indices_.front(), GL_STATIC_DRAW);

QGLFunctions::glVertexAttribPointer(VERTICES, 3, GL_FLOAT, false, 0, 0);
QGLFunctions::glEnableVertexAttribArray(VERTICES);

GLuint programId = shaderProg_.programId();

QGLFunctions::glBindAttribLocation(programId, VERTICES, "Vertex");


And I'm trying to draw them inside QGLWidget::paintGL()

glDrawElements(GL_QUADS, indices_.size(), GL_SHORT, 0);

V-man
04-28-2012, 05:32 PM
Did you check if all is OK? Did you use glGetError()?

http://www.opengl.org/wiki/Common_Mistakes#glGetError

Christian Lohr
05-02-2012, 12:53 PM
Good idea. I fixed it (it was an invalid enum). Instead of


glDrawElements(GL_QUADS, indices_.size(), GL_SHORT, 0);

I wrote



glDrawElements(GL_QUADS, indices_.size(), GL_UNSIGNED_SHORT, 0);