最简单的d3d

#include "3D_Draw.h"

/************************************************************************/
/* initializing Direct3D steps       
1. fill IDirect3D9 and IDirect3DDevice9
2. D3DCAPS9, check the display adapter supports hardware vetex processing or not
3. D3DPRESENT_PARAMETERS, it will specify the characteristics of the IDirect3DDevice9
4. Create the IDirect3DDevice9, it will represents the physical hardware device
*/
/************************************************************************/

IDirect3D9* _d3d9 = NULL;
IDirect3DDevice9* g_pd3dDevice = NULL;
D3DDEVTYPE deviceType = D3DDEVTYPE_HAL;
HRESULT InitD3D( HWND hWnd)
{
//1.Create IDirect3D9, 这个对象有两个用途,一个是设备枚举,另一个就是创建IDirect3DDevice9
	//设备枚举:device enumeration 获取显卡的性能,显示模式,格式及其他信息
	_d3d9 = Direct3DCreate9(D3D_SDK_VERSION); //D3D_SDK_VERSION 保证使用正确的头文件

	if (NULL == _d3d9)
	{
	::MessageBox(NULL,"create Direct3D failed",NULL,0);
	return 0;
	}

//2. Check for hardware vp.检查定点运算类型
	//not all cards support hardware vertex processing, we must check it
	//建议在使用之前总是检查一下设备性能性能,看设备是否支持该特性

	//Fill the D3DCAPS9 structure with the apabilities of the primary display adapter
	D3DCAPS9 caps;
	_d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,    //Specifies the physical display adapter
		deviceType,                             //Specifies the device type to use or software device
		&caps);                                 //return the initialized capabilities structure
	// Can we use hardware vertex processing?
	int vp = 0; //注意,这个地方用vp保存了定点运算类型
	if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
	{
		// yes, save in 'vp' the fact that hardware vertex
		// processing is supported.
		vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
	}
	else
	{
		// no, save in 'vp' the fact that we must use software
		// vertex processing.
		vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
	}

