|
|
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?
- //the way I Create texture with RenderTraget
- // setup shadow map objects
- V_RETURN(m_pd3dDevice->CreateTexture(
- m_nSMTileWidth,
- m_nSMTileHeight,
- 1,
- D3DUSAGE_RENDERTARGET,
- SHADOW_MAP_FORMAT,
- D3DPOOL_DEFAULT,
- &m_pShadowMapTex,
- NULL
- ));
- V_RETURN(m_pShadowMapTex->GetSurfaceLevel(0, &m_pShadowMapSurf));
- //CShadowResultTexture class has a RanderTraget Texture in it and the texture create use the way below.
- //use shader to copy two textures
- void CQVSM::CopyTextureFromTo(CShadowResultTexture* pNewTexture,CShadowResultTexture* pOldTexture)
- {
- HRESULT hr;
- D3DXHANDLE hTechnique = m_pEffect_QVSM_ADAPTIVE->GetTechniqueByName("TShadowQVSM_ADPATIVE1");
- m_pEffect_QVSM_ADAPTIVE->SetTechnique(hTechnique);
- V(m_pEffect_QVSM_ADAPTIVE->Begin(NULL, 0));
- {
- hr=pOldTexture->StartRenderTarget();
- hr=pOldTexture->Clear(COLOR_WHITE);
- V(m_pEffect_QVSM_ADAPTIVE->BeginPass(0));
- {
- // Set the vertex stream, shader, and texture
- hr=m_pEffect_QVSM_ADAPTIVE->SetBool(bUse3D_Way,TRUE);
- hr=m_pEffect_QVSM_ADAPTIVE->SetMatrix(WorldViewProjectionMatrix,&(m_matWorld * m_matView * m_matProj));
- hr=m_pEffect_QVSM_ADAPTIVE->SetTexture("TestBaseTexture", pNewTexture->GetTexture());
- SettingVertexDeclarationAndStreamSource();
- m_pEffect_QVSM_ADAPTIVE->CommitChanges();
- // Draw the vertex buffer
- hr=m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
- }
- hr=m_pEffect_QVSM_ADAPTIVE->EndPass();
- hr=pOldTexture->EndRenderTarget();
- }
- hr=m_pEffect_QVSM_ADAPTIVE->End();
- }
- // vertext strucutre (just see the 3D)
- // now using
- // The 3-D vertex format and descriptor
- typedef struct {
- FLOAT x, y, z, w; // 3-D coordinates
- FLOAT u, v; // Texture coordinates
- } sVertex_3D;
- #define VERTEXFVF_3D (D3DFVF_XYZW | D3DFVF_TEX1)
- // now no used
- // The 2-D vertex format and descriptor
- typedef struct {
- FLOAT x, y, z; // 2-D coordinates
- FLOAT rhw; // rhw
- FLOAT u, v; // Texture coordinates
- } sVertex_2D;
- #define VERTEXFVF_2D (D3DFVF_XYZRHW | D3DFVF_TEX1)
- //set some vertex format and some parameters
- HRESULT CQVSM::SettingVertexDeclarationAndStreamSource()
- {
- HRESULT hr;
- if (m_bUseVertexDecl)
- {
- hr=m_pd3dDevice->SetVertexDeclaration(m_decl);
- hr=m_pd3dDevice->SetFVF(NULL);
- }
- else
- {
- if (m_bUse3D_Way)
- {
- hr=m_pd3dDevice->SetFVF(VERTEXFVF_3D);//SetVertexShader
- }
- else
- {
- hr=m_pd3dDevice->SetFVF(VERTEXFVF_2D);//SetVertexShader
- }
- }
- if (m_bUse3D_Way)
- {
- hr=m_pd3dDevice->SetStreamSource(0, g_pVB, 0,sizeof(sVertex_3D));
- }
- else
- {
- hr=m_pd3dDevice->SetStreamSource(0, g_pVB, 0,sizeof(sVertex_2D));
- }
- return S_OK;
- }
复制代码
- //shader code
- texture TestBaseTexture;
- sampler tex_2d = sampler_state
- {
- Texture = <TestBaseTexture>;
- MinFilter = LINEAR;
- MagFilter = LINEAR;
- MipFilter = NONE;
- AddressU = Clamp;
- AddressV = Clamp;
- };
- struct AK_VS_IN_3D
- {
- float4 v4_pos_in: POSITION; // incoming vertex pos
- float2 v2_tc: TEXCOORD0; // incoming texture coordinates
- };
- Ps_TC_IN VsTexture_3D(AK_VS_IN_3D IN)
- {
- Ps_TC_IN OUT=(Ps_TC_IN)0;
- OUT.v4_pos_prj=mul(IN.v4_pos_in,WorldViewProjectionMatrix);
- OUT.v2_tc=IN.v2_tc;//+float2(ShadowResultTextureTexelWidth * 0.5f,ShadowResultTextureTexelHeight * 0.5f);
- OUT.v4_pos_prj_var=float4(OUT.v4_pos_prj.xy/OUT.v4_pos_prj.w,0,1);
- return OUT;
- }
- Ps_DEFAULT_OUT PsTexture(Vs_DEFAULT_OUT IN)
复制代码
- //shader code
- texture TestBaseTexture;
- sampler tex_2d = sampler_state
- {
- Texture = <TestBaseTexture>;
- MinFilter = LINEAR;
- MagFilter = LINEAR;
- MipFilter = NONE;
- AddressU = Clamp;
- AddressV = Clamp;
- };
- struct AK_VS_IN_3D
- {
- float4 v4_pos_in: POSITION; // incoming vertex pos
- float2 v2_tc: TEXCOORD0; // incoming texture coordinates
- };
- Ps_TC_IN VsTexture_3D(AK_VS_IN_3D IN)
- {
- Ps_TC_IN OUT=(Ps_TC_IN)0;
- OUT.v4_pos_prj=mul(IN.v4_pos_in,WorldViewProjectionMatrix);
- OUT.v2_tc=IN.v2_tc;//+float2(ShadowResultTextureTexelWidth * 0.5f,ShadowResultTextureTexelHeight * 0.5f);
- OUT.v4_pos_prj_var=float4(OUT.v4_pos_prj.xy/OUT.v4_pos_prj.w,0,1);
- return OUT;
- }
- Ps_DEFAULT_OUT PsTexture(Vs_DEFAULT_OUT IN)
- {
- Ps_DEFAULT_OUT OUT;
- OUT.color = tex2D(tex_2d, IN.v2_tc.xy);
- return OUT;
- }
- VertexShader VsTextures[2] =
- {
- compile vs_3_0 VsTexture_2D(),
- compile vs_3_0 VsTexture_3D()
- };
- bool bUse3D_Way=true;
- technique TShadowQVSM_ADPATIVE1
- {
- pass p0 //for test
- {
- VertexShader = (VsTextures[bUse3D_Way]);
- PixelShader = compile ps_3_0 PsTexture();
-
- //ZWriteEnable=FALSE;
- //ZEnable=FALSE;
- }
- }
复制代码 |
|