Texturing

Started by Daslee, May 18, 2012, 12:23:17

Previous topic - Next topic

Daslee

Hello. Can somebody tell me how to texture objects? I know that i must enable texturing something like: glEnable(GL_TEXTURE_2D) then load texture, and bind it, but i do not know how, i never tried it because i don't know how.

Or maybe i can get link of tutorial?

matheus23

Thats right. You need to enable GL_TEXTURE_2D first, load a texture. While doing that, you will recieve a Texture ID. If you want to load that Texture later, just call glBindTexture(GL_TEXTURE_2D, texID);.

The tricky point is to load that texture. That will give you more infos:
http://lwjgl.org/wiki/index.php?title=Slick-Util_Library_-_Part_1_-_Loading_Images_for_LWJGL
http://lwjgl.org/wiki/index.php?title=Loading_PNG_images_with_TWL%27s_PNGDecoder

I personally prefer the PNGDecoder from "Matthias Mann", because it's a single Jar / Class file, and lets you mess around with everything. It will just perfectly return your ByteBuffer, needed for glTexImage2D(...blah..., ByteBuffer data); ;)
Then down thing is, that the PNG-decoder only decodes PNG's... Who would have thought that ?!?! ;)

Thats all I can say... But start with Slick's Utils.

Background knowings you don't need to know:

Slick util uses the Java's ImageIO class with BufferedImage. It loades the Images and takes out the raw data of the BufferedImage to use it to create a ByteBuffer. Bad thing with this is Performance. Also it only allows loading textures for GL_TEXTURE_2D (correct my if I'm wrong. I didn't look into that so deep), but it will allow any image format, which ImageIO supports.

The PNGDecoder however, is super-duper-fast and decodes the Image partly by itself, partly with the ZIP-Encoder. I use it to load textures while having great control about what happens. You can use glTexParameter just like you want them and you are able to use any texture format you want (i. e. GL_TEXTURE_RECTANGLE_ARB). ;)
My github account and currently active project: https://github.com/matheus23/UniverseEngine

mattdesl

Slick uses Matthias' PNG decoder, as well as a custom TGA loader. It only resorts to ImageIO/BufferedImage for other file types, or if a command line flag is set to use ImageIO for PNG decoding.

Also worth mentioning, Matthias Mann has written pure-Java decoders for BMP, JPEG and TGA formats. He's wrapped all of his decoders in a texture loading library, too:
http://hg.l33tlabs.org/TextureLoader/file/1814b67f3adc/src/de/matthiasmann/textureloader

EDIT:
If you're reading tutorials on Slick-Util, something that they always leave out is non-power-of-two (NPOT) texture coordinates. Using texcoords [0-1] will not work correctly if you're using Slick to load NPOT textures. Instead, you should be using Texture.getWidth/getHeight. Example here.

Daslee

Thanks guys i readed little about slick and tried to texture, so now i understand how to do that. :) But now there is one problem look at this picture, and tell me can i do that?:



mattdesl

The best solution is to just render your texture multiple times. e.g.
final int VERTICAL_REPEATS = 3; 

int startX = 50;
int startY = 50;
texture.bind(); //bind your tex
glBegin(GL_QUADS);
for (int i=0; i < VERTICAL_REPEATS; i++) {
    int y = startY + i * imgHeight;
    //draw a quad using the new y value... 
}
glEnd();


Another solution would be use GL_REPEAT, but that won't work with texture atlases or NPOT Slick textures.

Daslee

Nvm, resizing will be okay. :)

matheus23

Err... wait..
It's much easier. You don't even need 3 quads for something like that. Just use other Texture coords, that means (if you use LWJGL):
(for y axis repeat of 3 times:)
instead of calling

glTexCoord(0, 0);
glTexCoord(1, 0);
glTexCoord(1, 1);
glTexCoord(0, 1);


call

glTexCoord(0, 0);
glTexCoord(1, 0);
glTexCoord(1, 3);
glTexCoord(0, 3);


in other words:

glTexCoord(0, 0);
glTexCoord(XREPEAT, 0);
glTexCoord(XREPEAT, YREPEAT);
glTexCoord(0, YREPEAT);
My github account and currently active project: https://github.com/matheus23/UniverseEngine

mattdesl

matheus23 - Doesn't work with texture atlases or non-power-of-two Slick textures, like I said. :)