Search code examples
javaopengljavafxjogl

Jogl and JavaFX


I heared its possible to integrate Jogl into JavaFX with the NewtCanvasJFX but I can't get it to work. I tried something like that.

@Override
public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("JavaFX Jogl");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();

    //init Canvas
    final GLProfile glProfile = GLProfile.getDefault();
    final GLCapabilities capabilities = new GLCapabilities(glProfile);

    GLWindow glWindow = GLWindow.create(capabilities);

    NewtCanvasJFX glPanel = new NewtCanvasJFX(glWindow);
    glPanel.setWidth(300);
    glPanel.setHeight(300);

    StackPane openGLPane = new StackPane();
    openGLPane.getChildren().add(glPanel);

    glWindow.addGLEventListener(this);
}

I just need to get jogl and Javafx working together for a university project so if anyone has other solutions I would really appreciate them.


Solution

  • I couldnt get it to work with NewtCanvasJFX but I used gljpanel to combine jogl and javafx.

    It is important that all jogl related stuff is inside SwingUtilities.invokeLater otherwise nothing will get rendered. You can just use the canvas to add an gleventlistener.

    @Override
    public void start(Stage primaryStage) throws Exception{ 
        root = new StackPane();
    
        final GLProfile glProfile = GLProfile.getDefault();
        final GLCapabilities capabilities = new GLCapabilities(glProfile);
    
        canvas = new GLJPanel(capabilities);
    
        swingNode = new SwingNode();
    
        root.getChildren().add(swingNode);
    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                swingNode.setContent(canvas);
                //jogl stuff here           
            }
        });
    
    
        primaryStage.setTitle("JavaFX OpenGL");
        primaryStage.setScene(new Scene(root, 1920, 1080));
        primaryStage.show();         
    }