LWJGL Forum

Programming => OpenGL => Topic started by: padam1989 on September 15, 2009, 14:07:53

Title: binding texture to a quad..... ????
Post by: padam1989 on September 15, 2009, 14:07:53
Hola everyone.
According to this tutorial http://lwjgl.org/wiki/doku.php/lwjgl/tutorials/textures/tga (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.
Title: Re: binding texture to a quad..... ????
Post by: Evil-Devil on September 15, 2009, 14:27:26
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 ^^"
Title: Re: binding texture to a quad..... ????
Post by: padam1989 on September 15, 2009, 14:44:04
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.
Title: Re: binding texture to a quad..... ????
Post by: Evil-Devil on September 15, 2009, 15:15:00
Is the file found when reading from it or does it throws an exception?
Maybe you return null at loader.getTGATexture()?
Title: Re: binding texture to a quad..... ????
Post by: padam1989 on September 17, 2009, 06:43:29
i checked my code. my program can find the tga file and yeah i m getting null texture. Any Help ??
Title: Re: binding texture to a quad..... ????
Post by: Evil-Devil on September 17, 2009, 07:04:41
Maybe you can post the code for loading your tga. Else i could upload the full tga loader i wrote for the turorial.
Title: Re: binding texture to a quad..... ????
Post by: padam1989 on September 17, 2009, 11:05:35
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.
Title: Re: binding texture to a quad..... ????
Post by: 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...
Title: Re: binding texture to a quad..... ????
Post by: broumbroum on September 17, 2009, 13:56:56

        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);

:)
Title: Re: binding texture to a quad..... ????
Post by: padam1989 on September 17, 2009, 14:22:08
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.
Title: Re: binding texture to a quad..... ????
Post by: Evil-Devil on September 17, 2009, 14:33:54
I guess you have GL_TEXTURE_2D enabled or not?
Title: Re: binding texture to a quad..... ????
Post by: padam1989 on September 17, 2009, 14:52:36
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.
Title: Re: binding texture to a quad..... ????
Post by: 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 ;)
Title: Re: binding texture to a quad..... ????
Post by: Rene on September 17, 2009, 23:01:14
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. ;)
Title: Re: binding texture to a quad..... ????
Post by: padam1989 on September 18, 2009, 05:09:52
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.
Title: Re: binding texture to a quad..... ????
Post by: Evil-Devil on September 18, 2009, 12:03:04
I think it is more suitable than PNG. PNG offers custom compression and a its not that easy to read a png file when not using helpers like ImageIO. The drawback of TGA is that it comes with no compression and RLE is not really an issue to take care about. But it is very easy to read/write imho.

But as you said you're going for a graphics application and not a game i understand your point in adding PNG support.
Title: Re: binding texture to a quad..... ????
Post by: NateS on September 18, 2009, 12:26:43
In TWL, there is a possibly newer version of the PNG loader that is in Slick (same author, MatthiasM):
http://twl.l33tlabs.org/
Title: Re: binding texture to a quad..... ????
Post by: padam1989 on September 18, 2009, 15:14:12
Hola.
I am getting an exception when loading rle tga. Here is the exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 524288
       at com.gaanza.engine.test.TGALoader.loadCompressedData(TGALoader.java:119)
       at com.gaanza.engine.test.TGALoader.<init>(TGALoader.java:51)
       at com.gaanza.engine.test.WindowTest.init(WindowTest.java:96)
       at com.gaanza.engine.test.WindowTest.main(WindowTest.java:35)
Java Result: 1


and line which is throwing an exception is:

                   textureData[currentByte]=colorBuffer[0];    //R byte


Help !!

Gracias.
Title: Re: binding texture to a quad..... ????
Post by: Evil-Devil on September 21, 2009, 11:45:09
Weird, did the RLE work with the provided LWJGL TGA RLE file or does it crash too? Because i tested the tutorial code with both files and both had been displayed/loaded fine.
Title: Re: binding texture to a quad..... ????
Post by: padam1989 on September 28, 2009, 07:16:40
Quote from: Evil-Devil on September 21, 2009, 11:45:09
Weird, did the RLE work with the provided LWJGL TGA RLE file or does it crash too?

I was talking about the TGA RLE file that was provided by the tutorial. non-RLE file worked fine but RLE didn't worked.

Gracias.
Title: Re: binding texture to a quad..... ????
Post by: Evil-Devil on September 28, 2009, 10:51:20
Yea, that is what me makes wondering. Because the TGA RLE i put into the archive worked when i wrote and testet the tutorialcode. Will check it when iam home.
Title: Re: binding texture to a quad..... ????
Post by: Evil-Devil on September 28, 2009, 18:37:08
I can't see a difference, maybe iam blind ^^"

Here is the original full code i used for the tutorial. Just tested with the RLE file and it works fine.
TextureLoader.java

import static org.lwjgl.opengl.GL11.GL_LINEAR;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_MIN_FILTER;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glTexImage2D;
import static org.lwjgl.opengl.GL11.glTexParameteri;

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 org.lwjgl.opengl.EXTBgra;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;


/* =============================================================================
* TextureLoader.java LWJGL_Tests
*
* Copyright (c) 2009 Benjamin "Evil-Devil" Behrendt
* All rights reserved
* -------------------------------------------------------------------------- */

/**
* @author Benjamin "Evil-Devil" Behrendt
* @version 1.0, 09.03.2009
*/
public class TextureLoader {

private FileInputStream fis = null;
private DataInputStream dis = null;

public TextureLoader() {
}

public Texture load(String fileName) {
return loadFromFile(fileName);
}

private Texture loadFromFile(String file) {
Texture tex = null;

try {
if (dis != null) {
dis.close();
dis = null;
}

/* TGA Header
* see http://en.wikipedia.org/wiki/Truevision_TGA for more info
*/

dis = new DataInputStream(new FileInputStream(new File(file)));
byte[] TGARAWHEADER = new byte[12];
dis.readFully(TGARAWHEADER);

/* Uncompressed or compressed?
* 2 = Uncompressed
* 10 = Compressed
*/
if (TGARAWHEADER[2] == 2) {
tex = loadUncompressedData();
} else if (TGARAWHEADER[2] == 10) {
tex = loadCompressedData();
}
TGARAWHEADER = null;
dis.close();
dis = null;

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return tex;
}

private Texture loadUncompressedData() throws IOException {
byte[] TGAHEADER = new byte[6];
Texture tex = new Texture();

dis.readFully(TGAHEADER);
setTextureInfo(TGAHEADER,tex);

final byte[] textureData = new byte[tex.width * tex.height * tex.bpp];

dis.readFully(textureData);

tex = createTexture(tex, textureData);

return tex;
}

private Texture loadCompressedData() throws IOException {
byte[] TGAHEADER = new byte[6];
Texture tex = new Texture();

dis.readFully(TGAHEADER);
setTextureInfo(TGAHEADER,tex);

final byte[] textureData = new byte[tex.width * tex.height * tex.bpp];

final int pixelCount = tex.width * tex.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[tex.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);
   
    // PLAIN READ
    textureData[currentByte] = colorBuffer[0]; // R Byte
    textureData[currentByte+1] = colorBuffer[1]; // G Byte
    textureData[currentByte+2] = colorBuffer[2]; // B Byte*/
   
    // 32bit (alpha) check
    if (tex.bpp == 4)
    textureData[currentByte+3] = colorBuffer[3]; // A Byte       
   
    // increase the currByte counter
    currentByte+=tex.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
   
    // 32bit (alpha) check
    if (tex.bpp == 4)
    textureData[currentByte+3] = colorBuffer[3]; // A Byte
   
// increase the currByte counter
    currentByte+=tex.bpp;
    currentPixel++;
    }
    }

} while (currentPixel < pixelCount);

tex = createTexture(tex, textureData);

return tex;
}

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 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(GL_TEXTURE_2D,tex.name);
   
    GL11.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       GL11.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       
       GL11.glTexParameteri(GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
       
       // RGB OR RGBA - We assume that it is at least 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(GL_TEXTURE_2D,0,glColor2,tex.width,tex.height,0,glColor,GL11.GL_UNSIGNED_BYTE,textureBuffer);    
   
    return tex;
}


}

/* very simple texture class */
class Texture {
public int width = 0; // the width
public int height = 0; // the height
public byte bitdepth = 0; // the bitdepth, ie: 24bit
public int name = -1; // the opengl name by creation
public byte bpp = 0; // bits per pixel
}



/* =============================================================================
* TextureTest.java LWJGL_Tests
*
* Copyright (c) 2009 Benjamin "Evil-Devil" Behrendt
* All rights reserved
* -------------------------------------------------------------------------- */

/**
* @author Benjamin "Evil-Devil" Behrendt
* @version 1.0, 09.03.2009
*/

import org.lwjgl.opengl.GL11;

public class TextureTest extends Core {

private TextureLoader loader;
private Texture tex;

public static void main(String[] args) {
new TextureTest();
}

protected void initData() {
loader = new TextureLoader();
//tex = loader.load("lwjgl_logo.tga");
tex = loader.load("lwjgl_logo_rle.tga");
}

protected void initOpenGL() {
super.initOpenGL();
GL11.glEnable(GL11.GL_TEXTURE_2D);
// Enable Alphatesting
GL11.glAlphaFunc(GL11.GL_GREATER, 0.01f);
       GL11.glEnable(GL11.GL_ALPHA_TEST);
}

protected void render() {
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_COLOR_BUFFER_BIT);
GL11.glLoadIdentity();
GL11.glTranslatef(0f,0f,-10f);

GL11.glBindTexture(GL11.GL_TEXTURE_2D,tex.name);

GL11.glBegin(GL11.GL_QUADS);

GL11.glTexCoord2f(0,0);
GL11.glVertex3f(-4f,-3f,0f); // bottom left

GL11.glTexCoord2f(1,0);
GL11.glVertex3f(4f,-3f,0f); // bottom right

GL11.glTexCoord2f(1,1);
GL11.glVertex3f(4f,3f,0f); // top right

GL11.glTexCoord2f(0,1);
GL11.glVertex3f(-4f,3f,0f); // top left;

GL11.glEnd();
}
}



import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.OpenGLException;
import org.lwjgl.opengl.Util;
import org.lwjgl.util.glu.*;


/* =============================================================================
* Core.java LWJGL_Tests
*
* Copyright (c) 2007 Benjamin "Evil-Devil" Behrendt
* All rights reserved
* -------------------------------------------------------------------------- */

/**
* @author Benjamin "Evil-Devil" Behrendt
* @version 1.0, 21.08.2007
*/
public abstract class Core {

private long startTime = 0L;

protected Core() {
this.init();
}

private void init() {
try {
this.initDisplay();
this.initKeyboard();
this.initMouse();
} catch (LWJGLException e) {
System.out.println("Error while init:");
e.printStackTrace();
}

this.initOpenGL();
this.reSizeGLScene(Display.getDisplayMode().getWidth(),Display.getDisplayMode().getHeight());
this.initData();
this.initLight();

this.run();
}

private void initMouse() throws LWJGLException {
if (!Mouse.isCreated())
Mouse.create();
}

private void initKeyboard() throws LWJGLException{
if (!Keyboard.isCreated())
Keyboard.create();
}

private void initDisplay() throws LWJGLException {
Display.setFullscreen(false);
Display.setDisplayMode(new DisplayMode(640,480));
Display.create();
}

protected void initLight() {}

protected void initData() {}

protected void initOpenGL() {
GL11.glShadeModel(GL11.GL_SMOOTH);
       GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // black background
       GL11.glClearDepth(1.0f); // depth of 0 to 1        
       GL11.glDepthFunc(GL11.GL_LEQUAL);
       GL11.glEnable(GL11.GL_DEPTH_TEST); // enable depth testing
       GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
}

private void reSizeGLScene(int width, int height) {
       if (height == 0)
           height = 1;

       GL11.glViewport(0, 0, width, height);

       GL11.glMatrixMode(GL11.GL_PROJECTION);
       GL11.glLoadIdentity();

       // a nice 45° perspective
       GLU.gluPerspective(45.0f, (float) width / (float) height, 0.1f, 100.0f);
       // ortho view
       //GL11.glOrtho(0.0f,width,height,0.0f,0.0f,1.0f);

       GL11.glMatrixMode(GL11.GL_MODELVIEW);
       GL11.glLoadIdentity();
   }

private void cleanup() {
if (Display.isCreated()) {
Display.destroy();
       System.exit(0);
   }
}

protected void run() {
       this.startTime = Sys.getTime() + 1000;
       int FPS = 0;
       long frameTime = 0L;
     
       while (true) {
           
           if (Display.isCloseRequested()) {
               cleanup();
               break;
           }
           if (Display.isVisible()) {
           
               try {                
                   this.render(); // render our magnificent scene o_O
                   Display.update(); // update the display ^__^
                                       
               } catch (OpenGLException e) {
                   e.printStackTrace();
               }
               // catch keyboard/mouse events
               checkKeyboardInput();
               checkMouseInput();
           }
           
           if (this.startTime > Sys.getTime())
           FPS++;
           else {
        frameTime = 1000 + (this.startTime - Sys.getTime());
           this.startTime = Sys.getTime() + 1000;
           System.out.println(FPS + " frames in " + (frameTime / 1000f) + " seconds = " + (FPS / (frameTime / 1000f)));
            FPS = 0;
           }
           if (GL11.glGetError() != GL11.GL_NO_ERROR) {
            System.out.println(Util.translateGLErrorString(GL11.glGetError()));
           }
       }
}

protected void checkMouseInput() {}

protected void checkKeyboardInput() {
if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
this.cleanup();
}
}

protected void render() {}

}