简体   繁体   English

Android openGL 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.我在 android 中使用 openGl ES 3.0 在屏幕上渲染一个矩形,我想使用着色器根据顶点位置生成颜色混合。

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:我有 3 个信息:

  • Link failed because of invalid vertex shader.由于无效的顶点着色器,链接失败。

  • Language version '300' unknown, this compiler only supports up to version '320 es'语言版本 '300' 未知,此编译器最多只支持版本 '320 es'

  • Unexpected text found after #version directive. #version 指令后发现意外文本。

Your shader code does not compile successfully, because you missed the newlines ( \\n ).您的着色器代码未成功编译,因为您错过了换行符 ( \\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 :请参阅OpenGL ES 着色语言 3.00 规范 - 3.4 版本声明

The #version directive must be present in the first line of a shader and must be followed by a newline. #version指令必须出现在着色器的第一行中,并且必须后跟换行符。


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 :请参阅OpenGL ES 着色语言 3.00 规范 - 4.5.4 默认精度限定符

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.但如果只是颜色问题,它会缩小搜索范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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