4

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
SomeUser
  • 75
  • 7
  • Give us little more information. What is displayed. Are you sure that shaders are linked successfully? Maybe there is error while compiling shaders. GLES20.getShaderInfoLog() function gives you error messages. – oto Mar 14 '20 at 19:04
  • @oto nothing is displayed, only the background color. Where should I add this method? – SomeUser Mar 14 '20 at 19:34

2 Answers2

4

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
2

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.

oto
  • 383
  • 4
  • 18
  • the exception is thrown, I guess the program is not being linked? But I cant see the info? – SomeUser Mar 14 '20 at 19:48
  • GLES20.glGetProgramInfoLog(program) should contain the error. look for the text after "Could not link program: ". If exception confuses you just don't throw anything and print it like this: System.out.println(GLES20.glGetProgramInfoLog(program)); and see what it says. – oto Mar 14 '20 at 19:50
  • link failed because of invalid vertex shader. – SomeUser Mar 14 '20 at 20:10
  • Android's shader codes are little different from normal Java's opengl. Even if the shader was right you'll need camera Matrix there to display the shapes. It's little harder. I suggest you to follow this documentation: https://developer.android.com/guide/topics/graphics/opengl – oto Mar 14 '20 at 20:22