Vertex arrays - can't get them to work!

Yeah, hi. I’m having some troubble getting vertex arrays to work. I ran some simpler tests that worked but when I try to load an obj-file and render it the program crashes without a hint of what is wrong. (This is for an assignment but i really can’t figgure out how to do this, and besides, it’s only the very top of the iceberg. sigh)

Test class:

public class Test01 implements GLEventListener {

	private JFrame javaWindow;
    private GLCanvas glCanvas;
    private Animator a;
    private Model test;

    /**
     * @param width
     * @param height
     */
    public Test01( int width, int height ) {
        javaWindow = new JFrame();
        javaWindow.setSize( width, height );
        javaWindow.setLocation( 50, 50 );
        javaWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        glCanvas = new GLCanvas();

        // Makes this GLEventListener listen to the events sent by the glCanvas (ie glCanvas.display() or events sent
        // when resizing the window)
        glCanvas.addGLEventListener( this );

        // Add the canvas to the window
        javaWindow.getContentPane().add( glCanvas );

        // Show the window!
        javaWindow.setVisible( true );
        
        a = new Animator(glCanvas);
    }

    /**
     * Starts the main loop
     * 
     */
    public void start() 
    {
        a.start();
    }

	public void init( GLAutoDrawable arg0 ) 
	{
    	System.out.println( "Init event!" );
        final GL gl = glCanvas.getGL();
        gl.glClearDepth( 1.0f ); // Depth Buffer Setup
        gl.glEnable( GL.GL_DEPTH_TEST ); // Enables Depth Testing
        gl.glDepthFunc( GL.GL_LEQUAL );
        gl.glClearColor( 0.2f, 0.2f, 0.37f, 1.0f );
        gl.glShadeModel( GL.GL_SMOOTH );
        
        test = ObjectLoader.loadOBJ("data\\meshes\\cube.obj");
	}

	public void display( GLAutoDrawable arg0 ) 
	{
    	final GL gl = glCanvas.getGL();
        gl.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );	// Clear the color buffer (frame buffer) and
        																// depth buffer (Z-buffer)
        gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
        
        test.draw(gl);
        
        gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
        
        gl.glFlush();
	}

	public void displayChanged( GLAutoDrawable arg0, boolean arg1, boolean arg2 ) 
	{
	}

	public void reshape( GLAutoDrawable arg0, int x, int y, int width, int height ) 
	{
    	System.out.println( "Reshape event! Setting viewport and projection" );
        final GL gl = glCanvas.getGL();
        final GLU glu = new GLU();
        if ( height <= 0 ) // avoid a divide by zero error!
        	height = 1;
        final float h = (float) width / (float) height;
        gl.glViewport( 0, 0, width, height );
        gl.glMatrixMode( GL.GL_PROJECTION );
        gl.glLoadIdentity();
        glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
        gl.glMatrixMode( GL.GL_MODELVIEW );
        gl.glLoadIdentity();
	}

	public static void main( String[] args ) 
	{
    	Test01 main = new Test01( 640, 480 );
        main.start();
	}

}

The model class:


public class Model 
{
	
//	private boolean hasNormals, hasTexture;
//	private Material material;
	private FloatBuffer faces; //, colors, normals, texCords;
	
	public Model(FloatBuffer faces)
	{
		this.faces = faces;
	}
	
	public void draw(GL gl)
	{
		gl.glVertexPointer(3, GL.GL_TRIANGLES, 0, faces);
		
		gl.glDrawArrays(GL.GL_TRIANGLES, 0, (faces.capacity() / 3));
	}
	
	public FloatBuffer getFaces() 
	{
		return faces;
	}
}

And the crash info i suppose:


# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x037955f2, pid=3044, tid=2560
#
# Java VM: Java HotSpot(TM) Client VM (1.6.0_03-b05 mixed mode, sharing)
# Problematic frame:
# C  0x037955f2
#

******

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

OS: Windows XP Build 2600 Service Pack 2

CPU:total 1 (1 cores per cpu, 1 threads per core) family 15 model 2 stepping 7, cmov, cx8, fxsr, mmx, sse, sse2

Memory: 4k page, physical 785904k(193428k free), swap 1136468k(598216k free)

