Omegaspard
Omegaspard

Reputation: 1980

glClearColor display ony a black screen

I struggle to understand why the window I'm doing with openGL stays black.

I don't see where I made a mistake in my code :

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()
  }
}

I also tried to put the glClear inside the display method and also put a glClearColor in the init method.

EDIT: I found it. Actually the display and init meth were never called. The listener wasn't attached to the canvas and then it never received any event.

The problem is that I was missing the line

canvas.addGLEventListener(this)

just after the canvas initialisation. (this line)

val canvas = new GLCanvas(capabilities)

Upvotes: 4

Views: 205

Answers (1)

Omegaspard
Omegaspard

Reputation: 1980

(I'm answering my own question)

So actually the problem was that the display and init method were never called. As far as I understood it, the GLEventListener is waiting for event and those would trigger the call of the init and display method.

The "thing" that would notice the GLEventListener is the canvas, yet my canvas and the GLEventListener weren't binded.

To do so I added the line

canvas.addGLEventListener(this)

Just after I initialized the canvas and it then I could notice the init and display method called.

Upvotes: 3

Related Questions