我有以下着色器:
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;" +
"}";
我想将 mvp 矩阵作为属性传递,但它似乎没有正确绑定。我正在使用自动分配的绑定。当我在链接程序后查询属性位置时,如下所示:
// 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");
返回的句柄是:
mTextureHandle: 0
mMVP矩阵句柄:1
mPositionHandle:2
然而,从这里的文档:http ://www.khronos.org/opengles/sdk/docs/man/xhtml/glBindAttribLocation.xml , mMVPMatrixHandle 应该分配 4 个连续的句柄,一个用于矩阵的每一列(即 mMVPMatrix 应该有手柄 1,2,3,4)。事实并非如此,我不知道为什么...
结果,我无法在屏幕上绘制任何东西。为了完整起见,我尝试按如下方式加载矩阵:
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);
其中 t1-t4 是包含 mvp 矩阵各行的缓冲区:
{
1.74f, 0, 0, 0,
0, 2.9f, 0, 0,
0, 0, -2.414f, -1f,
-2.088f, 0, -2.66f, 3
}
当我将 uMVPMatrix 定义为统一 mat4 并使用 glUniformMatrix4fv 加载它时,它会起作用。