2

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?

Brendan Hill
  • 3,406
  • 4
  • 32
  • 61
  • 1
    Very basic [JavaFX 3D app](https://stackoverflow.com/questions/19621423/javafx-materials-bump-and-spec-maps/19624126#19624126). Example mixing JavaFX controls in [a 2D scene with 3D in a SubScene](https://stackoverflow.com/questions/19459012/how-to-create-custom-3d-model-in-javafx-8/19487494#19487494). [Fxyz](https://github.com/FXyz/FXyz) has a sample app. – jewelsea Dec 31 '22 at 07:01
  • 1
    [JavaFX 3D support](https://stackoverflow.com/questions/19621423/javafx-materials-bump-and-spec-maps/19624126#19624126) may be sufficient for what you want to do, or it may not. Performance-wise it may be fine (it is a hardware accelerated system). But, there won’t be the feature support, documentation, frameworks, higher level libraries and community you may get by choosing alternate technology. – jewelsea Dec 31 '22 at 07:09
  • I wrote a tutorial about mixing JOGL with OpenJFX, maybe it might help you: https://gouessej.wordpress.com/2020/04/05/javafx-et-jogl-fonctionnent-ensemble-javafx-and-jogl-work-together/ – gouessej Jan 02 '23 at 11:29
  • JOGL is significantly lower-level than LibGDX though, right? Like, manually building vertex buffers and stuff.... – Brendan Hill Jan 03 '23 at 23:58

2 Answers2

3

Maybe you can use https://github.com/AlmasB/FXGL which also offers 3D support. JOGL can also be used by JavaFX.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • Thanks mipa - I think these are viable options too, although I have a preference for a higher-level framework than JOGL (which is just a binding for the low-level OpenGL) and also a personal preference for Swing over JavaFX. Also FXGL says that 3D is experimental so hesitant to invest too much time. – Brendan Hill Jan 01 '23 at 03:23
2

I managed to create a solution on the stack:

  • Kotlin 1.7
  • JDK 17
  • Swing
  • FlatLightLaf
  • LibGDX 1.11
  • LWJGL 2

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:

  • I don't know why the wizard didn't create DesktopLauncher and MyGdxGame in Kotlin after specifying Kotlin as the language - but it is easy to auto-convert

  • It appears that LibGDX cannot use LWJGL3 as the backend if you want to embed in Swing (various pages mentioned this, I didn't research further what the latest status is)

  • There appear to be viable options using JavaFX however my preference is Swing and I got it to work, so I didn't investigate them further.

Brendan Hill
  • 3,406
  • 4
  • 32
  • 61