Hello Guest

VBO not Displaying

  • 3 Replies
  • 3426 Views
VBO not Displaying
« on: August 15, 2011, 01:07:01 »
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.

Code: [Select]
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.
« Last Edit: August 15, 2011, 15:21:39 by AberrantWolf »

Re: VBO not Displaying
« Reply #1 on: August 15, 2011, 12:09:54 »
Where do you set the triangle's color? The default glColor is white, not black, so maybe that's the issue?

Re: VBO not Displaying
« Reply #2 on: August 15, 2011, 15:20:33 »
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...

Re: VBO not Displaying
« Reply #3 on: August 16, 2011, 06:09:37 »
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...)

Code: [Select]
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?