No context is current or a function that is not available in the current context was called. The JVM will abort execution. (lwjgl)

NicoHatProbleme2

Bekanntes Mitglied
Ich habe mich jetzt etwas mit lwjgl opengl beschäftigt und bekam diese Fehlermeldung. Ich weiß nicht, wie ich diesen beheben kann.

Java:
GL11.glBegin(GL11.GL_QUADS);

Ich nutze schon zuvor
Code:
GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();

Und der Bildschirm steht schon
 

httpdigest

Top Contributor
Und der Bildschirm steht schon
Das ist natürlich erstmal das wichtigste. Sonst sieht man ja nix. :)

Ansonsten hast du natürlich viel zu wenig Informationen gegeben, dass man dir noch nicht mal im Ansatz helfen könnte.
  • Welche OpenGL Version requestest du denn?! glBegin ist in einem >= 3.2 Core profile nicht mehr verfügbar
  • Rufst du glBegin in einem anderen Thread auf? (anders als der, in dem du den OpenGL Context gebunden hast.)
 

NicoHatProbleme2

Bekanntes Mitglied
Ich habe schon alles in einem Thread, doch es könnte sein, dass meine Version über 3.2 ist. Was könnte ich dann stattdessen nutzen?

(also .Version() gibt 3.3.2+13) aus
 
Zuletzt bearbeitet:

httpdigest

Top Contributor
Ich habe schon alles in einem Thread
Und dieser eine Thread ist auch der main Thread? Also der Thread, der implizit durch die Java main() Methode existiert und in dessem Kontext diese Methode aufgerufen wird?
Mit anderen Worten: Du hast hoffentlich nirgendswo new Thread(...) stehen oder eine Klasse, die extends Thread hat.
Die meisten GLFW Methoden müssen nämlich vom main Thread aufgerufen werden.

(also .Version() gibt 3.3.2+13) aus
Die LWJGL Version hat damit nichts zu tun. Ich rede von der OpenGL Version, die du durch GLFW anfordern lässt.
Irgendwo in deinem Code müsste sowas stehen:
Java:
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
oder so ähnlich.
 

NicoHatProbleme2

Bekanntes Mitglied
Und dieser eine Thread ist auch der main Thread? Also der Thread, der implizit durch die Java main() Methode existiert und in dessem Kontext diese Methode aufgerufen wird?
Mit anderen Worten: Du hast hoffentlich nirgendswo new Thread(...) stehen oder eine Klasse, die extends Thread hat.
Die meisten GLFW Methoden müssen nämlich vom main Thread aufgerufen werden.
Jap, dieser Thread ist der main thread. Dass ein anderer Thread nicht funktioniert, musst ich schon herausfinden.
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
Jap, genau das habe ich dort stehen.
 

NicoHatProbleme2

