将typedef结构转换为NSMutableArray

时间:2013-07-31 07:22:35

标签: iphone ios objective-c opengl-es

我试图弄清楚如何采用如下的typdef结构:

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

并将其转换为单独的数组。

我有以下作为我的转化数组:

+ (...)arrayConverter: (Vertex *) v
{
    // Turn typeDef struct into seperate arrays
    NSMutableArray *position          = [[NSMutableArray alloc] init];
    NSMutableArray *color             = [[NSMutableArray alloc] init];
    NSMutableArray *texcord           = [[NSMutableArray alloc] init];
    const NSMutableArray *vertexdata  = [[NSMutableArray alloc] init];

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

    for (int c=0; c<(sizeof(v->Color)/sizeof(v)); c++) {
        [color addObject:[NSNumber numberWithFloat:v->Color[c]]];
    }

    for (int t=0; t<(sizeof(v->TexCoord)/sizeof(v)); t++) {
        [texcord addObject:[NSNumber numberWithFloat:v->TexCoord[t]]];
    }

    [vertexdata addObjectsFromArray:position];
    [vertexdata addObjectsFromArray:color];
    [vertexdata addObjectsFromArray:texcord];

NSLog(@"\n sizeof Position: %lu\n sizeof Color: %lu\n sizeof TexCoord: %lu\n sizeof Vertex: %lu\n", sizeof(v->Position), sizeof(v->Color), sizeof(v->TexCoord), sizeof(v));
NSLog(@"\n Position array: %@\n Color array: %@\n TexCord array: %@\n",position, color, texcord);
NSLog(@"\n Vertex data: %@\n",vertexdata);

    return vertexdata;
}

出于某种原因,我只获得每个位置,颜色和特殊记录的第一行数据。如何获取我传递的其余数据。

请参阅以下传入的数据。

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

NSLog信息:

2013-07-31 01:42:45.346
 sizeof Position: 12
 sizeof Color: 16
 sizeof TexCoord: 8
 sizeof Vertex: 4
2013-07-31 01:42:45.347 
 Position array: (
    "0.5",
    "-0.5",
    1
)
 Color array: (
    0,
    0,
    0,
    1
)
 TexCord array: (
    1,
    0
)
2013-07-31 01:42:45.348  
 Vertex data: (
    "0.5",
    "-0.5",
    1,
    0,
    0,
    0,
    1,
    1,
    0
)

3 个答案:

答案 0 :(得分:7)

编辑(根据您的问题):

 NSMutableArray *array = [NSMutableArray array];
 Vertex data;
 int i;
  for (i = 1;  i <= yourCount;  i++) 
  {
     NSValue *value = [NSValue valueWithBytes:&data objCType:@encode(Vertex)];
     [array addObject:value];
  }

按如下方式检索这些结构:

NSValue *structValue = [array objectAtIndex:0];
Vertex *myNode = (Vertex *)[structValue pointerValue];

例如:

float postionValue = myNode->Position[0];
float colorValue = myNode->Color[1];
float textCoordValue = myNode->TexCoord[1];

答案 1 :(得分:2)

在添加到数组之前,您可以尝试将Vertex结构转换为NSData。在NSArray中存储结构的方法要简单得多。

// make a NSData object
NSData *myData = [NSData dataWithBytes:&myStruct length:sizeof(myStruct)];

// make a new PacketJoin
MyStruct myStruct;
[myData getBytes:&myStruct length:sizeof(myStruct)];

答案 2 :(得分:0)

抱歉,我误解了你的问题。 现在我明白你需要将1个顶点位置,颜色,texcoords输出到数组中。 如此快速的答案是

+ (...)arrayConverter: (Vertex *) v
{
    // Turn typeDef struct into seperate arrays
    NSMutableArray *position          = [[NSMutableArray alloc] init];
    NSMutableArray *color             = [[NSMutableArray alloc] init];
    NSMutableArray *texcord           = [[NSMutableArray alloc] init];
    const NSMutableArray *vertexdata  = [[NSMutableArray alloc] init];

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

    for (int c=0; c<4; c++) {
        [color addObject:[NSNumber numberWithFloat:v->Color[c]]];
    }

    for (int t=0; t<2; t++) {
        [texcord addObject:[NSNumber numberWithFloat:v->TexCoord[t]]];
    }

    [vertexdata addObjectsFromArray:position];
    [vertexdata addObjectsFromArray:color];
    [vertexdata addObjectsFromArray:texcord];

    return vertexdata;
}