Search code examples
androidopengl-esshaderglslesopengl-es-3.0

Android openGL ES 3.0 shaders are not working


I am using openGl ES 3.0 in android to render a rect on screen, and I want to use shaders to generate a mix of colors depending on vertex positions.

I am sure that the problem is in the vertex and in the fragment shader code, but I don't know what is it, these are the shader codes:

 private final String vertexShaderCode =

            "#version 300 es"+
            "in vec3 position;" +
            "out vec3 color;"+

                    "void main() {" +
                    "  gl_Position = vec4(position.x,position.y,position.z,1.0);" +
                    "  color = vec3(position.x+0.5,1.0,position.y+0.5);"+
                    "}";




    private final String fragmentShaderCode =

                    "#version 300 es"+
                    "in vec3 color;"+
                    "out vec4 out_color;"+


                    "void main() {" +
                    "  out_color= vec4(color.x,color.y,color.z,1.0);" +
                    "}";

I am trying to fill the shape with color based on vertex position, so its a mixture of generated colors for each pixel.

But I don't why its not working?

UPDATE:

I have 3 info:

  • Link failed because of invalid vertex shader.

  • Language version '300' unknown, this compiler only supports up to version '320 es'

  • Unexpected text found after #version directive.


Solution

  • Your shader code does not compile successfully, because you missed the newlines (\n). The version declaration has to be in a separate line:

    "#version 300 es\n"+
    

    See OpenGL ES Shading Language 3.00 Specification - 3.4 Version Declaration:

    The #version directive must be present in the first line of a shader and must be followed by a newline.


    Furthermore you have to add a precision qualifier for the floating point variables to the fragment shader. e.g.:

    "#version 300 es\n"+
    "precision mediump float;\n"+
    

    See OpenGL ES Shading Language 3.00 Specification - 4.5.4 Default Precision Qualifiers:

    The fragment language has no default precision qualifier for floating point types. Hence for float, floating point vector and matrix variable declarations, either the declaration must include a precision qualifier or the default float precision must have been previously declared.