【问题标题】:Converting an NSMutableArray to an Array of C Structs - iPhone - OpenGL ES将 NSMutableArray 转换为 C 结构数组 - iPhone - OpenGL ES
【发布时间】:2012-03-08 07:10:55
【问题描述】:

我有一个基于 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 强。

提前谢谢你!

【问题讨论】:

  • Vertex3D 您在 const Vertex3D tigerBottomPositions[] 中使用的不能与您定义的类 Vertex3D 相同.

标签: iphone objective-c c opengl-es struct


【解决方案1】:

你需要一个动态数组...给定一个 C 结构 Vertex3D(不是 Objective-C 类):

NSArray *arr = ...;
const size_t count = [arr count];
Vertex3D *positions = (Vertex3D*)calloc(count, sizeof(Vertex3D));

for (size_t i=0; i<count; ++i) {
    positions[i].x = ...;
    // ... 
}

// ... use positions

free(positions); // clean up

【讨论】:

  • Georg - 谢谢 - 但我很困惑我应该把这段代码放在哪里 - 我是对的,我需要在我的 SceneObject 类中创建一个单独的方法?另外,我如何才能从类的其余部分引用位置 - 我似乎无法在头文件中声明位置。
  • @Ohn:这取决于你想在哪里以及如何使用它。如果您只想使用一次数据,您可以在需要的地方进行转换。如果您想多次使用它,您可以在解析后执行此操作并将其存储以备后用。
  • 太好了 - 谢谢 - 我将如何存储位置以便其他班级可以看到?
  • @Ohn:作为实例变量。不要忘记存储大小。
  • 谢谢 - 非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多