Bug in getNumberOfSetTextures()?

Hi,



i think I have found a problem with TextureState.getNumberOfSetTextures().

In my program I need to add texture on the last texture unit and sometimes to remove it. The problem is that every time I add texture, getNumberOfSetTextures() is incremented, but when I remove the last etxture, it is not decrement.

Or maybe I dont understand correctly what getNumberOfSetTextures() is for.



that's a test program :


        // first texture
        TextureState ts = display.getRenderer().createTextureState();
        ts.setEnabled(true);
        Texture text = TextureManager.loadTexture(TestBugTexture.class
                .getClassLoader().getResource("jmetest/data/images/Monkey.jpg"),
                Texture.MM_LINEAR, Texture.FM_LINEAR);
        ts.setTexture(text);
        ts.getTexture().setWrap(Texture.WM_WRAP_S_WRAP_T);
        rootNode.setRenderState(ts);
        
        // second texture
        Texture text2 = TextureManager.loadTexture(TestBugTexture.class
                .getClassLoader().getResource("jmetest/data/images/Fieldstone.jpg"),
                Texture.MM_LINEAR, Texture.FM_LINEAR);
        // add 1 texture at the end
        int index = ts.getNumberOfSetTextures(); // get next unit
        ts.setTexture(text2, index);
        System.out.println("index = " + index); // index is 1
        // remove texture
        ts.removeTexture(text2);
        // add 1 texture at the end
        index = ts.getNumberOfSetTextures();
        System.out.println("index = " + index); // index is 2
        ts.setTexture(text2, index);
        // remove texture
        ts.removeTexture(text2);
        // add 1 texture at the end
        index = ts.getNumberOfSetTextures();
        System.out.println("index = " + index); // index is 3
        ts.setTexture(text2, index);
        // etc ...



In fact, getNumberOfSetTextures() return texture.size(), but when a texture is removed, texture unit become null in array so "texture" is an array of null elements, growing every time I add new texture.

I use jME1.0 but I have tested with jME 2.0 TextureState and I have the same problem (not a lot of differences).

I think it must return something like "lastTexture" which is update by resetFirstLast(). I have try to return lastTexture instead, but it cause other problems.

Am I wrong or is it a real problem ? And what can be the solution ?
Thanks

For what it’s worth, this is what I did in ardor3d after seeing this bug report.

Ok, so you change the behavior of getNumberOfSetTextures(), but don't you think it can be alter others functions because in many cases getNumberOfSetTextures() is used in for loop to read each array element, and it won't work if you have an array of textures like that : [texture, texture, null, texture] (last won't be read)

Or we must replace any use of getNumberOfSetTextures() by getMaxTextureIndexUsed() ?

[EDIT] (Actually I changed my mind now a third time)



Well,…yes! you would have to replace the method by getMaxTextureIndexUsed() which is really doing what the method's name means. Of course there will be problems. Actually I will have to change code for me as well.  But that is the consequence of working with a trunk. Things are changing…

We also need to change code in many files of jme because this function is used many times. And as you (and me) many people have to know these changes (if we do them), to prevent problems.

pitchonel said:

Or we must replace any use of getNumberOfSetTextures() by getMaxTextureIndexUsed() ?


You'll notice the change I linked to is part of a bigger changeset.  It's easier for Ardor3D to do that kind of change though since we're pre 1.0 :)

Indeed, I didn't see the big change  :-o and it's easier for you to make it.

Ok so it will have a big patch for jme, but now I can't do it for jme2.0 cause I'am working with 1.0. So if someone can do it for jme 2.0 it will be great, otherwise I will do when I work with 2.0.