PDA

View Full Version : QGLWidget + QOpenGLVertexArrayObject = no output



alketi
17th April 2015, 20:05
I'm using Qt 5.2 and a QGLWidget.

I'm simply attempting to create a white square using two triangles, but something is going wrong -- no output appears and, of course, there's no feedback as to why.

I'm sure I've screwed up something with either the vertex buffer and/or the shader language. And, I'm hoping it's pretty obvious...

Can someone point me in the right direction to get this working?

Cheers



QOpenGLBuffer m_vertexBuffer;
QOpenGLBuffer m_colorBuffer;
QOpenGLVertexArrayObject m_vao;
QOpenGLShaderProgram *m_program;




void GLSurfaceWidget::initializeGL()
{
// Initialize OpenGL Backend
initializeOpenGLFunctions();
// printVersionInformation();

qglClearColor(QColor(125,125,140));

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

static GLfloat lightPosition[4] = { 0, 0, 10, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);

GLfloat xMax, yMax, zMax;
xMax = 2;
yMax = 2;
zMax = 2;

std::vector<QVector3D> boxTriangles;
std::vector<QVector3D> boxColors;

boxTriangles.push_back(QVector3D(0, 0, zMax)); boxColors.push_back(QVector3D(1,1,1));
boxTriangles.push_back(QVector3D(xMax, 0, zMax)); boxColors.push_back(QVector3D(1,1,1));
boxTriangles.push_back(QVector3D(0, yMax, zMax)); boxColors.push_back(QVector3D(1,1,1));
boxTriangles.push_back(QVector3D(0, yMax, zMax)); boxColors.push_back(QVector3D(1,1,1));
boxTriangles.push_back(QVector3D(xMax, 0, zMax)); boxColors.push_back(QVector3D(1,1,1));
boxTriangles.push_back(QVector3D(xMax, yMax, zMax)); boxColors.push_back(QVector3D(1,1,1));

int positionSize = boxTriangles.size() * sizeof(QVector3D);
int colorSize = boxColors.size() * sizeof(QVector3D);

// Create Shader (Do not release until VAO is created)
m_program = new QOpenGLShaderProgram();

m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
"#version 120\n" //330\n"
"void main(void)\n"
"{\n"
"gl_FrontColor = gl_Color;\n"
"gl_Position = ftransform();\n"
"}");

m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
"#version 120\n"
"void main()\n"
"{\n"
"gl_FragColor = gl_Color;\n"
"}\n");

m_program->link();
m_program->bind();
int positionAttr = m_program->attributeLocation("position");
int colorAttr = m_program->attributeLocation("color");
// int matrixAttr = m_program->attributeLocation("matrix");

// Create Vertex Buffer (Do not release until VAO is created)
m_vertexBuffer = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
m_vertexBuffer.create();
m_vertexBuffer.bind();
m_vertexBuffer.setUsagePattern(QOpenGLBuffer::Stat icDraw);
m_vertexBuffer.allocate(&(boxTriangles.front()), positionSize + colorSize);

// Create Vertex Array Object
m_vao.create();
m_vao.bind();
m_program->enableAttributeArray(positionAttr);
m_program->enableAttributeArray(colorAttr);
// (int location, GLenum type, int offset, int tupleSize, int stride = 0);
m_program->setAttributeBuffer(positionAttr, GL_FLOAT, 0, sizeof(QVector3D), 0);
m_program->setAttributeBuffer(colorAttr, GL_FLOAT, 0, sizeof(QVector3D), 0);

// Release (unbind) all
m_vao.release();
m_vertexBuffer.release();
m_program->release();

}




void GLSurfaceWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45 + m_zoomOffset, m_aspect, 0.1, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0, 0.0, -10.0);
glRotatef(m_xRot / 16.0, 1.0, 0.0, 0.0);
glRotatef(m_yRot / 16.0, 0.0, 1.0, 0.0);
glRotatef(m_zRot / 16.0, 0.0, 0.0, 1.0);

draw();

// Render using our shader
m_program->bind();
{
m_vao.bind();
glDrawArrays(GL_TRIANGLES, 0, 6);//sizeof(sg_vertexes) / sizeof(sg_vertexes[0]));
m_vao.release();
}
m_program->release();
}