High Calibre Wizard
High Calibre Wizard

Reputation: 187

Shaders in libgdx have no effect [Desktop]

This is mostly a general question, since I can't get any shader to work at all. The usual sprites and textures render just fine, it just doesn't happen anything with the shaders. Not getting any error messages from the shader log either. As far as I understand, for a filter of the type below, one only need to set the shader to the batch, like batch.setShader(shader), and set any uniforms, and the batch will take care of the rest. If I am wrong, please tell me my errors.

Fragment shader, supposed to blur

//"in" attributes from our vertex shader
varying vec2 v_texCoord0;

//declare uniforms
uniform sampler2D uImage0;
uniform vec2 uResolution;

uniform float radius;
uniform float dirx;
uniform float diry;

void main()
{
//this will be our RGBA sum
vec4 sum = vec4(0.0);

//our original texcoord for this fragment
vec2 tc = v_texCoord0;

//the amount to blur, i.e. how far off center to sample from
//1.0 -> blur by one pixel
//2.0 -> blur by two pixels, etc.
float blur = radius / uResolution.x;

//the direction of our blur
//(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = dirx;
float vstep = diry;

//apply blurring, using a 9-tap filter with predefined gaussian weights

sum += texture2D(uImage0, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
sum += texture2D(uImage0, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(uImage0, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(uImage0, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;

sum += texture2D(uImage0, vec2(tc.x, tc.y)) * 0.2270270270;

sum += texture2D(uImage0, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(uImage0, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(uImage0, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(uImage0, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;

//discard alpha for our simple demo, multiply by vertex color and return
gl_FragColor = vec4(sum.rgb, 1.0);
}

Vertex shader

attribute vec4 a_color;     
attribute vec2 a_texCoord0; 
attribute vec3 a_position;      

uniform mat4 u_projTrans;   

varying vec4 v_color;       
varying vec2 v_texCoord0;

void main(){
v_color = a_color;          
v_texCoord0 = a_texCoord0;          
gl_Position = u_projTrans * vec4(a_position,1.0)    ;           
}

Setting up the shader. Tried different values here

    public void setupShader(){
    ShaderProgram.pedantic=true;
    shader = new ShaderProgram(Gdx.files.internal("shaders/pass.vert"),Gdx.files.internal("shaders/scanlines.frag"));
    shader.begin();
    shader.setUniformf("radius", 5f);
    shader.setUniformf("dirx", 5f);
    shader.setUniformf("diry", 5f);
    shader.end();

    if(shader.isCompiled())
        batch.setShader(shader);
    else
        Settings.log(shader.getLog());
}

The render method. I've not put anything concerning shaders here.

    @Override
    public void render(float delta) {
    Settings.clearScreen();     //usual clear screen calls from here
    batch.setProjectionMatrix(cam.combined);
    cam.update();

    detectClicks();             
    checkBallScreenEdges();

    batch.begin();
        draw(delta);
    batch.end();



}

Upvotes: 1

Views: 422

Answers (1)

High Calibre Wizard
High Calibre Wizard

Reputation: 187

Thanks to Tenfour04 I got a nice scanlines shader to work (tested with another than the one above):

    @Override
public void render(float delta) {
    Settings.clearScreen();
    batch.setProjectionMatrix(cam.combined);
    cam.update();

    batch.setShader(SpriteBatch.createDefaultShader());
    main.buffer.begin();
    batch.begin();
        draw(delta);
    batch.end();
    main.buffer.end();

    //POST PROCESSING
    Texture bufferedTexture = main.buffer.getColorBufferTexture();
    batch.setShader(main.shader);
    batch.begin();
    batch.draw(bufferedTexture, 0, 0, Settings.WIDTH, Settings.HEIGHT, 0, 0, Settings.WIDTH, Settings.HEIGHT, false, true); //need to flip texture
    batch.end();
}

and setting up the shader:

    buffer = new FrameBuffer(Pixmap.Format.RGBA8888,Settings.WIDTH,Settings.HEIGHT,false);

    ShaderProgram.pedantic=false;
    shader = new ShaderProgram(Gdx.files.internal("shaders/pass.vert"),Gdx.files.internal("shaders/scanlines.frag"));
    shader.begin();
    shader.setUniformf("uResolution",(float)Settings.WIDTH,(float)Settings.HEIGHT);
    shader.end();

Wished the libgdx wiki had more examples.

Upvotes: 2

Related Questions