vm_info: Java HotSpot(TM) Client VM (1.6.0_03-b05) for windows-x86, built on Sep 24 2007 22:24:33 by "java_re" with unknown MS VC++:1310

(Had to cut out some stuff to get below 10000 characters.)
I’ve also tried to get VBOs to work but I get the same (or at least a strikingly similar) problem. Any help will be appreciated.

usually somewhere down in the error dump it says which dll (on windows) caused the crash. probably one that belongs to the graphics card driver.

Hmm. Well, here is the whole thing:


# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x037955f2, pid=3044, tid=2560
#
# Java VM: Java HotSpot(TM) Client VM (1.6.0_03-b05 mixed mode, sharing)
# Problematic frame:
# C  0x037955f2
#
# 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 (0x030b3400):  JavaThread "AWT-EventQueue-0" [_thread_in_native, id=2560]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000000

Registers:
EAX=0x04621088, EBX=0x043a0140, ECX=0x00000000, EDX=0x00000024
ESP=0x0304f5b4, EBP=0x0304f5c0, ESI=0x00000000, EDI=0x00000000
EIP=0x037955f2, EFLAGS=0x00010246

Top of Stack: (sp=0x0304f5b4)
0x0304f5b4:   00000024 043a0140 00000000 00000004
0x0304f5c4:   69769943 043a0140 04621084 00000000
0x0304f5d4:   00000024 030b3400 043a0140 0304f618
0x0304f5e4:   26b06170 043a93f8 037955c0 695f1017
0x0304f5f4:   00000000 0000007f 00000000 00000024
0x0304f604:   26b06170 10003ff5 00000004 00000000
0x0304f614:   00000024 0304f658 0095a430 030b34ec
0x0304f624:   0304f674 00000004 00000000 00000024 

Instructions: (pc=0x037955f2)
0x037955e2:   8b 35 ec 98 3a 04 8b 76 04 8b f9 c1 e7 04 03 f7
0x037955f2:   8b 3e 8b 6e 04 89 38 89 68 04 8b 7e 08 8b 6e 0c 


Stack: [0x03000000,0x03050000),  sp=0x0304f5b4,  free space=317k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  0x037955f2

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.sun.opengl.impl.GLImpl.glDrawArrays(III)V+0
j  model.Model.draw(Ljavax/media/opengl/GL;)V+25
j  test.Test01.display(Ljavax/media/opengl/GLAutoDrawable;)V+30
j  com.sun.opengl.impl.GLDrawableHelper.display(Ljavax/media/opengl/GLAutoDrawable;)V+29
j  javax.media.opengl.GLCanvas$DisplayAction.run()V+80
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.GLCanvas$DisplayOnEventDispatchThreadAction.run()V+35
j  java.awt.event.InvocationEvent.dispatch()V+11
j  java.awt.EventQueue.dispatchEvent(Ljava/awt/AWTEvent;)V+26
j  java.awt.EventDispatchThread.pumpOneEventForFilters(I)Z+156
j  java.awt.EventDispatchThread.pumpEventsForFilter(ILjava/awt/Conditional;Ljava/awt/EventFilter;)V+30
j  java.awt.EventDispatchThread.pumpEventsForHierarchy(ILjava/awt/Conditional;Ljava/awt/Component;)V+11
j  java.awt.EventDispatchThread.pumpEvents(ILjava/awt/Conditional;)V+4
j  java.awt.EventDispatchThread.pumpEvents(Ljava/awt/Conditional;)V+3
j  java.awt.EventDispatchThread.run()V+9
v  ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
  0x003f6000 JavaThread "DestroyJavaVM" [_thread_blocked, id=2980]
  0x030f0c00 JavaThread "Thread-2" [_thread_blocked, id=2556]
=>0x030b3400 JavaThread "AWT-EventQueue-0" [_thread_in_native, id=2560]
  0x02b53800 JavaThread "AWT-Windows" daemon [_thread_in_native, id=2564]
  0x02b52c00 JavaThread "AWT-Shutdown" [_thread_blocked, id=2576]
  0x02b25c00 JavaThread "Java2D Disposer" daemon [_thread_blocked, id=876]
  0x02abcc00 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=3172]
  0x02ab8000 JavaThread "CompilerThread0" daemon [_thread_blocked, id=2936]
  0x02ab6c00 JavaThread "Attach Listener" daemon [_thread_blocked, id=2968]
  0x02ab6000 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=3000]
  0x02ab1400 JavaThread "Finalizer" daemon [_thread_blocked, id=1060]
  0x02aad000 JavaThread "Reference Handler" daemon [_thread_blocked, id=516]

