I have an array of IDs for each vertex in my model. I want to render these to a texture so that I can check which vertex was picked in the viewport. My code looks like this

Qt Code:
  1. #include "MyQOpenGLWidget.h"
  2. #include <iostream>
  3. #include <QToolTip>
  4.  
  5. MyQOpenGLWidget::MyQOpenGLWidget(QWidget* parent) : QOpenGLWidget(parent)
  6. {
  7. m_program = 0;
  8. m_fboProgram = 0;
  9.  
  10. m_vertices[0] = -1.0f;
  11. m_vertices[1] = 1.0f;
  12. m_vertices[2] = -1.0f;
  13. m_vertices[3] = 0.95f;
  14. m_vertices[4] = -0.95f;
  15. m_vertices[5] = 1.0f;
  16.  
  17. m_vertices[6] = 1.0f;
  18. m_vertices[7] = 1.0f;
  19. m_vertices[8] = 1.0f;
  20. m_vertices[9] = 0.95f;
  21. m_vertices[10] = 0.95f;
  22. m_vertices[11] = 1.0f;
  23.  
  24. m_vertices[12] = 1.0f;
  25. m_vertices[13] = -1.0f;
  26. m_vertices[14] = 1.0f;
  27. m_vertices[15] = -0.95f;
  28. m_vertices[16] = 0.95f;
  29. m_vertices[17] = -1.0f;
  30.  
  31. m_vertices[18] = -1.0f;
  32. m_vertices[19] = -1.0f;
  33. m_vertices[20] = -1.0f;
  34. m_vertices[21] = -0.95f;
  35. m_vertices[22] = -0.95f;
  36. m_vertices[23] = -1.0f;
  37.  
  38. m_id[0] = 1;
  39. m_id[1] = 2;
  40. m_id[2] = 3;
  41. m_id[3] = 4;
  42. m_id[4] = 5;
  43. m_id[5] = 6;
  44. m_id[6] = 7;
  45. m_id[7] = 8;
  46. m_id[8] = 9;
  47. m_id[9] = 10;
  48. m_id[10] = 11;
  49. m_id[11] = 12;
  50. }
  51.  
  52. MyQOpenGLWidget::~MyQOpenGLWidget(void)
  53. {
  54. makeCurrent();
  55.  
  56. delete m_program;
  57.  
  58. m_vbo.destroy();
  59. m_vao.destroy();
  60.  
  61. doneCurrent();
  62. }
  63.  
  64. void MyQOpenGLWidget::initializeGL()
  65. {
  66. initializeOpenGLFunctions();
  67.  
  68. m_program = new QOpenGLShaderProgram();
  69. m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "vshader.glsl");
  70. m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, "fshader.glsl");
  71. m_program->link();
  72. m_program->bind();
  73.  
  74. m_vbo.create();
  75. m_vbo.bind();
  76. m_vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
  77. m_vbo.allocate(m_vertices, sizeof(m_vertices));
  78.  
  79. m_vao.create();
  80. m_vao.bind();
  81. m_program->enableAttributeArray(0);
  82. m_program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
  83.  
  84. m_vao.release();
  85. m_vbo.release();
  86. m_program->release();
  87.  
  88.  
  89.  
  90. m_fboProgram = new QOpenGLShaderProgram();
  91. m_fboProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, "vshaderPicking.glsl");
  92. m_fboProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, "fshaderPicking.glsl");
  93. m_fboProgram->link();
  94. m_fboProgram->bind();
  95.  
  96. m_vboId.create();
  97. m_vboId.bind();
  98. m_vboId.setUsagePattern(QOpenGLBuffer::StaticDraw);
  99. m_vboId.allocate(m_id, sizeof(m_id));
  100.  
  101. m_vbo.bind();
  102.  
  103. m_vaoId.create();
  104. m_vaoId.bind();
  105. m_fboProgram->enableAttributeArray(0);
  106. m_fboProgram->setAttributeBuffer(0, GL_UNSIGNED_INT, 0, 1, 0);
  107. m_fboProgram->enableAttributeArray(1);
  108. m_fboProgram->setAttributeBuffer(1, GL_FLOAT, 0, 2, 0);
  109.  
  110. m_vaoId.release();
  111. m_vboId.release();
  112. m_vbo.release();
  113. m_fboProgram->release();
  114.  
  115. glViewport(0, 0, this->width(), this->height());
  116. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  117. glPointSize(2);
  118.  
  119. glGenTextures(1, &m_idTexture);
  120. glBindTexture(GL_TEXTURE_2D, m_idTexture);
  121. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  122. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  123. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  124. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  125. glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, this->width(), this->height(), 0, GL_RED_INTEGER, GL_UNSIGNED_INT, 0);
  126. glBindTexture(GL_TEXTURE_2D, 0);
  127.  
  128. glGenTextures(1, &m_depthTexture);
  129. glBindTexture(GL_TEXTURE_2D, m_depthTexture);
  130. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  131. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  132. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  133. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  134. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, this->width(), this->height(), 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
  135. glBindTexture(GL_TEXTURE_2D, 0);
  136.  
  137. glGenFramebuffers(1, &m_pickingFBO);
  138. glBindFramebuffer(GL_FRAMEBUFFER, m_pickingFBO);
  139. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_idTexture, 0);
  140. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);
  141. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  142. }
  143.  
  144. void MyQOpenGLWidget::resizeGL(int w, int h)
  145. {
  146. glViewport(0, 0, w, h);
  147. }
  148.  
  149. void MyQOpenGLWidget::paintGL()
  150. {
  151. m_program->bind();
  152. {
  153. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  154. m_vao.bind();
  155. glDrawArrays(GL_POINTS, 0, 12);
  156. m_vao.release();
  157. }
  158. m_program->release();
  159.  
  160. m_fboProgram->bind();
  161. {
  162. glBindFramebuffer(GL_FRAMEBUFFER, m_pickingFBO);
  163. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  164. m_vaoId.bind();
  165. glDrawArrays(GL_POINTS, 0, 12);
  166. m_vaoId.release();
  167.  
  168. GLint* imagen = new GLint[this->width()*this->height()];
  169. memset(imagen, 0, this->width()*this->height()*sizeof(GLint));
  170. glReadPixels(0, 0, this->width(), this->height(), GL_RED_INTEGER, GL_UNSIGNED_INT, imagen);
  171.  
  172. int v;
  173. for(auto i = 0; i < this->height(); i++)
  174. {
  175. for(auto j = 0; j < this->width(); j++)
  176. {
  177. v = imagen[i*this->width()+j];
  178. std::cout << v << ",";
  179. }
  180. std::cout << std::endl;
  181. }
  182. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  183. }
  184. m_fboProgram->release();
  185. }
  186.  
  187. void MyQOpenGLWidget::mousePressEvent(QMouseEvent* e)
  188. {
  189. QToolTip::showText(e->globalPos(), QString::number(e->pos().x()) + "," + QString::number(e->pos().x()), this, rect(), 0);
  190. QOpenGLWidget::mouseMoveEvent(e);
  191. }
To copy to clipboard, switch view to plain text mode 

and the shaders

Qt Code:
  1. //VERTEX
  2. #version 410
  3.  
  4. layout(location=0) in uint vId;
  5. layout(location=1) in vec2 vPosition;
  6.  
  7. flat out uint fId;
  8.  
  9. void main()
  10. {
  11. gl_Position = vec4(vPosition, 1.0, 1.0);
  12.  
  13. fId = vId;
  14. }
  15. //FRAGMENT
  16. #version 410
  17.  
  18. flat in uint fId;
  19. in vec2 fPosition;
  20.  
  21. out uint fragId;
  22.  
  23. void main()
  24. {
  25. fragId = fId;
  26. }
To copy to clipboard, switch view to plain text mode 

I can see that there is a number being rendered for each vertex in the model, however, the value of this number is not the IDs corresponding to each vertex. In my example code I expect to have a value betwen 1-12 and the result after reading the texture with glReadPixels(...) looks like

Qt Code:
  1. 1048431821,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1048444928,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  2. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  3. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  4. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  5. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  6. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  7. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  8. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  9. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  10. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  11. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  12. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  13. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  14. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  15. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  16. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1048444928,0,0
  17. ,0,0,0,0,0,0,0,0,0,0,0,0,0,
To copy to clipboard, switch view to plain text mode 

where you can see that the background has a value of 0 and the actual vertices have very big values. Can someone spot any problem in my code? Some suggested that
Qt Code:
  1. m_fboProgram->enableAttributeArray(0);
  2. m_fboProgram->setAttributeBuffer(0, GL_UNSIGNED_INT, 0, 1, 0);
To copy to clipboard, switch view to plain text mode 
may be the root of the problems, but then I don't know what to use.