Shader attribute mat4 not binding correctly (Opengl ES 2.0 Android)

牧云@^-^@ 提交于 2019-12-11 02:52:43

问题


I have the following shader:

protected final static String vertexShaderCode =
    "attribute vec4 vPosition;" +
    "attribute vec2 texCoord;" +
    "attribute mat4 uMVPMatrix;   \n" +

    "varying vec2 vTexCoord;" +

    "void main() {" +
    "  gl_Position = uMVPMatrix * vPosition;" +
    "  vTexCoord = texCoord;" +
    "}";

I want to pass in the mvp matrix as an attribute, however it doesn't seem to be bound correctly. I'm using auto-assigned binding. When I query the attribute locations after linking the program as follows:

// program linked previously
GLES20.glUseProgram(program);
GLES20.glEnable(GLES20.GL_TEXTURE_2D);

GLES20.glEnable(GLES20.GL_BLEND);
mPositionHandle = GLES20.glGetAttribLocation(program, "vPosition");
mTextureHandle = GLES20.glGetAttribLocation(program, "texCoord");
mtextureSampHandle = GLES20.glGetAttribLocation(program, "textureSamp");
mMVPMatrixHandle = GLES20.glGetAttribLocation(program, "uMVPMatrix");

The returned handles are:

mTextureHandle: 0

mMVPMatrixHandle: 1

mPositionHandle: 2

However, from the documention here: http://www.khronos.org/opengles/sdk/docs/man/xhtml/glBindAttribLocation.xml, mMVPMatrixHandle should be assigned 4 consecutive handles, one for each column of the matrix (i.e. mMVPMatrix should have handles 1,2,3,4). This isn't the case and I have no idea why...

As a result, I'm unable to draw anything to the screen. For completeness, I'm attempting to load the matrix as follows:

    GLES20.glVertexAttribPointer(mMVPMatrixHandle2, 4, GLES20.GL_FLOAT, false, 0, t1);
    GLES20.glVertexAttribPointer(mMVPMatrixHandle2 + 1, 4, GLES20.GL_FLOAT, false, 0, t2);
    GLES20.glVertexAttribPointer(mMVPMatrixHandle2 + 2, 4, GLES20.GL_FLOAT, false, 0, t3);        
    GLES20.glVertexAttribPointer(mMVPMatrixHandle2 + 3, 4, GLES20.GL_FLOAT, false, 0, t4);

where t1-t4 are buffers that contain the individual rows of the mvp matrix:

{
    1.74f, 0, 0, 0,
    0, 2.9f, 0, 0,
    0, 0, -2.414f, -1f,
    -2.088f, 0, -2.66f, 3
}

which works when I define uMVPMatrix as a uniform mat4 and load it in with glUniformMatrix4fv.


回答1:


Turns out that the issue wasn't with my card, but the phone. When tried on a Samsung Galaxy S2, the attributes were properly assigned and it worked as expected.

The phone in question is the HTC Incredible S, which had an Adreno 205 graphics card. I imagine that there must be an issue with the Opengl implementation on the phone/graphics card. A solution could be to define the matrix as four vec4s in the shader and calculate the position element by element.



来源:https://stackoverflow.com/questions/13897232/shader-attribute-mat4-not-binding-correctly-opengl-es-2-0-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!