Android openGL ES 3.0 着色器不工作

Android openGL ES 3.0 shaders are not working

我在 android 中使用 openGl ES 3.0 在屏幕上渲染一个矩形,我想使用着色器根据顶点位置生成混合颜色。

我确定问题出在顶点和片段着色器代码中,但我不知道是什么,这些是着色器代码:

 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);" +
                    "}";

我正在尝试根据顶点位置用颜色填充形状,因此它是为每个像素生成的颜色的混合。

但我不知道为什么它不起作用?

更新:

我有 3 个信息:

您可以像这样显示着色器错误:

    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));
    }

你能显示任何颜色形状吗?形状未显示的原因可能有很多。但如果只是颜色问题,它会缩小搜索范围。

您的着色器代码未成功编译,因为您错过了换行符 (\n)。版本声明必须在单独的行中:

"#version 300 es\n"+

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.


此外,您必须向片段着色器添加浮点变量的精度限定符。例如:

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

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.