gpt4 book ai didi

scala - glClearColor 只显示黑屏

转载 作者:行者123 更新时间:2023-12-01 01:40:34 24 4
gpt4 key购买 nike

我很难理解为什么我用 openGL 做的窗口保持黑色。

我没有看到我在代码中哪里出错了:

import com.jogamp.opengl.awt.GLCanvas
import com.jogamp.opengl.{GL, GLAutoDrawable, GLCapabilities, GLEventListener, GLProfile}
import javax.swing.{JFrame, WindowConstants}

class Game extends JFrame ("Just a window OMG.") with GLEventListener {

val profile: GLProfile = GLProfile.get(GLProfile.GL4)
val capabilities = new GLCapabilities(profile)
val canvas = new GLCanvas(capabilities)

this.setName("Just a window OMG.")
this.getContentPane.add(canvas)
this.setSize(800, 600)
this.setLocationRelativeTo(null)
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
this.setVisible(true)
this.setResizable(false)
canvas.requestFocusInWindow

def play(): Unit = {

}

override def display(drawable: GLAutoDrawable): Unit = {
val gl = drawable.getGL.getGL4
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)

gl.glFlush()
}

override def dispose(drawable: GLAutoDrawable): Unit = {}

override def init(drawable: GLAutoDrawable): Unit = {
val gl = drawable.getGL.getGL4
gl.glClearColor(1f, 0f, 0f, 1.0f)
}

override def reshape(drawable: GLAutoDrawable, x: Int, y: Int, width: Int, height: Int): Unit = {}
}

object Main {
def main(args: Array[String]): Unit = {
val game = new Game()
game.play()
}
}

我还尝试将 glClear 放入 display 方法中,并在 init 方法中放入 glClearColor 。

编辑:
我找到了。
实际上从来没有调用过显示和初始化方法。
监听器没有附加到 Canvas 上,然后它从未收到任何事件。

问题是我错过了这条线
canvas.addGLEventListener(this)

就在 Canvas 初始化之后。
(这一行)
val canvas = new GLCanvas(capabilities)

最佳答案

(我正在回答我自己的问题)

所以实际上问题是从来没有调用过 display 和 init 方法。
据我了解,GLEventListener 正在等待事件,这些事件将触发 init 和 display 方法的调用。

会注意到 GLEventListener 的“东西”是 Canvas ,但我的 Canvas 和 GLEventListener 没有绑​​定。

为此,我添加了该行

canvas.addGLEventListener(this)

就在我初始化 Canvas 之后,我可以注意到调用了 init 和 display 方法。

关于scala - glClearColor 只显示黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58148282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com