TextureRenderer for dinamic texture

here is my little problem: a square in a canvas much like Andrew Davidson´s new JOGL stuff. im using active rendering, and basically i want to draw a different image(actually a diferent part of a png image) in each passing of the rendering loop. im using alpha testing and blending so i dont draw the parts of the image i dont want (if its a ball, i want only the ball, not a white square behind it.). but it keeps the image drawn the last time, and since the images are changing constantly, its a problem. if i use clearRect in the graphics (as sen bellow) i get a black square. How do i clean the texture to draw a fresh image without pieces of the old image still showing? Any ideas are welcome. the code below would be somewhere in the renderScene code, witch is called constantly to draw on the screen. testTextureRenderer os a TextureRenderer object, bim is a previously loaded BufferedImage, animposx is a int indicating the beginning of the image to be drawn. later on on the code i would use testTexture (a Texture object) methods like enable, bind and disable, when rendering the square, wich is in a glList.

In each passing of the loop im updating witch part of the image i want showing. the whole problem is to clean the Graphics2D of the TextureRenderer before drawing the next image, but keeping the transparent part still visible (i still want to see the canvas behind it where there pixels with alpha>0 in the image).


        Graphics2D g = testTextureRenderer.createGraphics();
        g.clearRect(64, 64, 64, 64);
        g.drawImage(bim.getSubimage(animposx, 0, 128, 128), null, 0, 0);
        animposx++;
        if(animposx >= 127)
            {animposx = 0;}
        testTextureRenderer.sync(0, 0, 128, 128);
        testTexture = testTextureRenderer.getTexture();
        g.dispose();

If all you’re doing is altering which part of an image is used for texturing, you could just load the entire image as a texture and alter the texture coordinates used each frame

I think bleb is right based on what you’ve said; it would be much more efficient to load your entire PNG into the TextureRenderer the first frame and then use e.g. drawOrthoRect or draw3DRect to snip pieces out of it and draw them to the screen.

You could also load your PNG into a Texture object using TextureIO and use Texture.getSubImageTexCoords() and manual OpenGL calls to put the sub-image on the screen.

To clear out a piece of the TextureRenderer, use this code snippet:


    g.setComposite(AlphaComposite.Clear);
    g.fillRect(x, y, w, h);
    g.setComposite(AlphaComposite.Src);