【问题标题】:OpenGL ES 2.0: glGetAttribLocation returns -1. Shader optimization?OpenGL ES 2.0:glGetAttribLocation 返回 -1。着色器优化?
【发布时间】:2013-09-01 17:41:39
【问题描述】:

我正在为 Android 编写一个 3D 应用程序,但每当我调用 glGetAttribLocation() 时,我总是得到 -1。我很清楚 GLSL 编译器会删除我的着色器中未使用的变量,但据我所知,一切都在使用,但我仍然得到一个空白屏幕。为什么 GLSL 找不到我的属性?任何帮助表示赞赏。

相关代码:

顶点着色器:

attribute vec3 vertex; 
attribute vec3 normal; 
uniform mat4 modelViewMatrix; 
uniform mat4 MVPMatrix;

/*varying vec3 lightPosEye;*/
varying vec3 normalEye; 
/*varying vec3 vertEye;*/

void main() { 

    /*Calculate normal matrix*/
    mat4 normalMat = modelViewMatrix;
    normalMat = inverse(normalMat);
    normalMat = transpose(normalMat);
    normalEye = normalize(vec3(normalMat * vec4(vNormal, 0.0)));

    /*lightPosEye = modelViewMatrix * vec3(0.0, 0.8, 0.0);*/

    /*vertEye = modelViewMatrix * vPosition;*/

    gl_Position = MVPMatrix * vec4(vPosition, 1.0);
}

片段着色器:

precision mediump float; 
/*uniform vec4 vColor; */

/*varying vec3 lightPosEye;*/
varying vec3 normalEye; 
/*varying vec3 vertEye;*/

void main() { 

    gl_FragColor = vec4(normalEye, 1.0); 
};

绘制方法:

public void draw(float[] mMVPMatrix, float[] mModelViewMatrix){

        GLES20.glUseProgram(mProgram);

        //get handle to vertex shader's vPosition
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

        //enable vertex attrib array
        GLES20.glEnableVertexAttribArray(mPositionHandle);

        //load up coordinate data
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, 
                GLES20.GL_FLOAT, false, 0, meshVertBuffer);

        mNormalHandle = GLES20.glGetAttribLocation(mProgram, "vNormal");

        //enable vertex attrib array
        GLES20.glEnableVertexAttribArray(mNormalHandle);

        //load up coordinate data
        GLES20.glVertexAttribPointer(mNormalHandle, COORDS_PER_VERTEX, 
                GLES20.GL_FLOAT, false, 0, meshNormBuffer);



//      //get handle to fragment shader's vColor
//      mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
//      
//      //set color uniform
//      GLES20.glUniform4fv(mColorHandle, 1, colors, 0);



        //get handle for and load MVP matrix
        mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "MVPMatrix");

        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);

        //load MV matrix
        mModelViewMatrixHandle = GLES20.glGetUniformLocation(mProgram, "modelViewMatrix");
        GLES20.glUniformMatrix4fv(mModelViewMatrixHandle, 1, false, mModelViewMatrix, 0);

        //draw!
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, meshVerts.length/COORDS_PER_VERTEX);

        //disable vertex attrib array
        GLES20.glDisableVertexAttribArray(mPositionHandle);
    }

【问题讨论】:

    标签: java android eclipse opengl-es glsl


    【解决方案1】:

    您显然没有检查编译器错误。因为如果你有,你会看到这个:

    gl_Position = MVPMatrix * vec4(vPosition, 1.0);
    

    vPosition 未在您的着色器中任何地方定义。您可能打算将 attribute vec3 vertex; 重命名为。

    始终检查您的编译器错误。

    【讨论】:

    • 谢谢,我急于找出错误是什么,我忘了改回变量名。我会确保更加小心
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多