游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2662|回复: 5

Copy Texture (RenderTarget) to Texture (RenderTarget)

[复制链接]

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
发表于 2008-4-16 01:17:00 | 显示全部楼层 |阅读模式
I create two render traget texture.
First one is set to be SetRendeTarget and draw some 3d objects in it.
And then I want to copy the content of first texture to the second texture, they are both Usage of RenderTraget. Could somebody tell me how can I do reach the gaol?
Now I copy them by Shader.My method is as below:
If first texture is filled some pixel of rendering some 3d objects and second texture is the destination texture.
1.Set second as RenderTraget by function SetRenderTarget
2.Set technique and the pass to draw the first texture as a fullscreen-quad(fill the screen,the texture size is equal to be the screen size)
3.Render the first texture
4.EndRednerTarget
5.I get the second texture copy from first texture

But the second texture has a little distortion with first texture.
(equal pixel=520372,not equal pixel=3916(texture size is 1024x512)
The different pxiel between first texture and second texture is 3916.

How can I copy two RenderTraget texture without any distortion?


  1. //the way I Create texture with RenderTraget
  2.         // setup shadow map objects
  3.         V_RETURN(m_pd3dDevice->CreateTexture(
  4.                 m_nSMTileWidth,
  5.                 m_nSMTileHeight,
  6.                 1,
  7.                 D3DUSAGE_RENDERTARGET,
  8.                 SHADOW_MAP_FORMAT,
  9.                 D3DPOOL_DEFAULT,
  10.                 &m_pShadowMapTex,
  11.                 NULL
  12.                 ));

  13.         V_RETURN(m_pShadowMapTex->GetSurfaceLevel(0, &m_pShadowMapSurf));

  14. //CShadowResultTexture class has a RanderTraget Texture in it and the texture create use the way below.
  15. //use shader to copy two textures
  16. void CQVSM::CopyTextureFromTo(CShadowResultTexture* pNewTexture,CShadowResultTexture* pOldTexture)
  17. {
  18.         HRESULT hr;

  19.         D3DXHANDLE hTechnique = m_pEffect_QVSM_ADAPTIVE->GetTechniqueByName("TShadowQVSM_ADPATIVE1");

  20.         m_pEffect_QVSM_ADAPTIVE->SetTechnique(hTechnique);
  21.         V(m_pEffect_QVSM_ADAPTIVE->Begin(NULL, 0));
  22.         {
  23.                 hr=pOldTexture->StartRenderTarget();
  24.                 hr=pOldTexture->Clear(COLOR_WHITE);

  25.                 V(m_pEffect_QVSM_ADAPTIVE->BeginPass(0));
  26.                 {
  27.                         // Set the vertex stream, shader, and texture

  28.                         hr=m_pEffect_QVSM_ADAPTIVE->SetBool(bUse3D_Way,TRUE);
  29.                         hr=m_pEffect_QVSM_ADAPTIVE->SetMatrix(WorldViewProjectionMatrix,&(m_matWorld * m_matView * m_matProj));
  30.                         hr=m_pEffect_QVSM_ADAPTIVE->SetTexture("TestBaseTexture", pNewTexture->GetTexture());

  31.                         SettingVertexDeclarationAndStreamSource();

  32.                         m_pEffect_QVSM_ADAPTIVE->CommitChanges();

  33.                         // Draw the vertex buffer
  34.                         hr=m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
  35.                 }

  36.                 hr=m_pEffect_QVSM_ADAPTIVE->EndPass();
  37.                 hr=pOldTexture->EndRenderTarget();
  38.         }

  39.         hr=m_pEffect_QVSM_ADAPTIVE->End();

  40. }

  41. // vertext strucutre (just see the 3D)

  42. // now using
  43. // The 3-D vertex format and descriptor
  44. typedef struct {
  45.         FLOAT x, y, z, w;     // 3-D coordinates
  46.         FLOAT u, v;        // Texture coordinates
  47. } sVertex_3D;
  48. #define VERTEXFVF_3D (D3DFVF_XYZW | D3DFVF_TEX1)

  49. // now no used
  50. // The 2-D vertex format and descriptor
  51. typedef struct {
  52.         FLOAT x, y, z;     // 2-D coordinates
  53.         FLOAT rhw;         // rhw
  54.         FLOAT u, v;        // Texture coordinates
  55. } sVertex_2D;
  56. #define VERTEXFVF_2D (D3DFVF_XYZRHW | D3DFVF_TEX1)

  57. //set some vertex format and some parameters

  58. HRESULT CQVSM::SettingVertexDeclarationAndStreamSource()
  59. {
  60.         HRESULT hr;

  61.         if (m_bUseVertexDecl)
  62.         {
  63.                 hr=m_pd3dDevice->SetVertexDeclaration(m_decl);
  64.                 hr=m_pd3dDevice->SetFVF(NULL);
  65.         }
  66.         else
  67.         {
  68.                 if (m_bUse3D_Way)
  69.                 {
  70.                         hr=m_pd3dDevice->SetFVF(VERTEXFVF_3D);//SetVertexShader
  71.                 }
  72.                 else
  73.                 {
  74.                         hr=m_pd3dDevice->SetFVF(VERTEXFVF_2D);//SetVertexShader
  75.                 }
  76.         }

  77.         if (m_bUse3D_Way)
  78.         {
  79.                 hr=m_pd3dDevice->SetStreamSource(0, g_pVB, 0,sizeof(sVertex_3D));
  80.         }
  81.         else
  82.         {
  83.                 hr=m_pd3dDevice->SetStreamSource(0, g_pVB, 0,sizeof(sVertex_2D));
  84.         }

  85.         return S_OK;
  86. }



复制代码



  1. //shader code

  2. texture TestBaseTexture;
  3. sampler tex_2d = sampler_state
  4. {
  5.         Texture = <TestBaseTexture>;
  6.         MinFilter = LINEAR;
  7.         MagFilter = LINEAR;
  8.         MipFilter = NONE;

  9.         AddressU = Clamp;
  10.         AddressV = Clamp;
  11. };

  12. struct AK_VS_IN_3D
  13. {
  14.         float4 v4_pos_in: POSITION;                        // incoming vertex pos
  15.         float2 v2_tc: TEXCOORD0;                                // incoming texture coordinates
  16. };

  17. Ps_TC_IN VsTexture_3D(AK_VS_IN_3D IN)
  18. {
  19.         Ps_TC_IN OUT=(Ps_TC_IN)0;
  20.         OUT.v4_pos_prj=mul(IN.v4_pos_in,WorldViewProjectionMatrix);
  21.         OUT.v2_tc=IN.v2_tc;//+float2(ShadowResultTextureTexelWidth * 0.5f,ShadowResultTextureTexelHeight * 0.5f);
  22.         OUT.v4_pos_prj_var=float4(OUT.v4_pos_prj.xy/OUT.v4_pos_prj.w,0,1);
  23.         return OUT;       
  24. }

  25. Ps_DEFAULT_OUT PsTexture(Vs_DEFAULT_OUT IN)
复制代码


  1. //shader code

  2. texture TestBaseTexture;
  3. sampler tex_2d = sampler_state
  4. {
  5.         Texture = <TestBaseTexture>;
  6.         MinFilter = LINEAR;
  7.         MagFilter = LINEAR;
  8.         MipFilter = NONE;

  9.         AddressU = Clamp;
  10.         AddressV = Clamp;
  11. };

  12. struct AK_VS_IN_3D
  13. {
  14.         float4 v4_pos_in: POSITION;                        // incoming vertex pos
  15.         float2 v2_tc: TEXCOORD0;                                // incoming texture coordinates
  16. };

  17. Ps_TC_IN VsTexture_3D(AK_VS_IN_3D IN)
  18. {
  19.         Ps_TC_IN OUT=(Ps_TC_IN)0;
  20.         OUT.v4_pos_prj=mul(IN.v4_pos_in,WorldViewProjectionMatrix);
  21.         OUT.v2_tc=IN.v2_tc;//+float2(ShadowResultTextureTexelWidth * 0.5f,ShadowResultTextureTexelHeight * 0.5f);
  22.         OUT.v4_pos_prj_var=float4(OUT.v4_pos_prj.xy/OUT.v4_pos_prj.w,0,1);
  23.         return OUT;       
  24. }

  25. Ps_DEFAULT_OUT PsTexture(Vs_DEFAULT_OUT IN)
  26. {
  27.         Ps_DEFAULT_OUT OUT;
  28.         OUT.color = tex2D(tex_2d, IN.v2_tc.xy);
  29.         return OUT;       
  30. }

  31. VertexShader VsTextures[2] =
  32. {
  33.         compile vs_3_0 VsTexture_2D(),
  34.         compile vs_3_0 VsTexture_3D()
  35. };

  36. bool bUse3D_Way=true;

  37. technique TShadowQVSM_ADPATIVE1
  38. {
  39.         pass p0 //for test
  40.         {
  41.                 VertexShader = (VsTextures[bUse3D_Way]);
  42.                 PixelShader  = compile ps_3_0 PsTexture();
  43.                
  44.                 //ZWriteEnable=FALSE;
  45.                 //ZEnable=FALSE;  
  46.         }
  47. }
复制代码

201

主题

1437

帖子

1963

积分

金牌会员

Rank: 6Rank: 6

积分
1963
QQ
发表于 2008-4-16 21:01:00 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

414

主题

611

帖子

621

积分

高级会员

Rank: 4

积分
621
 楼主| 发表于 2008-4-17 00:43:00 | 显示全部楼层

Re:Copy Texture (RenderTarget) to Texture (RenderTarget)

好的,我想??Texture(RenderTarget)到另一??Texture(RenderTarget),我是用shader code的方式去??,但是我????惺д娴那樾?就是?????pixel的?色不?原?淼囊??
half-pixel offseting的修正我後?硪灿锌?],但是情形?是差不多.最後我找到一??函式叫IDirect3DDevice9::StretchRect,它支援RT texture to RT texture的???幼?所以我的???解?Q了.不好意思,我??英文的用意,是因?槲矣腥??獾恼???l??,要改?橹形?要多花一??r?.?????irectX的人,英文的?????不??刑????.

0

主题

34

帖子

34

积分

注册会员

Rank: 2

积分
34
发表于 2008-4-24 16:04:00 | 显示全部楼层

Re:Copy Texture (RenderTarget) to Texture (RenderTarget)

。。。。

36

主题

1047

帖子

1147

积分

金牌会员

Rank: 6Rank: 6

积分
1147
发表于 2008-4-25 00:23:00 | 显示全部楼层

Re: Re:Copy Texture (RenderTarget) to Texture (RenderTarget)

akira32: Re:Copy Texture (RenderTarget) to Texture (RenderTarget)

好的,我想??Texture(RenderTarget)到另一??Texture(RenderTarget),我是用shader code的方式去??,但是我...

去国外论坛用英文,到国内论坛就应该用中文.

0

主题

34

帖子

34

积分

注册会员

Rank: 2

积分
34
发表于 2008-4-26 14:41:00 | 显示全部楼层

Re:Copy Texture (RenderTarget) to Texture (RenderTarget)

多谢分享
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-22 16:54

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表