9
votes

I'm trying to understand shaders using libgdx, coming from an XNA/HLSL background. I'm trying to get a vert/frag shader pair to reproduce the output I get without a shader, but it's not displaying anything.

Shader creation:

void SetupShader()
{
    ShaderProgram.pedantic = false;
    shader = new ShaderProgram(
            Gdx.files.internal("assets/default.vert").readString(),
            Gdx.files.internal("assets/default.frag").readString());
    if(!shader.isCompiled()) {
        Gdx.app.log("Problem loading shader:", shader.getLog());
    }
    batch.setShader(shader);
}

default.vert:

attribute vec4 a_Position;
attribute vec4 a_Normal;
attribute vec2 a_TexCoord;
attribute vec4 a_Color;

uniform mat4 u_projTrans;

varying vec2 v_texCoords;
varying vec4 v_color;

void main() {
    v_color = a_Color;
    v_texCoords = a_TexCoord;
    gl_Position = u_projTrans * a_Position;
}

default.frag:

#ifdef GL_ES
precision mediump float;
#endif

varying vec2 v_texCoords;
varying vec4 v_color;

void main() {
    gl_FragColor = v_color;
}

Rendering:

batch.begin();
for (GameObject gObj : gameObjects)
    gObj.Draw(batch);
batch.end();

Any suggestions here? I'm new to OpenGL-ES as well, so I may be missing something obvious. I looked around a bit before posting, and the doc for SpriteBatch.setShader(ShaderProgram) was as follows:

Sets the shader to be used in a GLES 2.0 environment. Vertex position attribute is called "a_position", the texture coordinates attribute is called called "a_texCoords0", the color attribute is called "a_color". See ShaderProgram.POSITION_ATTRIBUTE, ShaderProgram.COLOR_ATTRIBUTE and ShaderProgram.TEXCOORD_ATTRIBUTE which gets "0" appened to indicate the use of the first texture unit. The projection matrix is uploaded via a mat4 uniform called "u_proj", the transform matrix is uploaded via a uniform called "u_trans", the combined transform and projection matrx is is uploaded via a mat4 uniform called "u_projTrans". The texture sampler is passed via a uniform called "u_texture". Call this method with a null argument to use the default shader.

1
Hmm, the GLSL spec contains: In general, the language’s use of this character set is case sensitive.. Does this mean Identifiers are case sensitive? The Identifiers section does not contain a word about it. (the vertex shader contains a_Position, the comment a_position)Stefan Hanke
Yep, looks like you were right. I checked into how SpriteBatch creates its shader, and it was primarily a caps issue. I also figured out what the default shaders are- I'll post those below.user312364

1 Answers

13
votes

After looking at the code in SpriteBatch it seems that I had a few mistakes in my code. Below are the correct shaders:

default.vert:

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

uniform mat4 u_projTrans;

varying vec4 v_color;
varying vec2 v_texCoords;

void main() {
    v_color = a_color;
    v_texCoords = a_texCoord0;
    gl_Position = u_projTrans * a_position;
}

default.frag:

#ifdef GL_ES
    precision mediump float;
#endif

varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;

void main() {
    gl_FragColor = v_color * texture2D(u_texture, v_texCoords);
}