duohafa / QtDirect3D

QDirect3DWidget implementation similar to the built-in QOpenGLWidget

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Project Status Pull Requests Welcome


Qt Direct3D / DirectX Widgets

This project contains Direct3D widgets to use within the Qt Framework for DirectX 9, 10, 11 and 12.

There are also Direct3D widgets that supports Dear ImGui that you can find within this project.

Contents

Getting Started

Clone the repository:

git clone --recursive https://github.com/giladreich/QtDirect3D.git

The main interesting directories within this project are source and examples:

  • source - Containing the widgets that you can copy to your projects - depending on the Direct3D version you use. Note that under each Direct3D version, there are also ImGui widgets - depending on your use case, you can decide which widget to use.

  • examples - Containing Qt projects that integrate the widgets - showing real examples of how to use and interact with the widgets.

Prerequisites

  • Visual Studio 2019 - Also note that if you prefer to use Visual Studio as the IDE, rather than QtCreator, make sure to also install Qt VS Tools extension.

  • Qt 5.15.0 - install the following kits -> msvc2019 & msvc2019_64 (might work with other Qt versions that uses QtMsBuild).

  • CMake - You can install any version greater than v12.

Build Examples

Building the project can be done in the following ways:

Using the Widget

In your Qt Widgets Application project, do the following steps:

  • Copy into your project the widget you chose to use and add it as an existing item.
  • In your MainWindow.ui file, you will need to promote the centeralWidget to the widget you copied.
  • For simplicity and purpose of this example, rename centeralWidget to view.
  • Compile in order to let Qt uic & moc compiler do their magic and have proper intellisense.

At this point you should have a basic setup to get started and interact with the widget.

In the MainWindow.h file, you will need to create the following Qt slots:

public slots:
    void init(bool success);
    void tick();
    void render();
    void renderUI();
Slot Remarks
bool init(bool success) Additional initialization step. success will be true if the widget successfully initialized.
void tick() Update the scene, i.e. change vertices positions.
void render() Present the scene.
void renderUI() Present ImGui windows.

In your MainWindow.cpp file, connect the Widget's signals to our previously created slots:

connect(ui->view, &QDirect3DXXWidget::deviceInitialized, this, &MainWindow::init);
connect(ui->view, &QDirect3DXXWidget::ticked, this, &MainWindow::tick);
connect(ui->view, &QDirect3DXXWidget::rendered, this, &MainWindow::render);
connect(ui->view, &QDirect3DXXWidget::renderedUI, this, &MainWindow::renderUI);

That's it! At this point you can start adding your own implementation to it.

Note that the scene will not start processing frames immediately, because it gives us better control in the MainWindow when we are ready to start. A good practice would be to add this line in the MainWindow::init function:

QTimer::singleShot(500, this, [&] { ui->view->run(); });

We give it a short delay of 500 milliseconds in case things are still initializing in the background before we start processing frames.

Handling Close Event

To properly close the application and clean up any used resources, we override and manipulate the closeEvent. When the user exit the application, we call event->ignore() to postpone the close event and then we call release on our widget. Lastly, giving the application a short delay of 500 milliseconds before we finally accept the close event:

void MainWindow::closeEvent(QCloseEvent * event)
{
	event->ignore();

	ui->view->release();
	m_bWindowClosing = true;
	QTime dieTime = QTime::currentTime().addMSecs(500);
	while (QTime::currentTime() < dieTime)
		QCoreApplication::processEvents(QEventLoop::AllEvents, 100);

	event->accept();
}

Show Case

Using the widget with ImGui and creating a basic scene:

Qt and ImGui

Game Engine editor using the widget (YouTube playlist):

YouTube Playlist

FX Editor

Flag Editor

Motivation

I've been working with MFC GUI applications for quite a while and also used Qt in other projects of mine. When it comes to Desktop GUI applications, I think there is no question or doubt that using Qt is probably a better choice for many reasons.

While trying to port some of the MFC applications to use Qt, I noticed that Qt only provides QOpenGLWidget, but no QDirect3DWidget. This is the point where I started researching the subject and realized that it can be done in different ways.

For simplicity and portability reasons, I decided to disable the rendering engine Qt internally are using for painting into the widget surface. This allowed me to draw into the surface of the widget without other rendering engines interfering with the scene. Having this achieved, made it exciting using the power of Qt, whilst using Direct3D API as the rendering engine.

Contributing

Pull-Requests are greatly appreciated should you like to contribute to the project.

Same goes for opening issues; if you have any suggestions, feedback or you found any bugs, please do not hesitate to open an issue.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the license for more details.

About

QDirect3DWidget implementation similar to the built-in QOpenGLWidget

License:MIT License


Languages

Language:C 54.5%Language:C++ 44.0%Language:CMake 1.4%Language:Shell 0.1%