I am trying to create a program with LWJGL and OpenGL 3. However, when my code gets to the following line:
program = glCreateProgram();
My program exits with the following output:
Version.getVerson() => 3.1.2 build 29
glfwInit() => true
glfwCreateWindow() => long
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007faa30c86a67, pid=15863, tid=0x00007faa5a409700
#
# JRE version: Java(TM) SE Runtime Environment (8.0_131-b11) (build 1.8.0_131-b11)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [liblwjgl_opengl.so+0x43a67] Java_org_lwjgl_opengl_GL20_glCreateProgram+0x7
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/tague/IdeaProjects/Jaav/hs_err_pid15863.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.
#
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
I tried to add this line to my code, directly after my glfwMakeContextCurrent
:
System.out.println("glGetString(GL_VERSION) => " + glGetString(GL_VERSION));
But now, the program simply exits with a segfault there instead.
I am using the following GLFW window hints:
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // Hide window at the start.
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // Window is resizable.
Edit: Here's the complete program, excluding things that aren't being called.
Main.java:
package me.tague.jaav;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import java.nio.*;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
public class Main {
public long window;
private Mesh main;
// Run the actual LWJGL app.
public void run () {
System.out.println("Version.getVerson() => " + Version.getVersion());
init();
// System.out.println("glGetString(GL_VERSION) => " + glGetString(GL_VERSION)); // <=== ANOTHER SOURCE OF THE ERROR!!!
float[] mainVerts = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
main = new Mesh(mainVerts);
// ...
}
private void init () {
// Print GLFW errors to System.err.
GLFWErrorCallback.createPrint(System.err).set();
// Set up GLFW.
if (!glfwInit()) throw new IllegalStateException("glfwInit() => false");
else System.out.println("glfwInit() => true");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // Hide window at the start.
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // Window is resizable.
// Create the window.
window = glfwCreateWindow(1024, 768, "Hello!", NULL, NULL);
if (window == NULL) throw new RuntimeException("glfwCreateWindow() => NULL");
else System.out.println("glfwCreateWindow() => long");
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());
// Center the window
glfwSetWindowPos(
window,
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2
);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(-1);
glfwShowWindow(window);
}
public static void main(String[] args) {
new Main().run();
}
}
Mesh.java:
package me.tague.jaav;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import java.nio.FloatBuffer;
public class Mesh {
public Material material;
public Mesh(float[] vertices) {
// Set up a default, generic material.
material = new Material();
// ... some more stuff that isn't called yet.
}
}
Material.java:
package me.tague.jaav;
import java.io.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
public class Material {
private int program;
public Material () {
makeProgram("shaders/generic/vert.gl", "shaders/generic/frag.gl");
}
/**
* Make an OpenGL Program, given vertex and fragment source files.
* @param vert The filename for the vertex shader to use.
* @param frag The filename for the fragment shader to use.
* @return The ID for the loaded program.
*/
private void makeProgram(String vert, String frag) {
// Create the program in OpenGL.
program = glCreateProgram(); // <==== THE SOURCE OF THE ERROR !!!!!
// ... more stuff that never runs
}
}
I found a solution: I was missing GL.createCapabilities()
in my init
. This function is required before any gl* calls are made.