当前位置: 首页 > 知识库问答 >
问题:

用Jframe和canvas实现时,LWJGL显示崩溃

彭梓
2023-03-14

我正在努力使用LWJGL使用OpenGL创建一个带有照明引擎的2D Java游戏,但是在尝试链接键盘输入时,我遇到了一堵墙。

渲染循环工作正常,但当我尝试实现JFrame/canvas和getParent/keylister组合时,应用程序在启动后立即崩溃。我必须在netbeans中关闭应用程序-窗口不会响应右键单击开始工具栏中的应用程序条目。

public static void main(String[] args) {
  Main main = new Main();
  main.run();

}

public void run() {
  initialize();
  //animLoop();
}

private void initialize() {
  try {
    Frame theFrame = new Frame("Inlight");
    Canvas theCanvas = new Canvas();
    theCanvas.setMinimumSize(new Dimension(windowWidth, windowHeight));
    theFrame.setSize(windowWidth, windowHeight);
    theCanvas.requestFocusInWindow();

    theFrame.add(theCanvas);

    theFrame.setVisible(true);
    //before doing the following, I need to create the canvas within which openGL does it's rendering 
    //create it before applying keylistener

    Display.setDisplayMode(new DisplayMode(windowWidth, windowHeight));
    Display.setParent(theCanvas);
    Display.getParent().addKeyListener(new InlightKeyListener());
    Display.create(new PixelFormat(0, 16, 1));

  } catch (Exception e) {
    e.printStackTrace();
  }

  shaderProgram = glCreateProgram();
  fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  StringBuilder fragmentShaderSource = new StringBuilder();

  try {
    String line;
    BufferedReader reader = new BufferedReader(new FileReader("src/shader.frag"));
    //points to the shader doc
    while ((line = reader.readLine()) != null) {
      fragmentShaderSource.append(line).append("\n");
    }
  } catch (IOException e) {}

  glShaderSource(fragmentShader, fragmentShaderSource);
  glCompileShader(fragmentShader);
  if (glGetShaderi(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE) {
    System.err.println("Fragment shader not compiled!");
  }

  glAttachShader(shaderProgram, fragmentShader);
  glLinkProgram(shaderProgram);
  glValidateProgram(shaderProgram);


  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, windowWidth, windowHeight, 0, 1, -1);
  glMatrixMode(GL_MODELVIEW);

  glEnable(GL_STENCIL_TEST);
  glClearColor(0, 0, 0, 0);
  System.out.println("Done initialize");
}

public synchronized void animLoop() {
  //This method will loop the render
  long startTime = System.currentTimeMillis();
  //sets the starting time to the current time
  long curTime = startTime;
  //The current time measurement, so at thestart the curTime = starting time  
  while (curTime - startTime < 1800) {
    long timePassed = System.currentTimeMillis() - curTime;
    //Makes the timePassed variable equal to the System's current time - the last measured current time.
    curTime += timePassed;
    //updates the measurement of the current time to the actual current time. (I imagine some small amount to time is lost while it is updated. this is negligible.)
    organiseTitle();
    //sets up the ojects to display the title screen for 1800 milliseconds
    render();
    //draws the new screen scene to the display
    clearObj();
    //cleans up the items built for that last render
  }

  while (!Display.isCloseRequested()) {
    long timePassed = System.currentTimeMillis() - curTime;
    //Makes the timePassed variable equal to the System's current time - the last measured current time.
    curTime += timePassed;
    //updates the measurement of the current time to the actual current time. (I imagine some small amount to time is lost while it is updated. this is negligible.)
    Organiselevel1(20, 200);
    render();
    //draws the new screen scene to the display
    clearObj();
    //cleans up the items built for that last render
  }
  glDeleteShader(fragmentShader);
  glDeleteProgram(shaderProgram);
  Display.destroy();
  //closes the window
}

当然,在这一点之后还有更多代码,但它都已经过测试并正常工作。

共有1个答案

田永春
2023-03-14

天啊,我刚刚意识到我的主循环调用被注释掉了。

/facepalm

 类似资料:
  • 最近我使用LWJGL制作了一个应用程序,我决定将JFrame设置为显示的父级。我这样做是因为它提供了更好的调整大小和移动功能。特别是在windows上,LWJGL似乎有一个错误,当移动和调整大小时,应用程序会冻结。 问题是,当我在我的mac上尝试应用程序时,它比我的windows电脑快得多,性能真的很差。当我在mac上使用常规的本机显示时,它在60fps下运行流畅,但奇怪的是:当我在mac上的JF

  • 我创建了一个JFrame,其中包含一个JSplitPane,它在左侧包含一个画布,在右侧包含一个JPanel。画布包含LWJGL显示,而JPanel中几乎没有JTextFields。问题是,当我按下LWJGL显示器上的鼠标按钮时,我无法再将文本写入JTextFields。当我最小化应用程序并将其返回时,它会一直工作,直到我再次按下显示器。 为什么我会遇到这个问题?我怎么能修好它? 注意:我可以专注

  • 我最近发现了如何使用LWJGL和OpenGL渲染3D立方体,我非常激动,我渲染了2000个立方体,并有效地冻结了我的计算机。我听说过诸如显示列表和VBO之类的东西,但即使在谷歌搜索之后,我也不知道如何使用它们。 目前,我有 渲染我的立方体。调用只是渲染一个立方体 其中cap是顶部纹理,side是侧面纹理。 我真正需要的帮助是弄清楚如何使我的代码VBO和/或显示列表兼容,以提高性能。我还认为,如果可

  • 我有一个包含动态内容的JFrame和 只有当用户按下JButton时才显示框架,my_frame类只包含一个构造函数(用于第一次调用)和一个用于刷新其内容的刷新方法。现在我希望当JFrame再次变得可见时,my_frame拦截事件并自动调用refresh方法。我该怎么做?

  • 编辑:我已经回答了这个问题,代码已经被更改,所以它是可用的,请在您自己的项目中随意使用代码。你好,利亚姆 我目前正在LWJGL中进行3D模型渲染,我遇到了一个问题,当我运行程序时,显示会出现,并且一切都与模型无关。我测试了3D空间代码,绘制了一些随机点,我可以看到它们并四处走动,所以我的3D空间代码正在工作,但模型代码不行。 我的问题如下:我的显示代码有问题吗?还是我的模型加载代码有问题?下面是我

  • 有问题显示纹理在我的3D框在LWJGL使用光滑。早些时候我犯了一个错误: BasicShader类: BasicVertex.vs basicfragment.fs