我正在尝试为我的某个应用程序创建动画背景。
基本上,我希望我的活动以animated Perlin or Simplex noise为背景。为了降低能耗,我尝试使用OpenGL完成所有工作。不幸的是,我对OpenGL知之甚少。
我从Displaying Graphics with OpenGL ES开始,它有效。然后我尝试替换Square的绘图代码和顶点/片段着色器,但结果是运行时异常。
我正在尝试使用以下顶点着色器:
uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
void main()
{
gl_Position = uMVPMatrix * vPosition;
}
并稍微更改了Ashima Arts的片段着色器:
float snoise(vec3 v) {
// ..
}
uniform float time; // Used for texture animation
void main( void )
{
vec3 v_texCoord3D = gl_Position.xyz;
vec3 uvw = v_texCoord3D + 0.1*vec3(snoise(v_texCoord3D + vec3(0.0, 0.0, time)),
snoise(v_texCoord3D + vec3(43.0, 17.0, time)),
snoise(v_texCoord3D + vec3(-17.0, -43.0, time)));
// Six components of noise in a fractal sum
float n = snoise(uvw - vec3(0.0, 0.0, time));
n += 0.5 * snoise(uvw * 2.0 - vec3(0.0, 0.0, time*1.4));
n += 0.25 * snoise(uvw * 4.0 - vec3(0.0, 0.0, time*2.0));
n += 0.125 * snoise(uvw * 8.0 - vec3(0.0, 0.0, time*2.8));
n += 0.0625 * snoise(uvw * 16.0 - vec3(0.0, 0.0, time*4.0));
n += 0.03125 * snoise(uvw * 32.0 - vec3(0.0, 0.0, time*5.6));
n = n * 0.7;
// A "hot" colormap - cheesy but effective
gl_FragColor = vec4(vec3(1.0, 0.5, 0.0) + vec3(n, n, n), 1.0);
}
绘制功能:
public void draw(float[] mvpMatrix)
{
GLES20.glUseProgram(m_program);
int positionHandle = GLES20.glGetAttribLocation(m_program, "vPosition");
GLES20.glEnableVertexAttribArray(positionHandle);
int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, m_vertexBuffer);
int timeHandle = GLES20.glGetUniformLocation(m_program, "time");
GLES20.glUniform1f(timeHandle, time);
int mvpMatrixHandle = GLES20.glGetUniformLocation(m_program, "uMVPMatrix");
BackgroundRenderer.checkGlError("glGetUniformLocation");
GLES20.glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0);
BackgroundRenderer.checkGlError("glUniformMatrix4fv");
GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, m_drawListBuffer);
GLES20.glDisableVertexAttribArray(positionHandle);
}
不幸的是,我得到的只是
java.lang.RuntimeException: glGetUniformLocation: glError 1282
在这条线上提出
int mvpMatrixHandle = GLES20.glGetUniformLocation(m_program, "uMVPMatrix");
我应该如何更改我的代码?
答案 0 :(得分:3)
glGetError (...)
,因此您认为int mvpMatrixHandle = GLES20.glGetUniformLocation(m_program, "uMVPMatrix");
是GL_INVALID_OPERATION
的来源,而实际上glUseProgram (...)
是第一行函数中的代码可以触发它。我已经更正了你的顶点和片段着色器,以便它们可以编译......你不能在片段着色器中使用gl_Position
,这就是你的着色器无法编译而你的程序没有链接的原因。 / p>
uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
varying vec4 vtx_pos;
void main()
{
gl_Position = uMVPMatrix * vPosition;
vtx_pos = gl_Position;
}
varying vec4 vtx_pos;
uniform float time; // Used for texture animation
float snoise(vec3 v) {
// ..
}
void main( void )
{
vec3 v_texCoord3D = vtx_pos.xyz;
vec3 uvw = v_texCoord3D + 0.1*vec3(snoise(v_texCoord3D + vec3(0.0, 0.0, time)),
snoise(v_texCoord3D + vec3(43.0, 17.0, time)),
snoise(v_texCoord3D + vec3(-17.0, -43.0, time)));
// Six components of noise in a fractal sum
float n = snoise(uvw - vec3(0.0, 0.0, time));
n += 0.5 * snoise(uvw * 2.0 - vec3(0.0, 0.0, time*1.4));
n += 0.25 * snoise(uvw * 4.0 - vec3(0.0, 0.0, time*2.0));
n += 0.125 * snoise(uvw * 8.0 - vec3(0.0, 0.0, time*2.8));
n += 0.0625 * snoise(uvw * 16.0 - vec3(0.0, 0.0, time*4.0));
n += 0.03125 * snoise(uvw * 32.0 - vec3(0.0, 0.0, time*5.6));
n = n * 0.7;
// A "hot" colormap - cheesy but effective
gl_FragColor = vec4(vec3(1.0, 0.5, 0.0) + vec3(n, n, n), 1.0);
}