1

I've been starting out using LibGDX to develop for PC as well as Android. I'm currently working on a little project requiring me to write custom shaders in GLSL (based on full-screen quads). Coming from a HLSL environment I had a little trouble setting the whole shader system up in combination with SpriteBatch (as I want to keep the code simple where I can).

I have the following shader code working on desktop:

Vertexshader:

attribute vec4 a_position;
uniform mat4 u_projectionViewMatrix;
void main()
{
    gl_Position = a_position * u_projectionViewMatrix;
}

Fragmentshader:

uniform vec2 effectOrigin;
uniform vec2 effectDir;
void main()
{
    float distToOrigin = distance(effectOrigin.xy, gl_FragCoord.xy);
    gl_FragColor = vec4(mod(distToOrigin+effectDir.x*30, 30)-15, 0.0, 0.0, 1.0);
}

As I said it works on desktop (Windows), and gives me a nice circle pattern. On Android however it doesn't compile and gives me just the cleared background color. I suspected it had something to do with OpenGL ES 2 permissions on Android so I added this line to the manifest: <uses-feature android:glEsVersion="0x00020000" android:required="true" />, but this didn't seem to make a difference.

I also thought it might have something to do with the precision of the floats and vectors, but I wasn't able to figure out how I would have to change them in order to fix it.

Is there someone who can help me on this? I haven't been able to find an answer elsewhere!

Thanks in advance,

Yuri

P.S. Is there a way to see what went wrong during compiling of the shaders? I know debugging is hard on shaders, but it would be awesome to have at least some idea of where it could've gone wrong during compilation.

5
  • 2
    You can get shader compiler logs in Libgdx with: x = new ShaderProgram(...); if (!x.isCompiled()) { Gdx.app.log("Shader", x.getLog()); }. Also did you enable useGL20 in your app config? Commented Apr 2, 2013 at 21:38
  • Thanks on the shader log! Going to give that a try. If you mean enable useGL20 in cfg.useGL20 = true;, then yes. Commented Apr 2, 2013 at 21:42
  • It'd be great if you could answer your own question with the actual problem you ran into (I'm assuming there is something interesting going on if the shader works on Desktop and not on Android ...) Commented Apr 3, 2013 at 1:59
  • I did, I answered it in the original question, at the end ;)! Commented Apr 3, 2013 at 11:31
  • This will be handy for others that are GLSL porting to Android. However, your answer should probably go in the "answer" box below. Please read stackoverflow.com/faq#howtoask Commented Apr 3, 2013 at 15:46

1 Answer 1

3

Fixed it using P.T.´s suggestion on logging the compilation! There were two problems in the fragment shader, the fixed shader looks like this:

precision mediump float;
uniform vec2 effectOrigin;
uniform vec2 effectDir;
void main()
{
    float distToOrigin = distance(effectOrigin.xy, gl_FragCoord.xy);
    gl_FragColor = vec4(mod(distToOrigin+effectDir.x*30.0, 30.0)-15.0, 0.0, 0.0, 1.0);
}

I added the precision definition at the top, and also changed the constant values from integers to floats (e.g. 15 to 15.0) to fix it!

Thanks P.T.!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.