PDA

View Full Version : Using vertex & index buffer objects for <QOpenGLWidget>



ruuruu62
21st March 2015, 21:05
Hi ,

I have been having some difficulty in properly linking my index buffer object to the shader program and was hoping some one could spot the issue. Currently the program is quite strait forward and tries to simply draw a triangle that is initialzed in the initializeGL() function.



void myglclass::initializeGL()
{
initializeOpenGLFunctions();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glClearColor(0, 0, 0, 1);
m_projection.setToIdentity();
m_projection.perspective(fov, aspect, zNear, zFar);
initShaders();
translation.setToIdentity();

for (int i = 1; i < FRAME_WIDTH; i++) indarr[i]=(GLushort)i; // index = 1, 2, 3
FRAME[0] = QVector3D(-1.0f, 1.0f, 2.0f); //simple 3 vert. trianlge
FRAME[1] = QVector3D(-1.0f, -1.0f, 2.0f);
FRAME[3] = QVector3D(1.0f, 1.0f, 2.0f);

vbo.create();
ind.create();
vbo.bind();
vbo.allocate(FRAME, FRAME_WIDTH * sizeof(QVector3D));
ind.bind();
ind.allocate(indarr, FRAME_WIDTH * sizeof(GLushort));
qDebug() << "intialized";
}


The main issue is when I try to bind my QOpenGLBuffer to the shader program. I can't seem to figure out how I specify one as an index buffer and the other as the vertex buffer when I call my draw function.



void myglclass::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program.setUniformValue("qt_mvpMatrix", m_projection);
vbo.bind();
ind.bind();
GLint vertexLocation = program.attributeLocation("qt_Vertex");
program.enableAttributeArray(vertexLocation);
program.setAttributeBuffer(vertexLocation, GL_FLOAT,0,3,sizeof(QVector3D));


glDrawElements(GL_TRIANGLES, FRAME_WIDTH/3, GL_FLOAT, 0);
}


For your reference this is how I defined the relevant data types.



#define FRAME_WIDTH 3
private:
QMatrix4x4 m_projection,translation;
QOpenGLShaderProgram program;
QOpenGLBuffer ind,vbo;
QVector3D FRAME[FRAME_WIDTH];
GLushort indarr[FRAME_WIDTH];


Any suggestions or comments are much appreciated. I've gotten everything working with simpler data types & static draw calls but using the shader programs has been a bit tricky.