views:

20

answers:

1

I need to load textures in background thread in OpenGL ES. But glGenTextures always returns zero when called in background thread.

-(void) someMethodInMainThread {
   [self performSelectorInBackground:@selector(load) withObject:nil];
}

-(void) load {
   GLuint textureID = 0;
   glGenTextures(1, &textureID);        
}

textureID is zero. If i change code to [self performSelector:@selector(tmp) withObject:nil]; it will work correct and return 1. How should i load textures in background thread?

+1  A: 

This is a common error, each OpenGL context can be active (current) in one thread only, so when you create a new thread, it doesn't have any OpenGL context, and all GL calls fail.

Solution: Create another OpenGL context, make it current in your background thread. To load textures, you also want to share OpenGL names (texture ids, etc) with the main context.

Matias Valdenegro
I have to use EAGLSharegroup?
Division
Looks like it, i'm not an iPhone specialist, but by googling i see that's the handle to share resources between GL contexts.
Matias Valdenegro