javajavafxlibgdxandroid-jetpack-composejogl

How to embed a 3D graphics panel (LibGDX or JOGL) into a desktop GUI (JavaFX or Desktop Compose) in 2022?


I am working on a 3D visualization tool as a prototype desktop app in Java/Kotlin.

I need the GUI power of JavaFX or Compose for Desktop (or Swing, worst case) but I need the main panel of the window to be a high performance dynamic 3D graphics using LibGDX, JOGL or similar low-level library.

I have searched many SO posts and pages but found no way to do this so far.

Can anyone suggest the best way to embed a 3D panel into one of the big desktop GUI frameworks? Or a demo repo which does this already?


Solution

  • I managed to create a solution on the stack:

    I got it working by following these steps:

    1. Use the LibGDX setup wizard with settings:

      • Kotlin (under Advanced)
      • Desktop (LWJGL 3) only -- note this will be changed to LWJGL 2 laterenter image description here
    2. Modify build.gradle line:

    api "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"

    to

    api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"

    1. Convert DesktopLauncher to Kotlin code in IntelliJ (open file, Ctrl+Alt+Shift+K):

    2. Replace DesktopLauncher code with:

    package au.com.brendanhill
    
    import com.badlogic.gdx.backends.lwjgl.LwjglAWTCanvas
    import java.awt.BorderLayout
    import java.awt.Container
    import javax.swing.JButton
    import javax.swing.JFrame
    import javax.swing.SwingUtilities
    
    
    // Please note that on macOS your application needs to be started with the -XstartOnFirstThread JVM argument
    class DesktopLauncher : JFrame() {
    
        init {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
            val container: Container = getContentPane()
            val canvas = LwjglAWTCanvas(Basic3DTest())
            container.add(JButton("test button"), BorderLayout.WEST)
            container.add(canvas.getCanvas(), BorderLayout.CENTER)
            pack()
            setVisible(true)
            setSize(800, 600)
        }
    }
    
    fun main(arg: Array<String>) {
        SwingUtilities.invokeLater { DesktopLauncher() }
    }
    
    1. It then launches successfully with the graphics panel in the window with other Swing components:

    enter image description here The source code is available here for reference:

    https://github.com/brendanhillovida/try-libgdx-kotlin-gradle-swing

    Notes: