Using Qt with DirectX? - c++

What exactly are my options? I have programs I need to write in OpenGL and DirectX, and I'd like to use Qt for OpenGL, and not have to re-implement half my program for the DirectX components of my task.
I've looked on Google and I have found references to people complaining about Direct3D being a dependency of Qt, and people talking about implementing QD3DWidget sub-classing QWidget in a similar fashion to QGLWidget, yet nobody talked about how to implement it or where any examples are.
I need help. I want to know if it is possible? What would I need to do to get it working? Has it been done before?

its pretty straightforward than I thought,
-> Create a QWidget
-> Override paintEngine() method, does nothing, just returns NULL
-> Assign HWND to widget->winId()
#ifdef USE_QTGUI
QApplication a(argc, argv);
CD3DWidget wndw; wndw.show(); wndw.resize(1280,960);
hWnd = wndw.winId();
#else
hWnd = CreateAppWindow(name,300,300);
#endif
//CD3DWidget class contains only the following definitions
CD3DWidget::CD3DWidget(QWidget * parent):QWidget(parent){ }
QPaintEngine *CD3DWidget::paintEngine (){ return NULL; }
Thanks,
CV

List of changes:
Qt 4.6 introduces many new features and improvements as well as bugfixes
over the 4.5.x series.
......................
The experimental Direct3D paint engine has been removed. The reason for
this is that Nokia focuses on OpenGL for desktop hardware accelerated
rendering.
......................

For all its worth, here is how to get Ogre3D's output to a Qt 4.5 window. Basically, you grab a rendering surface and you use it to render the output. This is a "Qt in D3D/OpenGL application approach".
Here are OpenGL examples with Qt 4.5 using OpenGL window.
If I understand it correctly, the Direct3D support is experimental and is only used for painting of windows.

Related

How to embed opencascade V3d_View in gtkmm widget

I'm trying to port the code from https://github.com/eryar/occQt to gtkmm, by creating a custom widget and overriding the Gtk::widget::on_realize() method like
void OccView::on_realize() {
// Create Aspect_DisplayConnection
Handle(Aspect_DisplayConnection) display_connection = new Aspect_DisplayConnection();
// Get graphic driver if it exists, otherwise initialize it.
Handle(Graphic3d_GraphicDriver) graphic_driver;
if (!graphic_driver) {
graphic_driver = new OpenGl_GraphicDriver(display_connection);
}
// Get window handle. This returns something suitable for all platforms.
Window x_window = GDK_SURFACE_XID(get_native()->get_surface()->gobj());
// Create window for platform.
Handle(Xw_Window) xw_window = new Xw_Window(display_connection, x_window);
// Create V3dViewer and V3d_View
mViewer = new V3d_Viewer(graphic_driver, Standard_ExtString("viewer3d"));
mView = mViewer->CreateView();
// Set window for the view
mView->SetWindow(xw_window);
if (!xw_window->IsMapped()) {
xw_window->Map();
}
// Create AISInteractiveContext
mContext = new AIS_InteractiveContext(mViewer);
// Set up lights etc
mViewer->SetDefaultLights();
mViewer->SetLightOn();
mView->SetBackgroundColor(Quantity_NOC_BLACK);
mView->MustBeResized();
mView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_GOLD, 0.08, V3d_ZBUFFER);
mContext->SetDisplayMode(AIS_Shaded, Standard_True);
// Call base method
Gtk::Widget::on_realize();
}
but the Gtk::Window stays empty after appending the OccView object. What am I doing wrong? Is there a working example on how to integrate the Opencascade V3d_View into a Gtk::Widget, or the gtkmm framework in general?
I haven't used GTK since university, so my experience is pretty basic here.
There are two basic approaches for embedding OpenGL-based viewer into GTK:
Ask OCCT to create OpenGL context for a native window taken from a normal Widget or entire window.
Wrap existing OpenGL context created by GUI library itself, e.g. Gtk::GLArea.
Your current code tries to follow the first approach used by conventional samples for Qt Widgets and MFC coming with OCCT. I guess it should be feasible, but implies some limitations and issues with mixing GTK widgets, as GTK will not be aware of OpenGL usage.
In contrast, Gtk::GLArea looks like a "modern" way for embedding OpenGL renderer designed by GTK developers and expected to work transparently.
Therefore, I've tried implementing a Hello-World sample using Gtk::GLArea (based on a development snapshot of OCCT 7.6.0dev):
https://github.com/gkv311/occt-samples-gtk
I don't bring the whole code of the sample here, as it is quite large in size.
Putting OCCT Viewer into Gtk::GLArea includes some tricky parts like:
Wrapping native Window into Aspect_Window (it could be also Xw_Window like in your sample, more general Aspect_NeutralWindow or another subclass).
Wrapping OpenGL context created by Gtk::GLArea into Aspect_RenderingContext/OpenGl_Context.
Wrapping offscreen buffer (FBO) used by Gtk::GLArea for rendering content into OpenGl_FrameBuffer.
Putting all viewer redraws into dedicated callback for Gtk::GLArea::signal_render().
Redirecting user input to viewer (with help of AIS_ViewController).
It is important to note, that GTK may be run in different context:
X11 server - X Window is created and GLX is used for OpenGL.
This is default OCCT configuration for Linux;
Wayland - native window is not X Window and EGL is used for OpenGL context.
OCCT does support EGL but as a dedicated configuration as alternative to GLX, while GTK handles this in runtime somehow. In addition, OCCT does not (yet) provide any wrapper for a Wayland native window, though it might be not critical for using.
GTK also has an option to use OpenGL ES instead of OpenGL.
Initially I expected Gtk::GLArea to work natively, but instead a very basic sample (without OCCT viewer) displays artifacts to me (widgets randomly blacked) on Xubuntu 18.04, though it works as expected on Ubuntu 21.04 (within Xorg session). I don't know if it is a bug fixed in GTK implementation, or there is something that should be fixed in a sample to workaround problem on older Linux.

