VBO not Displaying

Started by AberrantWolf, August 15, 2011, 01:07:01

Previous topic - Next topic

AberrantWolf

I'm sure that I missed something.

The following code compiles fine, and when it runs, I get no errors. All that happens is I see a white background (the clear color).

As I understand it, I should see one white triangle filling the lower-left half of the screen.

package lwjgltest;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;

/**
 *
 * @author Scott
 */
public class LwjglTest {

	/**
	 * @param args the command line arguments
	 */
	
	public void run() {
		try {
			Display.setDisplayMode(new DisplayMode(640, 480));
			Display.create();
		} catch (LWJGLException ex) {
			ex.printStackTrace();
			System.exit(0);
		}
		
		// init opengl here...
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		
		// create the vertex buffer data
		FloatBuffer vert_b = BufferUtils.createFloatBuffer(8);
		float[] verts = {
			-1.0f, -1.0f,
			1.0f, -1.0f,
			-1.0f, 1.0f,
			1.0f, 1.0f
		};
		vert_b.put(verts);
		
		//'buffer' here holds the one int of data that becomes the vram buffer's id
		IntBuffer buffers = BufferUtils.createIntBuffer(1);
		GL15.glGenBuffers(buffers);
		
		int v_buffer_id = buffers.get(0);
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, v_buffer_id);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vert_b, GL15.GL_STATIC_DRAW);
		
		//run loop
		while( !Display.isCloseRequested() ) {
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
			
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, v_buffer_id);
			GL20.glEnableVertexAttribArray(0);
			GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0L);
			
			GL11.glColor3f(1.0f, 1.0f, 1.0f);
			GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
			
			Display.update();
		}
		
		Display.destroy();
	}
	
	public static void main(String[] args) {
		LwjglTest test = new LwjglTest();
		test.run();
	}
}


My sincerest apologies for what I know is going to have been a stupid mistake. It's been a while since I did any VBOs in OpenGL, and now I'm trying to refresh my memory while also learning the java library for it.

The VBO tutorial on the wiki looks VERY unlike the vanilla OpenGL tutorials I've found online. So I'm trying to reconcile the two, and hopefully I can give back by updating the wiki's VBO tutorial to not have to use ARB extensions, etc.

Thanks in advance for any help.

CodeBunny

Where do you set the triangle's color? The default glColor is white, not black, so maybe that's the issue?

AberrantWolf

Oops, I had been setting the color to black. I just double-checked with a black clear color and I still don't see anything. :\ Let me go back and fix the code I posted...

AberrantWolf

Okay, I got things working by removing the GL20 lines and using some GL11.glVertexPointer action.

Here's the working demo thus far -- let me know if I'm doing something in a manner less than proper for modern OpenGL. (I know, I should be using shaders instead of letting the fixed-function do its thing; that comes next...)

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;

/**
 *
 * @author Scott
 */
public class LwjglTest {

	/**
	 * @param args the command line arguments
	 */
	
	public void run() {
		try {
			Display.setDisplayMode(new DisplayMode(640, 480));
			Display.create();
		} catch (LWJGLException ex) {
			ex.printStackTrace();
			System.exit(0);
		}
		
		// init opengl here...
		
		// vertex locations
		float[] verts = {
			-1.0f, -1.0f,
			1.0f, -1.0f,
			-1.0f, 1.0f,
			1.0f, 1.0f
		};
		
		// vertex indices
		int[] idxs = {
			0, 1, 2, 3
		};
		
		// create the vertex and index buffer data
		FloatBuffer vert_b = BufferUtils.createFloatBuffer(8);
		vert_b.put(verts).rewind();
		
		IntBuffer indices = BufferUtils.createIntBuffer(4);
		indices.put(idxs).rewind();
		
		//'buffer' here holds the one int of data that becomes the vram buffer's id
		IntBuffer buffers = BufferUtils.createIntBuffer(2);
		GL15.glGenBuffers(buffers);
		
		System.out.println("buffers: " + buffers.get(0) + " " + buffers.get(1));
		
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffers.get(0));
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vert_b, GL15.GL_STATIC_DRAW);
		
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, buffers.get(1));
		GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW);
		
		//run loop
		while( !Display.isCloseRequested() ) {
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
			
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffers.get(0));
			GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, buffers.get(1));
			GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
			
			GL11.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);
			GL11.glDrawElements(GL11.GL_TRIANGLE_STRIP, 3, GL11.GL_UNSIGNED_INT, 0);
			
			GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
			GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
			
			Display.update();
		}
		
		GL15.glDeleteBuffers(buffers);
		Display.destroy();
	}
	
	public static void main(String[] args) {
		LwjglTest test = new LwjglTest();
		test.run();
	}
}


I would like very much to use my experience to update the VBO tutorial on the main site, also. In fact, I would very much enjoy the challenge of getting a complete "Getting Started with LWJGL and modern OpenGL" series. Should I start a new thread to ask for permissions?