1

I am trying to figure out how to take a typdef struct such as the following:

typedef struct {
    float Position[3];
    float Color[4];
    float TexCoord[2];
} const Vertex;

and turn it into seperate arrays.

I have the following as my conversion array:

+ (...)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;
}

For some reason I only get the first line of data for each position, color and texcoord. How can I get the rest of the data I am passing in.

See below of data being passed in.

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 information:

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
)
3
  • 1
    p<(sizeof(v->Position)/sizeof(v)) - why sizeof(v)? did you mean sizeof(*v) instead? (I still don't understand why that's useful, but anyway...) Commented Jul 31, 2013 at 7:28
  • @H2CO3 It gives me the actual numbers for each position, color and texcoord. Commented Jul 31, 2013 at 8:01
  • Oh I've missed that part when you concatenate position, color and texcoord arrays into one resulting NSMutableArray. Why do you need this? Commented Jul 31, 2013 at 8:31

3 Answers 3

7

Edit (According to your question):

 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];
  }

To retrieve these structs as follows:

NSValue *structValue = [array objectAtIndex:0];
Vertex *myNode = (Vertex *)[structValue pointerValue];

For example:

float postionValue = myNode->Position[0];
float colorValue = myNode->Color[1];
float textCoordValue = myNode->TexCoord[1];
Sign up to request clarification or add additional context in comments.

9 Comments

Can you explain this code with comments please? what is cstruct? is that where Vertex should be?
Okay what about the @encode(struct cstruct)? should that be replaced with Vertex as well? and also why "&data"?
I get an error there that says "@encode" of incomplete type 'struct Vertex'.
It's incorrect to take NSValue from pointer using valueWithPointer: method. Read apple's documentation. This method does not copy the contents of aPointer, so you must not to free the memory at the pointer destination while the NSValue object exists. (developer.apple.com/library/mac/#documentation/Cocoa/Reference/…)
How can I actually see the number instead of memory address?
|
2

You may try convert you Vertex struct to NSData before adding to array. It is much simpler approach to store structs in NSArray.

// make a NSData object
NSData *myData = [NSData dataWithBytes:&myStruct length:sizeof(myStruct)];

// make a new PacketJoin
MyStruct myStruct;
[myData getBytes:&myStruct length:sizeof(myStruct)];

2 Comments

I get an error at "getBytes". Can't send 'Vertex' to parameter of type 'void'.
[d getBytes:&v length:sizeof(v)]; I don't get error in this case. Maybe you forget ampersand (&) before parameter v.
0

Sorry, I had misunderstood your question. Now I've understand that you need to output your 1 vertice position,color,texcoords into array. So quick answer is

+ (...)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<3; p++) {
        [position addObject:[NSNumber numberWithFloat:v->Position[p]]];
    }

    for (int c=0; c<4; c++) {
        [color addObject:[NSNumber numberWithFloat:v->Color[c]]];
    }

    for (int t=0; t<2; t++) {
        [texcord addObject:[NSNumber numberWithFloat:v->TexCoord[t]]];
    }

    [vertexdata addObjectsFromArray:position];
    [vertexdata addObjectsFromArray:color];
    [vertexdata addObjectsFromArray:texcord];

    return vertexdata;
}

8 Comments

Correct! I want to take my typdef struct and turn it into one array.
But I am not getting all of the data to pass in. Only the first line.
Had you tried to replace this line sizeof(v->Position)/sizeof(v) with a constant?
I get the same result that I had with using sizeof.
What do you have in the resulting array when you pass vertex {{1,2,3}, {10,20,30, 40},{100,200}} ?
|

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.