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



我的代码:

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
}

所以我得到了一个glGetUniformLocation: glError 1281 at MyGLRenderer.checkGlError("glGetUniformLocation"); 代码行调试后,我发现我的 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(program, "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(program, "in_texcoord"); 将返回一个有效的 id,> -1

显然,如果不使用,GLSL 编译器可以去除变量和可变变量,因此您的a_normal变量似乎也可能被删除。

相关内容

  • 没有找到相关文章

最新更新