convert NSArray in float[]

Apprentice
Posts: 9
Joined: 2010.02
Post: #1
Hi,
anyone know, how convert an NSArray to float[]?

NSArray *_vertices; //my Vertex Data -> 1.0, 1.0, 1.0

tell OpenGL to use my NSArray but how??

glVertex3fv();
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Here's one (probably not particularly fast) way to do it:

Code:
int count, index;
float * floatArray;

count = [myNSArray count];
floatArray = (float *) malloc(sizeof(float) * count);
for (index = 0; index < count; index++) {
  floatArray[index] = [[myNSArray objectAtIndex: index] floatValue];
}

(untested code, but should work)

If this is a list of vertices you'll be passing to OpenGL, you might not want to put it into an NSArray in the first place if you can avoid it...
Quote this message in a reply
Member
Posts: 114
Joined: 2005.03
Post: #3
Using an NSEnumerator might be slightly faster:
Code:
NSEnumerator *enumerator;
float * floatArray;
id floatObject;

floatArray = (float *) malloc(sizeof(float) * [myNSArray count]);
enumerator = [myNSArray objectEnumerator];
while(floatObject = [enumerator nextObject])
{
  floatArray[index] = [floatObject floatValue];
}
(also untested)

But of course, this is still a slooooooow thing to do, so follow ThemsAllTook's advice and don't use an NSArray for this if possible.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #4
Instead of not doing it at all, its probably acceptable to just do it once (instead of once per frame).

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2010.02
Post: #5
ThemsAllTook Wrote:Here's one (probably not particularly fast) way to do it:

Code:
int count, index;
float * floatArray;

count = [myNSArray count];
floatArray = (float *) malloc(sizeof(float) * count);
for (index = 0; index < count; index++) {
  floatArray[index] = [[myNSArray objectAtIndex: index] floatValue];
}

(untested code, but should work)

If this is a list of vertices you'll be passing to OpenGL, you might not want to put it into an NSArray in the first place if you can avoid it...

thanks, works well
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #6
Cochrane Wrote:Using an NSEnumerator might be slightly faster:
That's what I keep thinking too, but I haven't found any noticeable difference yet anywhere I've tested for it.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #7
You will notice it, eventually! A few years ago I was profiling some shadow volume extrusion code and it turned out that using std::vector's array operator was killing performance. Casting my array's front() to a raw pointer, and accessing that brought CPU hit in the internal loops from something like 80% of CPU time down to essentially nothing.

I'm not suggesting premature optimization, but keep in mind to shark your code now and then. You might write a correct but slow path and forget all about it until, 6 months from now when you're really stress testing your code, you wonder "Why is this so slow?"

What I'd do, personally, is let NSArray manage your storage while you're building or otherwise manipulating your data. But once your data's ready, copy it over to a raw array and let go of the NSArray.

Or, if you're a C++ guy like me, just use std::vector, std::set, etc. They're surprisingly fast (enough).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  float FBO on iPhone (invalid operation error) Fenris 11 8,440 Jan 17, 2013 05:40 AM
Last Post: Fenris
  Best way to readback float pixels? kelvin 8 5,646 Feb 27, 2008 10:23 PM
Last Post: kelvin
  Convert Maya model into OpenGL creater16 13 9,074 Jul 28, 2005 03:58 AM
Last Post: creater16
  can display float numbers but not integers WhatMeWorry 5 5,149 Jan 3, 2005 10:27 AM
Last Post: ThemsAllTook