Loading PNG file as a texture

Hi,

I'm fairly new here and a little new to the 3D graphics arena though I have some experience.  I stumbled across the jMonkeyEngine and it seems like a perfect fit for a "pet project" I've been wanting to write.  Things are going pretty good, but for some reason, I'm having a terrible time attempting to apply a texture to a model.  Here is a snippet of code:



      //
      // Read the texture from the PNG file as a buffered image.
      //       
      URL imageUrl = ClassLoader.getSystemResource("com/bix/util/m2/TestModels/boat.png");
      java.awt.Image bimg = null;
      try {
         bimg = ImageIO.read (imageUrl);
         if (bimg == null) {
            log.error ("ERROR loading image from URL");
         }
      } catch (IOException e2) {
         // TODO Auto-generated catch block
         e2.printStackTrace();
      }
      
      Texture t2d;
      boolean loadFromUrl = true;
      if (loadFromUrl) {
         t2d = TextureManager.loadTexture (imageUrl, Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, 0.0f, false);
      } else {
         t2d = TextureManager.loadTexture (bimg, Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, 0.0f, false);
      }

      //
      // Set the texture information.
      //
      TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
      ts.setTexture(t2d);



The problem occurs when loadFromUrl = false.  For some bizarre reason, the texture is not applied to the model.  I've debugged through the code for TextureManager (v2.0 latest from SVN) and everything seems correct.  In fact, I even did a byte by byte comparison of the data at some of the lowest levels of the TextureManager class and at least the first 5 or 10 bytes are identical.  I'm really stuck here and if anyone has any suggestions, please let me know, I'd really appreciate it.

Regards.

Did the model you loaded have texture coordinates?  Without texture coordinates, the renderer will not know how to sample the texture and apply its pixels to the model.

Yes, and again, the actual image file is identical.  It's just being loaded in two different ways.  I put that flag in there to try and show that (that's not how it is in my code, obviously :D).  If I didn't have texture coordinate for the model, neither image would work I believe.

try this:



URL url = Thread.currentThread().getContextClassLoader().getResource( path );

Nope, but thanks for trying.



I honestly do not think that it has anything to do with what I'm doing (beyond not using jME correctly, perhaps).  As I said in my original post, I traced through a large amount of the TextureManager class watching the data.  The data seems to BOTH be identical when loaded.  The only "odd" part that I saw that was different was the generation of the TextureKey.  When passed the filename, it uses that as an input to the constructor of the TextureKey, but when receiving a java.awt.Image object, it passes a null as the first parameter to the constructor.  I'm wondering if this is a bug in jME or perhaps an oversight.



Does anyone else have any other suggestions?  I guess today I'll go deeper inside of the TextureManager class to see if I can find out why.



Btw, the end result of it working is a model with a correct texture.  When it's not working, the model is pure white.



Thanks again.

Btw, the first parameter to TextureKey is location, so passing null would seem correct.

So I usually suspect code I've written as the cause to most of my pain, but in this case, I think there may be an issue with TextureManager loading textures from java.awt.Image.  I wrote a custom loader for the PNG file I was processing and registered that with the TextureManager class and everything worked perfectly.  It would seem that, for some unknown reason, when TextureManager loads the image from a java.awt.Image, that image cannot be found using the created TextureKey when the mesh is rendered.  I'm only saying this after at least 6 to 8 hours of debugging the code in TextureManager.  I don't have any final answers to the solution as to exactly "why" it doesn't work, only that it doesn't but I've found a workaround.

Only a few ppl use the TextureKeys directly so I guess its possible there is a problem there.  Although I have never had one and I load AWT images all the time…



The TextureManager is kind of slated for some (near) future work and this should be looked at then.

I don't use the TextureKey objects directly, I'm only saying that from my debugging it would appear that they may be the root cause of why this doesn't work.  I discovered this by debugging into the TextureManager code and seeing how it created the images/textures.



As for doing work on it, or other aspects of jMonkeyEngine, I may be able help out some if coding is needed.  While I only have (what I would consider) a novice level amount of experience in 3D graphics, I do have over 25 years of programming experience with the last decade or so being in Java.  I've been completely engrossed in this little pet project I'm working on using jME and have been learning more and more about it very quickly.  Just shoot me a message if you're interested.  I'm a real stickler for good javadocs and comments, so usually, when I first jump into a project my first task is to spend time documenting everything as best as I can.  I figure if I can't document it, I can't understand it (which is a bad thing).