binding texture to a quad..... ????

Started by padam1989, September 15, 2009, 14:07:53

Previous topic - Next topic

padam1989

Hola everyone.
According to this tutorial http://lwjgl.org/wiki/doku.php/lwjgl/tutorials/textures/tga i ahve loaded the tga image into a texture object. Now i want to ask how to bind the texture object to my quad cuz i checked the doc and saw that glBindTexture takes int as parameters. Here is the code:
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);				// Select Our Texture

        GL11.glBegin(GL11.GL_QUADS);
		// Front Face
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		// Back Face
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		// Top Face
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		// Bottom Face
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		// Right face
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		// Left Face
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
	GL11.glEnd();


texture is reference variable holding reference to the object of Texture class which holds the information of tga. Now what shud i give as a second parameter to glBindTexture(int,int). Please Help.
Gracias.

Evil-Devil

Like in the tutorial you have to provide the glName of that texture.

From the the tutorial:
GL11.glBindTexture(GL_TEXTURE_2D,tex.name);


If something more unclear, just shout and I'll see what i can do ^^"

padam1989

Gracias.
i am getting nullpointerexception. Here is the code:
private void init(){
        loader=new TGALoader("/data/lwjgl_logo.tga");
        texture=loader.getTGATexture();
    }

private void render(){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();
        GL11.glTranslatef(0.0f,0.0f,-5.0f);						// Move Into The Screen 5 Units
        GL11.glRotatef(xrot,1.0f,0.0f,0.0f);						// Rotate On The X Axis
	GL11.glRotatef(yrot,0.0f,1.0f,0.0f);						// Rotate On The Y Axis
	GL11.glRotatef(zrot,0.0f,0.0f,1.0f);						// Rotate On The Z Axis

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.name);				// Select Our Texture

        GL11.glBegin(GL11.GL_QUADS);
		// Front Face
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		// Back Face
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		// Top Face
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		// Bottom Face
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		// Right face
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		// Left Face
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
	GL11.glEnd();
    }


exception is getting thrown at render method, at glBindTexture() command.

Evil-Devil

Is the file found when reading from it or does it throws an exception?
Maybe you return null at loader.getTGATexture()?

padam1989

i checked my code. my program can find the tga file and yeah i m getting null texture. Any Help ??

Evil-Devil

Maybe you can post the code for loading your tga. Else i could upload the full tga loader i wrote for the turorial.

padam1989

Here is the code of my TGALoader class.
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

/**
 *
 * @author Java Guy
 */
public class TGALoader {
    private Texture tex;
    private DataInputStream dis;

    public TGALoader(String filename){
        byte[] TGARAWHEADER=new byte[12];
        try {
            dis = new DataInputStream(new FileInputStream(new File(filename)));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TGALoader.class.getName()).log(Level.SEVERE, null, ex);
//            System.out.println("not found");
        }

