By omartayeb
on Wed, 06/20/2012 - 17:06
I'm having an issue with texturing models that are being loaded on a background thread then shown.
My code renders models in a similar way as the ImageTargets example:
[CODE]
....
glUseProgram(shaderProgramID);
glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)&model.vertices);
glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)&model.normals);
glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)&model.texCoords);
glEnableVertexAttribArray(vertexHandle);
glEnableVertexAttribArray(normalHandle);
glEnableVertexAttribArray(textureCoordHandle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, [thisTexture textureID]);
glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE, (const GLfloat*)&modelViewProjection.data[0]);
glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT, (const GLvoid*)&model.indices);
.....
[/CODE]
This is called on the same thread as the renderFrameQCAR function.
While this is running I may be loading a model in the background with textures. Everything is done on a background thread except this function which is done on the main thread using:
[CODE]
....
if ([texture loadImage:file]) {
[self performSelectorOnMainThread:@selector(generateTexture:) withObject:texture waitUntilDone:YES];
}
....
-(void)generateTexture:(Texture *)texture {
GLuint id;
glGenTextures(1, &id);
[texture setTextureID: id];
glBindTexture(GL_TEXTURE_2D, id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, [texture width], [texture height], 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)[texture pngData]);
}
[/CODE]
This works fine most of the time, but every once in a while the model appears black (no texture) or the texture gets associated with another model or sometimes it adds that texture (and stretches it) to the background camera view (so it's always visible, only way to get it off the camera view is by calling onPause then onResume).
Only things I can think of is:
Can the generateTexture function be interrupting the model rendering code in the middle causing it to get confused with what to texture?
Does the generateTexture method sometimes need to be called on a thread other than the main thread? Does renderFrameQCAR sometimes get called on the main thread and sometimes on another thread?
I've tried wrapping the render code and the generateTexture function with @synchronized(self){}, but that didn't help.
I haven't been able to figure it out, let me know if you need more code.
Re: Texturing problem
Re: Texturing problem