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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With