【发布时间】:2014-11-23 16:53:19
【问题描述】:
我正在尝试创建一个 OpenGL 顶点着色器,它对每个顶点都有一个额外的转换矩阵。我的着色器代码如下所示:
uniform mat4 mvpMatrix;
attribute vec3 coordinates;
attribute mat4 vertexTransformation;
attribute vec4 vertexColor;
varying vec4 v_color;
void main()
{
vec4 pos = vec4( coordinates, 1 );
pos = vertexTransformation * pos;
pos = mvpMatrix * pos;
gl_Position = pos;
v_color = color;
}
每当我在 android 模拟器中执行此操作时,模拟器都会崩溃。
我试图找出问题并发现它在我访问vertexTransformation 属性时发生。即使不涉及进一步的矩阵运算,以下代码也会导致崩溃。
uniform mat4 mvpMatrix;
attribute vec3 coordinates;
attribute mat4 vertexTransformation;
attribute vec4 vertexColor;
varying vec4 v_color;
void main()
{
vec4 pos = vec4( coordinates, 1 );
pos = mvpMatrix * pos;
gl_Position = pos;
vec4 col = vec4( 0,0,0,1 );
if ( vertexTransformation[0][0] == 0.5 )
v_color = color;
else
v_color = vec4( 1, 1, 1, 1 );
}
我正在使用glBufferData 来传递数据:
ByteBuffer vertexData = ...;
vertexData.position( 0 );
GLES20.glBufferData( GLES20.GL_ARRAY_BUFFER, vertexData.remaining(), vertexData, GLES20.GL_STATIC_DRAW );
然后将属性与
绑定int handle = GLES20.glGetAttribLocation( programId, "vertexTransformation" );
GLES20.glEnableVertexAttribArray( handle );
GLES20.glVertexAttribPointer( handle, 16, GLES20.GL_FLOAT, false, 92, 28 );
我做错了什么?有什么方法可以为顶点提供矩阵属性并在顶点着色器中使用它们?
【问题讨论】:
-
shader中如何传递属性?
-
ByteBuffer vertexData = ...;顶点数据.position(0); GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexData.remaining(), vertexData, GLES20.GL_STATIC_DRAW);
-
我的意思是 glVertexAttribPointer 调用 (add them to the post)
-
你设置了多少个顶点属性数组?一个
mat4顶点属性相当于4 个vec4s,并且应该有4 个单独的属性指针(N,N+1,N+2,N+3),其中N是“vertexTransformation”的属性位置。您很可能没有其他 3 个数组(N+1 到 N+3)设置为有效状态。
标签: android opengl-es vertex-shader