1

我正在为 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);
    }
4

1 回答 1

2

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

gl_Position = MVPMatrix * vec4(vPosition, 1.0);

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

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

于 2013-09-01T09:41:39.233 回答