Dynamically updated texture on a quad - how?

I'm trying to do dynamically updated texture for a map quad on the UI. No matter how I try, it always ends up eating memory away. Is there an example for that or do you have any ideas what's wrong with my code? :slight_smile:



         ByteBuffer buffer2 = ByteBuffer.wrap(staticLayerSet);
         staticLayer.setData(buffer2);
         if (staticTexState==null)
         {
            System.out.println("NEW STATIC TEX STATE");
            staticTexState = J3DCore.getInstance().getDisplay().getRenderer().createTextureState();
         } else
         {
            TextureManager.releaseTexture(staticTexState.getTexture().getTextureKey());
         }
         staticLayerTex = new Texture();
         staticLayerTex.setImage(staticLayer);
         staticTexState.setTexture(staticLayerTex);
         staticTexState.setNeedsRefresh(true);



As you can see, I'm trying to release memory with TextureManager. Yet somehow it doesnt work...

http://wwwhomes.uni-bielefeld.de/krabien/jmestuff/textureLayers/TestTextureSplatting.java

Looking at this code, I thought I'm doing it right..it seems that I'm not.  :?

try use a difference texture state intead of changing texture within the same texture state.



and just use texture manager to load in the texture set it directly to ur newly created texture state.

thanks for the advice! :slight_smile: can you give a short code example?

Neakor, is that because the mipmaps must be regenerated when the texture changes?

I've rewritten the code by your advice, still it eats up a tone of memory after a little while - something is eating it, and still I don't know what…  :? If I don't do updates like that to texturestate, memory use is stable…


         ByteBuffer buffer2 = ByteBuffer.wrap(staticLayerSet);
         staticLayer.setData(buffer2);
         if (staticTexState==null)
         {
            System.out.println("NEW STATIC TEX STATE");
            staticTexState = J3DCore.getInstance().getDisplay().getRenderer().createTextureState();
            staticLayerTex = new Texture();
            staticLayerTex.setImage(staticLayer);
            staticTexState.setTexture(staticLayerTex);
            staticTexState.setNeedsRefresh(true);
         } else
         {
            staticLayerTex.setImage(staticLayer);
            staticTexState = J3DCore.getInstance().getDisplay().getRenderer().createTextureState();
            staticTexState.setTexture(staticLayerTex);
            staticTexState.load();
            TextureManager.releaseTexture(staticLayerTex.getTextureKey());
         }
         
         for (Quad q:updatedQuads)
         {
            q.setRenderState(staticTexState);
            q.updateRenderState();
         }



irrisor said:

What about using ImageGraphics?


Any link for that?

What about using ImageGraphics?

Google is your friend :wink:



http://www.jmonkeyengine.com/doc/com/jmex/awt/swingui/ImageGraphics.html



Hey Irrisor, is there any tests that use this??

timong said:

Any link for that?

um, no, sorry. JMEDesktop is using it to update it's texture...

Short code example:
1. setup like this (parts copied from JMEDesktop)

TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        ts.setCorrection( TextureState.CM_PERSPECTIVE );
        texture = new Texture();
        texture.setFilter( Texture.FM_LINEAR );
        texture.setMipmapState( mipMapping ? Texture.MM_LINEAR_LINEAR : Texture.MM_LINEAR );

        graphics = ImageGraphics.createInstance( this.width, this.height, mipMapping ? 2 : 0 );
        texture.setImage( graphics.getImage() );

        ts.setTexture( texture );
        ts.apply();
        someSpatial.setRenderState( ts );


2. somewhere in you render loop do

if ( graphics.isDirty() ) {
   graphics.update( texture );
}


3. to paint on your texture simply do

Graphics g = graphics.create()
// paint on g
g.dispose();

Thanx! my GPLv3 code with your advice:



   public static HashMap<Integer, Color> colorCache = new HashMap<Integer, Color>();
   public void paintPoint(ImageGraphics set, int x, int y, int side, int r, int g, int b, int a)
   {
      for (byte[] p : sideOffsets[side])
      {
         //a = 255;
         int k = (r<<24)+(g<<16)+(b<<8)+a;
         Color c = colorCache.get(k);
         if (c==null)
         {
            c = new Color(Math.max(0, Math.min(r,255)),Math.max(0, Math.min(g,255)),Math.max(0, Math.min(b,255)),a);
            colorCache.put(k, c);
         }
         set.setColor(c);
         set.drawRect(x*pointSizeX+p[1], y*pointSizeY+p[0], p[3], p[2]);
      }
   }





         if (staticTexState==null)
         {
            System.out.println("NEW STATIC TEX STATE");
            staticTexState = J3DCore.getInstance().getDisplay().getRenderer().createTextureState();
            staticLayerTex = new Texture();
            staticLayerTex.setFilter( Texture.FM_LINEAR );
            staticLayerTex.setMipmapState(Texture.MM_LINEAR);
            staticLayerTex.setImage(staticLayerGraphics.getImage());
            staticTexState.setTexture(staticLayerTex);
            staticTexState.apply();
            
         } else
         {
            staticLayerGraphics.update(staticLayerTex,false);
            staticTexState.apply();
            
         }
         
         for (Quad q:updatedQuads)
         {
            if (q.getRenderState(RenderState.RS_TEXTURE)!=staticTexState) {
               q.setRenderState(staticTexState);
            }
            q.updateRenderState();
         }