So I have occasionally 2 or more GLCanvas instances on screen at once. I’ve had an issue for quite a while now where the textures are only showing up in the first canvas. So I thought I’d try and get to the bottom of the problem today. The code is quite old, so I’m not using the new Texture class. Instead I have a home grown impl. that uses various sources (disk, jars, network) for my textures. Once loaded I run the code below on each texture.
What I’m starting to suspect from my reading around is that I should probably do a glBindTexture & glTexImage2D on each GLCanvas init method. ie the GL instance on each canvas is different and therefore needs it’s own texture bindings??
However I also came accross some snips of info in the userguide and api docs about sharing a context in order to share textures. It’s not clear to me how I can implement that. I’m guessing I’d need to create my first GLCanvas, then in the init method extract the created context via getContext, then pass this back in to the next canvas instance via createContext. I had a quick test with this approach and the textures are still missing, but first I want to make sure I’m taking the correct approach?
Cheers
Peter
private final void setupTexture(GL gl, GLU glu, int id, boolean mipmap, byte[] data, int width, int height) {
ByteBuffer dataBB = ByteBuffer.allocateDirect(data.length);
dataBB.put(data);
dataBB.rewind();
gl.glBindTexture(GL.GL_TEXTURE_2D, textureList[id]);
if (mipmap) {
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_MIPMAP_NEAREST);
//Auto Gen MipMaps
glu.gluBuild2DMipmaps(GL.GL_TEXTURE_2D,
GL.GL_RGBA,
width,
height,
GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE,
dataBB);
} else {
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);
}
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
dataBB.rewind();
gl.glTexImage2D(GL.GL_TEXTURE_2D,
0,
GL.GL_RGBA,
width,
height,
0,
GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE,
dataBB);
}