Creating textures during runtime

I’m working on our GUI and need to create a blank texture of size X, Y with alpha, that we can later draw into manually.

What’s the correct way to…

  1. Create this blank texture. I tried TextureLoader.constructTexture, but none of the parameters are documented and I have no idea what they are. I tried just new Texture2D(), but it crashed with a null pointer. I couldn’t find any demos that created textures at runtime.

  2. Draw into the texture on a per-pixel level. I’m assuming you somehow get a Graphics2D object for drawing into the texture… but if we gotta poke pixels into an image byte array, that works too.

  3. Update the texture back to Xith/OpenGL

Much obliged.

Okay, figured out the first two… but the texture doesn’t seem to update if I draw on it after creation.

If I want to draw on the texture with the Graphics2D after the texture has been created, does anyone know how to push/flag it back to Xith for update? Texture2D.setDirty(true) doesn’t do it.

Hmm, this works almost perfectly…

… except it’s upside-down, compared to drawing onto the BufferedImage before creating the texture. But doing it this way, you can modify the texture after it has been created, and the changes go into effect immediately.

did you try rearranging your texture coords to rotate the texture ?

But thanks a lot for posting this, I will need this as well :slight_smile:

Well, I have a textured-quad creator function. I just pass a boolean whether to create the vertices in rightside-up or upside-down order, and then use that whenever I know I’m going to be either using file-loaded textures (rightside-up vertices) or generated/runtime-modified textures (upside-down vertices). True, you could do the same to the UV instead of the vertices, but I didn’t think of that at the time.

You definately don’t want to be flipping a runtime-modified texture every time you do an update to it.

I’m stuck right now though… I’m using some code I found for Text2D, which basically uses a generated texture to have AWT draw font text onto it. But I can’t get the background to be transparent, because I think it starts as opaque black when created, and nothing you do (including painting with a fully alpha color, which I think goes on transparent over the existing opaque black, not replacing it) seems to work. I recall seeing the per-pixel byte array available somewhere, I’ll probably just go replace that here next, and see if that works. But that’s another one you gotta keep upside-down, since it’s runtime generated.

Well, no matter what I do, I cannot get the texture background to be transparent. I can even set every pixel to a 100% transparent color, but when drawn in Xith, it comes out black.

Any thoughts? I’ve done a dozen different things, it all comes out the same… no matter what I do, the texture simply will not render transparent. Is there some sort of switch on the texture, the scenegraph object, or something involving using the Foreground node that indicates it has transparency or something? Testing the default pixel of the freshly-created texture, it’s 100% transparent black already (ARGB=0).

Note that I don’t want to set a transparency level on the entire texture… just on the background pixels so the text doesn’t have a background color.

Solution… the geom object requires the following appearance component:
ta.setTransparencyMode(TransparencyAttributes.BLEND_SRC_ALPHA);

Sorry to bump; couldn’t find a newer thread on the same topic.

I’ve been using Jeramie’s method, with the new flag to make the new TextureLoader load by-ref.

The alpha channel is working properly.

However, my paint updates aren’t being reflected to the scene. (I’m painting the texture before calling renderOnce().) I can confirm that it is using the correct texture, and that the paint method is being called. Also, it doesn’t appear to be an alpha-related issue, because the opaque parts of the texture (currently red) aren’t being painted over either.

Any help?

  
public XDForegroundOverlay(int width, int height, Texture2D startTex) {
    super();
    mainOvl = startTex;   //Texture2D
    mainOImg = (ImageComponent2D)(mainOvl.getImage(0)); //ImageComponent2D
    myBuf = mainOImg.getImage(); //BufferedImage
    myGfx = myBuf.createGraphics(); //Graphics2D


and later:

    
    myGfx.setColor(Color.white);
    myGfx.drawString("myon: "+System.currentTimeMillis(),512,256);
    myGfx.setColor(Color.blue);
    myGfx.drawRect(0,0,1023,511);

Thanks in advance for any help.

Hello,

you need to call updateArea() on the ImageComponent to push your changes down to OpenGL.

If you created the ImageComponent from a BufferedImage then the byte[] is updated from the BufferedImage.
The last step updates the DirectByteBuffer from the byte[] array.

If you are creating procedural images then I suggest that you use the byte[] or DirectByteBuffer interface instead of the BufferedImage interface (which is slow).

Ciao Matthias Mann

Look at the Fun with textures source code from the xith site. I did a number of alpha mask operations. This code uses an Alpha mask image for layered textures,ou should be able to generate a BufferedImage with the same properties as the example and draw mask patterns in it

To mess with modify textures you need to pull the ImageComponent2D and then the Image from that, update the image and update the ImageComponent2D

Thanks for your help. It works now!