Bug 402

Summary: Texture Buffer Size
Product: [JogAmp] Jogl Reporter: Guilherme Amorim <seu.gui.x>
Component: openglAssignee: Sven Gothel <sgothel>
Status: VERIFIED INVALID    
Severity: major    
Priority: ---    
Version: 2   
Hardware: pc_x86_64   
OS: windows   
Type: --- SCM Refs:
Workaround: ---
Attachments: Zip containing code that throws the exception and the Image file
Exception Description

Description Guilherme Amorim 2010-07-04 18:42:11 CEST
While loading the texture the following exception may occur:

javax.media.opengl.GLException: java.lang.IndexOutOfBoundsException: Required 196608 remaining bytes in buffer, only had 0
at com.sun.gluegen.runtime.BufferFactory.rangeCheckBytes(BufferFactory.java:311)
        at com.sun.opengl.impl.gl2.GL2Impl.glTexImage2D(GL2Impl.java:18663)
...

The following line cause the exception:

gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, 3, img.getWidth(), img.getHeight(), 0, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buffer);

"buffer" is a ByteBuffer variable which holds data from a BufferedImage


The whole code is attached

Thanks
Comment 1 Guilherme Amorim 2010-07-04 18:43:53 CEST
Created attachment 145 [details]
Zip containing code that throws the exception and the Image file
Comment 2 Guilherme Amorim 2010-07-04 18:49:03 CEST
Created attachment 146 [details]
Exception Description
Comment 3 Sven Gothel 2010-07-06 05:56:26 CEST
After minor test case fixes to allow it to run under JOGL2,
plus adding trace and debug pipeline, the output is:

+++

Info: XInitThreads() called for concurrent Thread support
glGenTextures(<int> 0x1, <[I>, <int> 0x0)
glBindTexture(<int> 0xDE1, <int> 0x1)
glTexImage2D(<int> 0xDE1, <int> 0x0, <int> 0x3, <int> 0x100, <int> 0x100, <int> 0x0, <int> 0x1907, <int> 0x1401, <java.nio.Buffer> java.nio.DirectByteBuffer[pos=196608 lim=196608 cap=196608])Exception in thread "Timer-0" javax.media.opengl.GLException: java.lang.IndexOutOfBoundsException: Required 196608 remaining bytes in buffer, only had 0

+++

As you can see, we calculate the proper byte size of 196608, which is to be expected,
but you buffer can't offer any - since it's position is at it's limit.

A simple "buffer.rewind()" before passing it to the GL texture funtion solves your bug, 
so we can read all required bytes,

Now the output is:

+++

Info: XInitThreads() called for concurrent Thread support
glGenTextures(<int> 0x1, <[I>, <int> 0x0)
glBindTexture(<int> 0xDE1, <int> 0x1)
glTexImage2D(<int> 0xDE1, <int> 0x0, <int> 0x3, <int> 0x100, <int> 0x100, <int> 0x0, <int> 0x1907, <int> 0x1401, <java.nio.Buffer> java.nio.DirectByteBuffer[pos=0 lim=196608 cap=196608])
glTexParameteri(<int> 0xDE1, <int> 0x2801, <int> 0x2601)
glTexParameteri(<int> 0xDE1, <int> 0x2800, <int> 0x2600)
glShadeModel(<int> 0x1D01)
...

+++

And the cube is no more yellow :)