Can't get a simple Vertex Array to work!

So I have a very simple renderer that uses a display list. I am trying to convert it to use a VBO. The step before a VBO is doing a vertex array (upload every frame). I can not get that to work. I’ve been trying everything I can think of for like an hour and got nowhere.

Here’s the old displaylist code:


public final class RenderReticle<Void> extends Renderer<Void> {
	public static final RenderReticle INSTANCE = new RenderReticle();
	
	private static final float size = 25f;
	private static final float halfSize = size / 2;
	
	private DisplayList displayList;
	private Texture texture;
	
	private RenderReticle() {
		
	}
	
	@Override
	public void init() {
		this.displayList = new DisplayList();
		this.displayList.startDrawing();
		
		glBegin(GL_QUADS);
		glTexCoord2f(0f, 0f); glVertex2f(0.0f, 0.0f);
		glTexCoord2f(0f, 1f); glVertex2f(0.0f, size);
		glTexCoord2f(1f, 1f); glVertex2f(size, size);
		glTexCoord2f(1f, 0f); glVertex2f(size, 0.0f);
		glEnd();

		this.displayList.stopDrawing();
		
		this.texture = Texture.get("reticle");
	}
	
	@Override 
	public void render(final Void object) {
		gluSet2D();
		
		glTranslatef(Window.instance().getWidth() / 2 - halfSize, Window.instance().getHeight() / 2 - halfSize, 0);
		
		this.texture.bind();
		this.displayList.call();
		
		gluSet3D();
	} 
}

And here’s the new vertex array code:


public final class RenderReticle<Void> extends Renderer<Void> {
	public static final RenderReticle INSTANCE = new RenderReticle();
	
	private static final float size = 1f;
	private static final float halfSize = size / 2;
	
	private Texture texture;
	
	private FloatBuffer vBuffer;
	private FloatBuffer tBuffer;

	private RenderReticle() {
		
	}
	
	@Override
	public void init() {
		this.vBuffer = BufferUtils.createFloatBuffer(8);
		this.vBuffer.put(0).put(0);
		this.vBuffer.put(0).put(size);
		this.vBuffer.put(size).put(size);
		this.vBuffer.put(size).put(0);
		this.vBuffer.flip();
		
		this.tBuffer = BufferUtils.createFloatBuffer(8);
		this.tBuffer.put(0).put(0);
		this.tBuffer.put(0).put(1);
		this.tBuffer.put(1).put(1);
		this.tBuffer.put(1).put(0);
		this.tBuffer.flip();
		
		this.texture = Texture.get("reticle");
	}
	
	@Override 
	public void render(final Void object) {
		gluSet2D();
		
		glTranslatef(Window.instance().getWidth() / 2 - halfSize, Window.instance().getHeight() / 2 - halfSize, 0);
		
		this.texture.bind();

		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	
		glVertexPointer(4, GL_FLOAT, 2 << 2, this.vBuffer);
		glTexCoordPointer(4, GL_FLOAT, 2 << 2, this.tBuffer);
		glDrawArrays(GL_QUADS, 0, 4);
		
		glDisableClientState(GL_VERTEX_ARRAY);
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		
		gluSet3D();
	} 
}

“gluSet2D” and “gluSet3D” are functions I wrote. They basically just do glOrtho and the perspective matrix respectively.

So, how come the first one works but the second one doesn’t? (It just displays nothing on the screen).

You are doing everything right, except that your vertex has only 2 components, not 4. So use a 2 as the first argument to glVertexPointer and glTexCoordPointer.

EDIT: Btw.: this is not an error, but specifying the stride (third argument to glVertexPointer/glTexCoordPointer) with “2<<2” (which produces the correct result 8 ) is a bit non-intuitive. You could use “4 * 2” or “2 * 4” meaning (2 floats, each taking 4 bytes). That would be a bit better to read/understand.
But since you are not using interleaved vertex arrays, you can just as well leave that third parameter 0.

Oh, I thought the first parameter was the number of vertices. Doh.

EDIT: nevermind it works, I’m just dumb.

@KaiHH: I prefer <<2 over *4, as it immediately shows we are bitshifting from ints/floats to bytes, whereas *4 can have any meaning (are we working with an attribute with 4 elements?). IMHO it helps to create this convention of using bit shifts to go from one primitive width to another.