GLSL中的textureProj给出nan和inf

时间:2017-06-07 16:47:47

标签: c++ opengl glsl texture-mapping uv-mapping

以下情景:

我正在创建一个OpenGL纹理和一个帧缓冲区以及相关的对象

GLuint frameBuffer = 0, colorBuffer, depthBuffer;

glGenFramebuffers(1, &frameBuffer);

int maskRenderingWidth = 1 * settings.width, maskRenderingHeight = 1 * settings.height;

//define texture 
glUseProgram(screenMaskRenderingProgramId);
GLuint texture;
glGenTextures(1, &texture);

glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, maskRenderingWidth, maskRenderingHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glPixelStorei(GL_PACK_ALIGNMENT, 1);


glUseProgram(offscreeenMaskRenderingProgramId);

//setup framebuffer stuff
glGenRenderbuffers(1, &colorBuffer);
glGenRenderbuffers(1, &depthBuffer);

glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);

glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, maskRenderingWidth, maskRenderingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

glBindRenderbuffer(GL_RENDERBUFFER, colorBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, maskRenderingWidth, maskRenderingHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBuffer);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers);

然后我将一个多边形渲染到我放入纹理的帧缓冲区。然后我绘制另一个多边形,但这一次到屏幕缓冲区。最后,我在整个屏幕上跨越四边形并将uv坐标(0,0),(0,1),(1,1)和(1,0)以及之前创建的纹理传递给它。

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(textureLocation,0);

我想要实现的是,如果那个纹理在某些纹理上有某些值,我想绘制相应的纹理值,否则我想在coord(u,v)上输出 不变。

if(textureProj(mask, uv_coords).a == 1) {
    gl_FragColor = textureProj(mask, uv_coords);
}

经过一番尝试,我发现总是从纹理中抽出一切。似乎我的纹理在某种程度上是腐败的,因为isinf和isnan为每个uv_coord的textureProj(mask,uv_coords)返回true。怎么可能这样,有人知道这里可能会被打破什么?

in vec4 uv_coords;
uniform sampler2D mask;
void main() {
    vec4 c = textureProj(mask, uv_coords);
    bvec4 inf = isinf(c);
    if(inf.r == true && inf.g == true && inf.b == true && inf.a == true) {
        gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
    }
});

这会产生完全白色的屏幕。同样适用于

inf = isnan(c)

任何人都知道这里有什么想法吗?

编辑:

我忘了提到如果我在片段着色器中省略了if子句并且只做了

in vec4 uv_coords;
uniform sampler2D mask;
void main() {
    vec4 c = textureProj(mask, uv_coords);
    gl_FragColor = c;   });

正确绘制纹理。让它更奇怪。

EDIT2:

好的,我read没有设置gl_FragColor会给出未定义的行为。所以我的方法是将两个纹理上传到特定的片段着色器并在一次传递中渲染所有内容

0 个答案:

没有答案