简体   繁体   中英

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.

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. eg:

"#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.

You can display shader errors like this:

    GLES20.glAttachShader(program, vertexShader);
    GLES20.glAttachShader(program, pixelShader);
    GLES20.glLinkProgram(program);
    int[] linkStatus = new int[1];
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
    if (linkStatus[0] != GLES20.GL_TRUE) {
        GLES20.glDeleteProgram(program);
        throw new RuntimeException("Could not link program: "
                + GLES20.glGetProgramInfoLog(program));
    }

Was you able to display any color shape? There can be many reasons why shapes are not displayed. But if it's only matter of colors, it narrows down the search area.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM