0
\$\begingroup\$

I want to make background of my game like lava via perlin noise. I've read some tutorials but they were too old and some api calls aren't exist already. If I understand right in libgdx I need to create a runtime-generated texture (FrameBuffer) on x=0 and y=0 with width=Screen width and height=Screen height and apply shader to it.

I tried to do:

class A extends ApplicationAdapter {
    FrameBuffer   background;
    ShaderProgram shaderProgram;

    public void create() {
       background = new FrameBuffer(
           Pixmap.Format.RGBA8888, 
           Gdx.graphics.getWidth(), 
           Gdx.graphics.getHeight(), 
           false
       );
       shaderProgram = /* stuff to pickup .vert and .frag */
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


        background.begin();
        batch.setProjectionMatrix(
          new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight())
        );
        batch.begin();
        batch.setShader(shaderProgram);
        batch.end();
        background.end();
    }
}

My shaders are simple red fill programs:

//Vertex
attribute vec4 a_position;

uniform mat4 u_projTrans;

varying vec2 v_Pos;

void main() {
    gl_Position = u_projTrans * a_position;

    v_Pos = gl_Position;
}

//Fragment
#ifdef GL_ES
    precision mediump float;
#endif

uniform mat4 u_projTrans;

varying vec2 v_Pos;

void main() {
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

Nothing has been drawn. I expect that all my viewport become red

Function that picks up shaders is working, i tested at sprites

\$\endgroup\$
  • \$\begingroup\$ Your question is not clear. What is the issue you're experiencing? What did you try already? What result are you trying to achieve? \$\endgroup\$ – liggiorgio Apr 7 '18 at 13:27
  • \$\begingroup\$ @liggiorgio i updated question \$\endgroup\$ – Herrgott Apr 7 '18 at 14:15
  • \$\begingroup\$ Why not just draw a rectangle and apply the shader to it? \$\endgroup\$ – Bálint Apr 7 '18 at 15:19
  • \$\begingroup\$ @Balint how to do it? \$\endgroup\$ – Herrgott Apr 7 '18 at 17:05
1
\$\begingroup\$

The first step in creating and using a shader in libgdx is to create the shader vert and frag components. You already have these in your question.

The second step is to create the shader in libgdx and that is done with this code:

ShaderProgram.pedantic = false;
ShaderProgram shader = new ShaderProgram(
        Gdx.files.internal("shaders/vert.vert"),
        Gdx.files.internal("shaders/frag.frag"));

I use .vert and .frag to identify my shader components but these are not required and you can use any naming convention you like.

Once you have crated your shader you should check that it has loaded and has compiled without error with:

if (!shader.isCompiled()) {
    System.err.println(shader.getLog());
    System.exit(0);
}

if (shader.getLog().length()!=0){
    System.out.println(shader.getLog());
}

You can now add your shader to your spritebatch:

SpriteBatch sb = new SpriteBatch();
sb.setShader(shader);

To make it only fill the background use the shader on one spritebatch and fill the screen then use another spritebatch to draw on top.

more information on shaders can be found:

gamedevelopment.blog

Cheat Sheet

https://thebookofshaders.com/

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.