Problems outputting gl_PrimitiveID to custom frame buffer object (FBO)

我们两清 提交于 2019-12-11 06:23:47

问题


I have a very basic fragment shader which I want to output 'gl_PrimitiveID' to a fragment buffer object (FBO) which I have defined. Below is my fragment shader:

#version 150

uniform vec4 colorConst;

out vec4 fragColor;
out uvec4 triID;

void main(void)
{ 
   fragColor = colorConst;
   triID.r = uint(gl_PrimitiveID);
}

I setup my FBO like this:

  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 glReadPixels gives me a 'GL_INVALID_OPERATION' error? 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 am getting the 'GL_INVALID_OPERATION' error and how I can solve it?

Is there an alternative way of outputting 'gl_PrimitiveID'?

PS: The reason I want to output 'gl_PrimitiveID' like this is explained here: Picking triangles in OpenGL core profile when using glDrawElements


回答1:


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

As stated on the OpenGL Wiki, you need to use GL_RED_INTEGER when transferring true integer data. Otherwise, OpenGL will try to use floating-point conversion on it.

BTW, make sure you're using glBindFragDataLocation to set up which buffers those fragment shader outputs go to. Alternatively, you can set it up explicitly in the shader if you're using GLSL 3.30 or above.



来源:https://stackoverflow.com/questions/9264346/problems-outputting-gl-primitiveid-to-custom-frame-buffer-object-fbo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!