I have a very simple fragment shader that I want to output 'gl_PrimitiveID' to the fragment buffer object (FBO) that I defined. The following is a fragment of a shader:
I configure my FBO as follows:
GLuint renderbufId0; GLuint renderbufId1; GLuint depthbufId; GLuint framebufId; // generate render and frame buffer objects glGenRenderbuffers( 1, &renderbufId0 ); glGenRenderbuffers( 1, &renderbufId1 ); glGenRenderbuffers( 1, &depthbufId ); glGenFramebuffers ( 1, &framebufId ); // setup first renderbuffer (fragColor) glBindRenderbuffer(GL_RENDERBUFFER, renderbufId0); glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, gViewWidth, gViewHeight); // setup second renderbuffer (triID) glBindRenderbuffer(GL_RENDERBUFFER, renderbufId1); glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB32UI, gViewWidth, gViewHeight); // setup depth buffer glBindRenderbuffer(GL_RENDERBUFFER, depthbufId); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, gViewWidth, gViewHeight); // setup framebuffer glBindFramebuffer(GL_FRAMEBUFFER, framebufId); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbufId0); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, renderbufId1); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbufId ); // check if everything went well GLenum stat = glCheckFramebufferStatus(GL_FRAMEBUFFER); if(stat != GL_FRAMEBUFFER_COMPLETE) { exit(0); } // setup color attachments const GLenum att[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}; glDrawBuffers(2, att); // render mesh RenderMyMesh() // copy second color attachment (triID) to local buffer glReadBuffer(GL_COLOR_ATTACHMENT1); glReadPixels(0, 0, gViewWidth, gViewHeight, GL_RED, GL_UNSIGNED_INT, data);
For some reason, does glReadPixels give me the error "GL_INVALID_OPERATION"? However, if I change the internal format of renderbufId1 from 'GL_RGB32UI' to 'GL_RGB', and I use 'GL_FLOAT' in glReadPixels instead of 'GL_UNSIGNED_INT', then everything works fine. Does anyone know why I get the error "GL_INVALID_OPERATION" and how can I solve it?
Is there an alternative way to output 'gl_PrimitiveID'?
PS: The reason I want to output 'gl_PrimitiveID', as described here: Choosing triangles in the OpenGL core profile when using glDrawElements