2013-08-02 38 views
0

我有一個typedef結構如參數如下:錯誤:發送「浮動[3]」到不兼容的類型「浮動」

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

我如何通過數據進行迭代,並添加到我的NSMutabl eArray沒有得到這個錯誤?

回答

1

如果你想遍歷的v->Position所有元素的一個頂點,它應該是

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

UPDATE:對於頂點數組,你的方法看起來是這樣的:

+ (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]))]; 
+0

當我做到這一點我沒有得到我的全部傳遞 – user1585646

+0

@ user1585646數據:你想添加一個頂點的位置或頂點數組的頂點? - 也許顯示如何調用該方法。 –

+0

頂點數組 – user1585646

相關問題