use*_*646 1 iphone opengl-es objective-c ios
我试图弄清楚如何采用如下的typdef结构:
typedef struct {
float Position[3];
float Color[4];
float TexCoord[2];
} const Vertex;
Run Code Online (Sandbox Code Playgroud)
并将其转换为单独的数组.
我有以下作为我的转换数组:
+ (...)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;
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我只获得每个位置,颜色和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}}
};
Run Code Online (Sandbox Code Playgroud)
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
)
Run Code Online (Sandbox Code Playgroud)
编辑(根据您的问题):
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];
}
Run Code Online (Sandbox Code Playgroud)
要检索这些结构,如下所示:
NSValue *structValue = [array objectAtIndex:0];
Vertex *myNode = (Vertex *)[structValue pointerValue];
Run Code Online (Sandbox Code Playgroud)
例如:
float postionValue = myNode->Position[0];
float colorValue = myNode->Color[1];
float textCoordValue = myNode->TexCoord[1];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
182 次 |
| 最近记录: |