我正在尝试编写一个可以显示三角形的简单程序,我无法弄清楚导致空指针异常的原因。
以下是代码:
public class Cut {
private static long window;
public static final long FRAME_RENDER_TIME_TARGET = 1000 / 60;
private static final float[] tridata = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
public static void main(String[] args) {
ResourceLoader.init();
System.out.println("If you started this from the command line it probably crashed.\nRerun it with the JVM argument \"-XstartOnFirstThread\"");
glInit();
loop();
glDeinit();
}
private static void glInit() {
GLFWErrorCallback.createPrint(System.err).set();
System.out.println("GLFW version: " + GLFW.glfwGetVersionString());
if (!GLFW.glfwInit()) {
System.err.println("Unable to initialize GLFW");
System.exit(-1);
}
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 4);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 1);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
window = GLFW.glfwCreateWindow(1024, 768, "Cut", NULL, NULL);
if (window == NULL) {
System.err.println("Unable to create OpenGL window");
GLFW.glfwTerminate();
System.exit(-1);
}
GLFW.glfwMakeContextCurrent(window);
//Enable v-sync
GLFW.glfwSwapInterval(1);
//GL.createCapabilities();
GLFW.glfwShowWindow(window);
GL.createCapabilities();
System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));
}
private static void glDeinit() {
GLFW.glfwTerminate();
}
private static void loop() {
int vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
int vbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
FloatBuffer buffer = FloatBuffer.wrap(tridata);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL20.glEnableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
while (!GLFW.glfwWindowShouldClose(window)) {
update();
render();
}
}
private static void render() {
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents();
}
private static void update() {
}
}
这是错误消息:
Exception in thread "main" java.lang.NullPointerException
at org.lwjglx.debug.opengl.GL30.glGenVertexArrays(GL30.java:107)
at cut.Cut.render(Cut.java:83)
at cut.Cut.loop(Cut.java:76)
at cut.Cut.main(Cut.java:31)
我正在使用macOS 10.13和Java 8.我知道macOS因OpenGL问题而闻名。我使用VM参数“-XstartOnFirstThread”。