【问题标题】:OpenGL ES - updating the vertex array, adding/removing verticesOpenGL ES - 更新顶点数组,添加/删除顶点
【发布时间】:2011-04-20 01:19:33
【问题描述】:

我想实现功能,以便在运行时向顶点数组添加/删除顶点。 有没有常见的方法?

顶点数据的推荐格式似乎是 C 结构数组, 所以我尝试了以下方法。将指向 Vertex 结构数组的指针作为属性:

@property Vertex *vertices;

然后创建一个新数组并将数据复制过来

- (void) addVertex:(Vertex)newVertex
{
    int numberOfVertices = sizeof(vertices) / sizeof(Vertex);
    Vertex newArray[numberOfVertices + 1];

    for (int i = 0; i < numberOfVertices; i++) 
        newArray[i] = vertices[i];

    newArray[numberOfVertices] = newVertex;
    self.vertices = newArray;
}

但没有运气。我对 C 不太有信心,所以这可能真的很微不足道..

【问题讨论】:

    标签: ios opengl-es


    【解决方案1】:

    我就是这样做的:

    //verts is an NSMutableArray and I want to have an CGPoint c array to use with 
    //  glVertexPointer(2, GL_FLOAT, 0, vertices);... so:
    
    CGPoint vertices[[verts count]];
    for(int i=0; i<[verts count]; i++)
    {
        vertices[i] = [[verts objectAtIndex:i] CGPointValue];
    }
    

    【讨论】:

      【解决方案2】:

      这是我现在的做法:

      // re-allocate the array dynamically. 
      // realloc() will act like malloc() if vertices == NULL
      Vertex newVertex = {{x,y},{r,g,b,a}};
      numberOfVertices++;
      vertices = (Vertex *) realloc(vertices, sizeof(Vertex) * numberOfVertices);
      if(vertices == NULL) NSLog(@"FAIL allocating memory for vertex array");
      else vertices[numberOfVertices - 1] = newVertex;
      
      // clean up memory once i don't need the array anymore
      if(vertices != NULL) free(vertices);
      

      我认为上面 icnivad 的方法更灵活,因为您可以使用 NSMutableArray 做更多的事情,但是使用带有 malloc/realloc 的普通 C 数组应该(很多?)更快。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-19
        • 2015-08-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多