着色器属性mat4不能正确绑定(OpenGL ES 2.0的Andr​​oid设备) [英] Shader attribute mat4 not binding correctly (Opengl ES 2.0 Android)

查看:260
本文介绍了着色器属性mat4不能正确绑定(OpenGL ES 2.0的Andr​​oid设备)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下着色器:

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的矩阵作为一个属性来传递,但它似乎并没有被正确绑定。我使用的是自动分配的绑定。当我连接程序后查询属性位置如下:

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

返回的句柄是:

mTextureHandle:0

mTextureHandle: 0

mMVPMatrixHandle:1

mMVPMatrixHandle: 1

mPositionHandle:2

mPositionHandle: 2

不过,从这里机制的文档: HTTP:// WWW .khronos.org / opengles / SDK /文档/人/ XHTML / glBindAttribLocation.xml ,mMVPMatrixHandle应分配连续4手柄,一个矩阵中的每一列(即mMVPMatrix应该有手柄1,2,3, 4)。这是不是这样的,我不知道为什么...

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

其中t1-t4的是包含在MVP矩阵的各个行缓冲器

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
}

这时候我定义uMVPMatrix作为一个统一的mat4并与glUniformMatrix4fv加载它。工作原理

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

推荐答案

原来,这个问题是不是我的卡,但手机。当在三星Galaxy S2试过了,属性被正确地分配和它的工作如预期。

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.

有问题的手机是HTC Incredible S的,它有一个205的Adreno图形卡。我想,必须有一个问题,与手机/显卡的OpenGL实现。溶液可以定义矩阵作为着色4 vec4s和由元件计算位置元素

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.

这篇关于着色器属性mat4不能正确绑定(OpenGL ES 2.0的Andr​​oid设备)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