VBO FloatBuffer crashing at glDrawElements

I am filling a FloatBuffer with vertices for drawing a terrain patch. If I follow the first set of steps below the program crashes when calling glDrawElements( … ), but if I follow the second set, it works perfectly. I am trying to figure out if its a problem with how I’m using the FloatBuffer or with the put( index, float ) method of the buffer itself.


First set of steps: - Create a FloatBuffer of the appropriate size vertBuffer = BufferUtil.newFloatBuffer( size * 3 ); - Rewind the buffer just to make sure - Due to having to step kind of backwards through the calculated vertices, use the put( index, float ) method vertBuffer.put( index, point.x ); vertBuffer.put( index + 1, point.y ); vertBuffer.put( index + 2, point.z ); - Flip the buffer vertBuffer.flip(); - Create the VBO vertID = createVBOID( gl ); gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vertID ); gl.glBufferData( GL.GL_ARRAY_BUFFER, vertBuffer.limit() * BufferUtil.SIZEOF_FLOAT, vertBuffer, GL.GL_STATIC_DRAW ); - Draw the buffer ( The indexBuffer is fine, the way it is set up/filled doesn't change between the crashing and working versions ) gl.glEnable( GL.GL_DEPTH_TEST ); gl.glEnableClientState( GL.GL_VERTEX_ARRAY ); gl.glBindBuffer( GL.GL_ARRAY_BUFFER_ARB, vertID ); gl.glVertexPointer( 3, GL.GL_FLOAT, 0, 0 ); gl.glBindBuffer( GL.GL_ELEMENT_ARRAY_BUFFER, indexID ); indexBuffer.rewind(); gl.glDrawElements( GL.GL_TRIANGLE_STRIP, indexBuffer.limit(), GL.GL_UNSIGNED_INT, 0 ); - Which leads to the crash below
Second set of steps (new bits in bold): - Create a float array and a FloatBuffer of the appropriate size [b]vertBufferArray = new float[ size * 3 ];[/b] vertBuffer = BufferUtil.newFloatBuffer( size * 3 ); - Rewind the buffer just to make sure - Due to having to step kind of backwards through the calculated vertices, put the floats into the float array at appropriate indices [b]vertBufferArray[ index ] = point.x; vertBufferArray[ index + 1 ] = point.y; vertBufferArray[ index + 2 ] = point.z;[/b] [b]- Do a bulk put operation to copy the float array into the buffer vertBuffer.put( vertBufferArray );[/b] - Flip the buffer vertBuffer.flip(); - Create the VBO vertID = createVBOID( gl ); gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vertID ); gl.glBufferData( GL.GL_ARRAY_BUFFER, vertBuffer.limit() * BufferUtil.SIZEOF_FLOAT, vertBuffer, GL.GL_STATIC_DRAW ); - Draw the buffer ( The indexBuffer is fine, the way it is set up/filled doesn't change between the crashing and working versions ) gl.glEnable( GL.GL_DEPTH_TEST ); gl.glEnableClientState( GL.GL_VERTEX_ARRAY ); gl.glBindBuffer( GL.GL_ARRAY_BUFFER_ARB, vertID ); gl.glVertexPointer( 3, GL.GL_FLOAT, 0, 0 ); gl.glBindBuffer( GL.GL_ELEMENT_ARRAY_BUFFER, indexID ); indexBuffer.rewind(); gl.glDrawElements( GL.GL_TRIANGLE_STRIP, indexBuffer.limit(), GL.GL_UNSIGNED_INT, 0 ); - Which leads to a nice terrain patch being drawn

So, my question is… why does using the FloatBuffer’s put( index, float ) method cause a crash?

Cheers,
Brett


#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x697b74fd, pid=4084, tid=3832
#
# Java VM: Java HotSpot(TM) Client VM (1.6.0-beta-b59g mixed mode)
# Problematic frame:
# C  [nvoglnt.dll+0x2b74fd]
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

---------------  T H R E A D  ---------------

Current thread (0x18064c00):  JavaThread "Java2D Queue Flusher" daemon [_thread_in_native, id=3832]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000000

Registers:
EAX=0x00000000, EBX=0x00000000, ECX=0x00000000, EDX=0x19d0ad80
ESP=0x185bf84c, EBP=0x0000096e, ESI=0x00000010, EDI=0x19d14098
EIP=0x697b74fd, EFLAGS=0x00010246

Top of Stack: (sp=0x185bf84c)
0x185bf84c:   00000005 19d0ad80 0000096e 00000000
0x185bf85c:   697b7893 0000096e 185bf880 185bf884
0x185bf86c:   19d0ad80 18da9280 00000000 0000096e
0x185bf87c:   19d14098 19d16a4c 19d0ad80 697ba59d
0x185bf88c:   18da9280 00000005 00000000 ffffffff
0x185bf89c:   0000096e 00001405 00000000 19d0ad80
0x185bf8ac:   00000003 00000000 00000000 00000020
0x185bf8bc:   19d0ad80 0000096e 69503242 19d0ad80 

Instructions: (pc=0x697b74fd)
0x697b74ed:   00 8b c1 74 0b 8b 82 a4 8c 01 00 8b 40 08 03 c1
0x697b74fd:   8b 10 bb 01 00 00 00 83 c0 04 3b f3 8b ca 7e 1c 


Stack: [0x18570000,0x185c0000),  sp=0x185bf84c,  free space=318k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [nvoglnt.dll+0x2b74fd]

[error occurred during error reporting, step 120, id 0xc0000005]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.sun.opengl.impl.GLImpl.glDrawElements0(IIIJ)V+0
j  com.sun.opengl.impl.GLImpl.glDrawElements(IIIJ)V+10
j  javax.media.opengl.DebugGL.glDrawElements(IIIJ)V+13
j  dimension.math.SplinePatch.drawIndexedData(Ljavax/media/opengl/GL;)V+74
j  dimension.math.SplinePatch.drawPatch(Ljavax/media/opengl/GL;)V+18
j  singularity.editor.TestGLJPanelListener.display(Ljavax/media/opengl/GLAutoDrawable;)V+154
j  com.sun.opengl.impl.GLDrawableHelper.display(Ljavax/media/opengl/GLAutoDrawable;)V+29
j  javax.media.opengl.GLJPanel$Updater.display(Ljavax/media/opengl/GLAutoDrawable;)V+224
j  javax.media.opengl.GLJPanel$DisplayAction.run()V+11
j  com.sun.opengl.impl.GLDrawableHelper.invokeGL(Ljavax/media/opengl/GLDrawable;Ljavax/media/opengl/GLContext;Ljava/lang/Runnable;Ljava/lang/Runnable;)V+418
j  javax.media.opengl.GLJPanel$1.run()V+1469
j  sun.java2d.opengl.OGLRenderQueue$QueueFlusher.run()V+95
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x180ed500 JavaThread "TimerQueue" daemon [_thread_blocked, id=688]
  0x180c5500 JavaThread "Image Fetcher 3" daemon [_thread_blocked, id=2792]
  0x180c6300 JavaThread "Image Fetcher 2" daemon [_thread_blocked, id=2060]
  0x180c6d00 JavaThread "Image Fetcher 1" daemon [_thread_blocked, id=240]
  0x180baa00 JavaThread "AWT-EventQueue-0" [_thread_blocked, id=2244]
=>0x18064c00 JavaThread "Java2D Queue Flusher" daemon [_thread_in_native, id=3832]
  0x18065900 JavaThread "Image Fetcher 0" daemon [_thread_blocked, id=624]
  0x18056900 JavaThread "AWT-Windows" daemon [_thread_in_native, id=3676]
  0x17bb4b00 JavaThread "AWT-Shutdown" [_thread_blocked, id=2404]
  0x17bb4600 JavaThread "Java2D Disposer" daemon [_thread_blocked, id=724]
  0x17b2d100 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=1052]
  0x17b2ab00 JavaThread "CompilerThread0" daemon [_thread_blocked, id=2492]
  0x17b29c00 JavaThread "Attach Listener" daemon [_thread_blocked, id=1812]
  0x16baf800 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=716]
  0x16baeb00 JavaThread "Surrogate Locker Thread (CMS)" daemon [_thread_blocked, id=3364]
  0x16ba4100 JavaThread "Finalizer" daemon [_thread_blocked, id=4076]
  0x16b9ff00 JavaThread "Reference Handler" daemon [_thread_blocked, id=2760]
  0x00037c00 JavaThread "main" [_thread_blocked, id=2936]

