2016-10-26 2 views
0

Im Code unten, Warum brauchen wir Scheiben? und wofür?Was sind Slices in 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; 
} 

Antwort

2

Dieser Code eine Kugel Gitter erzeugt, das wie folgt aussieht:

Sphere mesh

Quelle: https://commons.wikimedia.org/wiki/File:Sphere_wireframe_10deg_6r.svg CC BY 3.0

Wie Sie sehen im Bild, gibt es horizontale parallele Linien und vertikale Linien, die sich alle an den Polen treffen. Die horizontalen Linien werden typischerweise Parallele genannt, wohingegen die vertikalen Linien Meridiane genannt werden. Der Autor dieses Codes kannte diesen Begriff offenbar nicht, daher nannten sie ihn "Slices".