This is sort of a branch off of another thread now that the problem has changed. I have successfully initialized an QGLWidget, verified that the shaders loaded and linked, and that the textures loaded and were bound correctly. Now my OpenGL code displays only the clear color and not my glDrawElements call. Here is the QGLWidget object code and my shaders:


Qt Code:
  1. #include "myglwidget.h"
  2. #include <QDebug>
  3. #include <QString>
  4. #include <QVector3D>
  5. #include <QVector2D>
  6. #include "locale.h"
  7.  
  8. #define BACK_TILE_S 48
  9.  
  10. struct VertexData
  11. {
  12. QVector3D position;
  13. QVector2D texCoord;
  14. };
  15.  
  16. MyGLWidget::MyGLWidget(QWidget *parent) :
  17. QGLWidget(parent)
  18. {
  19.  
  20. }
  21.  
  22. MyGLWidget::~MyGLWidget(){
  23. glDeleteBuffers(2, vboid);
  24. }
  25.  
  26. void MyGLWidget::initializeGL(){
  27. VertexData verts[] = {
  28. {QVector3D(0, 0, 0), QVector2D(0, 0)},
  29. {QVector3D(0, 1, 0), QVector2D(1, 0)},
  30. {QVector3D(1, 0, 0), QVector2D(0, 1)},
  31. {QVector3D(1, 1, 0), QVector2D(1, 1)}
  32. };
  33. GLushort indices[] = {
  34. 0, 1, 2, 3
  35. };
  36. initializeGLFunctions();
  37.  
  38.  
  39. setlocale(LC_NUMERIC, "C");
  40.  
  41. default_prog.addShaderFromSourceFile(QGLShader::Vertex, ":/shaders/default.vert");
  42. default_prog.addShaderFromSourceFile(QGLShader::Fragment, ":/shaders/default.frag");
  43. default_prog.link();
  44.  
  45. default_prog.bind(); // this is in initgl since this is the
  46. // only shader we use... FOR NOW, therefore
  47. // we dont have to do it elsewhere
  48. setlocale(LC_ALL, "");
  49.  
  50. glDisable(GL_DEPTH_TEST);
  51. glDisable(GL_CULL_FACE);
  52. glEnable(GL_TEXTURE_2D);
  53. // normally here we would have the QImage buffered up already
  54. // and would bind as needed, but again, this is the only
  55. // texture we use for now
  56. back_texture = bindTexture(QImage(":/gfx/gfx/back.png"));
  57. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  58. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  59. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  60. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  61.  
  62.  
  63. glGenBuffers(2, vboid);
  64.  
  65. glBindBuffer(GL_ARRAY_BUFFER, vboid[0]);
  66. glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(VertexData), verts, GL_STATIC_DRAW);
  67. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboid[1]);
  68. glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLushort), indices, GL_STATIC_DRAW);
  69. glClearColor(1,0,0,1);
  70. }
  71.  
  72. void MyGLWidget::paintGL(){
  73. int texw;
  74. int texh;
  75. QMatrix4x4 proj;
  76. QMatrix2x2 scale;
  77. quintptr offset;
  78. int vertexLoc;
  79. int texcoordLoc;
  80. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  81. texw = (floor(width / ((double) BACK_TILE_S)) + 1);
  82. texh = (floor(height / ((double) BACK_TILE_S)) + 1);
  83.  
  84. proj.setToIdentity();
  85. proj.scale(texw * BACK_TILE_S, texh * BACK_TILE_S);
  86. proj.ortho(0, width, 0, height, -1, 1);
  87.  
  88. scale.setToIdentity();
  89. scale(0,0) = texw;
  90. scale(1,1) = texh;
  91.  
  92. default_prog.setUniformValue("texScale", scale);
  93. default_prog.setUniformValue("mvp_matrix", proj);
  94. default_prog.setUniformValue("texture", 0);
  95.  
  96. glBindBuffer(GL_ARRAY_BUFFER, vboid[0]);
  97. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboid[1]);
  98.  
  99. offset = 0;
  100.  
  101. vertexLoc = default_prog.attributeLocation("a_position");
  102. default_prog.enableAttributeArray(vertexLoc);
  103. glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void*) offset);
  104.  
  105. offset += sizeof(QVector3D);
  106.  
  107. texcoordLoc = default_prog.attributeLocation("a_texcoord");
  108. default_prog.enableAttributeArray(texcoordLoc);
  109. glVertexAttribPointer(texcoordLoc, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const void*) offset);
  110.  
  111. glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
  112. }
  113.  
  114.  
  115. void MyGLWidget::resizeGL(int w, int h){
  116. this->width = w;
  117. this->height = h;
  118. // glViewport(0, 0, w, h);
  119. }
To copy to clipboard, switch view to plain text mode 

default.vert
Qt Code:
  1. #version 150
  2.  
  3. uniform mat4x4 mvp_matrix;
  4. uniform mat2x2 texScale;
  5.  
  6. in vec4 a_position;
  7. in vec2 a_texcoord;
  8.  
  9. out vec2 v_texcoord;
  10.  
  11. void main() {
  12. v_texcoord = a_texcoord * texScale;
  13. gl_Position = mvp_matrix * a_position;
  14. }
To copy to clipboard, switch view to plain text mode 

default.frag
Qt Code:
  1. #version 150
  2.  
  3. uniform sampler2D texture;
  4.  
  5. in vec2 v_texcoord;
  6.  
  7. void main()
  8. {
  9. gl_FragColor = texture2D(texture, v_texcoord);
  10. }
To copy to clipboard, switch view to plain text mode