简体   繁体   English

线程“main”中的异常java.lang.RuntimeException:找不到OpenGL上下文

[英]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. initGLinitDisplay是错误的。

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.

相关问题 java.lang.RuntimeException:在线程中生成世界时在当前线程中找不到OpenGL上下文 - java.lang.RuntimeException: No OpenGL context found in the current thread while generating world in thread Java错误:线程“ main”中的异常java.lang.RuntimeException - Java Errors: Exception in thread “main” java.lang.RuntimeException 线程“ main”中的异常java.lang.RuntimeException:无法编译的源代码 - Exception in thread “main” java.lang.RuntimeException: Uncompilable source code 线程“main”中的异常 java.lang.RuntimeException:尚未实现 - Exception in thread "main" java.lang.RuntimeException: Not yet implemented 线程“main”中的异常 java.lang.RuntimeException: Stub XmlPullParserFactory - Exception in thread "main" java.lang.RuntimeException: Stub XmlPullParserFactory 线程“主”java.lang.RuntimeException 中的异常:矩阵是奇异的 - Exception in thread “main” java.lang.RuntimeException: Matrix is singular 线程“main”中的异常java.lang.RuntimeException:Stub - Exception in thread “main” java.lang.RuntimeException: Stub 线程“主”java.lang.RuntimeException 中的错误异常 - Error Exception in thread "main" java.lang.RuntimeException 线程“ main”中的异常java.lang.RuntimeException:无法启动Selenium会话:找不到 - Exception in thread “main” java.lang.RuntimeException: Could not start Selenium session: Not Found 线程“ main”中的异常java.lang.RuntimeException:执行Storm时发现了多个defaults.yaml资源 - Exception in thread “main” java.lang.RuntimeException: Found multiple defaults.yaml resources while executing Storm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM