Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glGenTextures returns zero in background thread

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?

like image 875
Division Avatar asked Aug 12 '10 13:08

Division


1 Answers

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.

like image 148
Dr. Snoopy Avatar answered Oct 21 '22 05:10

Dr. Snoopy