2016-10-26 61 views
0

在下面的代碼中,爲什麼我們需要切片?它有什麼用途?什麼是OpenGL中的切片?

//https://github.com/danginsburg/opengles-book-samples/blob/604a02cc84f9cc4369f7efe93d2a1d7f2cab2ba7/iPhone/Common/esUtil.h#L110 
int esGenSphere(int numSlices, float radius, float **vertices, 
       float **texCoords, uint16_t **indices, int *numVertices_out) { 
    int numParallels = numSlices/2; 
    int numVertices = (numParallels + 1) * (numSlices + 1); 
    int numIndices = numParallels * numSlices * 6; 
    float angleStep = (2.0f * ES_PI)/((float) numSlices); 

    if (vertices != NULL) { 
     *vertices = malloc(sizeof(float) * 3 * numVertices); 
    } 

    if (texCoords != NULL) { 
     *texCoords = malloc(sizeof(float) * 2 * numVertices); 
    } 

    if (indices != NULL) { 
     *indices = malloc(sizeof(uint16_t) * numIndices); 
    } 

    for (int i = 0; i < numParallels + 1; i++) { 
     for (int j = 0; j < numSlices + 1; j++) { 
      int vertex = (i * (numSlices + 1) + j) * 3; 

      if (vertices) { 
       (*vertices)[vertex + 0] = radius * sinf(angleStep * (float)i) * sinf(angleStep * (float)j); 
       (*vertices)[vertex + 1] = radius * cosf(angleStep * (float)i); 
       (*vertices)[vertex + 2] = radius * sinf(angleStep * (float)i) * cosf(angleStep * (float)j); 
      } 

      if (texCoords) { 
       int texIndex = (i * (numSlices + 1) + j) * 2; 
       (*texCoords)[texIndex + 0] = (float)j/(float)numSlices; 
       (*texCoords)[texIndex + 1] = 1.0f - ((float)i/(float)numParallels); 
      } 
     } 
    } 

    // Generate the indices 
    if (indices != NULL) { 
     uint16_t *indexBuf = (*indices); 
     for (int i = 0; i < numParallels ; i++) { 
      for (int j = 0; j < numSlices; j++) { 
       *indexBuf++ = i * (numSlices + 1) + j; 
       *indexBuf++ = (i + 1) * (numSlices + 1) + j; 
       *indexBuf++ = (i + 1) * (numSlices + 1) + (j + 1); 

       *indexBuf++ = i * (numSlices + 1) + j; 
       *indexBuf++ = (i + 1) * (numSlices + 1) + (j + 1); 
       *indexBuf++ = i * (numSlices + 1) + (j + 1); 
      } 
     } 
    } 

    if (numVertices_out) { 
     *numVertices_out = numVertices; 
    } 

    return numIndices; 
} 

回答