LWJGL Forum
Programming => OpenGL => Topic started by: jorrit on August 14, 2017, 13:23:20
-
Hi, I'm a new LWJGL user and I'm having trouble with glGenBuffers. I first started with the simple tutorial (the one that shows a red window) from the main site and that works fine. However, then I wanted to extend it to draw a triangle but my program now crashes in GL15.glGenBuffers():
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007fee6321f1d, pid=4320, tid=10784
#
# JRE version: Java(TM) SE Runtime Environment (8.0_91-b14) (build 1.8.0_91-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.91-b14 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [lwjgl_opengl.dll+0x11f1d]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Personal\projects\Orbis\hs_err_pid4320.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
My code:
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.system.MemoryStack;
import java.nio.IntBuffer;
import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.system.MemoryUtil.NULL;
public class Main {
private long window;
int vertexBuffer;
static final float DATA[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
private void run() {
init();
loop();
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
glfwTerminate();
glfwSetErrorCallback(null).free();
}
private void init() {
GLFWErrorCallback.createPrint(System.err).set();
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(300, 300, "Hello World", NULL, NULL);
if (window == NULL) {
throw new RuntimeException("Failed to create the GLFW window");
}
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
glfwSetWindowShouldClose(window, true);
}
});
try (MemoryStack stack = stackPush() ) {
IntBuffer pWidth = stack.mallocInt(1);
IntBuffer pHeight = stack.mallocInt(1);
glfwGetWindowSize(window, pWidth, pHeight);
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window,
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwShowWindow(window);
setupGeometry();
}
private void setupGeometry() {
vertexBuffer = GL15.glGenBuffers(); // <----- CRASH HERE
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, DATA, GL15.GL_STATIC_DRAW);
}
private void loop() {
GL.createCapabilities();
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawTriangles();
glfwSwapBuffers(window);
glfwPollEvents();
}
}
private void drawTriangles() {
GL20.glEnableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer);
GL20.glVertexAttribPointer(
0,
3,
GL_FLOAT,
false,
0,
0L);
glDrawArrays(GL_TRIANGLES, 0, 3);
GL20.glDisableVertexAttribArray(0);
}
public static void main(String[] args) {
new Main().run();
}
}
This is on Windows btw. Any idea what could be wrong?
Thanks!
-
You are using OpenGL functions (in setupGeometry() called from init()) before calling GL.createCapabilities().
You must only use OpenGL functions after calling glfwMakeContextCurrent() as well as GL.createCapabilities().
-
Oh thank you! I'll try that out.
Yes. It works! Thanks you