简体   繁体   中英

ERROR: 0:4: 'gl_FragColor' : undeclared identifier & ERROR: 0:4: 'assign' : cannot convert from 'uniform 4-component vector of float' to 'float'

An OpenGL code throws two errors:

  1. ERROR: 0:4: 'gl_FragColor': undeclared identifier
  2. ERROR: 0:4: 'assign': cannot convert from 'uniform 4-component vector of float' to 'float'

The source codes:

        mProgram = ShaderHelper.buildProgram(
            TextResourceReader.readTextFileFromResource(
                context,
                R.raw.triangle_fragment_shader
            ),
            TextResourceReader.readTextFileFromResource(
                context,
                R.raw.triangle_fragment_shader
            )
        )
        // Add program to OpenGL ES environment
        GLES20.glUseProgram(mProgram)

ShaderHelper

    public static int buildProgram(String vertexShaderSource, String fragmentShaderSource) {
        int vertexShader = ShaderHelper.compileVertexShader(vertexShaderSource);
        int fragmentShader = ShaderHelper.compileFragmentShader(fragmentShaderSource);

        int program = ShaderHelper.linkProgram(vertexShader, fragmentShader);
        ShaderHelper.validateProgram(program);

        return program;
    }

    public static int compileVertexShader(String shaderCode) {
        return compileShader(GL_VERTEX_SHADER, shaderCode);
    }

    public static int compileFragmentShader(String shaderCode) {
        return compileShader(GL_FRAGMENT_SHADER, shaderCode);
    }

    private static int compileShader(int type, String shaderCode) {
        // Set OpenGL version to 2 in advance, otherwise it will crash here.
        final int shaderObjectId = glCreateShader(type);
        if (shaderObjectId == 0) {
            VLog.w(TAG, "compileShader", "Could not create shader");
            return shaderObjectId;
        }

        glShaderSource(shaderObjectId, shaderCode);
        glCompileShader(shaderObjectId);

        final int[] compileStatus = new int[1];
        glGetShaderiv(shaderObjectId, GL_COMPILE_STATUS, compileStatus, 0);
        if (compileStatus[0] == 0) {
            glDeleteShader(shaderObjectId);
            VLog.d("Compile shader failed" + shaderCode);
            return shaderObjectId;
        }

//        LogWrapper.d(new String[] {"shaderCode", "GLLogInfo"}, new String[]{shaderCode, glGetShaderInfoLog(shaderObjectId)});;

        return shaderObjectId;
    }

    public static int linkProgram(int vertexShaderId, int fragmentShaderId) {
        final int programObjectId = glCreateProgram();
        if (programObjectId == 0) {
            VLog.w(TAG, "linkProgram", "Could not create new program");
            return programObjectId;
        }

        glAttachShader(programObjectId, vertexShaderId);
        glAttachShader(programObjectId, fragmentShaderId);

        glLinkProgram(programObjectId);
        final int[] linkStatus = new int[1];
        glGetProgramiv(programObjectId, GL_LINK_STATUS, linkStatus, 0);
        if (linkStatus[0] == 0) {
            glDeleteProgram(programObjectId);
            VLog.w(TAG, "linkProgram", glGetProgramInfoLog(programObjectId));
            return programObjectId;
        }
        return programObjectId;
    }

    public static boolean validateProgram(int programObjectId) {
//        if (!BuildConfig.DEBUG) {
//            return true;
//        }
        glValidateProgram(programObjectId);

        final int[] validateStatus = new int[1];
        glGetProgramiv(programObjectId, GL_VALIDATE_STATUS, validateStatus, 0);
        VLog.d("Results of validating program: ", new String[]{"validateStatus", "ProgramInfo"},
                new String[]{String.valueOf(validateStatus[0]), glGetProgramInfoLog(programObjectId)});

        return validateStatus[0] != 0;
    }

triangle_vertex_shader

attribute vec4 vPosition;
void main() {
    gl_Position = vPosition;
}

triangle_fragment_shader

precision mediump float;
uniform vec4 vColor;
void main() {
    gl_FragColor = vColor;
}

After a meticulous investigation, I found the root cause is that I provide fragment shader as vertex shader by mistake. The correct one is shown as below:

        mProgram = ShaderHelper.buildProgram(
            TextResourceReader.readTextFileFromResource(
                context,
                R.raw.triangle_vertex_shader
            ),
            TextResourceReader.readTextFileFromResource(
                context,
                R.raw.triangle_fragment_shader
            )
        )

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