Creating OpenGL Windows in Kotlin and LWJGL3

pancy
1 min readJul 24, 2017

--

Before writing OpenGL graphics, one needs a surface for creation. OpenGL purposefully doesn’t specify a platform-specific way of drawing a GL window. GLFW is a C library that does exactly that, and the latest version of LWJGL exposes it as a default window API, which is very straightforward to use in Kotlin:

import org.lwjgl.glfw.GLFW.*// ...// Setup an error callback 
GLFWErrorCallback.createPrint(System.err).set()
if ( !(glfwInit()) ) {
throw IllegalStateException("Unable to initialize window")
}
// setting GLFW window's configuration
// glfwDefaultWindowHints()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE) // resizeable window

val
window: Long = glfwCreateWindow(300, 300, "Hello Window!", 0, 0)
if ( window <= 0 ) {
println("Failed to create GLFW window")
glfwTerminate()
}
// Make the OpenGL context current
glfwMakeContextCurrent(window)

Also, the LWJGL3 team’s attempt to shadow C++ API in Java as much as possible using imported static methods is a clever idea because one can just read docs in C++ and the code is almost identical in Java. This means that in Kotlin it’s effectively similar too.

Oh, as a side note — I tried IntelliJ IDEA. Anyone who’s developing in Java and/or Kotlin and not using it should seriously consider switching! This goes especially for new Java/Kotlin developers since the IDE is super helpful!

--

--

pancy

I’m interested in Web3 and machine learning, and helping ambitious people. I like programming in Ocaml and Rust. I angel invest sometimes.