0

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 !

1
  • Vertex3D you use in const Vertex3D tigerBottomPositions[] can not be the same as the class Vertex3D the you defined. Commented Feb 16, 2012 at 21:10

1 Answer 1

1

You need a dynamic array... given a C structure Vertex3D (not an Objective-C class):

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
Sign up to request clarification or add additional context in comments.

4 Comments

Georg - thank you - but I'm confused where I would put this code - I am right that I would need to create a separate method in my SceneObject class ? Also how can I then reference positions from the rest of the class - I dont' seem to be able to declare positions in the header file.
@Ohn: That depends on where and how you want to use it. If you want to use the data only once, you could just convert it where you need it. If you want to use it multiple times you could do this after parsing and store it for later use.
Great - thanks - how would I store positions so that it is visible to the rest of the class ?
@Ohn: As an instance variable. Don't forget to store the size as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.