LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: mot on April 25, 2008, 09:12:53

Title: DevIL replacement, pure Java, JPEG & PNG?
Post by: mot on April 25, 2008, 09:12:53
I'm looking for a replacement for DevIL to be able to switch to LWJGL 2.0 sometime... and I don't want to use the default Java libraries to be still able to strip the Windows JRE to the bare essentials.

It should be able to load JPEG and PNG or TGA with alpha channel and write at least one popular format (BMP, TGA, whatever). A pure Java portable implementation would be preferred over speed. Any suggestions?

I really need to load JPEG because some textures compress very well using JPEG and switching to a lossless format would make them way too big.

Custom image formats are not the best solution for me either, I like being able to do a "Slices" export from Photoshop and get the resulting PNG/JPEG files into the game without an additional conversion step.

Any suggestions are welcome, thanks!
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: kappa on April 25, 2008, 13:16:01
The recommended alternative to Devil and Fmod is "slick-util"

it can be found at http://slick.cokeandcode.com/downloads/util/
with javadoc being at http://slick.cokeandcode.com/javadoc-util/
it includes examples so you can learn by example too.
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: Rainer on May 15, 2008, 19:34:23
I'm also having problems with the texture loading.
Using this loader works:
http://www.cokeandcode.com/info/showsrc/showsrc.php?src=../spaceinvaders104/org/newdawn/spaceinvaders/lwjgl/TextureLoader.java
But this filters textures just with GL_Linear.
I tryed to build mipmaps:
GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, 3, texture.getImageWidth(), texture.getImageHeight(), GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, textureBuffer);
but all i get is:
java.lang.IllegalArgumentException: Number of remaining buffer elements is 196608, must be at least 262144
        at org.lwjgl.BufferChecks.throwBufferSizeException(BufferChecks.java:130)

Anybody got an idea how to fix this?
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: bobjob on May 15, 2008, 21:53:54
some ideas: (Im sure u tried some of these already, but ill try help the best i can)

what image format are you using? .jpg? if so then it could be a problem with the
GL11.GL_RGBA
change it to
GL11.GL_RGB

have you changed the textureBuffer at all, is it back to its first position. does it need to be fliped.

double check that the numbers from getImageWidth, and getImageHieght are not actually bigger than the image you are using.

and make sure that the image is the power of 2.
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: Rainer on May 16, 2008, 07:04:43
You're right, it was the RGB thing, didn't see this one ;)
Additionally, i needed to change the order in which 2 things were done.
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: mot on May 22, 2008, 12:13:08
Someone mentioned it was possible to use DevIL from 1.1.4 with 2.0b1, so I tried it. And it works!

First I was getting library version mismatch errors when initializing OpenAL but solved it by shuffling the initialization code so that AL.create() is called BEFORE IL.create().

And 2.0b1 works fine for me without any additional modifications required.
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: Rainer on May 26, 2008, 09:33:28
I now ran into the next Problem, i can't get transparency to work.
This Texture:
(http://www.flugmodellbau.ch/luno.png)
i load  with:

...
if (bufferedImage.getColorModel().hasAlpha()) {
            srcPixelFormat = GL11.GL_RGBA;
            System.out.println("loaded tex with alpha");
        } else {
            srcPixelFormat = GL11.GL_RGB;
            System.out.println("loaded tex without alpha");
        }

        // convert that image into a byte buffer of texture data
        ByteBuffer textureBuffer = convertImageData(bufferedImage, texture);
       


// produce a texture from the byte buffer
        GL11.glTexImage2D(target,
                0,
                dstPixelFormat,
                get2Fold(bufferedImage.getWidth()),
                get2Fold(bufferedImage.getHeight()),
                0,
                srcPixelFormat,
                GL11.GL_UNSIGNED_BYTE,
                textureBuffer);

        if (target == GL11.GL_TEXTURE_2D) {
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_NEAREST);
            GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, 3, texture.getImageWidth(), texture.getImageHeight(), srcPixelFormat, GL11.GL_UNSIGNED_BYTE, textureBuffer);
        }
...

which tells me the texture has alpha.

then i render with:

tex.bind();
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_BLEND);


But it renders the texture with the transparent regions in black, not transparent.
What could be wrong?
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: bobjob on May 26, 2008, 09:51:40
GL11.glAlphaFunc(GL11.GL_GREATER,0.1f); // sets aplha function
GL11.glEnable(GL11.GL_ALPHA_TEST); // allows alpha channels or transperancy
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: Rainer on May 26, 2008, 10:00:14
I tried this before, unfortunately it does not change anything.
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: Orangy Tang on May 26, 2008, 10:13:02
Are you sure that both src and dest pixel formats are being set to RGBA?

Also, have you made sure to generate a texture id/object and have it currently bound (glBindTexture) before making your glTexImage2D calls?

Double also, I think you only need one of glTexImage2D / gluBuild2DMipmaps. glTexImage2D just uploads raw pixel data, gluBuild2DMipmaps resizes to power-of-two then uploads the texture and it's mipmaps. For now I'd get it working with one or the other first.
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: Rainer on May 26, 2008, 10:31:13
Quote from: Orangy Tang on May 26, 2008, 10:13:02
Are you sure that both src and dest pixel formats are being set to RGBA?

Also, have you made sure to generate a texture id/object and have it currently bound (glBindTexture) before making your glTexImage2D calls?

Double also, I think you only need one of glTexImage2D / gluBuild2DMipmaps. glTexImage2D just uploads raw pixel data, gluBuild2DMipmaps resizes to power-of-two then uploads the texture and it's mipmaps. For now I'd get it working with one or the other first.
yes, both of them are RGBA.
yes, it is bound.
you're right, gluBuild2DMipmaps is enough, i deleted the glTexImage2D statement, with no resulting changes.
i have no idea whats wrong  ???
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: Rainer on May 26, 2008, 11:47:21
If i load the Image, the bufferedImage i get is from type TYPE_BYTE_BINARY, which has no alpha according to the documentation.
However, bufferedImage.getColorModel().hasAlpha() returns true.
Should i convert to something like TYPE_4BYTE_ABGR?
Title: Re: DevIL replacement, pure Java, JPEG & PNG?
Post by: Rainer on May 26, 2008, 13:42:22
found the error, really dumb one of course...
GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, 3, texture.getImageWidth(), texture.getImageHeight(), srcPixelFormat, GL11.GL_UNSIGNED_BYTE, textureBuffer); changed to
GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, 3, texture.getImageWidth(), texture.getImageHeight(), srcPixelFormat, GL11.GL_UNSIGNED_BYTE, textureBuffer);

8)