Don’t use QT OpenGL anymore?

I am new to C++, OpenGL, and QT. In fact, I’ve never even touched QT. The reason for this is the following; I’ve made games in a few other languages in the past (mostly C# with Unity). Now I want to get into games with c++. However, I want to use pure C++, not even Unreal (maybe one day ), for games. To do this I know that for graphics I should almost definitely be using OpenGL (which I’m currently learning). I also am going to need to make GUIs. When I looked up what to use, it looked like the best option would probably be QT. This means I need QT to work with OpenGL. So again I looked it up. I found QT OpenGL on this post: How to work with OpenGL and QT?. When I looked up Qt OpenGL (https://doc.qt.io/qt-5/qtopengl-index.html) it said:
Warning: This module should not be used anymore for new code. Please use the corresponding OpenGL classes in Qt GUI.
So what is QT GUI?
And, how easy it to use QT with OpenGL?
As the new features of Qt 5.4 point out, the Qt Opengl module has been declared deprecated.
Deprecated features
Qt OpenGL:
The Qt OpenGL module (which contain classes that start with "QGL") is now deprecated in favor of the Qt GUI module (which contain
classes that start with "QOpenGL"). QGLWidget can now be replaced by
QOpenGLWidget.
That does not imply that:
the module has been removed but at any time Qt could remove it without notice.
You cannot use opengl in Qt, only you should not use the QGLX classes that belong to the Qt OpenGL package but you must use the QOpenGLX classes of the Qt Gui module, for example replace QGLWidget with QOpenGLWidget.
You can also find several examples here.
Qt is probably not the right tool for you. For one you want to use GLFW, which would cross into the turf of Qt and vice versa. A much better choice for game UI stuff is Dear ImGUI which is far better suited for the needs of game development.

QOpenGLWidget with QApplication?

We have a QWidget based application that was previously using a QWindow for OpenGL rendering. To fit that window in our application we had to use
QWidget QWidget::createWindowContainer(QWindow);
Previously we only used external to Qt OpenGL libraries for rendering.
We have a desire to switch from using QWindow to some kind of QWidget for compatibility with touch gestures, and generally better overall compatibility with the rest of our application. The latest recommended OpenGL compatible QWidget seems to be QOpenGLWidget, so we are trying to use that.
glContext is an OpenGLContext that we manage ourselves.
The problem now is that QOpenGLWidget is not a QSurface, so I cannot call
glContext->makeCurrent(this);
to make the context current on my custom QOpenGLWidget like we could before with our custom QWindow and our own OpenGLContext.
If I try to use QOpenGLWidget::makeCurrent(); then this uses the wrong context and tries to do stuff with some magical QT handled context or something I don't understand.
The result is that after creating our OpenGLContext when we try to call
glFunctions = glContext->versionFunctions<QOpenGLFunctions_3_3_Core>();
if(!glFunctions->initializeOpenGLFunctions())
printf("Could not initialize OpenGL functions.");
It always fails to initialize the OpenGL functions.
I have been reading over all the other resources I can find on the subject and came across this older Stack Overflow question that was similar:
QOpenGLWidget with shared OpenGL context?
The answer to that question did not solve my issue because we are using QApplication, not QGuiApplication as this is a QWidget based application.
QGuiApplicationPrivate::init() is never called and the QOpenGLContext::globalShareContext() returns a null pointer since it is not initialized.
Unfortunately I cannot wait for QOpenGLWidget::initializeGL() to be called to initialize the QT managed OpenGLContext since we already have lots of OpenGL resources over various classes that attempt to get initialized before that.
Has anyone else come across this same problem?
Any suggestions?
I am doing a little guess work here.
Probably your OpenGL is defaulting to ANGLE.
And your previous application OpenGL calls are platform specific OpenGL API.
try setting below attribute to your QApplication object.
appObject->setAttribute(Qt::AA_UseDesktopOpenGL, true);
then try calling
QOpenGLWidget::makeCurrent();
If things are still not working, configure your QT libraries for desktop OpenGL.
configure -opengl desktop
the below link gives you some information on Qt approach to OpenGL.
http://doc.qt.io/qt-5/windows-requirements.html
Put in comments, if this is not helping you, will delete the answer.

OpenGL in Qt Quick cross platform application

I'm trying to develop a cross platform (or at least desktop + embedded hardware) application. I would like to use Qt Quick to create a touch friendly GUI. I have been implemented a classical application with a QGLWidget displaying data. It is important that only a part of the window is in OpenGL. Because of this there are problems with EGLFS and LinuxFB. Only X11 (or maybe Wayland) can display the application properly (others generates a couple of errors about missing setParent function and the whole screen is black). Now I'm trying to achieve the same thing in QML. I want to use this OpenGL renderer as part of my QML application and some Qt Quick widgets around it. I found a couple of people asking about the same thing and the answer is always to subclass QDeclarativeItem and call the painter's beginNativePainting() (the others says to export it through QDeclarativeItem, but I cannot figure out how to do this). The problem is that on desktop, Qt 5.11 the native painter is not OpenGL. And in QT5 there is no way to force OpenGL graphics system. So when I try to get the OpenGL context (QGLContext::currentContext()) I always get NULL. Another problem: If I export my widget with qmlRegisterType("Test", 1, 0, "Test"); it becomes only visible when I use QDeclarativeView, but then it doesn't sees Qt Quick. If I use QQuickView it says module "Test" is not installed. How can I implement this properly?
QDeclarativeItem is from Qt Quick 1 and Qt4. With Qt 5 and Qt Quick 2 you should use QQuickItem.
There is at least 1 example of this provided with qt docs, which you can find in Qt Creator in the Welcome tab in the Examples section.

GUI Toolkit in OpenGL

I'd like to develop an application (in C++) similar to Pure Data, but with a cool GUI and a better documentation... Yes, something like Max/MSP or Reaktor, but free and open!
In order to create an appealing, reactive and portable interface I was thinking of using OpenGL.
In my mind there is something like Blender GUI.
Before starting to develop my custom GUI toolkit I googled around in order to understand if there exist something that I could use, and I found:
Juce: it seems quite supported, but I didn't understand if you can only embed OpenGL canvas in your interface or it is possible to render all the widgets using OpenGL.
nUI: it seems really cool and portable, but... Its forum is a desert, and it's really hard to find a tutorial!
ceGUI, FLTK, GLUI: so flat and gray ;-) and any aren't still maintained.
Do you know other toolkit? As you understand I'm looking for a portable library (in C++), fast and supported.
The other possibility is developing from scratch my custom toolkit using SDL or Freeglut, in this case which could be the best solution?
P.S.:
Reading other threads about this topic I noticed that many devs suggest using Qt... Could Qt relies on OpenGL for rendering? Or it could only host OpenGL canvas? Anyway do you think is possible (with good performance) creating something like this in Qt:
Reaktor GUI example 1
Reaktor GUI example 2
Blender Compositing example
There isn't really a good openGL toolkit, they tend to get invented for a particular app and then sort of abandoned.
Yes Qt works very well with openGL, there is an openGL QGlWidget with full hardware acceleration (and optional links to openCL). You can have as many QGLwidgets as you like in a Qt app - each with their own openGL commands inside them.
You can also mix Qt and openGL in the same QGlWidget (http://doc.qt.nokia.com/qq/qq26-openglcanvas.html)
Slightly off topic: You can also select Qt to use openGL for all it's rendering - this is still a bit experimental but means that 2d Qt can be much faster on some embedded platforms like phones.
edit: To clarify -the entire app are still normal Qt but drawn with openGL commands 'under the hood'
I like Clutter and MX.
See http://www.clutter-project.org/ and https://github.com/clutter-project/mx
Qt 5.7 and up offers QtQuick Controls 2.0 in QML, which are implemented in OpenGL.
http://doc.qt.io/qt-5/qtquickcontrols2-index.html
Their API is very stable and works on Android, iOS, macOS, Windows, GNU/Linux, etc.
Here is a small hello world in a great book about QML. https://qmlbook.github.io/en/ch02/index.html#hello-world
You write JavaScript in QML and the QMake build system turns it into C++ object code.
import QtQuick 2.5
Rectangle {
width: 360
height: 360
Text {
anchors.centerIn: parent
text: "Hello World"
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}