Problems issuing gl_PrimitiveID to the user frame buffer (FBO)

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:

#version 150 uniform vec4 colorConst; out vec4 fragColor; out uvec4 triID; void main(void) { fragColor = colorConst; triID.r = uint(gl_PrimitiveID); } 

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

0
source share
1 answer

glReadPixels (0, 0, gViewWidth, gViewHeight, GL_RED, GL_UNSIGNED_INT, data);

As stated in the OpenGL Wiki , you need to use GL_RED_INTEGER when transmitting true integer data. Otherwise, OpenGL will try to use floating point conversion.

By the way, make sure that you are using glBindFragDataLocation , to adjust, to which the output buffer the fragment shader. In addition, you can set it explicitly in the shader if you use GLSL 3.30 or higher.

+2
source

Source: https://habr.com/ru/post/1393963/


All Articles