JVM crash trying to use glDrawArrays

Started by avm1979, November 17, 2009, 03:23:25

Previous topic - Next topic

avm1979

I'm trying to use glDrawArrays and not having much luck.  Found some example code that uses LWJGL (and works), but I still can't get it to work in my game.  So I've started paring it down to the bare minimum in hopes of isolating the problem, and to my surprise got to a pretty small amount of code that STILL crashes the JVM.  I can't for the life of me see what I'm missing here, would really appreciate someone looking at it and pointing me in the right direction.

Here's the code I've got:

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

public class VertexArrayTest {

	public static void main(String[] args) throws LWJGLException {
		setDisplayMode();
		Display.setTitle("Vertex Array Test");
		Display.setFullscreen(false);
		Display.setVSyncEnabled(true);
		Display.create(new PixelFormat(32, 0, 24, 8, 0));
		Mouse.setGrabbed(false);

		GL11.glDisable(GL11.GL_TEXTURE_2D);
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glDisable(GL11.GL_LIGHTING);

		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		
		
		while (true) {
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GL11.glLoadIdentity();
			
			GL11.glMatrixMode(GL11.GL_PROJECTION); 
			GL11.glPushMatrix();
			GL11.glLoadIdentity();
			
			GL11.glOrtho(0, 1024, 0, 768, -1, 1);
			
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GL11.glPushMatrix();
			GL11.glLoadIdentity();
			GL11.glTranslatef(0.01f, 0.01f, 0);			
			
			renderVBO();
			
			GL11.glMatrixMode(GL11.GL_PROJECTION);
			GL11.glPopMatrix();
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GL11.glPopMatrix();
			
			if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
				break;
			}
			Display.update();
			
			try {
				Thread.sleep(3);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}		
	}

	private static void renderVBO() {
		FloatBuffer vertices = BufferUtils.createFloatBuffer(4 * 2);
		FloatBuffer colors = BufferUtils.createFloatBuffer(4 * 4);
		
		float [] v = new float [] {10,  10,
					 			  110, 200,
					 			  200, 200,
					 			  200, 110};
		float [] c = new float [] {1,1,1,1,
								   1,1,1,1,
								   1,1,1,1,
								   1,1,1,1};
		vertices.put(v);
		colors.put(c);

		int count = 4;
		
		vertices.flip();
		colors.flip();
		//GLUtils.setColor(new Color(255,255,255,255));
		
		GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
		GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
		
		GL11.glVertexPointer(2, GL11.GL_FLOAT, vertices);
		GL11.glColorPointer(4, GL11.GL_FLOAT, colors);
		
		GL11.glDrawArrays(GL11.GL_QUADS, 0, count);
		
		GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
		GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
		
	}
	
	private static boolean setDisplayMode() 
	{
		try {
			// get modes
			DisplayMode[] dm = org.lwjgl.util.Display.getAvailableDisplayModes(1024, 768, -1, -1, -1, -1, 60, 60);
	
			org.lwjgl.util.Display.setDisplayMode(dm, new String[] {
					"width=" + 1024,
					"height=" + 768,
					"freq=" + 60,
					"bpp=" + org.lwjgl.opengl.Display.getDisplayMode().getBitsPerPixel()
	         }); 
	      	return true;
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	
		return false;
	}
}


broumbroum

projective matrix musn't be reloaded each frame :
/** set up the proj mx */		
			GL11.glMatrixMode(GL11.GL_PROJECTION); 
			GL11.glLoadIdentity();
			GL11.glOrtho(0, 1024, 0, 768, -1, 1);
			/** then loop */				
		while (true) {
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);		
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GL11.glLoadIdentity(); /** here's ok with that single call */
			GL11.glTranslatef(0.01f, 0.01f, 0);			
			
			renderVBO();
			/** upadate and close the loop*/
                        Display....(); Thread....();
/**	this is unefficient !		GL11.glMatrixMode(GL11.GL_PROJECTION);
			GL11.glPopMatrix();
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GL11.glPopMatrix();*/

avm1979

Quote from: broumbroum on November 17, 2009, 13:00:19
projective matrix musn't be reloaded each frame :

Fair enough, I was pasting from code where the call to glOrtho has variable params.  But I'm pretty sure that's not causing the crash :)


What about the vertex array code?

Edit:  Never mind, figured it out.  In this call
GL11.glVertexPointer(2, GL11.GL_FLOAT, vertices);

The 2nd param is stride, not type.  I guess the type is just figured out based on the class of the last param.

That was 4 hours of my life well spent :)