glGetTexImage不适用于无符号整数纹理

时间:2014-05-21 06:29:44

标签: opengl textures

我使用整数纹理并将其绑定到帧缓冲区以获取渲染数据。

我将它绑定到四边形以在屏幕上显示它,并且我确定纹理的内容是正确的。

但是当我使用glGetTexImage来获取纹理的内容时,我会得到随机数。

这是创建帧缓冲区的代码:

glEnable(GL_TEXTURE_2D); 
glGenFramebuffers(1, &param.fbo);
glGenTextures(1,&param.triTex);
glBindTexture(GL_TEXTURE_2D, param.triTex); 

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE); 

glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, param.fboSize, param.fboSize, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, 0);
glBindFramebuffer(GL_FRAMEBUFFER, param.fbo); 
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, param.triTex, 0);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);

纹理和帧缓冲渲染没有问题。

这是获取内容的代码:

glBindTexture(GL_TEXTURE_2D, param.triTex);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
triTexData = (unsigned int *)malloc(param.fboSize * param.fboSize * sizeof(unsigned int));
memset(triTexData, 0, sizeof(unsigned int) * param.fboSize * param.fboSize);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, triTexData);
glBindTexture(GL_TEXTURE_2D,0);

片段着色器和几何着色器

#version 330
flat in int color;
out vec4 fragColor;
void main(void)     
{
    fragColor = vec4(color,0.0,0.0,0.0);
}

#version 330
layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
flat out int color;
void main(void)
{
gl_Position = gl_in[0].gl_Position;
color = gl_PrimitiveIDIn;
EmitVertex();
gl_Position = gl_in[1].gl_Position;
color = gl_PrimitiveIDIn;
EmitVertex();
gl_Position = gl_in[2].gl_Position;
color = gl_PrimitiveIDIn;
EmitVertex();
EndPrimitive();
};

1 个答案:

答案 0 :(得分:2)

您的片段着色器实际上应该以这种方式编写,以输出到GL_R32UI图像:

#version 330

flat in  int  color;
     out uint fragColor;

void main (void) {
  fragColor = color;
}

目前,您没有回读"随机数字数字,您只是看到当您将浮点数据解释为整数而没有正确转换时会发生什么。 不愉快,是吗?