LWJGL代码中第一次gl *调用时的分段错误

时间:2017-07-08 15:50:04

标签: java opengl lwjgl glfw

我正在尝试使用LWJGL和OpenGL 3创建一个程序。但是,当我的代码到达以下行时:

program = glCreateProgram();

我的程序以下列输出退出:

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)

我尝试在glfwMakeContextCurrent

之后直接将此行添加到我的代码中
System.out.println("glGetString(GL_VERSION) => " + glGetString(GL_VERSION));

但现在,该程序只是以段落错误而退出

我正在使用以下GLFW窗口提示:

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.

编辑:这是完整的程序,不包括未被调用的内容。

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
    }    
}

1 个答案:

答案 0 :(得分:1)

我找到了一个解决方案:我在GL.createCapabilities()中遗漏了init。在进行任何gl *调用之前,此功能是必需的。