        if(TGARAWHEADER[2]==2){
            try {
                tex = loadUncompressedData();
            } catch (IOException ex) {
                Logger.getLogger(TGALoader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else if(TGARAWHEADER[2]==10){
            try {
                tex = loadCompressedData();
            } catch (IOException ex) {
                Logger.getLogger(TGALoader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        TGARAWHEADER=null;
    }

    private void setTextureInfo(byte[] header,Texture tex){
        tex.width=header[1]*256+header[0];
        tex.height=header[3]*256+header[2];
        tex.bitdepth=header[4];
        tex.bpp=(byte)(header[4]/8);
    }

    private Texture loadUncompressedData() throws IOException{
        byte[] TGAHEADER=new byte[6];
        Texture tex1=new Texture();
        dis.readFully(TGAHEADER);
        setTextureInfo(TGAHEADER,tex1);
        final byte[] textureData=new byte[tex1.width*tex1.height*tex1.bpp];
        dis.readFully(textureData);
        tex1=createTexture(tex1,textureData);
        return tex1;
    }

    private Texture loadCompressedData() throws IOException{
        byte[] TGAHEADER=new byte[6];
        Texture tex1=new Texture();
        dis.readFully(TGAHEADER);
        setTextureInfo(TGAHEADER,tex1);
        final byte[] textureData=new byte[tex1.width*tex1.height*tex1.bpp];
        final int pixelCount=tex1.width*tex1.height;  //amount of pixels in this image
        int currentByte=0;  //the byte we read from
        int currentPixel=0; //the pixel we handle
        int chunkHeader=0;
        byte[] chunkByte=new byte[1];
        byte[] colorBuffer=new byte[tex1.bpp];

        do{
            dis.readFully(chunkByte);
            chunkHeader=chunkByte[0] & 0XFF;
            //Raw chunk
            if(chunkHeader<128){
                chunkHeader++;  //number of raw pixels
                //pixel reading
                for(int counter=0;counter<chunkHeader;counter++){
                    dis.readFully(colorBuffer);
                    textureData[currentByte]=colorBuffer[0];    //R byte
                    textureData[currentByte+1]=colorBuffer[1];  //G byte
                    textureData[currentByte+2]=colorBuffer[2];  //B byte

                    //32 bit (alpha) check
                    if(tex1.bpp==4){
                        textureData[currentByte+3]=colorBuffer[3];  //A byte
                    }
                    //increase the currentByte counter
                    currentByte+=tex1.bpp;
                    currentPixel++;
                }
            }
            else{
                //RLE Header
                chunkHeader=127;
                //read the data
                dis.readFully(colorBuffer);
                //pixel loop
                for(int counter=0;counter<chunkHeader;counter++){
                    textureData[currentByte]=colorBuffer[0];    //R byte
                    textureData[currentByte+1]=colorBuffer[1];  //G byte
                    textureData[currentByte+2]=colorBuffer[2];  //B byte
                    //32 bit (alpha) check
                    if(tex1.bpp==4){
                        textureData[currentByte+3]=colorBuffer[3];  //A byte
                    }
                    //increase the currByte counter
                    currentByte+=tex1.bpp;
                    currentPixel++;
                }
            }
        }while(currentPixel<pixelCount);
        tex1=createTexture(tex1,textureData);
        return tex1;
    }

    private Texture createTexture(Texture tex,byte[] textureData){
        final ByteBuffer textureBuffer=ByteBuffer.allocateDirect(textureData.length).order(ByteOrder.nativeOrder());
        textureBuffer.clear();
        textureBuffer.put(textureData);
        textureBuffer.flip();
        final IntBuffer glName=ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
        GL11.glGenTextures(glName);
        tex.name=glName.get(0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex.name);
        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);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);

        //RGB or RGBA- we assume that it is atleast 24 bit
        int glColor=tex.bitdepth==32?GL12.GL_BGRA:GL12.GL_BGR;
        int glColor2=tex.bitdepth==32?GL11.GL_RGBA:GL11.GL_RGB;

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, glColor2, tex.width, tex.height, 0, glColor, GL11.GL_UNSIGNED_BYTE, textureBuffer);
        return tex;
    }

    public Texture getTGATexture(){
        return this.tex;
    }
}


Gracias.

Fool Running

Unless I'm blind, it looks like TGARAWHEADER in the constructor never gets filled with any data. This means that it will never try to load anything and this.tex will always be null...
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

broumbroum

        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);

you should also wrap the s coord :
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);

:)

padam1989

Quote from: Fool Running on September 17, 2009, 12:31:47
Unless I'm blind, it looks like TGARAWHEADER in the constructor never gets filled with any data. This means that it will never try to load anything and this.tex will always be null...

Yeah you are right, that was my silly mistake. Now i am having new problem, texture doesn't get rendered. The quad is simple plain white. Help !!. Here is my render and init code:
private void init(){
        loader=new TGALoader("data/lwjgl_logo.tga");
        texture=loader.getTGATexture();
    }

private void render(){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();
        GL11.glTranslatef(0.0f,0.0f,-5.0f);						// Move Into The Screen 5 Units
        GL11.glRotatef(xrot,1.0f,0.0f,0.0f);						// Rotate On The X Axis
	GL11.glRotatef(yrot,0.0f,1.0f,0.0f);						// Rotate On The Y Axis
	GL11.glRotatef(zrot,0.0f,0.0f,1.0f);						// Rotate On The Z Axis

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.name);				// Select Our Texture

        GL11.glBegin(GL11.GL_QUADS);
		// Front Face
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		// Back Face
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		// Top Face
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		// Bottom Face
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		// Right face
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		// Left Face
		GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
	GL11.glEnd();
    }


Gracias.

Evil-Devil

I guess you have GL_TEXTURE_2D enabled or not?

padam1989

Quote from: Evil-Devil on September 17, 2009, 14:33:54
I guess you have GL_TEXTURE_2D enabled or not?

hehehe  ;D yeah i forgot to enable texture mapping. Now everything is perfect. Gracias, this forum is very useful and friendly. I just need one more favor if someone know the links to code of png loader or ideas to write my own pngloader class, and i couldn't find proper png format specification yet.
Gracias.

Evil-Devil

Why do you want to use png?

I don't have link...but Slick and the search should provide enough info. You can create texture of png with the help of ImageIO and BufferedImages... In my opinion not needed. Stick to TGA and DDS ;)

Rene

May I ask why TGA is better than PNG? If I'm correct, TGA's don't have more possibility's than PNG's. They also use more space. And PNG is a free and open format.

Correct me if I'm wrong. ;)
When I am king, they shall not have bread and shelter only, but also teachings out of books, for a full belly is little worth where the mind is starved - Mark Twain

padam1989

Quote from: Evil-Devil on September 17, 2009, 15:10:25
Why do you want to use png?

I don't have link...but Slick and the search should provide enough info. You can create texture of png with the help of ImageIO and BufferedImages... In my opinion not needed. Stick to TGA and DDS ;)

I am learning lwjgl not just to make games, i also want to make some other graphics applications and they should support all images. So thats why i was asking about png loader. and yeah right now i am looking at the png loader class source of open source slick library. Gracias for the info.

And yeah like rene i would also like to know why TGA is better than PNG ???

Gracias.