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

最佳答案

您显然没有检查编译器错误。因为如果有的话,您会看到以下内容:

gl_Position = MVPMatrix * vec4(vPosition, 1.0);


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

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

10-07 16:17
查看更多