Other Threads:
  0x16b9d100 VMThread [id=576]
  0x17b2e400 WatcherThread [id=3512]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 4032K, used 170K [0x02980000, 0x02d80000, 0x02d80000)
  eden space 3968K,   4% used [0x02980000, 0x029aaba8, 0x02d60000)
  from space 64K,   0% used [0x02d70000, 0x02d70000, 0x02d80000)
  to   space 64K,   0% used [0x02d60000, 0x02d60000, 0x02d70000)
 concurrent mark-sweep generation total 258048K, used 2131K [0x02d80000, 0x12980000, 0x12980000)
 concurrent-mark-sweep perm gen total 15184K, used 12435K [0x12980000, 0x13854000, 0x16980000)

Dynamic libraries:
0x00400000 - 0x00415000 	C:\Program Files\Java\jdk1.6.0\jre\bin\java.exe
0x7c900000 - 0x7c9b0000 	C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c8f4000 	C:\WINDOWS\system32\kernel32.dll
0x77dd0000 - 0x77e6b000 	C:\WINDOWS\system32\ADVAPI32.dll
0x77e70000 - 0x77f01000 	C:\WINDOWS\system32\RPCRT4.dll
0x6d590000 - 0x6d5e6000 	C:\Program Files\Java\jdk1.6.0\jre\bin\MSVCR71.dll
0x6d840000 - 0x6da69000 	C:\Program Files\Java\jdk1.6.0\jre\bin\client\jvm.dll
0x77d40000 - 0x77dd0000 	C:\WINDOWS\system32\USER32.dll
0x77f10000 - 0x77f57000 	C:\WINDOWS\system32\GDI32.dll
0x76b40000 - 0x76b6d000 	C:\WINDOWS\system32\WINMM.dll
0x6d510000 - 0x6d58b000 	C:\Program Files\Java\jdk1.6.0\jre\bin\MSVCP71.dll
0x6d2f0000 - 0x6d2f8000 	C:\Program Files\Java\jdk1.6.0\jre\bin\hpi.dll
0x76bf0000 - 0x76bfb000 	C:\WINDOWS\system32\PSAPI.DLL
0x6d7f0000 - 0x6d7fc000 	C:\Program Files\Java\jdk1.6.0\jre\bin\verify.dll
0x6d380000 - 0x6d3a1000 	C:\Program Files\Java\jdk1.6.0\jre\bin\java.dll
0x6d830000 - 0x6d83f000 	C:\Program Files\Java\jdk1.6.0\jre\bin\zip.dll
0x6d000000 - 0x6d1ba000 	C:\Program Files\Java\jdk1.6.0\jre\bin\awt.dll
0x73000000 - 0x73026000 	C:\WINDOWS\system32\WINSPOOL.DRV
0x77c10000 - 0x77c68000 	C:\WINDOWS\system32\msvcrt.dll
0x76390000 - 0x763ad000 	C:\WINDOWS\system32\IMM32.dll
0x774e0000 - 0x7761d000 	C:\WINDOWS\system32\ole32.dll
0x5ed00000 - 0x5edcc000 	C:\WINDOWS\system32\opengl32.dll
0x68b20000 - 0x68b40000 	C:\WINDOWS\system32\GLU32.dll
0x73760000 - 0x737a9000 	C:\WINDOWS\system32\DDRAW.dll
0x73bc0000 - 0x73bc6000 	C:\WINDOWS\system32\DCIMAN32.dll
0x5ad70000 - 0x5ada8000 	C:\WINDOWS\system32\uxtheme.dll
0x18470000 - 0x184a0000 	C:\PROGRA~1\COMMON~1\SYMANT~1\ANTISPAM\asOEHook.dll
0x7c000000 - 0x7c054000 	C:\WINDOWS\system32\MSVCR70.dll
0x74720000 - 0x7476b000 	C:\WINDOWS\system32\MSCTF.dll
0x7c9c0000 - 0x7d1d5000 	C:\WINDOWS\system32\shell32.dll
0x77f60000 - 0x77fd6000 	C:\WINDOWS\system32\SHLWAPI.dll
0x773d0000 - 0x774d2000 	C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll
0x5d090000 - 0x5d127000 	C:\WINDOWS\system32\comctl32.dll
0x6d290000 - 0x6d2e2000 	C:\Program Files\Java\jdk1.6.0\jre\bin\fontmanager.dll
0x69500000 - 0x69a3b000 	C:\WINDOWS\system32\nvoglnt.dll
0x6d5f0000 - 0x6d603000 	C:\Program Files\Java\jdk1.6.0\jre\bin\net.dll
0x71ab0000 - 0x71ac7000 	C:\WINDOWS\system32\WS2_32.dll
0x71aa0000 - 0x71aa8000 	C:\WINDOWS\system32\WS2HELP.dll
0x6d610000 - 0x6d619000 	C:\Program Files\Java\jdk1.6.0\jre\bin\nio.dll
0x19ba0000 - 0x19bb0000 	C:\WINDOWS\system32\ctagent.dll
0x6d1e0000 - 0x6d20f000 	C:\Program Files\Java\jdk1.6.0\jre\bin\cmm.dll
0x5b860000 - 0x5b8b4000 	C:\WINDOWS\system32\netapi32.dll
0x71b20000 - 0x71b32000 	C:\WINDOWS\system32\MPR.dll
0x75f60000 - 0x75f67000 	C:\WINDOWS\System32\drprov.dll
0x71c10000 - 0x71c1e000 	C:\WINDOWS\System32\ntlanman.dll
0x71cd0000 - 0x71ce7000 	C:\WINDOWS\System32\NETUI0.dll
0x71c90000 - 0x71cd0000 	C:\WINDOWS\System32\NETUI1.dll
0x71c80000 - 0x71c87000 	C:\WINDOWS\System32\NETRAP.dll
0x71bf0000 - 0x71c03000 	C:\WINDOWS\System32\SAMLIB.dll
0x75f70000 - 0x75f79000 	C:\WINDOWS\System32\davclnt.dll
0x77920000 - 0x77a13000 	C:\WINDOWS\system32\SETUPAPI.dll
0x77b40000 - 0x77b62000 	C:\WINDOWS\system32\appHelp.dll
0x76fd0000 - 0x7704f000 	C:\WINDOWS\system32\CLBCATQ.DLL
0x77050000 - 0x77115000 	C:\WINDOWS\system32\COMRes.dll
0x77120000 - 0x771ac000 	C:\WINDOWS\system32\OLEAUT32.dll
0x77c00000 - 0x77c08000 	C:\WINDOWS\system32\VERSION.dll
0x76980000 - 0x76988000 	C:\WINDOWS\system32\LINKINFO.dll
0x76990000 - 0x769b5000 	C:\WINDOWS\system32\ntshrui.dll
0x76b20000 - 0x76b31000 	C:\WINDOWS\system32\ATL.DLL
0x769c0000 - 0x76a73000 	C:\WINDOWS\system32\USERENV.dll
0x6d410000 - 0x6d42e000 	C:\Program Files\Java\jdk1.6.0\jre\bin\jpeg.dll
0x1a410000 - 0x1a457000 	C:\Java\Projects\Singularity\run\lib\jogl\jogl.dll


