convert NSArray in float[]
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();
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();
Here's one (probably not particularly fast) way to do it:
(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...
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...
Using an NSEnumerator might be slightly faster:
(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.
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];
}
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.
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!
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
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.
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).
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).
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
float FBO on iPhone (invalid operation error) | Fenris | 11 | 16,616 |
Jan 17, 2013 05:40 AM Last Post: Fenris |
|
Best way to readback float pixels? | kelvin | 8 | 9,358 |
Feb 27, 2008 10:23 PM Last Post: kelvin |
|
Convert Maya model into OpenGL | creater16 | 13 | 15,356 |
Jul 28, 2005 03:58 AM Last Post: creater16 |
|
can display float numbers but not integers | WhatMeWorry | 5 | 7,585 |
Jan 3, 2005 10:27 AM Last Post: ThemsAllTook |