#1316

Simplest way to render with OpenGL in Rust Qt program?

Openfaernopened 1y ago

I'm trying to figure out how I can use OpenGL to draw parts of my UI that is otherwise a Qt Quick app using this library. An example in the examples/ dir would be amazing, and I can help contribute one once I understand how to to make one.

Will I be able to achieve this using only Rust + QML? Or will C++ be needed? What I have found/read so far:

Activity

2
LeonMatthesKDAB1y ago

I think @vimpostor or @ahayzen-kdab can answer this question best.

ahayzen-kdab1y ago

There are multiple ways of integrating OpenGL with QML which adds to the fun.

With the Servo demo we went with https://doc.qt.io/qt-6/qquickframebufferobject.html as this allows for using it as a normal QML element. For this to work we needed to implement two classes in Rust QQuickFramebufferObject and QQuickFramebufferObject::Renderer.

For the QQuickFramebufferObject this is implemented via the ServoWebView https://github.com/KDABLabs/cxx-qt-servo-webview/blob/main/src/webview.rs#L67 the only required method to implement there is the cxx_override one of createRenderer https://github.com/KDABLabs/cxx-qt-servo-webview/blob/main/src/webview.rs#L359 Everything else in this class is for passing input info to Servo.

For the QQuickFramebufferObject::Renderer this is implemented via QServerRenderer https://github.com/KDABLabs/cxx-qt-servo-webview/blob/main/src/renderer.rs#L44 here you must implement the render method and can implement the synchoronize method. In the render method you then need to render to your FBO, in the servo demo we blit the texture from servo to the FBO. You can also see in the initialise method we ask servo to find the current context.

You will likely need some C++ we had some to help going between certain types, although now that CXX supports static methods some of these might not be needed :-)

Generally it's all quite fiddly and I agree and simple example would be good of how to draw a OpenGL triangle from Rust 😅