0

我有一个 typedef 结构,如下所示:

typedef struct {
    float Position[3];
    float Color[4];
    float TexCoord[2];
} Vertex;

我想遍历位置、颜色和 TexCoord 的数据:

+ (void)arrayConverter: (Vertex *) v
{

    // Turn typeDef struct into seperate arrays
    NSMutableArray *position   = [[NSMutableArray alloc] init];

    for (int p=0; p<(sizeof(v->Position)/sizeof(v)); p++) {
        [position addObject:[NSNumber numberWithFloat:v[p].Position]]; // Error: Sending 'float[3]' to parameter of incompatible type 'float'
    }
}

传入的数据:

Vertex Square_Vertices[] = {
    // Front
    {{0.5, -0.5, 1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, 0.5, 1}, {0, 0, 0, 1}, {1, 1}},
    {{-0.5, 0.5, 1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, -0.5, 1}, {0, 0, 0, 1}, {0, 0}},
    // Back
    {{0.5, 0.5, -1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, -0.5, -1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, -0.5, -1}, {0, 0, 0, 1}, {0, 0}},
    {{-0.5, 0.5, -1}, {0, 0, 0, 1}, {1, 1}},
    // Left
    {{-0.5, -0.5, 1}, {0, 0, 0, 1}, {1, 0}},
    {{-0.5, 0.5, 1}, {0, 0, 0, 1}, {1, 1}},
    {{-0.5, 0.5, -1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, -0.5, -1}, {0, 0, 0, 1}, {0, 0}},
    // Right
    {{0.5, -0.5, -1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, 0.5, -1}, {0, 0, 0, 1}, {1, 1}},
    {{0.5, 0.5, 1}, {0, 0, 0, 1}, {0, 1}},
    {{0.5, -0.5, 1}, {0, 0, 0, 1}, {0, 0}},
    // Top
    {{0.5, 0.5, 1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, 0.5, -1}, {0, 0, 0, 1}, {1, 1}},
    {{-0.5, 0.5, -1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, 0.5, 1}, {0, 0, 0, 1}, {0, 0}},
    // Bottom
    {{0.5, -0.5, -1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, -0.5, 1}, {0, 0, 0, 1}, {1, 1}},
    {{-0.5, -0.5, 1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, -0.5, -1}, {0, 0, 0, 1}, {0, 0}}
};

如何遍历数据并添加到我的 NSMutableArray 而不会出现此错误?

4

1 回答 1

1

如果要遍历v->Position单个顶点的所有元素,则应该是

for (int p=0; p<(sizeof(v->Position)/sizeof(v->Position[0])); p++) {
    [position addObject:[NSNumber numberWithFloat:v->Position[p]]];
}

更新:对于顶点数组,您的方法可能如下所示:

+ (void)arrayConverter: (Vertex *)vertices count:(NSUInteger)count
{
    NSMutableArray *position   = [[NSMutableArray alloc] init];
    Vertex *v = vertices;
    for (NSUInteger i = 0; i < count; i++) {
        for (int p=0; p<(sizeof(v->Position)/sizeof(v->Position[0])); p++) {
            [position addObject:[NSNumber numberWithFloat:v->Position[p]]];
        }
        v++;
    }
}

如果

Vertex Square_Vertices[] = { ... }

是一个顶点数组,你可以调用该方法

[YourClass arrayConverter:Square_Vertices count:(sizeof(Square_Vertices)/sizeof(Square_Vertices[0]))];
于 2013-08-02T13:02:14.683 回答