我试图弄清楚如何采用 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;
}
出于某种原因,我只得到每个位置、颜色和 texcoord 的第一行数据。我怎样才能得到我传递的其余数据。
请参阅下面的传入数据。
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
)