multiple GLCanvas - shared textures?

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

Hi all, sorry to bump this, but I still need some help here. I’d like to release a new version of my game this month so I really need to get to the bottom of it.

Cheers
Peter

Your texture creation code looks right at a quick glance. By default, textures aren’t shared between GL contexts (and each GLCanvas has a different context).

You can share objects (textures, shaders etc.) between components using the four-parameter version of the GLCanvas constructor, passing in the context you want to share with as the third parameter (on a sidenote, you can get the context from the getContext() method of GLCanvas). Alternatively, create a GLPBuffer as your shared context object, and pass its context into both GLCanvas when constructing them.

Great! that works a treat.

I didn’t use the GLPBuffer object because I’m not sure what advantage that would give me. I’m still quite new to this. But if my asumption is correct then that would be dependant pbuffer’s which I don’t think all my users have support for. At least I was using some pbuffers at some point for offscreen rendering which worked fine for me but not many of my players.

Thanks for your help! this one has been bugging me for quite some time :slight_smile: