Loading textures of a JPG or TGA file

Hey guys,



I am trying to create a texture, from a tga, or a jpg file (they are all in one package and the file is found):



URL monkeyLoc = this.getClass().getResource("t.tga");

TextureState ts = display.getRenderer().createTextureState();

Texture t = TextureManager.loadTexture(Toolkit.getDefaultToolkit().createImage(monkeyLoc), Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true);

ts.setTexture(t);

b.setRenderState(ts);





However, it doesnt render it at all, and the 3d-box is covered with a huge "texture missing"…



In the log, it comes up saying:



WARNING: Problem creating buffered Image: Width (-1) and height (-1) must be > 0



any ideas guys?



Thanks Mo

hey there,



whatever this does,

Toolkit.getDefaultToolkit().createImage(monkeyLoc)


it's doing it wrong.  ;) It does not create a valid image. Try:

Texture t = TextureManager.loadTexture(monkeyLoc, Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true);



hey,



Toolkit is the java's default classes to do common things, like creating an Image from a given path of a file. (it creates an Image object, not bufferedImage, and as you may know you can't cast to a super).



I have used Toolkit many many times all over my previous projects (not this one), and it has worked just fine. However, it just doesnt work in this case. I have literally gone ups and downs, even I have tried to get the databyte of the image to write it manually, but it just doesnt work. It really has started to p*** me off lol…



Also, what you suggested didnt work, since the method loadTexture(…) takes an Image object as the parameter and not a String or a Url object. So that would just give me a compile error.



Any other ideas are welcomed.

Thanks

Mo






Just guessing as I can't test it right now, 'cause I'm at work.



Try this:


URL monkeyLoc = YourClass.class.getClassLoader().getResource("t.tga");



BTW: This is my helper-class to load textures. Maybe it will help you:

   public static TextureState loadTexture(String ressource)
   {
       TextureState ts  = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
       ts.setTexture(
         TextureManager.loadTexture(
         Utils.class.getClassLoader().getResource(
         ressource),
         Texture.MinificationFilter.Trilinear,
         Texture.MagnificationFilter.Bilinear));
       return ts;
   }

hey,



erm the above didnt work either…though slightly different result…



Before it used to give me a warning that the width+height of the texture was <= 0, this time it gave me a nullpointerexception…



I believe (i maybe wrong) that the way ttrocha suggested resulted in the code not finding the file at all, whereas previously I had problem rendering the file correctly.



By the way, just for the sake of the argument, I am still using the same images that came with the jME's test package, i.e. monkey.tga…



Please do let me know if there are any other ways around this problem.



Thank you,

Mo

Ahm,…btw. where exactly did you place your t.tga?



if your project-structure e.g. is



root

|

org.test



YouClass.java

org.test.res



  t.tga



you have to get it


URL monkeyLoc = YourClass.class.getClassLoader().getResource("org/test/res/t.tga");



Hey,



nope…didnt work either…



The t.tga is in the same folder/package as the java file…



root

|

testpro



  m

 

  m.java

  t.tga



I just used:

TextureManager.loadImage("t.tga", false).getHeight(); and did a System.out.println() and it gave me no errors and gave me the correct size - however this method and what it returns is implemented by the jME guys…



However, the loadTexture(URL,…) uses the java's image class… is there a difference in the way java normally read the file? (what the heck, might as well create a new image.tga file with photoshop and test it :S )



Thanks for the help…




Plz provide your source-code… that would make the search easier.



And what  does didn'T work mean? Nullpointer-Exception again?

Hey,



yes, nullpointerexception… also, bear in mind that im just making something really simple since i am new to jME (not so new to Java however).



also some of the commented bits, im gonna uncomment once i move to StandardGame instead of SimpleGame…it was much easier to control the camera in SimpleGame … :S




package m;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Image;
import com.jme.image.Texture;
import com.jme.light.PointLight;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import java.awt.Toolkit;
import java.net.URL;

public class m1 extends SimpleGame {

//StandardGame game = new StandardGame("game1");;
//Node rootNode;
//Camera cam;
    public static void main(String[] args)
    {
        m1 app = new m1();
        //app.setConfigShowMode(ConfigShowMode.ShowIfNoConfig);
        app.start();
        //app.game.start();
        //app.InitGame();
        
    }

    protected void InitGame() {

        display.getRenderer().setBackgroundColor(ColorRGBA.black);

        Box b = new Box("Floor", new Vector3f(0.5f, 0.5f, 0.5f), 3.5f, 0.3f, 3.5f);
        b.setModelBound(new BoundingBox());
        //b.setRandomColors();
        b.updateModelBound();
        
        //rootNode.setLightCombineMode(LightCombineMode.CombineClosest);
        rootNode.attachChild(b);

        URL monkeyLoc = this.getClass().getResource("x.jpg");
        TextureState ts = display.getRenderer().createTextureState();

        //testing to see whether image actually can be fetched...
        Image tx = TextureManager.loadImage(monkeyLoc, false);
        System.out.println(tx.getHeight());

        Texture t = TextureManager.loadTexture(Toolkit.getDefaultToolkit().createImage(monkeyLoc), Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true);

        ts.setTexture(t);
        
        b.setRenderState(ts);
        
        PointLight l = new PointLight();
        l.setLocation(new Vector3f(0, 10, 5));
        l.setDiffuse(ColorRGBA.cyan);
        l.setEnabled(true);
        LightState ls = display.getRenderer().createLightState();
        ls.attach(l);
        //ls.detachAll();
        rootNode.setRenderState(ls);
        

        cam.setLocation(new Vector3f(0f,7.3f,8f));
        cam.lookAt(b.getWorldTranslation(), new Vector3f(0,1,0));        
        cam.update();      

    }

    //@Override
    protected void simpleInitGame() {
        this.InitGame();
    }

}



Thanks

Appreciated. Mo

Can you verfiy that your build folder(Folder where your java class files are created).

The folder that m.class is located also has the x.tga file in it when you compile.

Sometimes IDEs dont copy over tga file times by default.

hi,



yes it has that file. Netbeans 6.7 (what im using) copied that and so did I. I also - just to make sure - copied the file to every folder of the project, just to make sure that the file wouldnt be a problem…



Again, i would say that the file is found just fine, however there is some sort of problem with rendering the file. I have even tried to recreate another TGA and JPG file (as you can see, there was t.tga and now there is also x.tga), yet the problem is still there.



I am using Mac OS X 10.5.8, Java SE 6 64bit and Java 5 32bit/64bit…currently it is set to use Java SE 6 64bit.



Thank you very much for all the help.

Mo

mofirouz said:

Also, what you suggested didnt work, since the method loadTexture(...) takes an Image object as the parameter and not a String or a Url object. So that would just give me a compile error.


the loadTexture methods are ALL overloaded with Image, URL  and String. Just take a look at TestTextureState in the jmetest package to see how to load textures that are located with your java files.


        Texture texture = TextureManager.loadTexture(
                TestTextureState.class.getClassLoader().getResource("jmetest/data/model/marble.bmp"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear);
        texture.setWrap(Texture.WrapMode.Repeat);
        ts.setTexture(texture);



in your case that would mean:



        Texture texture = TextureManager.loadTexture(
                YourClassName.getClass().getClassLoader().getResource("testpro/m/m.tga"),
                Texture.MinificationFilter.BilinearNearestMipMap,
                Texture.MagnificationFilter.Bilinear);
        texture.setWrap(Texture.WrapMode.Repeat);
        ts.setTexture(texture);



the path will also change if you change your Working Directory (e.g. in Eclipse). Furthermore, you have to Refresh a Source Folder in Eclipse in order for it to copy the texture to your binary folder. Maybe even try to manually copy it.

mofirouz said:

Again, i would say that the file is found just fine,


if you use URL to locate it and you get a NullPointerException "url is null" the file is not found.

hey dhdd thanks for the reply and sorry for the late reply - was at work…



Yes you are right - I didnt look properly - I am so sorry…the loadTexture() does take URLs/Strings…



----



I did try what you suggested, and this is the error i got:


WARNING: loadImage(URL file, boolean flipped): file is null, defaultTexture used.
Uncaught error fetching image:
java.lang.NullPointerException


then the frame/window hanged, and was unusable (had to force terminate from netbeans).

---

Previously, if I just put the


this.getClass().getResource("m.tga");


inside the program, it would return an Image object - I tried to take the 'height' of the image and it was the correct height - plus i didnt get any errors nor warnings like above.

---

I will try more in the morning and will let you know how I'll get on.

THANK you ALL for the help.
Mo

I duplicated your size -1 message not sure what throws it but easy fix is to change code to.



   

       

Texture t = TextureManager.loadTexture(

            monkeyLoc,

        Texture.MinificationFilter.Trilinear,

        Texture.MagnificationFilter.Bilinear);



Notice no (True) at the end

hey gbluntzer,



OMG! LOL… it did actually fix the problem!!!



THANK YOU THANK YOU THANK YOU!!!



can't believe that was this easy to fix!



Thanks again,

Mo