Creating OpenGL Windows in Kotlin and LWJGL3

Joe Chasinga
Jul 24, 2017 · 1 min read

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!

Joe Chasinga

Written by

Former restauranteur, architect, and artist. Open-sourcerer at heart, father, and engineer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade