Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I still don't think that there is a clear answer here. So here is how I made it work through a 2D texture:</p> <pre><code>// First, create a frame buffer: glGenFramebuffers(1, &amp;fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); // Then generate your texture and define an unsigned int array: glGenTextures(1, &amp;textureid); glBindTexture(GL_TEXTURE_2D, textureid); glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, w, h, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Attach it to the frame buffer object: glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, textureid, 0); // Before rendering glBindFramebuffer(GL_FRAMEBUFFER, fbo); GLuint buffers[2] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 }; // this assumes that there is another texture that is for the render buffer. Color attachment1 is preserved for the element ids. glDrawBuffers(2, buffers); // Clear and render here glFlush(); // Flush after just in case glBindFramebuffer(GL_FRAMEBUFFER, 0); </code></pre> <p>On the GLSL side the fragment shader should have (4.3 core profile code here):</p> <pre><code>layout(location = 0) out vec4 colorOut; // The first element in 'buffers' so location 0 layout(location = 1) out uvec4 elementID; // The second element in 'buffers' so location 1. unsigned int vector as color // ... void main() { //... elementID = uvec4( elementid, 0, 0, 0 ); // Write the element id as integer to the red channel. } </code></pre> <p>You can read the values on the host side:</p> <pre><code>unsigned int* ids = new unsigned int[ w*h ]; glBindTexture(GL_TEXTURE_2D, textureid); glGetTexImage(GL_TEXTURE_2D, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, ids); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload