gpt4 book ai didi

ios - 什么是 OpenGL 中的切片?

转载 作者:行者123 更新时间:2023-11-28 21:19:31 26 4
gpt4 key购买 nike

在下面的代码中,为什么我们需要切片?它有什么用?

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

最佳答案

该代码生成如下所示的球体网格:

Sphere mesh

来源:https://commons.wikimedia.org/wiki/File:Sphere_wireframe_10deg_6r.svg抄送 3.0

如图所示,有水平平行线和垂直线,它们都在两极相交。水平线通常称为平行线,而垂直线称为子午线。该代码的作者显然不知道这个术语,因此他们将其称为“切片”。

关于ios - 什么是 OpenGL 中的切片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com