android,opengles 2.0中的奇怪错误,调用glUniformMatrix4fv(uMatrixLocation,10,false,matrix,0)

时间:2014-01-14 11:44:54

标签: android opengl-es matrix

在某些设备上(到目前为止,三星Galaxy note 2和3 + Xperia Z1)我通过调用glUniformMatrix4fv(uMatrixLocation,10,false,matrix,0)来设置变换矩阵,从而抛出此错误。

错误是:

java.lang.IllegalArgumentException: length - offset < count*16 < needed
    at android.opengl.GLES20.glUniformMatrix4fv(Native Method)

它是由我的textureShaderProgram代码中的这一行引起的:

glUniformMatrix4fv(uMatrixLocation, 10, false, matrix, 0);

uMatrixLocation是矩阵var的位置指针 10是转换矩阵的数量(所以 matrix.length()= 10 * 16 = 160 matrix是包含所有转换数据的float [] varible 0是矩阵中的偏移量

我试图增加count变量以解决它但没有成功。

生成此错误的谷歌源代码位于:

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/jni/android_opengl_GLES20.cpp

并且这些特定行进行检查并抛出上述错误:

   _remaining = _env->GetArrayLength(value_ref) - offset;
   if (_remaining < count*16) {
       _exception = 1;
       _exceptionType = "java/lang/IllegalArgumentException";
      _exceptionMessage = "length - offset < count*16 < needed";
      goto exit;
   }

关于为什么这些设备会抛出错误的任何线索? 这些设备上的cpp文件可能与其他设备不同吗?

已添加20140114 18:47 这些是在矩阵变量中传递给glUniformMatrix4fv(uMatrixLocation, 10, false, matrix, 0)的浮点值:

my float is: 0.5625
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: -1.0
my float is: 0.0
my float is: 0.0
my float is: -0.8425926
my float is: 0.0
my float is: 1.0
my float is: 0.5625
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: -1.0
my float is: 0.0
my float is: 0.098958336
my float is: -1.1574074
my float is: 0.0
my float is: 1.0
my float is: 0.5625
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: -1.0
my float is: 0.0
my float is: -0.296875
my float is: -1.1574074
my float is: -0.0
my float is: 1.0
my float is: 0.5625
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: -1.0
my float is: 0.0
my float is: -0.098958336
my float is: -1.1574074
my float is: -0.0
my float is: 1.0
my float is: 0.5625
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: -1.0
my float is: 0.0
my float is: 0.296875
my float is: -1.1574074
my float is: 0.0
my float is: 1.0
my float is: 0.5625
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: -1.0
my float is: 0.0
my float is: 0.9010416
my float is: -0.8240741
my float is: 0.0
my float is: 1.0
my float is: 0.5625
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: -1.0
my float is: 0.0
my float is: 0.9010416
my float is: 0.8240741
my float is: 0.0
my float is: 1.0
my float is: 0.5625
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: -1.0
my float is: 0.0
my float is: 0.9010416
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.5625
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 1.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: 0.0
my float is: -1.0
my float is: 0.0
my float is: 0.0
my float is: -1.1574074
my float is: 0.0
my float is: 1.0

这是我的顶点着色器:

uniform mat4 u_Matrix[10]; 

attribute lowp float a_MatrixIndex;

attribute vec4 a_Position;  
attribute vec2 a_TextureCoordinates;
attribute vec3 a_Color;

varying vec3 v_Color;
varying vec2 v_TextureCoordinates;

void main()                    
{                    
v_Color = a_Color;        
int MatrixIndex = int(a_MatrixIndex);

    v_TextureCoordinates = a_TextureCoordinates;          
    gl_Position = u_Matrix[MatrixIndex] * a_Position;    
}

2 个答案:

答案 0 :(得分:0)

如果matrix1是包含浮点值的对象,则需要将其转换为如下数组。

context.uniformMatrix4fv(loc, false, new Float32Array(matrix1.getAsArray()) );

答案 1 :(得分:0)

现在已经解决了。问题实际上是在Android&gt; 4.2执行:

glUniformMatrix4fv(uMatrixLocation, count, false, matrix, 0);
必须改变

。 count变量必须与矩阵变量内的转换矩阵的“数量”完全匹配。 (正如deathember在上面的一条评论中暗示的那样)

所以解决方案只是将count变量传递给这个函数而不是硬编码。

你仍然可以使用相同的texture_vertex代码!! 就我而言,它看起来像这样: (即使我将这个程序用于长度为1到9的矩阵数组,你可以在这里设置最高的矩阵数)

uniform mat4 u_Matrix[9]; 

attribute lowp float a_MatrixIndex;

attribute vec4 a_Position;  
attribute vec2 a_TextureCoordinates;
attribute vec3 a_Color;

varying vec3 v_Color;
varying vec2 v_TextureCoordinates;

void main()                    
{                    
v_Color = a_Color;        
int MatrixIndex = int(a_MatrixIndex);

    v_TextureCoordinates = a_TextureCoordinates;          
    gl_Position = u_Matrix[MatrixIndex] * a_Position;    
}

如果您想了解我测试的更多信息,请点击此处查看我的博客:

Android device fragmentation, it finally happened