着色器仅渲染屏幕的1/4

时间:2016-05-03 16:57:52

标签: opengl libgdx glsl shader

我目前正在尝试创建高斯模糊着色器,虽然我已经成功创建了模糊效果,但我的着色器仅渲染屏幕的右下角,如图所示:Screenshot of effect只是为了说清楚;它没有重新缩放图像,它只是没有渲染其余部分。

这是我的着色器代码,其中包含我的想法:

顶点(水平,垂直几乎相同。见下文)

#version 400

in vec2 a_texCoord0; //My tex coords

out vec2 blurTextureCoords[11]; //Sample 11px (i.e, 5 right 5 left of center)

uniform float u_width; //Width of the screen. Used to calculate pixel size

void main(void){
    gl_Position = vec4(a_texCoord0, 0.0, 1.0);
    vec2 centerTexCoords = a_texCoord0 * 0.5 + 0.5; //Maybe this is somehow wrong?
    float pixelSize = 1.0 / u_width; //This is kind of interesting, because my shader sometimes tells me that "no uniform value was found with name 'u_width', but it still seems to work as if I change the values manually (ex. set it to 1920) it still looks normal.

    for(int i=-5; i<=5; i++) {
        blurTextureCoords[i+5] = centerTexCoords + vec2(pixelSize * i, 0.0); //I also thought that it might be because I multiply a float with an integer, but if I do float(i) instead of just i it still looks the same.
    }

}

片段

#version 400
#ifdef GL_ES
precision mediump float;
#endif

in vec2 blurTextureCoords[11];

out vec4 out_colour;

uniform sampler2D u_texture;

void main(void){
    //The actual bluring
    out_colour = vec4(0.0);
    out_colour += texture(u_texture, blurTextureCoords[0]) * 0.0093;
    out_colour += texture(u_texture, blurTextureCoords[1]) * 0.028002;
    out_colour += texture(u_texture, blurTextureCoords[2]) * 0.065984;
    out_colour += texture(u_texture, blurTextureCoords[3]) * 0.121703;
    out_colour += texture(u_texture, blurTextureCoords[4]) * 0.175713;
    out_colour += texture(u_texture, blurTextureCoords[5]) * 0.198596;
    out_colour += texture(u_texture, blurTextureCoords[6]) * 0.175713;
    out_colour += texture(u_texture, blurTextureCoords[7]) * 0.121703;
    out_colour += texture(u_texture, blurTextureCoords[8]) * 0.065984;
    out_colour += texture(u_texture, blurTextureCoords[9]) * 0.028002;
    out_colour += texture(u_texture, blurTextureCoords[10]) * 0.0093;
}

附加代码

创建着色器:

//I also have one for the vertical shader, it's almost exactly the same.
horizontalShader = new ShaderProgram(
    Gdx.files.internal("graphics/shaders/post-processing/blur/horizontalBlur.vert"),
    Gdx.files.internal("graphics/shaders/post-processing/blur/blur.frag"));
horizontalShader.pedantic = false;
horizontalShader.begin();
horizontalShader.setUniformf("u_width", Gdx.graphics.getWidth());
horizontalShader.end();

if (horizontalShader.getLog().length() != 0) {
    System.out.println("Horizontal shader! \n" + horizontalShader.getLog());
}

渲染到FBO然后进入屏幕:

// Horozontal blur
horizontalFBO.begin();
spriteBatch.begin();
spriteBatch.setShader(horizontalShader);
background_image.draw(spriteBatch);
spriteBatch.end();
horizontalFBO.end();

// Vertical blur
verticalFBO.begin();
spriteBatch.begin();
spriteBatch.setShader(verticalShader);
spriteBatch.draw(horizontalFBO.getColorBufferTexture(), 0, 0);
spriteBatch.end();
verticalFBO.end();

// Normal FBO (screen)
spriteBatch.begin();
spriteBatch.setShader(null);
spriteBatch.draw(verticalFBO.getColorBufferTexture(), 0, 0);
spriteBatch.end();

其他信息

我使用了两个FBO,但似乎这些不是问题的根源,因为如果我只是使用这些着色器直接渲染到屏幕上,问题仍然存在。

我有两个顶点着色器,一个用于水平,另一个用于垂直模糊。唯一的区别是统一名称u_width变为u_heightblurTextureCoords[i+5] = centerTexCoords + vec2(pixelSize * i, 0.0);变为blurTextureCoords[i+5] = centerTexCoords + vec2(0.0, pixelSize * i);

0 个答案:

没有答案