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

Qt Code:
  1. QOpenGLBuffer m_vertexBuffer;
  2. QOpenGLBuffer m_colorBuffer;
  3. QOpenGLVertexArrayObject m_vao;
  4. QOpenGLShaderProgram *m_program;
To copy to clipboard, switch view to plain text mode 


Qt Code:
  1. void GLSurfaceWidget::initializeGL()
  2. {
  3. // Initialize OpenGL Backend
  4. initializeOpenGLFunctions();
  5. // printVersionInformation();
  6.  
  7. qglClearColor(QColor(125,125,140));
  8.  
  9. glEnable(GL_DEPTH_TEST);
  10. glEnable(GL_CULL_FACE);
  11. glShadeModel(GL_SMOOTH);
  12. glEnable(GL_LIGHTING);
  13. glEnable(GL_LIGHT0);
  14.  
  15. static GLfloat lightPosition[4] = { 0, 0, 10, 1.0 };
  16. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  17.  
  18. GLfloat xMax, yMax, zMax;
  19. xMax = 2;
  20. yMax = 2;
  21. zMax = 2;
  22.  
  23. std::vector<QVector3D> boxTriangles;
  24. std::vector<QVector3D> boxColors;
  25.  
  26. boxTriangles.push_back(QVector3D(0, 0, zMax)); boxColors.push_back(QVector3D(1,1,1));
  27. boxTriangles.push_back(QVector3D(xMax, 0, zMax)); boxColors.push_back(QVector3D(1,1,1));
  28. boxTriangles.push_back(QVector3D(0, yMax, zMax)); boxColors.push_back(QVector3D(1,1,1));
  29. boxTriangles.push_back(QVector3D(0, yMax, zMax)); boxColors.push_back(QVector3D(1,1,1));
  30. boxTriangles.push_back(QVector3D(xMax, 0, zMax)); boxColors.push_back(QVector3D(1,1,1));
  31. boxTriangles.push_back(QVector3D(xMax, yMax, zMax)); boxColors.push_back(QVector3D(1,1,1));
  32.  
  33. int positionSize = boxTriangles.size() * sizeof(QVector3D);
  34. int colorSize = boxColors.size() * sizeof(QVector3D);
  35.  
  36. // Create Shader (Do not release until VAO is created)
  37. m_program = new QOpenGLShaderProgram();
  38.  
  39. m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
  40. "#version 120\n" //330\n"
  41. "void main(void)\n"
  42. "{\n"
  43. "gl_FrontColor = gl_Color;\n"
  44. "gl_Position = ftransform();\n"
  45. "}");
  46.  
  47. m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
  48. "#version 120\n"
  49. "void main()\n"
  50. "{\n"
  51. "gl_FragColor = gl_Color;\n"
  52. "}\n");
  53.  
  54. m_program->link();
  55. m_program->bind();
  56. int positionAttr = m_program->attributeLocation("position");
  57. int colorAttr = m_program->attributeLocation("color");
  58. // int matrixAttr = m_program->attributeLocation("matrix");
  59.  
  60. // Create Vertex Buffer (Do not release until VAO is created)
  61. m_vertexBuffer = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
  62. m_vertexBuffer.create();
  63. m_vertexBuffer.bind();
  64. m_vertexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
  65. m_vertexBuffer.allocate(&(boxTriangles.front()), positionSize + colorSize);
  66.  
  67. // Create Vertex Array Object
  68. m_vao.create();
  69. m_vao.bind();
  70. m_program->enableAttributeArray(positionAttr);
  71. m_program->enableAttributeArray(colorAttr);
  72. // (int location, GLenum type, int offset, int tupleSize, int stride = 0);
  73. m_program->setAttributeBuffer(positionAttr, GL_FLOAT, 0, sizeof(QVector3D), 0);
  74. m_program->setAttributeBuffer(colorAttr, GL_FLOAT, 0, sizeof(QVector3D), 0);
  75.  
  76. // Release (unbind) all
  77. m_vao.release();
  78. m_vertexBuffer.release();
  79. m_program->release();
  80.  
  81. }
To copy to clipboard, switch view to plain text mode 


Qt Code:
  1. void GLSurfaceWidget::paintGL()
  2. {
  3. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  4.  
  5. glMatrixMode(GL_PROJECTION);
  6. glLoadIdentity();
  7. gluPerspective(45 + m_zoomOffset, m_aspect, 0.1, 100);
  8.  
  9. glMatrixMode(GL_MODELVIEW);
  10. glLoadIdentity();
  11.  
  12. glTranslatef(0.0, 0.0, -10.0);
  13. glRotatef(m_xRot / 16.0, 1.0, 0.0, 0.0);
  14. glRotatef(m_yRot / 16.0, 0.0, 1.0, 0.0);
  15. glRotatef(m_zRot / 16.0, 0.0, 0.0, 1.0);
  16.  
  17. draw();
  18.  
  19. // Render using our shader
  20. m_program->bind();
  21. {
  22. m_vao.bind();
  23. glDrawArrays(GL_TRIANGLES, 0, 6);//sizeof(sg_vertexes) / sizeof(sg_vertexes[0]));
  24. m_vao.release();
  25. }
  26. m_program->release();
  27. }
To copy to clipboard, switch view to plain text mode