problems creating textures

Hello,

I have got problems creating a texture with lwjgl.
Here’s the code:

loading data:


tga x = new tga();
        x.load(new File("./test2.tga"));
        
        ByteBuffer f = x.getPixelData();
        
        TexBuf = BufferUtils.createIntBuffer(5);
        GL11.glGenTextures(TexBuf);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, TexBuf.get(0));
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, x.getWidth(), x.getHeight(),
                    0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, x.getPixelData()); *
        

rendering code:


GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
            GL11.glLoadIdentity(); 
          
            GL11.glPushMatrix();
            GL11.glTranslatef(0.0f, 0.0f, -12.0f );
            
            GL11.glBegin(GL11.GL_QUADS);
                  GL11.glTexCoord2f(0.0f,0.0f);
                  GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
                  
                  GL11.glTexCoord2f(1.0f,0.0f);
                  GL11.glVertex3f(1.0f, -1.0f, 0.0f);
                  
                  GL11.glTexCoord2f(1.0f,1.0f);
                  GL11.glVertex3f(1.0f, 1.0f, 0.0f);
                  
                  GL11.glTexCoord2f(0.0f,1.0f);
                  GL11.glVertex3f(-1.0f, 1.0f, 0.0f);      
            GL11.glEnd();
            GL11.glPopMatrix();
          
            

The tga getPixelData Method returns a ByteBuffer with the Pixeldata. I use only 24Bit TGAs with no alphabits.
I get a java.nio.BufferOverflowException in the line marked with the *

What am I doing wrong? I can’t really figure out at the moment. If you want I can post the tga-loading code, but it is really hacked together.

Greetings
thalador

My first guess, without seeing the code for the tga loader, is that getPixels is returning a buffer which is not rewound. Make sure that the when the image is loaded the buffer is rewound.

Hello again,

yes, that was really the problem. I forgot to rewind() the buffer. I am very new to these nio-Buffers and I start to think that they hate me :slight_smile:

The next problem is already there: I can only see a white rectangle with no texture, but there should be one. First I thougth that my tga loader may be spilling out corrupt data, but the values look okay in debugger mode. Also glIsTexture returns true. So I googled a bit and they say that this happens if I call the texture functions before getting a GL-Context, but I am definetly calling the texture-functions after the whole lwjgl-window creation an d so. Another google result said, that this could happen, if the texture data is corrupt or the texture couldn’t be created.
To make sure that the error is not the tga loader I created a 2x2 texture by hand:


//another texture
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, TexBuf.get(1));
        ByteBuffer pix = BufferUtils.createByteBuffer(12);
        pix.put((byte)5); pix.put((byte)5); pix.put((byte)5);
        pix.put((byte)30); pix.put((byte)100); pix.put((byte)100);
        pix.put((byte)40); pix.put((byte)40); pix.put((byte)40);
        pix.put((byte)0); pix.put((byte)0); pix.put((byte)50);
        pix.rewind();
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, 2, 2,
                    0, GL11.GL_RGB, GL11.GL_BYTE, pix);
        
        System.out.println("" + TexBuf.get(1)+" "+ GL11.glIsTexture(TexBuf.get(1)));

output is: 2 true
The drawing code is still the same, except I use TexBuf.get(1) now and there is another GL11.glBindTexture(GL11.GL_TEXTURE_2D, TexBuf.get(1)); in it. Whats wrong this time?

Greetings
thalador

Hmm, have you actually enabled texturing with glEnable(GL_TEXTURE_2D) ?

Yepp I did enable it

I’ve uploaded the source, perhaps that helps:
GLTest.java
tga.java
test2.tga

Hmm, can’t nobody help me? I’ve gone through the hole forum searching for similiar problems, but I haven’t found something that could help me.
Or isn’t it possible to fill a ByteBuffer with a few Bytes and then pass this Buffer to glTexImage2D?
-Buffer is in native order
-Buffer is rewinded
-Buffer is direct
-Texturing is enabled
-glIsTexture returns true
-glBind is used

I can’t figure it out.

This is commented out


       //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
        //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
        //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        


Yes, that is right, I tried it uncommented but it still doesn’t work and for that parameters are used the standardvalues so it should work anyway, shouldn’t it?

I thought something like that:


byte [] b = new byte[12];
        b[0] = 23; b[1]=5; b[2]=50;
        b[3] = 0; b[4]=0; b[5]=50;
        b[6] = 50; b[7]=0; b[8]=0;
        b[9] = 23; b[10]=5; b[11]=50;
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, TexBuf.get(3));
        ByteBuffer b1 = BufferUtils.createByteBuffer(12);
        b1.put(b);
        b1.rewind();
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, 2, 2, 0, GL11.GL_RGB, GL11.GL_BYTE, b1);
        System.out.println("" + TexBuf.get(3)+" "+ GL11.glIsTexture(TexBuf.get(3)));

should work in any case.

thala

Look for the JCD utils in this forum. He has a working TGA loader :slight_smile:

Well I think it works finally, I had a ByteBuffer with ByteOrder.LITTLE_ENDIAN which I copied into one with ByteOrder.nativeOrder(). Damn it that hurts :slight_smile:

But why my above example doesn’t work I still don’t know, but it doesn’t matter, my tga-loader works.

The Code from Java Cool Dude is an interesting piece of Code for a newb like me :slight_smile:

Thanks to you all
Thala