Android 使用法线时打开GL ES 2.0-glGetAttribLocation返回-1

Android 使用法线时打开GL ES 2.0-glGetAttribLocation返回-1,android,opengl-es,Android,Opengl Es,我的代码: public void draw1(float[] mvpMatrix) { // Add program to OpenGL environment GLES20.glUseProgram(mProgram); // get handle to shape's transformation matrix mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

我的代码:

public void draw1(float[] mvpMatrix) {
    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

    // get handle to shape's transformation matrix
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    // get handle to vertex shader's vPosition member
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    MyGLRenderer.checkGlError("glGetUniformLocation");

    mNormalHandle = GLES20.glGetAttribLocation(mProgram, "a_normal");//new line

    fsTexture = GLES20.glGetUniformLocation(mProgram, "Texture");
    vsTextureCoord = GLES20.glGetAttribLocation(mProgram, "texCoord");
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
    GLES20.glUniform1i(fsTexture, 0);

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    MyGLRenderer.checkGlError("glUniformMatrix4fv");
    // Enable a handle to the triangle vertices

    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(
            mPositionHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false,
            vertexStride, vertexBuffer);
    GLES20.glVertexAttribPointer(vsTextureCoord, COORDS_PER_TEXTURE,
            GLES20.GL_FLOAT, false,
            TextureStride, texBuffer);
    GLES20.glEnableVertexAttribArray(vsTextureCoord);

    GLES20.glVertexAttribPointer(mNormalHandle, COORDS_PER_NORMAL, GLES20.GL_FLOAT, false,
            12, normalbuffer);//new line

    GLES20.glEnableVertexAttribArray(mNormalHandle);//new line


    // Draw the triangle
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, tablelamp21NumVerts);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    GLES20.glDisableVertexAttribArray(vsTextureCoord);
    GLES20.glDisableVertexAttribArray(mNormalHandle);//new line
}
所以我在MyGLRenderer上得到了一个glGetUniformLocation:glError 1281;代码行 调试后,我发现我的mNormalHandle是-1,即GLES20.glGetAttribLocation(mProgram,“a_normal”);返回-1,尽管我在顶点着色器中使用了它,并且使用了GLES20.glBindAttribLocation(mProgram,2,“a_normal”);在链接之前,请帮助。 着色器代码:

   private final String vertexShaderCode =
        "uniform mat4 uMVPMatrix;" +
                "uniform mat4 u_MVMatrix;" +

                "attribute vec4 vPosition;" +
                "attribute vec2 texCoord;" +
                "attribute vec3 a_normal;"+ //new line

                "varying vec3 v_normal;"+//new line
                "varying vec2 texCoordOut;" +
                "void main() {" +
                "   v_normal = vec3(u_MVMatrix * vec4(a_normal, 0.0));"+
                "   texCoordOut = texCoord;" +
                "   gl_Position = uMVPMatrix * vPosition;" +
                "}";

private final String fragmentShaderCode =
        "precision mediump float;" +
                "uniform sampler2D Texture;" +
                "varying vec2 texCoordOut;"+
                "void main() {" +
                " gl_FragColor = texture2D(Texture, texCoordOut);" +
                "}";

很抱歉回答一个过时的问题,但如果这能帮助其他开发人员:我认为GLSL编译器非常适合剥离未使用的变量

例如,使用Android仿真器的OpenGL/ES驱动程序编译的以下顶点和片段着色器:

private val vertexShaderCode = """attribute vec4 in_position;
            attribute vec2 in_texcoord;

            uniform mat4 projection;
            uniform mat4 view;
            uniform mat4 model;

            varying vec2 var_texcoord;

            void main() {
                var_texcoord = in_texcoord;
                gl_Position = projection * view * model * in_position;
            }
            """

    private val fragmentShaderCode = """
            precision mediump float;

            varying vec2 var_texcoord;

            uniform sampler2D texture;
            uniform vec4 color;

            void main() {
                gl_FragColor = color;
            }
            """
GLES20.glGetAttribLocation(程序,“in_texcoord”);将返回-1

现在,如果我们使用var_texcoord计算片段着色器中的颜色:

private val fragmentShaderCode = """
            precision mediump float;

            varying vec2 var_texcoord;

            uniform sampler2D texture;
            uniform vec4 color;

            void main() {
                gl_FragColor = texture2D(texture, var_texcoord);
            }
            """
GLES20.glGetAttribLocation(程序,“in_texcoord”);将返回有效的id>-1


显然,如果不使用,GLSL编译器可以剥离变量和可变变量,因此很可能您的a_法线变量也被删除。

您正在尝试获取
uMVPMatrix
,而着色器具有
u_MVMatrix
。我用uMVPMatrix更改了u_MVMatrix,但我仍然得到了错误,那么到底发生了什么,在执行之后,我得到了3D模型,但当我触摸屏幕时,我得到了上面的错误,因此为了纠正我尝试调试的错误,我发现我的mNormalHandle是-1。
a_normal
是顶点属性(不是统一变量)。