将 gl_PrimitiveID 输出到自定义帧缓冲区对象 (FBO) 时出现问题

发布于 2025-01-04 22:25:16 字数 2503 浏览 0 评论 0原文

我有一个非常基本的片段着色器,我想将其输出“gl_PrimitiveID”到我定义的片段缓冲区对象(FBO)。下面是我的片段着色器:

#version 150

uniform vec4 colorConst;

out vec4 fragColor;
out uvec4 triID;

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

我像这样设置我的 FBO:

  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);

出于某种原因,glReadPixels 给了我一个“GL_INVALID_OPERATION”错误?但是,如果我将 renderbufId1 的内部格式从“GL_RGB32UI”更改为“GL_RGB”,并且在 glReadPixels 中使用“GL_FLOAT”而不是“GL_UNSIGNED_INT”,则一切正常。有谁知道为什么我收到“GL_INVALID_OPERATION”错误以及如何解决它?

是否有输出“gl_PrimitiveID”的替代方法?

PS:我想像这样输出'gl_PrimitiveID'的原因解释如下: 使用 glDrawElements 时在 OpenGL 核心配置文件中选取三角形

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2025-01-11 22:25:16

glReadPixels(0, 0, gViewWidth, gViewHeight, GL_RED, GL_UNSIGNED_INT, 数据);

正如OpenGL Wiki所述,您需要使用GL_RED_INTEGER当传输真整数数据时。否则,OpenGL将尝试对其使用浮点转换。

顺便说一句,请确保您使用 glBindFragDataLocation 进行设置它缓冲那些片段着色器输出。或者,如果您使用的是 GLSL 3.30 或多于。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文