ported kev's TextureLoader

showsrc.com beautified
http://showsrc.com/$http://people.freenet.de/ki_onyx/Texture.java
http://showsrc.com/$http://people.freenet.de/ki_onyx/TextureLoader.java

plain source
Texture.java
TextureLoader.java

I hadn’t much luck with JCD’s loaders… and since I liked the concept of Kev’s loader pretty much, I just ported it :slight_smile:


TextureLoader textureLoader = new TextureLoader();
Texture fontTexture = textureLoader.getTexture("font","data/font.png");
[...]
for binding it just call:
fontTexture.bind();

I just thought I should post it before someone else reinvents the wheel :wink:

edit: bad bad board >:(

Cool oNyx, I’ll take a look at this as soon as I get some time. I really like JCD’s texture loader though, very easy to use imo.

What problems have you had with it?

There is one thing that’s puzzling me about JCD’s texture loader though, but I’m not sure if it’s something I’m doing or something in the code. Every time I load and display a texture it’s always displayed upside down. I’m pretty sure its something I’m not setting up right on the texture loading side of things, but I’ve only glanced through the texture chapter of the red book so I can’t be sure.

Anyone got any clues?

What problems have you had with it?

It bombed out at GLU.gluBuild2DMipmaps IIRC

Anyone got any clues?

Well, opengl stores them upside down. Therefore you have to flip it. See here:

http://showsrc.com/$http://people.freenet.de/ki_onyx/TextureLoader.java#215

It bombed out at GLU.gluBuild2DMipmaps IIRC

JCD’s worked fine for me, but as I said I’ve only glanced through the texture chapter, maybe JCD can shed some light.

Well, opengl stores them upside down.

That I didn’t know, I’ve just been flipping them in photoshop to solve the issue :slight_smile:
I’ll really have to read that red book properly sometime.

Cheers :slight_smile:

Hmmm I’ve updated my texture loader few days ago with an option to flip the images upside down.
I also corrected a bug where a texture is declared BGR while it’s clearly RGB.
Check it out in my work frame :stuck_out_tongue:

[quote]There is one thing that’s puzzling me about JCD’s texture loader though, but I’m not sure if it’s something I’m doing or something in the code. Every time I load and display a texture it’s always displayed upside down.
[/quote]
Tga files can have it’s image stored upside down. Last time I checked, JCD loader don’t check the upside down flag in the tga header. So wich way is up is determined by the program that made the file. PSP and adobe photoshop stores the images in different ways. (btw the flag is the 5th bit of the imageDescriptor field)

[quote]It bombed out at GLU.gluBuild2DMipmaps IIRC
[/quote]
This sounds like a bug in JCD image loader. It will fail when using non power of two texture that do not match opengls unpack alignment. UNPACK_ALIGNMENT must be set to to 1 before calling GLU.gluBuild2DMipmaps or it can throw a IndexOutOfBoundsException.

There is also a bug in gluBuild2DMipmaps wich causes non power of two texture to have stripes.

Kevs texture loader don’t have any of these problems because he converts the images to pow two before they are sent to opengl

[quote]Hmmm I’ve updated my texture loader few days ago with an option to flip the images upside down.
I also corrected a bug where a texture is declared BGR while it’s clearly RGB.
Check it out in my work frame :stuck_out_tongue:
[/quote]
Good to know.

I’ll keep it in mind for the case, that I ever need a AWT free version :wink:

edit: oh… I used power of 2 PNGs (24 and 32 bit)

I’m trying to use your code to get lwjgl to display a 2d png image on the screen, however I’m a bit stuck

in my init I have


TextureLoader textureLoader = new TextureLoader(); 
Texture fontTexture = textureLoader.getTexture("font","data/test.png"); 

I also declared fontTexture at the top using:


private static Texture fontTexture; 

then in my render part I have:


fontTexture.bind();

everything works fine when I leave the bind() part out, but when I put it in I get the following output:

Loading texture data/test.png to ID: 1 wrap:true mipmapped:false
java.lang.NullPointerException
at org.lwjgl.examples.Game.render(Game.java:170)
at org.lwjgl.examples.Game.run(Game.java:115)
at org.lwjgl.examples.Game.main(Game.java:45)
Press any key to continue…

This should probably go into the Clueless Newbie section, because my main question is of course how the hell do I get a 2d sprite on the screen using lwjgl. I figured on the texture loading, I’ve done the first 5 nehe tutorials with success. I had a look at the Space Invaders 103 tutorial. But I can’t get it to work :frowning: So I guess I’m clueless, but I’m just missing something.

It looks like you’ve declared fontTexture twice - the first class member one gets hidden by the second local one you declare (and assign to) in your init. Remove your duplicate ‘Texture’ delclaration in the init method.

Noob question inc ???

What image formats are supported by awt’s BufferedImage?

I’m gonna use this purrrty textureloader as soon as I find textures to load ;D

edit: added a ‘s’ to formats :wink:

thanks for the help, I got the texture on a cube now! :o ;D

celebrates

What image formats are supported by awt’s BufferedImage?

All :slight_smile:

The question is wich formats does ImageIO support.

[quote]static String[] getReaderFormatNames()
Returns an array of Strings listing all of the informal format names understood by the current set of registered readers.
[/quote]

Oh and one thing I’ve to add:

If you setup the perspective by yourself you don’t necessarly need to flip the textures.

e.g.
GLU.gluOrtho2D(0, Window.getWidth(), Window.getHeight(), 0);

Then 0,0 is in the upper left and textures doesnt need to be flipped. However, it will break alot of things (eg text printing methods).

You could also flip the image once by yourself. Well, I guess I’ll just continue flipping in the loader… and once I’m done, I’ll use a little programm to flip all images and then I’ll just remove the flip part. I know some commercial games wich just did that and I have to admit it’s the most painless way :wink:

A Wise Bunny will flip the texture coordinates, not the projection :wink:

Cas :slight_smile:

A Wise Bunny will flip the texture coordinates, not the projection :wink:

Of corse. Well, in my case it’s a major headache and since I’ll need a post processing stage anyways (pngout) I can just flip 'em aswell.

But then again… I guess there is an easy way to flip the coords (eg just writing a method wich calls glTexCoord2f with flipped coords) or maybe there is an ogl way to do it? :>

[quote]But then again… I guess there is an easy way to flip the coords (eg just writing a method wich calls glTexCoord2f with flipped coords) or maybe there is an ogl way to do it? :>
[/quote]
You could probably set the texture coord matrix to flip on the y axis (-1 y scale) but thats likely to cause problems if you ever tried to use tex coord generation etc.

Hmhmhm ok.

I say yes to post processing :stuck_out_tongue: ;D

I’m really in need of some help on getting alpha channels/png’s with transparency to work.

I would like to have png’s in some transparent form, but I have no idea what the problem is, I tried almost all options in formats but I’m either getting a NullPointer error which stops the game from running, or my texture is colored black (the background color) instead of being transparent, I’m having another texture moving behind the transparent one. Could anyone give me some pointers? or do I need to dive into blending/masks instead of just using the textureloader from this topic?

A texture/file loader will only get you part of the way there - actually importing the image data into a texture. At this point all you’ll have is a texture with RGBA data, you have to tell OpenGL how you want it to use the alpha data to draw the texture.

Simplest weighted-average type blending is:
glEnable(GL.GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glBlendFunc(GL_SRC_ALPHA, GL_ONE) gets you additive blending, which is what you want if you’re doing some particle type effects.

I got it now, I was just unsure about whether the textureloader was already doing transparency because of .hasAlpha() stuff in it. But the way you showed it works perfect, and I also know where to look now if I want more. So thanks! :slight_smile:

because of .hasAlpha() stuff in it

hasAlpha() is used to (surprise) determine if the image has an alpha channel or not. If so, RGBA is used otherwise RGB (in order to avoid the wasting of valuable vram) :slight_smile: