Jin
Jin

Reputation: 890

OpenGL ES 2.0: glGetAttribLocation returns -1. Shader optimization?

I'm writing a 3D application for Android, but whenever I call glGetAttribLocation(), I always get -1. I am well aware that the GLSL compiler removes unused variables in my shaders, but as far as I can tell everything is being used and I still get a blank screen. Why can't GLSL find my attributes? Any help is appreciated.

Relevant code:

Vertex Shader:

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);
}

Fragment Shader:

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

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

void main() { 

    gl_FragColor = vec4(normalEye, 1.0); 
};

Draw Method:

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);
    }

Upvotes: 1

Views: 1337

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473407

You clearly did not check for compiler errors. Because if you had, you'd have seen this:

gl_Position = MVPMatrix * vec4(vPosition, 1.0);

vPosition is not defined anywhere in your shader. You probably meant to rename attribute vec3 vertex; to that.

Always check your compiler errors.

Upvotes: 2

Related Questions