【发布时间】:2012-03-20 05:42:51
【问题描述】:
我一直在做一些简单的着色器,我遇到了一个随机发生的错误,当我开始渲染我的场景时,有时网格会用额外的向量渲染,如果我终止了活动,然后我打开了相同的活动有时会在没有额外向量的情况下进行渲染。
我的猜测是,当我终止 Activity 时,GPU 上的内存并没有完全清除。更奇怪的是,这些额外的多边形有时会使用我的着色器逻辑进行渲染,而有时它们会像填充随机正方形一样渲染。
我快疯了我已经查看了所有代码,从我读取 obj 的地方到我设置顶点属性的地方,如果你以前见过这个,请告诉我。顺便说一句,我正在使用带有 android 2.1 的摩托罗拉里程碑。
这是我创建一个简单三角形并设置顶点属性的相关代码:
//This is where I create the mesh
mMesh = new Mesh();
mMesh.setVertices(new float[]{-0.5f, 0f, 0.5f,
0.5f, 0f, -0.5f,
-0.5f, 0f, -0.5f});
ArrayList<VertexAttribute> attributes = new ArrayList<VertexAttribute>();
attributes.add(new VertexAttribute(Usage.Position, 3, ProgramShader.POSITION_ATTRIBUTE));
VertexAttributes vertexAttributes = new VertexAttributes(attributes.toArray(new VertexAttribute[attributes.size()]));
mMesh.setVertexAttributes(vertexAttributes);
...
...
.......
//This is where I send the mesh to opengl
for(VertexAttribute attr :mVertexAttributes.getAttributes().values()){
mVertexBuffer.position(attr.offset);
int handler = shader.getHandler(attr.alias);
if(handler != -1){
try{
GLES20.glVertexAttribPointer(handler, attr.numComponents, GLES20.GL_FLOAT, false, mVertexAttributes.vertexSize, mVertexBuffer);
GLES20.glEnableVertexAttribArray(handler);
}catch (RuntimeException e) {
Log.d("CG", attr.alias);
throw e;
}
}
}
//(length = 3 for a triangle)
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, length);
这里有一些屏幕截图供您查看问题:
这里还有一个我在手机上运行应用程序时拍摄的视频链接。
【问题讨论】:
-
您需要开始发布代码片段。尤其是您设置顶点并将它们传递给着色器的位,还有您的
onPause()和onResume功能。 -
问题的一些代码和图像可能会有所帮助(如果您没有足够的代表来内联它们,只需发布指向图像共享站点的链接)。我能感觉到你的痛苦,但我认为没有人能像现在这样帮助你。也许你只是在做一些别人可以发现的简单错误。
-
@Paul-Jan 我添加了一些截图和视频的链接,如果你仍然需要访问代码我会把它放在 github 中,但同时我希望这有助于缩小路径错误可能是。
-
检查你的顶点数组的大小,因为你可能弄乱了它的长度
标签: android opengl-es opengl-es-2.0 garbage