VBO example wont work

Started by shaddow, September 09, 2012, 14:09:55

Previous topic - Next topic

shaddow

Hi guys
i just tried the example from: http://lwjgl.org/wiki/index.php?title=The_Quad_with_DrawElements

When i execute this example, the window stays blue without showing any white quad. The same with the version that draws the quad with arrays instead of elements. I have no idea why the quad wont show. In order to verify that my laptop is actually capable, i tried the example from lwjgl:  org.lwjgl.test.opengl.VBOTest
With this test the object is drawn.

Does anyone have a hint on my problem?

delt0r

Perhaps some code would help... Not that you think you have typed/cut/paste but what is really there? Since the last time i ran examples they all worked...but that was a while ago?
If you want a plot read a book and leave Hollywood out of it.

shaddow

alright thats my example:
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
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;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.PixelFormat;

public class TheQuadExampleDrawElements {
	// Entry point for the application
	public static void main(String[] args) {
		new TheQuadExampleDrawElements();
	}
	
	// Setup variables
	private final String WINDOW_TITLE = "The Quad: glDrawElements";
	private final int WIDTH = 320;
	private final int HEIGHT = 240;
	// Quad variables
	private int vaoId = 0;
	private int vboId = 0;
	private int vboiId = 0;
	private int indicesCount = 0;
	
	public TheQuadExampleDrawElements() {
		// Initialize OpenGL (Display)
		this.setupOpenGL();
		
		this.setupQuad();
		
		while (!Display.isCloseRequested()) {
			// Do a single loop (logic/render)
			this.loopCycle();
			
			// Force a maximum FPS of about 60
			Display.sync(60);
			// Let the CPU synchronize with the GPU if GPU is tagging behind
			Display.update();
		}
		
		// Destroy OpenGL (Display)
		this.destroyOpenGL();
	}
	
	public void setupOpenGL() {
		// Setup an OpenGL context with API version 3.2
		try {
			PixelFormat pixelFormat = new PixelFormat();
			ContextAttribs contextAtrributes = new ContextAttribs(3, 2)
				.withProfileCore(true)
				.withForwardCompatible(true);
			
			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
			Display.setTitle(WINDOW_TITLE);
			Display.create(pixelFormat, contextAtrributes);
			
			GL11.glViewport(0, 0, WIDTH, HEIGHT);
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(-1);
		}
		
		// Setup an XNA like background color
		GL11.glClearColor(0.4f, 0.6f, 0.9f, 0f);
		
		// Map the internal OpenGL coordinate system to the entire screen
		GL11.glViewport(0, 0, WIDTH, HEIGHT);
	}
	
	public void setupQuad() {
		// Vertices, the order is not important.
		float[] vertices = {
				-0.5f, 0.5f, 0f,	// Left top			ID: 0
				-0.5f, -0.5f, 0f,	// Left bottom		ID: 1
				0.5f, -0.5f, 0f,	// Right bottom		ID: 2
				0.5f, 0.5f, 0f		// Right left		ID: 3
		};
		// Sending data to OpenGL requires the usage of (flipped) byte buffers
		FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
		verticesBuffer.put(vertices);
		verticesBuffer.flip();
		
		// OpenGL expects to draw vertices in counter clockwise order by default
		byte[] indices = {
				// Left bottom triangle
				0, 1, 2,
				// Right top triangle
				2, 3, 0
		};
		indicesCount = indices.length;
		ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
		indicesBuffer.put(indices);
		indicesBuffer.flip();
		
		// Create a new Vertex Array Object in memory and select it (bind)
		// A VAO can have up to 16 attributes (VBO's) assigned to it by default
		vaoId = GL30.glGenVertexArrays();
		GL30.glBindVertexArray(vaoId);
		
		// Create a new Vertex Buffer Object in memory and select it (bind)
		// A VBO is a collection of Vectors which in this case resemble the location of each vertex.
		vboId = GL15.glGenBuffers();
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);
		// Put the VBO in the attributes list at index 0
		GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
		// Deselect (bind to 0) the VBO
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
		
		// Deselect (bind to 0) the VAO
		GL30.glBindVertexArray(0);
		
		// Create a new VBO for the indices and select it (bind)
		vboiId = GL15.glGenBuffers();
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
		GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
		// Deselect (bind to 0) the VBO
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
	}
	
	public void loopCycle() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		
		// Bind to the VAO that has all the information about the vertices
		GL30.glBindVertexArray(vaoId);
		GL20.glEnableVertexAttribArray(0);
		
		// Bind to the index VBO that has all the information about the order of the vertices
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
		
		// Draw the vertices
		GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount, GL11.GL_UNSIGNED_INT, 0);
		
		// Put everything back to default (deselect)
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
		GL20.glDisableVertexAttribArray(0);
		GL30.glBindVertexArray(0);
	}
	
	public void destroyOpenGL() {		
		// Disable the VBO index from the VAO attributes list
		GL20.glDisableVertexAttribArray(0);
		
		// Delete the vertex VBO
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
		GL15.glDeleteBuffers(vboId);
		
		// Delete the index VBO
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
		GL15.glDeleteBuffers(vboiId);
		
		// Delete the VAO
		GL30.glBindVertexArray(0);
		GL30.glDeleteVertexArrays(vaoId);
		
		Display.destroy();
	}

}

delt0r

Well i ran it and it doesn't work here either. I don't have time to work out what is going on. But i added checkErrors everywhere and got nothing. Its does not quite look right to me. I think there needs to be some glEnableClientState calls. But i use the GL2.X VBO IIRC. The 3.X may need less calls.
If you want a plot read a book and leave Hollywood out of it.

shaddow

Well okay it was a pretty stupid mistake, my ubuntu had not yet installed the latest nvdia driver. Fixing this, fixed the problem.

I also got this example working: http://openglbook.com/the-book/chapter-3-index-buffer-objects-and-primitive-types/
But this book states its prerequisite is opengl4.0. My graphiccard only supports 3.2. Nonetheless it works.

Furthermore the example from the wiki confuses me. There are references to GL15, GL20 and GL30. Still, it works. But why is it, that different methods of different GL Versions can be called among each other? How can I distinct between supported calls and unsupported ones? Actually my assumption was, that if i tried to make a GL30 call, than I would need to use only methods from GL30, for there are fewer methods in GL30 than in GL15. This switching between the versions really confuses me. Can someone enlighten me?

delt0r

So i need to update my drivers too eh.

A lot of things in older versions are kept in the next version. Only some things are deprecated. I do believe that lwjgl keeps the method in the first version that the method was in the core version. 

I use static imports and it gets rid of the clutter.

If you want a plot read a book and leave Hollywood out of it.