多窗口D3D程序应如何去实现?

天堂里的死神 2005-02-16 02:46:36
查阅DX文档似乎没有这方面的Sample。

OGRE多窗口是多个RenderWindow,每个RenderWindow初始化的时候都会通过CreateDevice创建一个与这个Window相对应的Device,然后创建SwapSurface和ZBuffer。但只有第一个窗口是最重要的窗口,这个窗口初始化的时候会根据这个窗口的Device来创建TextureManager和MaterialManager。
这就有个问题:像OGRE这样来实现(也没有见到有人用OGRE这么做的例子),用一个窗口的Device建立的TextureManager和Texture在另一个Device的窗口上使用的时候不会出问题么?

多窗口的游戏如果真需要使用多Device和多个SwapChain来实现,应当怎么来实现呢?有没有相应的文档、教程或者其他资源?

很急,望指教 ^_^
...全文
850 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
天堂里的死神 2005-02-18
  • 打赏
  • 举报
回复
多谢大家,这么短的时间内就能解决,真的非常感谢。
chijingde 2005-02-17
  • 打赏
  • 举报
回复
呵呵

就为了楼主的翻译

也要顶一下


PS:3DMAX是后者
寻开心 2005-02-17
  • 打赏
  • 举报
回复
能够解决各个窗口独立显示之后,剩下的就是两个窗口之间的通讯问题了,不难理解阿。
天堂里的死神 2005-02-17
  • 打赏
  • 举报
回复
哦,这样子,多谢你。要这样看来的话,多窗口还真的是没什么用处……

还有最后一个问题了,昨天晚上用魔兽地图编辑器的时候想到的,魔兽编辑器有一个单位编辑功能,也就是地图在右边的区域内,但左边有一个与之分离的单位视窗,用来显示当前选定单位的动画模型。
还会悬浮一个悬浮窗口,全是图片,点某个图片就可以在地图上栽种这个单位。

这种风格的东东应该是多窗口吧?

盼望回复 ^_^
天堂里的死神 2005-02-16
  • 打赏
  • 举报
回复
OGRE真害人啊!!!哼!!!发现了!!!
原来D3D9插件里面RenderWindow的Create语句里面有这一条292行:
if( driver && mParentHWnd == NULL )
{
//创建Device
}
else
mpDevice=NULL;

而现实是,mParentHWnd至今为止所有赋值中都只有一个NULL,所以OGRE里面,只要使用了D3D9插件,永远只能创建单窗口程序。
多窗口程序?那里大大的一个else mpDevice=NULL;怎么可能创建得出多窗口程序嘛 T_T,
唉~~怎么看代码的时候这么不小心,这句if竟然没看进去,哭T_T_T_T_T_T
天堂里的死神 2005-02-16
  • 打赏
  • 举报
回复
多谢happy__888先生,翻译的同时也渐渐明白了OGRE的做法,但是OGRE的做法还是有很不明白的地方,下一楼讨论,这一楼放上小生翻译后的文章,望指正。


在DX8中,对多窗口的支持是通过创建更多的Swap Chains来提供的。SDK中没有相关的例子而且文档也只是泛泛而谈。这篇文章就是为了解决这个问题,它将向您展示应当如何一步步地实现在多个分离窗口中渲染多个视图。

第一步:设置父框架窗口

在多视图的应用程序中,我们需要从最高层次的框架——这个框架将包含所有在他用户区之内的子视图窗口——开始我们的旅程。当父框架创建的时候,我们需要创建Direct3D Device接口,为其指定使用窗口模式,而且设置这最高层次的窗口句柄作为“焦点窗口”(Focus Window怎么翻译 T_T?)的句柄:

g_pD3D=Direct3DCreate8(D3D_SDK_VERSION);
if (!g_pD3D) return -1;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
// Use the current display mode. 使用当前的显示模式
D3DDISPLAYMODE mode;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &mode))) {
SAFE_RELEASE(g_pD3D);
return -1;
}
d3dpp.BackBufferFormat = mode.Format;
d3dpp.BackBufferWidth = mode.Width;
d3dpp.BackBufferHeight = mode.Height;
d3dpp.EnableAutoDepthStencil=TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// m_hWnd is handle to top level window m_hWnd是最高层窗口的句柄
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice) ) ) {
SAFE_RELEASE(g_pD3D);
return -1;
}

注意上面代码处于简单考虑并没有去测试深度缓存的格式(?depth format),而只是选择了一个确定的格式(D3DFMT_D16)。您的程序应该为需要渲染的Render Target选择一个可接受的深度缓存格式。

Device都需要有帧缓存,这样子视图才能进行渲染,同时,深度缓冲也应当被不同的视图进行共享。帧缓存和深度缓存都被设置为全屏幕大小,以考虑到可能窗口会被改变大小的情况。如果不的话,窗口改变大小的时候,就需要Reset Device和重新创建Swap Chain。

Step 2 - Setting Up View Windows
第二步:设置子视图窗口

现在我们可以准备创建我们的子窗口也就是视图窗口,并把它们与交换链关联以使得他们可以被渲染到Device上。当窗口创建后,下面的代码将为子窗口创建一个交换链:

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
// Use the current display mode. 使用当前的显示模式
D3DDISPLAYMODE mode;
g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &mode);
d3dpp.BackBufferFormat = mode.Format;
// m_hWnd contains child window handle m_hWnd储存子窗口的句柄
d3dpp.hDeviceWindow=m_hWnd;
// m_pSwapChain is IDirect3DSwapChain * m_pSwapChain是一个IDirect3DSwapChain*对象
g_pd3dDevice->CreateAdditionalSwapChain(&d3dpp, &m_pSwapChain);

经过这些代码之后,m_pSwapChain变量就储存了IDirect3DSwapChain接口的指针,这个接口将储存子窗口视图区所对应的帧缓冲。

Step 3 - Rendering a View
第三步:渲染视图

在渲染每个视图窗口之前,我们必须使得Device来渲染对应的帧缓冲,这我们就需要用到SetRenderTarget方法。我们向其中传入子窗口SwapChain交换链的后备缓冲BackBuffer,以及使用最开始跟着Device一起创建的深度缓冲。

LPDIRECT3DSURFACE8 pBack=NULL,pStencil=NULL;
m_pSwapChain->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&pBack);
g_pd3dDevice->GetDepthStencilSurface(&pStencil);
g_pd3dDevice->SetRenderTarget(pBack,pStencil);
pBack->Release();
pStencil->Release();

注意我们必须Release掉Stencil和BackBuffer的指针,因为GetBackBuffer和GetDepthStencilSurface这两个函数都会调用COM的AddRef方法,来增加相应COM接口的引用计数,因此如果不删除它们,将会导致内存泄露。

我们现在已经做好准备渲染视图窗口了。渲染的方法看起来和我们平常用的方法差不多,只是有一点:我们现在需要调用Swap Chain的接口,而不是Device的接口。

g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0,0);
if (SUCCEEDED(g_pd3dDevice->BeginScene())) {

// rendering code goes here 渲染代码写在这里

g_pd3dDevice->EndScene();
}
m_pSwapChain->Present(NULL,NULL,NULL,NULL);

Step 4 - Handling Resize of Child Views
第四步,子窗口的Resize问题

如果窗口的视图区大小和SwapChain的大小不一,那么DirectX将通过Stretch Blit来自动处理图像的伸缩变化。尽管这可能并不令人期待,因为这在视图区变大的时候将导致图像的模糊。

如果要解决这个问题,您需要为子窗口的WM_SIZE消息写一段处理代码,这段代码Release已经存在的Swap Chain交换链,并且使用第二步中的代码创建一个崭新的Swap Chain交换链。
天堂里的死神 2005-02-16
  • 打赏
  • 举报
回复
2 chijingde ^_^:
是前者,像3DMax 那样的应该也是前者还是后者呢?其实主要是看OGRE做了个这么个东西,好奇所以问问。

2 happy__888 ^_^:
多谢你,我英语差得翻译一会儿,您帮我看看理解是否正确好吗?谢谢
寻开心 2005-02-16
  • 打赏
  • 举报
