加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 当前线程中没有OpenGL上下文

发布时间:2020-12-15 04:28:23 所属栏目:Java 来源:网络整理
导读:我正在关注youtube上的教程,但我遇到了OpenGL的问题,我无法解决.我不知道如何解决它. Exception in thread "EndlessRunner" java.lang.IllegalStateException: No OpenGL context is current in the current thread.at org.lwjgl.opengl.GLContextWindows.cr
我正在关注youtube上的教程,但我遇到了OpenGL的问题,我无法解决.我不知道如何解决它.

Exception in thread "EndlessRunner" java.lang.IllegalStateException: No OpenGL context is current in the current thread.
at org.lwjgl.opengl.GLContextWindows.createFromCurrent(GLContextWindows.java:61)
at org.lwjgl.opengl.GLContext.createFromCurrent(GLContext.java:36)
at net.alfredo.Main.init(Main.java:59)
at net.alfredo.Main.run(Main.java:91)
at java.lang.Thread.run(Unknown Source)

主要

package net.alfredo;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;

import java.nio.ByteBuffer;

import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWvidmode;
import org.lwjgl.opengl.GLContext;

import Input.Input;

public class Main implements Runnable {


private Thread thread;
public boolean running = true;

private GLFWKeyCallback keyCallback;

public Long window;

public static void main(String args[]) {

    Main game = new Main();
    game.start();

}

public void start(){

    running = true;
    thread = new Thread(this,"EndlessRunner");
    thread.start();

}

public void init(){

    if(glfwInit() != GL_TRUE){
        System.err.println("La inicializacion de GLWF fallo!");
    }

    glfwWindowHint(GLFW_RESIZABLE,GL_TRUE);

    window = glfwCreateWindow(800,600,"Cuboid MMORPG",NULL,NULL);

    if(window == NULL){
        System.err.println("No se pudo crear la ventana!");
    }

    glfwSetKeyCallback(window,keyCallback = new Input());


    ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window,100,100);

    glfwShowWindow(window);
    GLContext.createFromCurrent();

    glClearColor(0.56f,0.250f,0.425f,1.0f);

    glEnable(GL_DEPTH_TEST);

    System.out.println("OpenGL: " + glGetString(GL_VERSION));

}

public void update(){
    glfwPollEvents();

    if(Input.keys[GLFW_KEY_SPACE]){
        System.out.println("Has precionado la tecla Espacio!");

    }

}

public void render(){
    glfwSwapBuffers(window);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

}

@Override
public void run() {
    init();
    while(running){
        update();
        render();

        if (glfwWindowShouldClose(window) == GL_TRUE){

            running = false;

        }

    }

    keyCallback.release();

}
}

解决方法

您需要调用 glfwMakeContextCurrent将OpenGL上下文绑定到您的线程. LWJGL website上也有一个工作示例.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读