Home > Blockchain >  Why is this glClear call leading to a runtime Error in Scala
Why is this glClear call leading to a runtime Error in Scala

Time:11-05

I am trying to create a window, open it and be able to close it and the program should stop after I closed the window.

The Code i wrote so far is mostly copied from this video: Setting Up OpenGL and creating a window

Here is my code:

Window.scala

// The Window displaying the rendered image with openGL
package Main
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;

import org.lwjgl.glfw.Callbacks.*;
import org.lwjgl.glfw.GLFW.*;
import org.lwjgl.opengl.GL11.*;
import org.lwjgl.system.MemoryStack.*;
import org.lwjgl.system.MemoryUtil.*;

class Window(width: Int, height: Int, name: CharSequence) {

  // Code from https://www.lwjgl.org/guide

  GLFWErrorCallback.createPrint(System.err).set();
  if (!glfwInit()) {
    throw new IllegalStateException("Can't create window")
  }

  var window = glfwCreateWindow(width, height, name, 0, 0);

  glfwMakeContextCurrent(window);

  while (!glfwWindowShouldClose(window)) {

    // glClear(GL_COLOR_BUFFER_BIT);

    glfwSwapBuffers(window);

    glfwPollEvents();

  }

  glfwTerminate();

}

Main.scala:

import Main._
@main def hello: Unit = {
  val window = Window(250,250, "Test");
}

I am using sbt version 1.7.2 to compile and run my code with the build.sbt:

import scala.collection.immutable.Seq
val scala3Version = "3.2.0"

lazy val root = project
  .in(file("."))
  .settings(
    name := "2DRenderer",
    version := "0.1.0-SNAPSHOT",
    scalaVersion := scala3Version,
    libraryDependencies  = "org.scalameta" %% "munit" % "0.7.29" % Test
  )

lazy val lwjglVersion = "3.2.1"

lazy val os = Option(System.getProperty("os.name", ""))
  .map(_.substring(0, 3).toLowerCase) match {
  case Some("win") => "windows"
  case Some("mac") => "macos"
  case _           => "linux"
}

libraryDependencies   = Seq(
  "org.lwjgl" % "lwjgl" % lwjglVersion,
  "org.lwjgl" % "lwjgl-opengl" % lwjglVersion,
  "org.lwjgl" % "lwjgl-glfw" % lwjglVersion,
  "org.lwjgl" % "lwjgl-stb" % lwjglVersion,
  "org.lwjgl" % "lwjgl-assimp" % lwjglVersion,
  "org.lwjgl" % "lwjgl-nanovg" % lwjglVersion,
  "org.lwjgl" % "lwjgl" % lwjglVersion classifier s"natives-$os",
  "org.lwjgl" % "lwjgl-opengl" % lwjglVersion classifier s"natives-$os",
  "org.lwjgl" % "lwjgl-glfw" % lwjglVersion classifier s"natives-$os",
  "org.lwjgl" % "lwjgl-stb" % lwjglVersion classifier s"natives-$os",
  "org.lwjgl" % "lwjgl-assimp" % lwjglVersion classifier s"natives-$os",
  "org.lwjgl" % "lwjgl-nanovg" % lwjglVersion classifier s"natives-$os"
)

fork := true

The code is running fine as long as I do not clear the Color Buffer with glClear(GL_COLOR_BUFFER_BIT). Running the code with clearing the buffer the loop, i get the following error message from sbt:

[info] running (fork) hello
[info] FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.        
[info]  at org.lwjgl.opengl.GL11C.glClear(Native Method)
[info]  at org.lwjgl.opengl.GL11.glClear(GL11.java:1045)
[info]  at Main.Window.<init>(Window.scala:31)
[info]  at Main$package$.hello(Main.scala:3)
[info]  at hello.main(Main.scala:2)
[error] Nonzero exit code returned from runner: 1
[error] (Compile / run) Nonzero exit code returned from runner: 1
[error] Total time: 1 s, completed 05.11.2022 12:58:36

Does anybody know why clearing the color buffer is resulting in this runtime error?

CodePudding user response:

The problem is not doing the glClear. The problem is, that you don't get a OpenGL context in the first place. See the log output:

[info] FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.        

This can happen e.g. if working remotely on a machine. Another cause is, that no OpenGL drivers are installed.

CodePudding user response:

The problem is not the glClear call but that I was not setting up the context correctly. According to the LWJGL wiki you can not call any function (i think this means gl_ functions) when you did not create the context like this:

glfwMakeContextCurrent(window);
GL.createCapabilities();

This solved my problem.

  • Related