简体   繁体   中英

loading texture from folder

I'm having a problem loading a JPEG onto a sphere. Currently, the only way I have gotten this to work is to have the image in the same package as the class loading it. The problem is, I don't want to have 500+ textures all under one package mixed with classes. I would rather have them in folders and sub-folders for organizational reasons.

If the texture is in the same package, this works:

InputStream iStream = null;

    try
    {
        iStream = getClass().getResourceAsStream(imageFile);
        TextureData data = TextureIO.newTextureData(iStream, false, null);
        testTex[i] = TextureIO.newTexture(data);
        testTex[i].getImageTexCoords();
        testTex[i].setTexParameteri(GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
        testTex[i].setTexParameteri(GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
        iStream.close();
        ...

The method that takes an image takes it in the form of:

String[] texture = { "bar1.jpg", "bar2.jpg" }; // imageFile is whichever is called

As the above code works perfectly, again, the drawback is that every texture will have to be stored within the same package as the class that is calling for the texture. But I want to call the texture like this from a folder within my project:

String[] texture = { "res/foo/bar2.jpg", "res/foo/bar2.jpg" };

How would I modify the above code to look for textures within a folder within the project?

// note leading '/' - indicates 'search from root of class-path'
String[] texture = { "/res/foo/bar2.jpg", "/res/foo/bar2.jpg" };
// ...
URL iUrl = getClass().getResource(texture[0]);
TextureData data = TextureIO.newTextureData(iUrl, false, "jpg");

In this example, we use an overloaded form of the TextureIO API loading method in newTextureData(URL,boolean,String) .

I think your problem is just that you're missing a slash before res. This will definitely work:

this.getClass().getResource("/res/img.jpg");

as long as the res package is in your src folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM