Creating a JOGL Texture object from the contents of a FBO.

Hello,

We are using a FBO to draw our minimap and would like to obtain a JOGL texture object from it. Currently the only way I know of for creating JOGL textures is this line in our texture loader:

TextureIO.newTexture(url, false, “png”);

Where url is a URL object to the resource.

If we have a JOGL FBO, what steps can we take to obtain a com.jogamp.opengl.util.texture.Texture?

EDIT: A quick Google turned up this. Any caveats to using this method?

TextureIO.newTexture(int target)
//Creates an OpenGL texture object associated with the given OpenGL texture target.

I think that might only create the texture id for the texture, but will not allocate any image data for (i.e. with a call to glTexImage2D()). You need to have image data before you can attach the texture to an FBO to specify the width, height, and format of the texture.

Yeah, the above didn’t work. Is there some way to obtain a TextureData by using glReadPixels?

What exactly didn’t work? It should work if you allocated image data of the correct dimensions and attached it to the FBO properly.

Alternatively, if you’ve already created the FBO (with attached textures) you can wrap the OpenGL texture id with a Texture instance by calling:


newTexture(int textureID, int target, int texWidth, int texHeight, int imgWidth, int imgHeight, boolean mustFlipVertically);
//Wraps an OpenGL texture ID from an external library and allows some of the base methods from the Texture class, such as binding and querying of texture coordinates, to be used with it.

There might be a problem with your FBO setup, could you post that code?

The following code is for LWJGL, but the OpenGL commands should be exactly the same except for the GL11 part.


//Let OpenGL give us an ID.
IntBuffer id = BufferUtils.createIntBuffer(1);
GL11.glGenTextures(id);
int textureID = id.get();
GL11.glBindTexture(textureID);

//Setup parameters
GL11.glTexParameteri(id, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
GL11.glTexParameteri(id, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
... //Clamping, e.t.c.

//Allocate a texture. THIS TEXTURE WILL HAVE RANDOM DATA A LA C++ ALLOCATION.
//Be sure to glClear your FBO before using it (like you normally would when rendering to anything)!!!
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, width, height, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (ByteBuffer)null);

//You now have an allocated texture accessable using textureID. Wrap it in the method Ihkbob submitted or something.
return whereverThisMethodIs.newTexture(textureID, width, height, width, height, true); //Flip might be a good idea