I am playing around with OpenGL and LWJGL. I have the new OpenGL Programming Guide book, and I wanted to see if I could port the first example to Java. However, I am getting a weird error. The first time glDrawArrays is called, OpenGL has an out of memory error.
I am not sure if I have something incorrectly setup in my init function. I am pretty sure my shaders are compiling fine. I am using a slight alteration of the ShaderUtil class on the wiki (
http://www.lwjgl.org/wiki/index.php?title=GLSL_Utility_Class&oldid=908).
I am not sure how to debug from this point. I think I might be misunderstanding how Java handles the FloatBuffers and stuff. But so far I have not been able to determine what I am doing wrong. Can anyone help point me in the right direction? Here is my code.
package org.opengl.chapter1;
import java.nio.FloatBuffer;
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;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.Util;
public class TriangleExample {
private int vao;
private float verticesData[] = {
-0.90f, -0.90f, // Triangle 1
0.85f, -0.90f,
-0.90f, 0.85f,
0.90f, -0.85f, // Triangle 2
0.90f, 0.90f,
-0.85f, 0.90f
};
private static final int NUM_VERTICES = 6;
private static final int VERT_SIZE = 2;
public void init()
{
vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
FloatBuffer vertices = BufferUtils.createFloatBuffer(NUM_VERTICES * VERT_SIZE);
vertices.put(verticesData);
int buffer = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertices, GL15.GL_STATIC_DRAW);
int program = ShaderUtils.createProgram("shaders/triangles.vert", "shaders/triangles.frag");
if (program == -1)
System.exit(1);
GL20.glUseProgram(program);
GL20.glVertexAttribPointer(0, VERT_SIZE, GL11.GL_FLOAT, false, 0, 0);
GL20.glEnableVertexAttribArray(0);
}
public void display()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL30.glBindVertexArray(vao);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, NUM_VERTICES);
Util.checkGLError();
GL11.glFlush();
}
public TriangleExample()
{
try {
Display.setDisplayMode(new DisplayMode(512, 512));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
init();
while (!Display.isCloseRequested()) {
display();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void main(String args[])
{
new TriangleExample();
}
}
EDIT: The solution was the call flip() on my FloatBuffer. I need to read up on these Buffers because I am really not sure exactly what they are doing.