将gl_PrimitiveID输出到自定义帧缓冲区对象(FBO)时出现问题 [英] Problems outputting gl_PrimitiveID to custom frame buffer object (FBO)

查看:293
本文介绍了将gl_PrimitiveID输出到自定义帧缓冲区对象(FBO)时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我这样设置我的FBO:

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

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

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?

是否有另一种输出'gl_PrimitiveID'的方法?

Is there an alternative way of outputting 'gl_PrimitiveID'?

PS:我想这样输出'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

推荐答案

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

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

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

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.

顺便说一句,请确保您使用的是 glBindFragDataLocation 来设置哪些缓冲区这些片段着色器输出将转到.另外,如果您使用的是GLSL 3.30或以下版本,则可以在着色器中明确设置.以上.

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.

这篇关于将gl_PrimitiveID输出到自定义帧缓冲区对象(FBO)时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