Java – there is no OpenGL context in the current thread
•
Java
I'm following the tutorial on youtube, but I've encountered an OpenGL problem that I can't solve I don't know how to solve it
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)
main
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();
}
}
Solution
You need to call glfwmakecontextcurrent to bind the OpenGL context to your thread There is also a working example on lwjgl website
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