VM Arguments:
jvm_args: -Dsun.java2d.opengl=true -Dsun.java2d.noddraw=true -Xincgc -Xmx256m -Xms256m -Djava.library.path=./lib/jogl
java_command: singularity.Singularity
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.6.0\
CLASSPATH=C:\Program Files\Java\jre1.5.0_06\lib\ext\QTJava.zip
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\NVIDIA Corporation\Cg\bin;C:\Program Files\Subversion\bin;C:\Program Files\Java\jdk1.6.0\bin;
USERNAME=bjgil
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 10 Stepping 0, AuthenticAMD



---------------  S Y S T E M  ---------------

OS: Windows XP Build 2600 Service Pack 2

CPU:total 1 family 6, cmov, cx8, fxsr, mmx, sse, mmxext, 3dnowext, 3dnow

Memory: 4k page, physical 1048052k(312872k free), swap 2520136k(1643448k free)

vm_info: Java HotSpot(TM) Client VM (1.6.0-beta-b59g) for windows-x86, built on Feb  6 2006 15:20:01 by "java_re" with unknown MS VC++:1310

I think using the flip() method of the Buffer class may be a mistake. Try just uniformly calling rewind() and see if that changes anything.

Thanks Ken, rewinding did the trick. Any tips on why flipping the buffer is the wrong thing to do here?

Cheers,
Brett

When to rewind/flip/clear your buffer simply depends on which region of the BB you want to be ‘selected’.

Say you write at #4, then #7, then #6, with limit #30

If you flip it now, the region will be 0…5
If you rewind it, the region will be 0…29

Simply read the javadocs to see what method does what, then figure out which indices you need and pick the right one.

AND…

fb.put( float ) changes the position
fb.put(int, float) does NOT change the position, so flipping it makes little sense (before: pos=0, lim=?, after: pos=0, lim=0)

You don’t even have to rewind it! The position is probably already/still 0…

Thanks Riven, a case of RTFMMC (RTFM More Carefully) on my part :wink: