My Fog of War implementation

Alright, this is the only way I could figure out how to do fog of war in my RTS game.  Allow me to explain:







In the above image, the black quad and green quad are actually composed of a grid of quads.



What I did was write a class using Graphics2D that just draws alpha colored ovals based on the unit position and sight radius.  The 512x512 playing field area is then split up into 16x16 quadrants.  The quadrant image data is only updated when a unit moves in that area or a unit in another quadrant spills over into it.  Thus saving many calculations, etc…  The 2D implementation of this works very well.  The problem I have is when trying to implement this into my FixedFramerateGame.



When Quadrant draws a new update I invoke the following method - image is a BufferedImage (TYPE_INT_ARGB):

this.texture = TextureManager.loadTexture(image, Texture.MM_LINEAR_LINEAR, Texture.FM_LINEAR, true);



NOW I can see that my method is quite taxing...as my memory usage goes up forever until my 2GB is gone.  It would seem that the other texture is still in memory.  So before this method I have done the following:

      
if (texture != null) {
// TextureManager.deleteTextureFromCard(texture);
TextureManager.releaseTexture(texture); }



Releasing the texture I notice that I go all the way up to 80MB and then (garbage collection?) kicks back down to 30MB (normal for my application).  During this "collection" my frame rate dips considerably.


My questions are these:
1) Is my creative application the best way to go about this?

2) Is there a way I can just keep redrawing to the same texture?  All the Quadrants class provides is a BufferedImage and I think the problem is that I am reallocating a new texture every time.

Thanks!

1) Is my creative application the best way to go about this?

2) Is there a way I can just keep redrawing to the same texture?  All the Quadrants class provides is a BufferedImage and I think the problem is that I am reallocating a new texture every time.

You can use the ImageGraphics class in jME to draw directly to an image.
To do this even faster you can use the stencil buffer together with a circle primitive to mark which parts of the scene should be visible, when you draw your scene the stencil test will discard any pixels that do not have the stencil value set.
Momoko_Fan said:

You can use the ImageGraphics class in jME to draw directly to an image.
To do this even faster you can use the stencil buffer together with a circle primitive to mark which parts of the scene should be visible, when you draw your scene the stencil test will discard any pixels that do not have the stencil value set.


I think I understand the concept.  So if I had a Quad and a Disk primitive, the Disk would be closer to the camera.  Lets say that without rotation the Quad is at (0, 0, 0) and the Disk is at (0, 0, 1) using the right handed coordinate system.  I would then apply some sort of StencilState(?) to my disk and then only the area of the Quad below the Disk would show?

Would I do something like this?


      Quad tempQuad = new Quad("tempQuad", 100, 100);
      tempQuad.getLocalTranslation().set(0, 0, 0);

      Disk tempCircle = new Disk("tempCircle", 2, 10, 50.0f);
      tempCircle.getLocalTranslation().set(0, 0, 1);

      StencilState ss = display.getRenderer().createStencilState();
                // Documentation on what StencilFunc, StencilFuncMask, etc.. do?
                // Is this where I would define my "mask" and so on?
      ss.setEnabled(true);

      tempCircle.setRenderState(ss);



I really can't find any example of this using jME.  The documentation for the StencilState class, if thats what I'm even supposed to be using, isn't very well documented.

I appreciate the help so far!