[英]Exception in thread “main” java.lang.RuntimeException: No OpenGL context found
I've recently started to use the lwjgl and haven't run into any problems. 我最近开始使用lwjgl并没有遇到任何问题。 Yesterday I went to create a new window (something I've done at least a dozen times, if not more) and it gave these errors when I ran it
昨天我去创建一个新窗口(我已经完成了至少十几次,如果不是更多的话),当我运行它时它会出现这些错误
Exception in thread "main" java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL11.glMatrixMode(GL11.java:2051)
at Main.initGL(Main.java:10)
at Main.main(Main.java:34)
My code is 我的代码是
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
public class Main
{
public static void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
public static void initDisplay()
{
try
{
Display.setDisplayMode(new DisplayMode(480, 600));
Display.setTitle("Texture Demo");
Display.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
Display.update();
}
public static void main(String[] args)
{
initGL();
initDisplay();
}
}
I can't see any errors and like I said, I've ran this code before. 我看不到任何错误,就像我说的,我之前运行过这段代码。
initGL
and initDisplay
are round the wrong way. initGL
和initDisplay
是错误的。
GL needs a context before you can start calling GL functions, so initDisplay()
and then initGL()
. GL需要一个上下文才能开始调用GL函数,所以
initDisplay()
然后是initGL()
。
I have had this problem recently while making a game. 我最近在制作游戏时遇到过这个问题。 The OpenGL initialization needs to be after the Display creation.
OpenGL初始化需要在显示创建之后。 And also, you must constantly update your Display or else it will immediately close on creation.
此外,您必须不断更新您的显示器,否则它将立即关闭创建。 An example here:
这里的一个例子:
public void run() {
while(!Display.isCloseRequested) {
Display.update()
// Add repainting and input here
}
}
And add the "run" method in your "main" method 并在“main”方法中添加“run”方法
If you change the init states it will work. 如果更改init状态,它将起作用。 So At first you have to do the initDisplay() beacuse the matrices will not find the OpenGL's display.
所以首先你必须做initDisplay()因为矩阵不会找到OpenGL的显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.