WJ的Direct3D简明教程2:Render-To-Texture

转载请注明:来自http://blog.csdn.net/skyman_2001

Rendering to a texture is one of the advanced techniques in Direct3D. On the one hand it is simple, on the other hand it is powerful and enables numerous special effects, such as glow effect, environment mapping, shadow mapping, and many more. Rendering to a texture is simply an extension to rendering to a surface.
1. Create the texture:
LPDIRECT3DTEXTURE9 pRenderTexture = NULL;
LPDIRECT3DSURFACE9 pRenderSurface = NULL, pBackBuffer = NULL;

pDevice()->CreateTexture(256, // width
256, // height
1, // number of mip levels
D3DUSAGE_RENDERTARGET, // must be!
D3DFMT_R8G8B8, // format
D3DPOOL_DEFAULT, // memory pool(must be!)
&pRenderTexture,
NULL);
2. Get the surface:
In order to access the texture memory a surface object is needed, because a texture object in Direct3D always uses such a surface to store its data.

pRenderTexture->GetSurfaceLevel(0,&pRenderSurface); // top-level surface
3. Render to the texture:
pDevice()->GetRenderTarget(0,&pBackBuffer); // back buffer

// render-to-texture
// set new render target
pDevice()->SetRenderTarget(0,pRenderSurface);
//clear texture
pDevice()->Clear(0,
NULL,
D3DCLEAR_TARGET D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(255,255,255),
1.0f,
0);
// render the scene
pDevice()->BeginScene();
...
pDevice()->EndScene();
4. Render the final scene using the texture:
// render scene with texture
// set back buffer
pDevice()->SetRenderTarget(0,pBackBuffer);
pDevice()->Clear(0,
NULL,
D3DCLEAR_TARGET D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0,0,0),
1.0f,
0);
pDevice()->BeginScene();
// set rendered texture
pDevice()->SetTexture(0,pRenderTexture);
// render the final scene
...
pDevice()->EndScene();
pDevice()->Present(NULL,NULL,NULL,NULL);

Lastly, there are some restrictions you have to take care of: (1) the depth stencil surface must always be greater or equal to the size of the render target; (2) the format of the render target and the depth stencil surface must be compatible and the multisample type must be the same.  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值