【问题标题】:android opengl rendering garbageandroid opengl 渲染垃圾
【发布时间】: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


【解决方案1】:

所以...我发现问题是我正在做的一件非常愚蠢的事情,

//on this line I was sending length, where length was
//the length of the vertices for the triangle it was "9" (x,y,z for each vertex)
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, length);
//I had to divide that by the number of components for each vertex
//so when the vertex only has position attributes (x,y,z) is divided by 3
//when you have more i.e. normals it will be divided by 6 (x,y,z, normalX, normalY, normalZ)
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, length/mVertexAttributes.vertexNumComponents);

我希望这对其他人有所帮助。

【讨论】:

  • 我犯了同样的错误:我把mVertexBuffer.limit() 放在count 上,这是缓冲区中float 数字的数量,而不是顶点的数量。
猜你喜欢
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 2011-10-24
  • 1970-01-01
相关资源
最近更新 更多