Other Threads:
  0x02aabc00 VMThread [id=2844]
  0x02ad7400 WatcherThread [id=872]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 960K, used 850K [0x229d0000, 0x22ad0000, 0x22eb0000)
  eden space 896K,  89% used [0x229d0000, 0x22a97b58, 0x22ab0000)
  from space 64K,  81% used [0x22ac0000, 0x22acd078, 0x22ad0000)
  to   space 64K,   0% used [0x22ab0000, 0x22ab0000, 0x22ac0000)
 tenured generation   total 4096K, used 414K [0x22eb0000, 0x232b0000, 0x269d0000)
   the space 4096K,  10% used [0x22eb0000, 0x22f179c8, 0x22f17a00, 0x232b0000)
 compacting perm gen  total 12288K, used 2600K [0x269d0000, 0x275d0000, 0x2a9d0000)
   the space 12288K,  21% used [0x269d0000, 0x26c5a0c8, 0x26c5a200, 0x275d0000)
    ro space 8192K,  62% used [0x2a9d0000, 0x2aed14a8, 0x2aed1600, 0x2b1d0000)
    rw space 12288K,  52% used [0x2b1d0000, 0x2b817278, 0x2b817400, 0x2bdd0000)

Dynamic libraries:
0x00400000 - 0x00423000 	C:\Program\Java\jre1.6.0_03\bin\javaw.exe
0x7c900000 - 0x7c9b2000 	C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c8f9000 	C:\WINDOWS\system32\kernel32.dll
0x77dc0000 - 0x77e6b000 	C:\WINDOWS\system32\ADVAPI32.dll
0x77e70000 - 0x77f02000 	C:\WINDOWS\system32\RPCRT4.dll
0x77fe0000 - 0x77ff1000 	C:\WINDOWS\system32\Secur32.dll
0x7e360000 - 0x7e3f0000 	C:\WINDOWS\system32\USER32.dll
0x77f10000 - 0x77f57000 	C:\WINDOWS\system32\GDI32.dll
0x76370000 - 0x7638d000 	C:\WINDOWS\system32\IMM32.DLL
0x62f00000 - 0x62f09000 	C:\WINDOWS\system32\LPK.DLL
0x75530000 - 0x7559b000 	C:\WINDOWS\system32\USP10.dll
0x77c00000 - 0x77c58000 	C:\WINDOWS\system32\msvcrt.dll
0x7c340000 - 0x7c396000 	C:\Program\Java\jre1.6.0_03\bin\msvcr71.dll
0x6d7c0000 - 0x6da0a000 	C:\Program\Java\jre1.6.0_03\bin\client\jvm.dll
0x76b30000 - 0x76b5e000 	C:\WINDOWS\system32\WINMM.dll
0x6d310000 - 0x6d318000 	C:\Program\Java\jre1.6.0_03\bin\hpi.dll
0x76be0000 - 0x76beb000 	C:\WINDOWS\system32\PSAPI.DLL
0x6d770000 - 0x6d77c000 	C:\Program\Java\jre1.6.0_03\bin\verify.dll
0x6d3b0000 - 0x6d3cf000 	C:\Program\Java\jre1.6.0_03\bin\java.dll
0x6d7b0000 - 0x6d7bf000 	C:\Program\Java\jre1.6.0_03\bin\zip.dll
0x6d000000 - 0x6d1c3000 	C:\Program\Java\jre1.6.0_03\bin\awt.dll
0x72fd0000 - 0x72ff6000 	C:\WINDOWS\system32\WINSPOOL.DRV
0x774d0000 - 0x7760d000 	C:\WINDOWS\system32\ole32.dll
0x5b270000 - 0x5b2a8000 	C:\WINDOWS\SYSTEM32\uxtheme.dll
0x73730000 - 0x73779000 	C:\WINDOWS\system32\ddraw.dll
0x73b90000 - 0x73b96000 	C:\WINDOWS\system32\DCIMAN32.dll
0x6d2b0000 - 0x6d303000 	C:\Program\Java\jre1.6.0_03\bin\fontmanager.dll
0x746f0000 - 0x7473b000 	C:\WINDOWS\system32\MSCTF.dll
0x751a0000 - 0x751ce000 	C:\WINDOWS\system32\msctfime.ime
0x7c9c0000 - 0x7d1d9000 	C:\WINDOWS\system32\shell32.dll
0x77f60000 - 0x77fd6000 	C:\WINDOWS\system32\SHLWAPI.dll
0x773c0000 - 0x774c3000 	C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2982_x-ww_ac3f9c03\comctl32.dll
0x5d5b0000 - 0x5d64a000 	C:\WINDOWS\system32\comctl32.dll
0x10000000 - 0x1004d000 	C:\Program\Java\jogl-1.1.1-rc6-windows-i586\lib\jogl.dll
0x5f220000 - 0x5f2ec000 	C:\WINDOWS\system32\OPENGL32.dll
0x5ff90000 - 0x5ffb1000 	C:\WINDOWS\system32\GLU32.dll
0x6d3e0000 - 0x6d3e6000 	C:\Program\Java\jre1.6.0_03\bin\jawt.dll
0x03070000 - 0x03075000 	C:\Program\Java\jogl-1.1.1-rc6-windows-i586\lib\jogl_awt.dll
0x69500000 - 0x699f3000 	C:\WINDOWS\system32\nvoglnt.dll

