generating textures.. the opengl way

hello,

i am trying to generate textures without using jogl’s Texture and TextureIO classes. this is what i try:


// the file is a 256*256*4 TGA so i am reading from the 12th byte
		try {
			fileinputstream.read( b,12,256*256*4-12 );
			buffer = ByteBuffer.wrap(b);//.allocate(256*256*4);
			buffer.rewind();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		texId = IntBuffer.allocate(1);
		gl.glGenTextures(1, texId);
		gl.glBindTexture(GL.GL_TEXTURE_2D, texId.get(0));
		gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 256, 256, 0, GL.GL_RGBA, GL.GL_BYTE, buffer);

if i look into the ByteBuffer the data really seems to be there. it contains values from -127 to 128.

then i try to render it this way


		gl.glBindTexture(GL.GL_TEXTURE_2D, texId.get(0));
		
		gl.glBegin(GL.GL_QUADS);
		gl.glTexCoord2d(0,0);
		gl.glVertex2i(0, 0);
		gl.glTexCoord2d(1,0);
		gl.glVertex2i(100, 0);
		gl.glTexCoord2d(1,1);
		gl.glVertex2i(100, 100);
		gl.glTexCoord2d(0,1);
		gl.glVertex2i(0, 100);
		gl.glEnd();

unfortunately i only see a white quad. no texture at all. when i use the textureIO functions the texture displays correctly.

what am i doing wrong? thanks!

Have you enabled textures?

gl.glEnable( GL.GL_TEXTURE_2D );

And set the MIN/MAG filters?

Google a bit and you’ll find what you need, or check the source of TextureIO

yes texture2d is enabled and min/mag filters are set.

i already googled and the sources i found seem to do the same stuff as i am. theres probably something trivial i am missing… but what?

maybe something with the way TGA stores its data? i read something about BGRA and not RGBA, so i changed the code to:

		 for (int i = 0; i < size; i += 4) {                 
             // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
             // Temporarily Store The Value At Image Data 'i'
             byte temp = buffer.get(i);                               
             // Set The 1st Byte To The Value Of The 3rd Byte
             buffer.put(i, buffer.get(i + 2));           
             // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
             buffer.put(i + 2, temp);                             
         }
				
		texId = IntBuffer.allocate(1);
		gl.glGenTextures(1, texId);
		gl.glBindTexture(GL.GL_TEXTURE_2D, texId.get(0));
		gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
		gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
		gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 256, 256, 0, GL.GL_BGRA, GL.GL_BYTE, buffer);

now at list i see the outlines of the texture’s image, but there is still something wrong with the channels…

ok, i just figured it out,

i was reading the wrong number of bytes from the TGA header. TGA has an 18byte header.

thanks all!