Thanks, matanui159!
I have learned to used VBO and have successfully drawn a triangle.
Here's my code:
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 java.nio.FloatBuffer;
public class VBO {
public static void main(String[] args) {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.setTitle("VBO");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(0);
}
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, Display.getWidth(), 0, Display.getHeight(), 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
final int numVertices = 3;
final int sizeVertex = 2;
final int sizeColor = 3;
FloatBuffer vertexData = BufferUtils.createFloatBuffer(numVertices * sizeVertex);
vertexData.put(new float[]{200f, 200f, 400f, 400f, 400f, 200f});
vertexData.flip();
FloatBuffer colorData = BufferUtils.createFloatBuffer(numVertices * sizeColor);
colorData.put(new float[]{1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f});
colorData.flip();
int vboVertexHandle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexData, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
int vboColorHandle = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboColorHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorData, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
while (!Display.isCloseRequested()) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
GL11.glVertexPointer(sizeVertex, GL11.GL_FLOAT, 0, 0L);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboColorHandle);
GL11.glColorPointer(sizeColor, GL11.GL_FLOAT, 0, 0L);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, numVertices);
GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
Display.update();
Display.sync(60);
}
GL15.glDeleteBuffers(vboVertexHandle);
GL15.glDeleteBuffers(vboColorHandle);
Display.destroy();
System.exit(0);
}
}
But when I try to replace it with this, I can't see any points.
final int numVertices = 1;
final int sizeVertex = 2;
final int sizeColor = 3;
FloatBuffer vertexData = BufferUtils.createFloatBuffer(numVertices * sizeVertex);
vertexData.put(new float[]{200f, 200f});
vertexData.flip();
FloatBuffer colorData = BufferUtils.createFloatBuffer(numVertices * sizeColor);
colorData.put(new float[]{1f, 0f, 0f});
colorData.flip();
I suspect it might be trying to draw something of size zero but I'm not sure how to fix that.