binding images to texture pauses my display loop!!!

Hi,

I’m sure there is a simple answer to this question-- I hope someone out there has a few seconds to point me in the right direction or even supply me with some code or psuedocode.

What’s the best way to bind images to a texture in real time?

My application needs to read jpegs from a remote database every 60 seconds or so. Every time it binds an image to a texture (using glTexImage2D) the display loop pauses for about 5 seconds. The slowness is definitely from the binding and not from the image loading. I’ve tried to move the code into its own thread, but I get strange behavior where the polygon appears but no texture is actually bound to it, or the entire polygon turns white.

As long as the binding occurs in the background then I actually don’t care about the speed of the binding-- I just don’t want it to pause and mess up my display loop!!!

Here’s my actual bindTexture method :


public void bindTexture(PhotoData photodata)
{
gl.glBindTexture(gl.GL_TEXTURE_2D, photodata.textureID);
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MAG_FILTER,gl.GL_LINEAR);
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MIN_FILTER,gl.GL_LINEAR);

ByteBuffer bb = ByteBuffer.wrap(photodata.data);
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, 3, photodata.width, photodata.height, 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, bb);
}

And this method is being called after I’ve loaded in an image (into my simple PhotoData class) remotely like so :


Image img = new ImageIcon(myURL).getImage();

Then I use PixelGrabber to get the bytes for the image before calling my bindTexture as above. Currently this is all triggered by pressing the space bar, so there is no timer thread at all right now, except I’m using the Animator class to display. I’m using Java 1.6 beta 2/JSR-231 beta 5 on a PC.

Thanks!

-spiraljetty

(PS I’ve posted this by mistake in the performance tuning board as well, so apologies for that…)

5 seconds sounds really excessive so I wonder whether something else is going wrong, such as leaking of OpenGL texture objects. Are you ever calling glDeleteTextures()? That having been said, if you’re updating a texture with data which is the same size as or smaller than the already-allocated OpenGL texture object’s dimensions, you should use glTexSubImage2D to update just the data. The TextureIO classes will do this automatically for you; see for example Texture.updateSubImage().

There are ways to set up another OpenGL context on another thread to perform true background texture loading, but this kind of code is likely to not be robust on all platforms and I think your problem is solvable with the above technique.

Hmm, the glTexSubImage2D didn’t work, probably because I am creating the texture for the first time, and not updating it. But I will try and create a bunch of empty textures at initialization and simply update them when the images are loaded, and also make sure to only draw the textures that actually have data. It does seem though that a lot of applications would require creating new textures on the fly.

Thanks for the help-- I’ll post if it solves the problem.

-spiraljetty

Then use the answers there too. It has nothing to do with OGL.