Bekanntes Mitglied
Java:
public class GLWindow {
    private String title;
    private int width, height;
    private long window;
    private boolean vSync;
    private int fps = 60;
    private double f = 1.0/fps;
    public GLWindow(String title, int width, int height, boolean vSync) {
        this.title = title;
        this.width = width;
        this.height = height;
        this.vSync = vSync;
        init();
    }
    public void init() {
        GLFW.glfwInit();
        GLFW.glfwDefaultWindowHints();
        setWindowData(GLFW.GLFW_VISIBLE, glBoolean(false));
        setResizable(true);
        setWindowData(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
        setWindowData(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2);
        setWindowData(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
        setWindowData(GLFW.GLFW_OPENGL_FORWARD_COMPAT, glBoolean(true));
      
        boolean maximized = false;
        if(width == 0 || height == 0) {
            width = 100;
            height = 100;
            setWindowData(GLFW.GLFW_MAXIMIZED, glBoolean(true));
            maximized = true;
        }
      
        window = GLFW.glfwCreateWindow(width, height, title, MemoryUtil.NULL, MemoryUtil.NULL);
        if(window == MemoryUtil.NULL) System.err.println("Failed to create GLFW Window");
      
        GLFW.glfwSetFramebufferSizeCallback(window, (window, width, height) -> {
            this.width = width;
            this.height = height;
        });
      
        if(maximized) GLFW.glfwMaximizeWindow(window);
        else {
            GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
            GLFW.glfwSetWindowPos(window, (vidMode.width()-width)/2, (vidMode.height()-height)/2);
        }
      
        GLFW.glfwMakeContextCurrent(window);
      
        setVsync(vSync);
      
        GL.createCapabilities();
      
        GLFW.glfwShowWindow(window);
      
        GL11.glClearColor(0, 0, 0, 0);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glEnable(GL11.GL_STENCIL_TEST);
        GL11.glEnable(GL11.GL_CULL_FACE);
        GL11.glCullFace(GL11.GL_BACK);
      
        run();
    }
    public void setFPS(int amount) {
        fps = amount;
        f = 1/fps;
    }
    public void run() {
        double lastFrameTime = 0.0;
        double time;
        double deltaTime;
        try {
            while(!windowShouldClose()) {
                time = Clock.getNanoTime();
                deltaTime = time - lastFrameTime;
                if(deltaTime < f && f > 0) {
                    if((f-deltaTime)*1E3 < 1) Thread.sleep(1);
                    Thread.sleep((long) ((f-deltaTime)*1E3));
                }
                time = Clock.getNanoTime();
                deltaTime = time - lastFrameTime;
                lastFrameTime = time;
                System.out.println("update");
                update(deltaTime);
            }
          
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        cleanup();
    }
    public void update(double dt) {
      
        GL11.glBegin(GL11.GL_QUADS); 
     
        GL11.glVertex2d(0, 0);
        GL11.glVertex2d(10, 0);
        GL11.glVertex2d(0, 10);
        GL11.glVertex2d(10, 10);
      
        GL11.glEnd();
      
      
        GLFW.glfwSwapBuffers(window);
        GLFW.glfwPollEvents();
    }
    public void cleanup() {
        GLFW.glfwDestroyWindow(window);
    }
    public void setClearColor(Color c) {
        setClearColor(c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha());
    }
    public void setClearColor(double r, double g, double b, double a) {
        GL11.glClearColor((float) r, (float) g, (float) b, (float) a);
    }
    public boolean isKeyPressed(int keycode) {
        return GLFW.glfwGetKey(window, keycode) == GLFW.GLFW_PRESS;
    }
    public boolean windowShouldClose() {
        return GLFW.glfwWindowShouldClose(window);
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String s) {
        GLFW.glfwSetWindowTitle(window, s);
    }
    public void setResizable(boolean b) {
        setWindowData(GLFW.GLFW_RESIZABLE, glBoolean(b));
    }
    public void setVsync(boolean b) {
        vSync = b;
        if(b) GLFW.glfwSwapInterval(1);
        else GLFW.glfwSwapInterval(0);
    }
    public boolean hasVsync(boolean b) {
        return vSync;
    }
    private void setWindowData(int a, int b) {
        GLFW.glfwWindowHint(a, b);
    }
    private int glBoolean(boolean b) {
        if(b) return GLFW.GLFW_TRUE;
        else return GLFW.GLFW_FALSE;
    }

}

Das hier habe ich außer dem
Code:
        GL11.glBegin(GL11.GL_QUADS);

        GL11.glVertex2d(0, 0);
        GL11.glVertex2d(10, 0);
        GL11.glVertex2d(0, 10);
        GL11.glVertex2d(10, 10);

        GL11.glEnd();
Und dem run()

aus einem Tutorial (nicht eins zu eins).

Den Rest habe ich aus einem Stack Overflow thread, da ich den Bildschirm erstmal austesten wollte.

Das mit dem run() habe ich aus einem meiner alten JFrame Projekte übernommen...
 
Zuletzt bearbeitet:

httpdigest

Top Contributor
Okay, dann wiederhole ich mich nochmal:
Die ganzen deprecated OpenGL Funktionen wie glBegin/glEnd/glVertex* und die ganzen anderen immediate Mode Funktionen, die du verwendest, sind schon seit über 14 Jahren deprecated (quasi mit dem Erscheinen des OpenGL 3.2 Core Profiles (was du nutzt)).
Du kannst glBegin/glEnd nicht mehr nutzen, wenn du OpenGL 3.2+ Core Profile verwendest.
Die einfachste Lösung wäre, einfach diese Zeilen zu entfernen:
Java:
setWindowData(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
setWindowData(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2);
setWindowData(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
setWindowData(GLFW.GLFW_OPENGL_FORWARD_COMPAT, glBoolean(true));
Das sorgt dann dafür, dass du ein Compatibility Profile bekommst und auch nur höchstens OpenGL 2.1 nutzen kannst.
Mache dich bitte zuerst mit den ganzen OpenGL Versionen und den OpenGL Profilen vertraut und welche OpenGL Funktionen nun in welcher OpenGL Version (OpenGL Version, nicht LWJGL Version!) eingeführt wurden und was deprecated ist.
 

Ähnliche Java Themen

Neue Themen


Oben