Log in or register to post comments

Texturing problem

October 5, 2011 - 2:25pm #1

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:

....
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);
.....

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:

....
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]);
}

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

December 8, 2011 - 5:19am #3

The way I got it to work was to implement a queue that had textures added to it from multiple threads. On the main thread a function was called every so often to empty this queue by calling glGenTextures(1, &id), glBindTexture(GL_TEXTURE_2D, id) on each element and emptying it.

Re: Texturing problem

November 24, 2011 - 1:39am #2

I'm facing a similar problem, only in my case it happens all the time with my Samsung Galaxy Tab p1000 and never happens with my Galaxy S II, both running android 2.3.3
Did you ever get it working?

Log in or register to post comments