android opengl渲染垃圾

标签 android opengl-es opengl-es-2.0 garbage

我一直在做一些简单的着色器,我遇到了一个随机发生的错误,当我开始渲染我的场景时,有时网格会用额外的向量渲染,如果我终止 Activity 然后打开相同的 Activity 它有时渲染时没有额外的向量。

我的猜测是,当我终止 Activity 时,GPU 上的内存并没有被完全清除。更奇怪的是,这些额外的多边形有时是使用我的着色器逻辑渲染的,有时它们渲染时就像填充了随机方 block 一样。

我快要疯了我已经检查了所有代码,从我读取 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);

这里有一些屏幕截图供您查看问题:

还有一个链接,指向我在手机上运行应用程序时拍摄的视频。

最佳答案

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

//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);

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

关于android opengl渲染垃圾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9781868/

相关文章:

android - Android 上 OpenGL ES 2.0 中的 frustumM/setLookAtM

android - 如何强制 SurfaceView 占据屏幕的特定部分

android - 同一滚动下的水平和垂直 RecyclerView

android - Android 上的 OpenGL ES 坐标系?

ios - 深度纹理作为颜色附件

android - 位图纹理不会显示在 Android 上的帧缓冲区表面中

android - 在 Google App Engine 上,我可以关联 Google OAuth 2 token 和使用 Android 的 AccountManager 获得的 SACSID token 吗?

java - 在 Android 中全屏显示来电 Activity

opengl-es - 优化基于 OpenGL 的动画/可缩放 2D UI 渲染的策略

ios - 游戏循环更新方法中使用的时间增量或时间戳是什么?