//3. Filling Out the D3DPRESENT_PARAMETERS Structure
	//this structure is used to specify some of the characteristics of the IDirect3DDevice9
	D3DPRESENT_PARAMETERS d3dpp;
	d3dpp.BackBufferWidth               = 800;
	d3dpp.BackBufferHeight              = 600;
	d3dpp.BackBufferFormat              = D3DFMT_A8R8G8B8; // pixel format
	d3dpp.BackBufferCount               = 1;
	d3dpp.MultiSampleType               = D3DMULTISAMPLE_NONE;
	d3dpp.MultiSampleQuality            = 0;
	d3dpp.SwapEffect                    = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow                 = hWnd;
	d3dpp.Windowed                      = true;
	d3dpp.EnableAutoDepthStencil        = true;
	d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8; // depth format
	d3dpp.Flags                      = 0;
	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
	d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;

	HRESULT hr = _d3d9->CreateDevice(
		D3DADAPTER_DEFAULT,
		deviceType,
		hWnd,
		D3DCREATE_HARDWARE_VERTEXPROCESSING,
		&d3dpp,
		&g_pd3dDevice
		);

	if(hr != S_OK)
	{
		::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
		return 0;
	}

	return hr;

}
void Cleanup()
{

}
void Render()
{
	if( g_pd3dDevice )
	{
		g_pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
			0x00000000, 1.0f, 0);
		g_pd3dDevice->Present(0, 0, 0, 0);// present backbuffer
	}
	return ;

}
HRESULT InitVB()
{
	return 0;
}
void SetupMatrices()
{

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要了解一下 QVideoWidget 的原理。QVideoWidget 是一个继承自 QWidget 的类,用于在 QWidget 上显示视频。它是通过使用 QAbstractVideoSurface 作为后端实现的,可以接收来自 QMediaPlayer 或其他支持 QAbstractVideoSurface 的视频源的数据,并将其渲染到 QWidget 上。 要播放 D3D 视频,需要使用 Direct3D 渲染器来将视频渲染到 QVideoWidget 上。这可以通过创建一个 D3D 纹理并将其与 QAbstractVideoSurface 关联来完成。下面是一个最简单的实现示例: ```cpp #include <QWidget> #include <QAbstractVideoSurface> #include <d3d9.h> class D3DVideoSurface : public QAbstractVideoSurface { public: D3DVideoSurface(QWidget* widget, IDirect3DDevice9* device) : m_widget(widget), m_device(device) { HRESULT hr = device->CreateTexture(640, 480, 1, D3DUSAGE_DYNAMIC, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_texture, NULL); Q_ASSERT(SUCCEEDED(hr)); } QList<QVideoFrame::PixelFormat> supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const override { Q_UNUSED(handleType); return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_RGB32; } bool present(const QVideoFrame& frame) override { Q_UNUSED(frame); IDirect3DSurface9* surface = nullptr; HRESULT hr = m_texture->GetSurfaceLevel(0, &surface); Q_ASSERT(SUCCEEDED(hr)); D3DLOCKED_RECT rect = { 0 }; hr = surface->LockRect(&rect, NULL, D3DLOCK_DISCARD); Q_ASSERT(SUCCEEDED(hr)); const int stride = rect.Pitch / 4; const uchar* src = frame.bits(); uchar* dst = reinterpret_cast<uchar*>(rect.pBits); for (int y = 0; y < frame.height(); ++y) { memcpy(dst + y * rect.Pitch, src + y * frame.bytesPerLine(), frame.width() * 4); } hr = surface->UnlockRect(); Q_ASSERT(SUCCEEDED(hr)); hr = m_device->BeginScene(); Q_ASSERT(SUCCEEDED(hr)); hr = m_device->StretchRect(surface, NULL, m_widget->winId(), NULL, D3DTEXF_LINEAR); Q_ASSERT(SUCCEEDED(hr)); hr = m_device->EndScene(); Q_ASSERT(SUCCEEDED(hr)); surface->Release(); return true; } private: QWidget* m_widget; IDirect3DDevice9* m_device; IDirect3DTexture9* m_texture; }; class D3DVideoWidget : public QWidget { public: D3DVideoWidget(QWidget* parent = nullptr) : QWidget(parent) { HRESULT hr = Direct3DCreate9Ex(D3D_SDK_VERSION, &m_d3d); Q_ASSERT(SUCCEEDED(hr)); D3DPRESENT_PARAMETERS params = { 0 }; params.Windowed = TRUE; params.SwapEffect = D3DSWAPEFFECT_DISCARD; params.BackBufferFormat = D3DFMT_UNKNOWN; params.BackBufferWidth = width(); params.BackBufferHeight = height(); params.EnableAutoDepthStencil = TRUE; params.AutoDepthStencilFormat = D3DFMT_D16; hr = m_d3d->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winId(), D3DCREATE_HARDWARE_VERTEXPROCESSING, &params, NULL, &m_device); Q_ASSERT(SUCCEEDED(hr)); m_surface = new D3DVideoSurface(this, m_device); } virtual QPaintEngine* paintEngine() const override { return nullptr; } virtual QAbstractVideoSurface* videoSurface() const override { return m_surface; } private: IDirect3D9Ex* m_d3d; IDirect3DDevice9Ex* m_device; D3DVideoSurface* m_surface; }; ``` 在这个示例中,我们创建了一个名为 D3DVideoWidget 的 QWidget 子类。它使用 Direct3D 9 渲染器将视频渲染到 QWidget 上。我们还创建了一个名为 D3DVideoSurface 的 QAbstractVideoSurface 子类,它将视频帧渲染到 Direct3D 纹理上,并在每一帧结束后将纹理渲染到 QWidget 上。 最后,我们可以使用 QMediaPlayer 或其他支持 QAbstractVideoSurface 的视频源来播放 D3D 视频。例如: ```cpp #include <QApplication> #include <QMediaPlayer> int main(int argc, char* argv[]) { QApplication app(argc, argv); D3DVideoWidget* videoWidget = new D3DVideoWidget; videoWidget->show(); QMediaPlayer* player = new QMediaPlayer; player->setVideoOutput(videoWidget->videoSurface()); player->setMedia(QUrl("file:///C:/test.avi")); player->play(); return app.exec(); } ``` 这个示例展示了如何使用 QMediaPlayer 和 D3DVideoWidget 播放 D3D 视频。我们将 QMediaPlayer 的视频输出设置为 D3DVideoSurface,这将自动将视频帧传递给 D3DVideoSurface 的 present() 方法进行渲染。然后,我们调用 play() 方法开始播放视频。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值