Opengl Framebuffer offscreen,闪烁

时间:2016-10-15 19:14:46

标签: c++ opengl glsl

我正在尝试将对象的位置渲染到纹理中,以进行修剪着色。但是当我将纹理显示为四边形时,我有一些奇怪的结果。

Flickering result

我创建了FBO:

glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glGenTextures(3, textureColorbuffer);
//... 
//Position texture
glBindTexture(GL_TEXTURE_2D, textureColorbuffer[2]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + 2, GL_TEXTURE_2D, textureColorbuffer[2], 0);

GLuint rboDepth;
glGenRenderbuffers(1, &rboDepth);
glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);

GLuint attachments[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, attachments);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    std::cerr << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);

然后我用这个着色器绘制不同的网格物体:

#version 330 core
layout (location = 0) out vec4 FragColor;
layout (location = 1) out vec4 BrightColor;
layout (location = 2) out vec3 posTexture;


uniform vec4 mainColor;
uniform int BloomEffect;
in vec3 ourPosition;

void main(){
    vec4 color = mainColor;
    posTexture = ourPosition;
    BrightColor = vec4(0,0,0,1);
    FragColor = color;
    }

然后我用这个着色器显示包含位置信息的纹理:

#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D screenTexture;
uniform sampler2D bloomBlur;

uniform vec2 screenSize;
uniform vec2 viewPos;
uniform sampler2D posTexture;

void main(){

vec4 hdrColor = texture(screenTexture, TexCoords);
vec4 bloomColor = texture(bloomBlur, TexCoords);
vec3 pos = texture(posTexture, TexCoords).rgb;
FragColor = vec4(pos.rgb, 1);
}

当我使用vec4将位置发送到纹理时,我没有闪烁问题,而内部格式是GL_RGB。我该如何解决这个问题?

编辑:拼写错误

编辑精度:其他纹理写得正确,只有当我使用posTexture时才会出现闪烁问题。

0 个答案:

没有答案