将 NSMutableArray 转换为 C 结构体数组 - iPhone - OpenGL ES
我有一个基于 OpenGL ES 的应用程序,它使用 NSXMLParser 解析 xml 文件中的信息。当文件中的每个场景对象都有顶点信息时,我将其保存在 NSMutableArray 中,如下所示:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (floatArray) {
if (currentlyParsing == kVerticeInformation) {
if (currentParserTagType == kGeometry) {
//NSLog(@"Loaded Vertices %@", string);
NSArray * nums = [string componentsSeparatedByString:@" "];
culmunativeCount += [nums count];
//NSLog(@"Culm Count is %d", culmunativeCount);
[fullParseString appendString : string];
if (checkSumCount <= culmunativeCount) {
//NSLog(@"Full Parse String is %@", fullParseString);
NSArray * finishedArray = [fullParseString componentsSeparatedByString:@" "];
//NSLog(@"Finihsed ARray is %d", [finishedArray count]);
[finishedParsingArray addObjectsFromArray:finishedArray];
//NSLog(@" FINISHED PARSING = %d", [finishedParsingArray count]);
NSUInteger baseIndex;
for (baseIndex=0; baseIndex <[finishedParsingArray count]; baseIndex +=3) {
NSString * xPoint = [finishedParsingArray objectAtIndex:baseIndex];
NSDecimalNumber * errorCheckX = [NSDecimalNumber decimalNumberWithString:xPoint];
float x = [errorCheckX floatValue];
NSString * yPoint = [finishedParsingArray objectAtIndex:baseIndex +1];
NSDecimalNumber * errorCheckY = [NSDecimalNumber decimalNumberWithString:yPoint];
float y = [errorCheckY floatValue];
NSString * zPoint = [finishedParsingArray objectAtIndex:baseIndex+2];
NSDecimalNumber * errorCheckZ = [NSDecimalNumber decimalNumberWithString:zPoint];
float z = [errorCheckZ floatValue];
vertex3D = [[Vertex3D alloc]init];
vertex3D.value1 = x;
vertex3D.value2 = y;
vertex3D.value3 = z;
[verticesArray addObject:vertex3D];
//NSLog(@"Vertices stored are %f, %f, %f", x,y,z);
}
currentlyParsing = kNilSetting;
checkSumCount = 0;
[finishedParsingArray removeAllObjects];
//Set object to adds vertice array to verticesArray;
vertex3D = nil;
objectToAdd.Vertices = verticesArray;
[verticesArray removeAllObjects];
//[finishedArray release];
culmunativeCount = 0;
[fullParseString setString:@""];
}
}
}
}
Vertex3D 是一个用于存储 x、y、z 点的自定义类:
#import <Foundation/Foundation.h>
@interface Vertex3D : NSObject
{
float value1;
float value2;
float value3;
}
@property (assign) float value1;
@property (assign) float value2;
@property (assign) float value3;
@end
然后我要做的就是将此数组传递给SceneObject 类为文件中的每个对象实例化。我们的想法是使用这些 SceneObject 对象来为 OpenGL ES 引擎提供数据。我正在使用 glDrawElements,因为 xml 文件使用索引。
问题是我无法将 NSMutable 数组传递给 OpenGL ES 调用(例如 glVertexPointer)。
从我一直在关注的一个例子 http://www.everita .com/lightwave-collada-and-opengles-on-the-iphone,看起来我需要按如下方式构造顶点数据才能使用索引:
const Vertex3D tigerBottomPositions[] = {
{0.176567, 0.143711, 0.264963},
{0.176567, 0.137939, 0.177312},
{0.198811, 0.135518, 0.179324},
…
};
这是我有点困惑的地方 - 我认为这是一个数组C 结构体,包含 3 个 Vertex3D 对象,每个对象包含三个浮点数。
有人可以建议我如何将 NSMutableArray 转换为正确格式的 C 结构以便与 OpenGL ES 一起使用吗?我也很感谢有关如何在代码中设置此 C 结构的帮助,因为我的 C 知识不如我的 obj c 强。
先感谢您 !
I have an OpenGL ES based app which parses in information from an xml file using NSXMLParser. As the vertice information comes in for each scene object in the file, I am saving it in an NSMutableArray as follows :
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (floatArray) {
if (currentlyParsing == kVerticeInformation) {
if (currentParserTagType == kGeometry) {
//NSLog(@"Loaded Vertices %@", string);
NSArray * nums = [string componentsSeparatedByString:@" "];
culmunativeCount += [nums count];
//NSLog(@"Culm Count is %d", culmunativeCount);
[fullParseString appendString : string];
if (checkSumCount <= culmunativeCount) {
//NSLog(@"Full Parse String is %@", fullParseString);
NSArray * finishedArray = [fullParseString componentsSeparatedByString:@" "];
//NSLog(@"Finihsed ARray is %d", [finishedArray count]);
[finishedParsingArray addObjectsFromArray:finishedArray];
//NSLog(@" FINISHED PARSING = %d", [finishedParsingArray count]);
NSUInteger baseIndex;
for (baseIndex=0; baseIndex <[finishedParsingArray count]; baseIndex +=3) {
NSString * xPoint = [finishedParsingArray objectAtIndex:baseIndex];
NSDecimalNumber * errorCheckX = [NSDecimalNumber decimalNumberWithString:xPoint];
float x = [errorCheckX floatValue];
NSString * yPoint = [finishedParsingArray objectAtIndex:baseIndex +1];
NSDecimalNumber * errorCheckY = [NSDecimalNumber decimalNumberWithString:yPoint];
float y = [errorCheckY floatValue];
NSString * zPoint = [finishedParsingArray objectAtIndex:baseIndex+2];
NSDecimalNumber * errorCheckZ = [NSDecimalNumber decimalNumberWithString:zPoint];
float z = [errorCheckZ floatValue];
vertex3D = [[Vertex3D alloc]init];
vertex3D.value1 = x;
vertex3D.value2 = y;
vertex3D.value3 = z;
[verticesArray addObject:vertex3D];
//NSLog(@"Vertices stored are %f, %f, %f", x,y,z);
}
currentlyParsing = kNilSetting;
checkSumCount = 0;
[finishedParsingArray removeAllObjects];
//Set object to adds vertice array to verticesArray;
vertex3D = nil;
objectToAdd.Vertices = verticesArray;
[verticesArray removeAllObjects];
//[finishedArray release];
culmunativeCount = 0;
[fullParseString setString:@""];
}
}
}
}
Vertex3D is a custom class to store the x,y,z points :
#import <Foundation/Foundation.h>
@interface Vertex3D : NSObject
{
float value1;
float value2;
float value3;
}
@property (assign) float value1;
@property (assign) float value2;
@property (assign) float value3;
@end
What I am then doing is passing this array to a SceneObject class which is instantiated for every object in the file. The idea is then to use these SceneObject objects to feed the OpenGL ES engine. I am using glDrawElements as the xml file uses indices.
The problem is that I cannot pass an NSMutable array into the OpenGL ES calls (such as glVertexPointer).
From an example I have been following at http://www.everita.com/lightwave-collada-and-opengles-on-the-iphone, it looks like I need to structure the vertices data as follows in order to use indices :
const Vertex3D tigerBottomPositions[] = {
{0.176567, 0.143711, 0.264963},
{0.176567, 0.137939, 0.177312},
{0.198811, 0.135518, 0.179324},
…
};
This is where I get a bit confused - I think this is an array of C structs which contains 3 Vertex3D objects, each of which contains three floats.
Can anybody advise me on how I can convert my NSMutableArray into a C struct in the correct format for use with OpenGL ES ? I'd also appreciate help on how to setup this C struct in code as my C knowledge is not as strong as my obj c.
Thank you in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要一个动态数组...给定一个 C 结构
Vertex3D
(不是 Objective-C 类):You need a dynamic array... given a C structure
Vertex3D
(not an Objective-C class):