VM Arguments:
jvm_args: -Djava.library.path=C:\Program\Java\jogl-1.1.1-rc6-windows-i586\lib;C:\Program\Java\jogl-1.1.1-rc6-windows-i586\lib
java_command: test.Test01
Launcher Type: SUN_STANDARD

Environment Variables:
PATH=C:\Program\Java\jre1.6.0_03\bin\client;C:\Program\Java\jre1.6.0_03\bin;C:\WINDOWS\SYSTEM32;C:\WINDOWS;C:\WINDOWS\SYSTEM32\WBEM;C:\Program\VDMSound\;C:\Program\QuickTime\QTSystem\
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 15 Model 2 Stepping 7, GenuineIntel



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

OS: Windows XP Build 2600 Service Pack 2

CPU:total 1 (1 cores per cpu, 1 threads per core) family 15 model 2 stepping 7, cmov, cx8, fxsr, mmx, sse, sse2

Memory: 4k page, physical 785904k(193428k free), swap 1136468k(598216k free)

vm_info: Java HotSpot(TM) Client VM (1.6.0_03-b05) for windows-x86, built on Sep 24 2007 22:24:33 by "java_re" with unknown MS VC++:1310


I’m not very experienced with things like this so…

Edit: My graphics-card is a GeForce4 Ti 4200 by the way, if that helps.

Make sure you called rewind() on your buffer after setting its content. You should also BufferUtils to allocate the FloatBuffer to make sure it’s a direct buffer.

Maybe I should have included the code for the object loader… well anyway, that shouldn’t be it. I used BufferUtils to make the buffer with the method newFloatBuffer and after putting in the coordinates I rewind the buffer.

The object loader (incomplete):


public class ObjectLoader 
{
	private static BufferedReader inputStream;
	
	private static int 	vertexCount, 
						normalCount, 
						texCordCount,
						groupCount,
						faceCount;
	
	public static Model loadOBJ(String path)
	{
		count(path);
		
		float[] vertices = new float[vertexCount],
				normals = new float[normalCount],
				texCords = new float[texCordCount];
		
		int v_i = 0, n_i = 0, t_i = 0,
			temp;
		
		FloatBuffer faces = BufferUtil.newFloatBuffer(faceCount);
		
		String[] p; // parse
		
		try 
		{
			inputStream = new BufferedReader(new FileReader(path));
			
			try
			{
				while (true) 
				{
					p = inputStream.readLine().split("\\s+");
					
					if(p.length > 0)
					{
		                if(p[0].equals("v"))
		                {
		                	vertices[v_i++] = Float.valueOf(p[1]);
		                	vertices[v_i++] = Float.valueOf(p[2]);
		                	vertices[v_i++] = Float.valueOf(p[3]);
		                }
		                else if(p[0].equals("vn"))
		                {
		                	normals[n_i++] = Float.valueOf(p[1]);
		                	normals[n_i++] = Float.valueOf(p[2]);
		                	normals[n_i++] = Float.valueOf(p[3]);
		                }
		                else if(p[0].equals("vt"))
		                {
		                	texCords[t_i++] = Float.valueOf(p[1]);
		                	texCords[t_i++] = Float.valueOf(p[2]);
		                }
		                else if(p[0].equals("f"))
		                {
		                	temp = (int)(Double.valueOf(p[1]) * 3) - 3;
		                	
		                	faces.put(vertices[temp]);
		                	faces.put(vertices[temp + 1]);
		                	faces.put(vertices[temp + 2]);
		                	
		                	temp = (int)(Double.valueOf(p[2]) * 3) - 3;
		                	
		                	faces.put(vertices[temp]);
		                	faces.put(vertices[temp + 1]);
		                	faces.put(vertices[temp + 2]);
		                	
		                	temp = (int)(Double.valueOf(p[3]) * 3) - 3;
		                	
		                	faces.put(vertices[temp]);
		                	faces.put(vertices[temp + 1]);
		                	faces.put(vertices[temp + 2]);
		                }
					}
				}
			}
			catch(NullPointerException e)
			{	}
			
			inputStream.close();
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			if (inputStream != null) 
			{
                try 
                {
					inputStream.close();
				} 
                catch (IOException e1) 
				{
					e1.printStackTrace();
				}
            }
		}
		
		faces.rewind();
		
		return new Model(faces);
	}
	
	private static void count(String path)
	{
		String[] p;
		
		BufferedReader tempInputStream = null;
		
		vertexCount = 0;
		normalCount = 0;
		texCordCount = 0;
		groupCount = 0;
		faceCount = 0;
		
		try 
		{
			tempInputStream = new BufferedReader(new FileReader(path));
			
			while (true) 
			{
				p = tempInputStream.readLine().split("\\s+");
				
//				for(int i = 0; i < p.length; i++)
//					System.out.print(p[i] + " ");
//				System.out.println();
				
				if(p.length > 0)
				{
	                if(p[0].equals("v"))
	                {
	                	vertexCount += 3;
	                }
	                else if(p[0].equals("vn"))
	                {
	                	normalCount += 3;
	                }
	                else if(p[0].equals("vt"))
	                {
	                	texCordCount += 3;
	                }
	                else if(p[0].equals("f"))
	                {
	                	faceCount += (p.length - 1) * 3;
	                }
				}
			}
		} 
		catch (FileNotFoundException e) 
		{	}
		catch (IOException e) 
		{
			if (tempInputStream != null) 
			{
                try 
                {
                	tempInputStream.close();
				} 
                catch (IOException e1) 
				{
					e1.printStackTrace();
				}
            }
		}
		catch(NullPointerException e)
		{	}
		
		try 
		{
			tempInputStream.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			System.out.println("Oh shi-!");
		}
	}
}

I’m actully a bit uncertain on the whole object loader part as well. I’d be happy for any advice you have to offer on that subject too…

The loader looks correct, although you’re it seems you’re ignoring the normals and texture coordinates after loading them into arrays.
Two suggestions:
always rewind the faces buffer before drawing since jogl might adjust the position after some calls, this may not do anything but it’s what my code does and that hasn’t crashed.
glVertexPointer’s first two arguments are the size of each vertex (ie number of individual coordinates, in this case 3, so you’re correct there), the second argument is vertex type, or the primitive type of the data storing the vertices. VertexPointer doesn’t care that they’re going to be used as GL_TRIANGLES, instead you should use GL_FLOAT since you’re using a FloatBuffer.

I think you’ve got it! The VertexPointer is naturally supposed to have a GL_FLOAT in there instead! I still don’t see anything but at least now it dosn’t crash. So it was that kind of mistake after all. ::slight_smile:
Anyway, thanks a lot for the help. :slight_smile: Maybe now I can move on with this thing. If I nead more help I’ll return.

I think your problem could easily be solved. Seriously.

Reduce the problem, until you find what causes it.

Try to render 1 triangle from a vertex-array, ditch all your fancy stuff (AKA uncertainties).