回复
Introduction
In DirectX 8, support for rendering to multiple windows is provided through the creation of additional swap chains. However, there are currently no examples of this in the SDK, and the documentation is a bit vague. This article is provided to fill the gaps, and will explain the steps you need to take to write an application that will render multiple views in separate windows.

Step 1 - Setting Up The Parent Frame
In an application with multiple views, we start with a top level frame that will contain child windows in its client area to display various views. Once the parent frame parent frame has been created, we create our Direct3D device interface, specifying windowed mode and setting the top level window handle as the focus window:

g_pD3D=Direct3DCreate8(D3D_SDK_VERSION);
if (!g_pD3D) return -1;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
// Use the current display mode.
D3DDISPLAYMODE mode;
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &mode))) {
SAFE_RELEASE(g_pD3D);
return -1;
}
d3dpp.BackBufferFormat = mode.Format;
d3dpp.BackBufferWidth = mode.Width;
d3dpp.BackBufferHeight = mode.Height;
d3dpp.EnableAutoDepthStencil=TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// m_hWnd is handle to top level window
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice) ) ) {
SAFE_RELEASE(g_pD3D);
return -1;
}

Note that for simplicity the above code does not test depth format, instead choosing a fixed format. Your application should determine a compatible depth format for the format of the rendering target.

The device has a frame buffer, which the child views will be rendered into, as well as a depth buffer which will be shared among the views. The frame buffer and depth buffer are sized to the full screen resolution, to allow for the fact that the window may later be resized. Otherwise, window size changes would require resetting the device and re-creating the swap chains.

Step 2 - Setting Up View Windows
Now we are ready to create our view windows, and associate them with swap chains that can be rendered to the device. Once the windows have been created, the following code generates a swap chain for the child window:

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
// Use the current display mode.
D3DDISPLAYMODE mode;
g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &mode);
d3dpp.BackBufferFormat = mode.Format;
// m_hWnd contains child window handle
d3dpp.hDeviceWindow=m_hWnd;
// m_pSwapChain is IDirect3DSwapChain *
g_pd3dDevice->CreateAdditionalSwapChain(&d3dpp, &m_pSwapChain);

After executing this code, the m_pSwapChain variable will contain a pointer to an IDirect3DSwapChain interface, which contains a frame buffer corresponding to the client area of the child window. This process is performed for each view window, so that that there is a swap chain for each view window.

Step 3 - Rendering a View
Prior to rendering each view, we must direct the device to render to the appropriate frame buffer, using the SetRenderTarget() method. We pass the back buffer from the window's swap chain, while using the depth buffer that was originally created with the device:

LPDIRECT3DSURFACE8 pBack=NULL,pStencil=NULL;
m_pSwapChain->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&pBack);
g_pd3dDevice->GetDepthStencilSurface(&pStencil);
g_pd3dDevice->SetRenderTarget(pBack,pStencil);
pBack->Release();
pStencil->Release();

Note that we release the stencil and backbuffer pointers after we use them, because the GetBackBuffer() and GetDepthStencilSurface() functions call AddRef() on these interfaces to increment their reference counters. Failing to release them would lead to a memory leak.

We are now ready to render the view. Rendering is performed within a scene in the normal manner, except that we call Present() on the swap chain interface rather than the device interface:

g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0x00000000,1.0,0);
if (SUCCEEDED(g_pd3dDevice->BeginScene())) {

// rendering code goes here

g_pd3dDevice->EndScene();
}
m_pSwapChain->Present(NULL,NULL,NULL,NULL);

Step 4 - Handling Resize of Child Views
DirectX will automatically deal with changes in the child view by using a stretch blit to present the swap chain if the dimensions have client area is not the same size as the swap chain's frame buffer. However, this may not be desirable, as it will cause aliasing if the client area is increased in size.

To prevent this, you can write a handler for the WM_SIZE message of the child window. The handler should release the existing swap chain, and create a new swap chain using the code from Step 2.

chijingde 2005-02-16
  • 打赏
  • 举报
回复
看不太明白

是多窗体还是在一个窗体里多个显示区域阿?

前者有必要吗?

反正肯定没法同时交互,所以还不如做成后者呢……

8,304

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