How to use OpenGL in JavaFX?
Asked Answered
L

6

17

I want to write a very simple Java 3D editor(for experiment). I know the basic JavaFX usage, and I know enough OpenGL knowledge. But all my OpenGL experience is from working with C/C++.

Could I make a 'canvas' in JavaFx application and map OpenGL viewport on it?

Loats answered 5/6, 2013 at 17:15 Comment(2)
There are not many examples on the internet, but an implementation can be seen here: java-gaming.org/topics/lwjgl-javafx-integration/27801/view.htmlGracielagracile
You can use this project as a base : github.com/Spasi/LWJGL-FXVilberg
L
18

Internally, JavaFX can use OpenGL as a rendering pipeline, so some care on integration between the two is required to avoid conflicts.

OpenGLNode in JavaFX

Richard Bair, JavaFX team lead posted on the openjfx development mailing list:

One thing I want to see done (for example) for the 8 update is to have an OpenGLNode or NativeSurfaceNode or something along those lines so that if you are doing your own D3D / OpenGL you can have a way to send those raw commands down to the graphics card but still have your node composited in the scene graph.

So a future JavaFX release update might include an OpenGLNode. Such a feature would probably not see inclusion in a JavaFX general availability release until the next JavaFX feature release after the initial Java 8 release (my guess is that would put it at about September 2014).

3rd Party OpenGL/JavaFX integration

You don't need to wait so long to start integrating JavaFX and OpenGL. All of the required source code to start an implementation is open in the OpenJFX repository, so you could try building a custom integration of that code with a library such as lwjgl or jogl.

This answer will get dated as developers start performing integrations of JavaFX with existing Java wrappers for OpenGL apis. Some developers have started such work already - run a google search of lwjgl javafx or jogl javafx to find out about current integration projects and their status.

The simplest integration is probably to have a 3rd party library render to an off screen buffer then transfer the pixels from the buffer to a JavaFX WritableImage or Canvas as required to get the OpenGL rendered graphics composited into the JavaFX scene graph.

JavaFX 3D API Alternative

JavaFX has its own lightweight 3D api that provides the ability to composite phong shaded 3d models into the JavaFX scene graph. The JavaFX 3D api is not going to provide all the power of a full OpenGL api integration, however using the JavaFX 3D api is relatively simple. A description, with code examples of the 3D features in Java 8 is on the open-jfx wiki.

Interactive Mesh provides a free 3D model importer for JavaFX, that allows you to very simply bring complex shaded and textured 3D models into a JavaFX scene graph.

There is a 3D Viewer project in the openjfx repository that you could fork to create a basis for your proposed JavaFX based 3D editor.

The nashorn JavaScript engine has a switch that allows you to use JavaFX and its 3D features from JavaScript, so you can use alternative jvm languages to access JavaFX 3D features if you prefer.

Loosejointed answered 5/6, 2013 at 21:7 Comment(3)
So, almost 1 year has passed... Are there any news about OpenGLNode?Evidently
No Sarge, you can try asking on the mailing list linked in the answer.Loosejointed
This is such a desirable addition to JavaFX. Wish this would materialise.Defend
S
2

You can use the OpenGLFX library, which mixes the previously mentioned JOGL/LWJGL and JavaFX. The code has the best performance available without changing the source code of JavaFX itself.

Sabadell answered 13/8, 2021 at 21:15 Comment(1)
This could turn out as the best available option of how to do this in JavaFX. The performance is awesome, it's cross platform, Windows/ Linux and potentially MacOS is supported, you can use it with LGWL or JOGL and it offers DirectX support via NV_DX_interop. I use it for my real time video streaming app and despite small issues which the dev is currently working on its working flawlessly. Really one of the best open source libs I found in the past recent years.Tinctorial
S
1

Yes, by all means, you can use JOGL to achieve effectively everything you could in Java with OpenGL as you can in C/C++.

Secretory answered 5/6, 2013 at 17:17 Comment(2)
You sure can use JOGL or LWJGL to use OpenGL from Java, but it is really not that simple to implement it in an existing JavaFX window.Gracielagracile
This does not answer the question much. The question asks how, the answer just points in a general direction.Karlmarxstadt
U
1

PanamaGL allows rendering OpenGL scenes in a JavaFX canvas. It uses recent Oracle FFM API as of JDK19 to bind to OpenGL, which make it extremely simple to build.

Unwelcome answered 31/3, 2023 at 15:33 Comment(0)
H
0

DriftFX allows you to render any OpenGL content directly into JavaFX nodes. Direct means that there is no transfer between GPU and main memory. The textures never leave the GPU.

You can access the project on GitHub : DriftFX

Halfway answered 25/3, 2023 at 14:16 Comment(0)
S
0

You can simply use NewtCanvasJFX available in JOGL >= 2.4.0. I advise you to read my article that explains in details how to mix JOGL with JavaFX/OpenJFX. NewtCanvasJFX acts as a bridge between JOGL NEWT GLWindow used to perform OpenGL rendering and an OpenJFX/JavaFX group that you can add into a Scene, then you add the Scene into the Stage of your OpenJFX/JavaFX Application.

Silk answered 10/8, 2023 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.