1

I am parsing a 3D file into OpenGL ES on an iOS device and after I get the vertices I can't seem to add them to the GLfloat containing my vertices. At the top of my file I declare this GLFloat:

    GLfloat gFileVertices[] = {
        -0.686713, 0.346845, 3.725390,          -0.000288, -0.000652, -0.000109,
        -0.677196, 0.350971, 3.675733,          -0.000288, -0.000652, -0.000109, 
        -0.673889, 0.340921, 3.726985,          -0.000288, -0.000652, -0.000109, 
        -0.677424, 0.337048, 3.775731,          -0.000283, -0.000631, -0.000071, 
        And so on...
    }

But how can I put that same data (x,y,z normal.x, normal.y, normal.z) into that array in an instance in which each of those are variables and there are a variable number of rows?

4

1 回答 1

0

解决方案是在运行时动态分配顶点缓冲区,而不是在编译时静态分配。在您的代码中,一旦程序编译完成,就无法更改 gFileVertices 的大小。

出于管理目的,我将改为使用单独的法线和顶点数组,而不是将数据交错为一个。

要解析文件,请确定顶点数并分配缓冲区。

GLfloat* verticesBuff = malloc(sizeof(GLfloat) * vertCount * 3); /* 3 floats per vert */
GLfloat* normalsBuff = malloc(sizeof(GLfloat) * vertCount * 3); /* 3 floats per vert */

然后将每个元素复制到新数组中:

  /* read from file or whatevs */
  for (int i = 0; i < vertCount; i ++)
  {
      verticesBuff[i * 3] = ... 
      verticesBuff[i * 3 + 1] = ...
      verticesBuff[i * 3 + 2] = ... 

      normalsBuff[i * 3] = ... 
      normalsBuff[i * 3 + 1] = ... 
      normalsBuff[i * 3 + 2] = ... 

  }

动态数组可以像静态一样在 OpenGL 中使用:

glVertexPointer(3, GL_FLOAT, 0, verticesBuff); 
glNormalPointer(GL_FLOAT, 0, normalsBuff);

而已!完成后请确保删除:

free(verticesBuff);
free(normalsBuff);
于 2013-07-11T21:04:38.567 回答