Search code examples
c++directxspriteshadervertex-buffer

DirectX: Small distortion between 2 sprite polygons


Hello I use the same way to render sprites with directx from a long time but here I am rendering the screen in a texture and then render it with a big sprite on the screen.

For the camera I use that: vUpVec=D3DXVECTOR3(0,1,0); vLookatPt=D3DXVECTOR3(0,0,0); vFromPt=D3DXVECTOR3(0,0,-1);

D3DXMatrixLookAtRH( &matView, &vFromPt, &vLookatPt, &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

D3DXMatrixOrthoRH( &matProj, 1,1, 0.5f, 20 );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

And to render the sprite: CUSTOMVERTEX* v; spritevb->Lock( 0, 0, (void**)&v, 0 );

v[0].position = D3DXVECTOR3(-0.5f,-0.5f,0); v[0].u=0; v[0].v=1;
v[1].position = D3DXVECTOR3(-0.5f,0.5f,0); v[1].u=0; v[1].v=0;
v[2].position = D3DXVECTOR3(0.5f,-0.5f,0); v[2].u=1; v[2].v=1;
v[3].position = D3DXVECTOR3(0.5f,0.5f,0); v[3].u=1; v[3].v=0;

spritevb->Unlock();


g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

This is very basic and works, my sprite is rendered on the screen full. But by looking closer I see that there's a small diagonal line through the screen (between the 2 polygons) not a colored one but like if them weren't perfectly positionned.

I thought about filtering and tried removing everything but maybe I forget something...

Thanks


Solution

  • To render to full screen best way is to not define any camera positions.

    If you use as input positions

    SimpleVertex vertices[] =
        {
            { XMFLOAT3( -1.0f, 1.0f, 0.5f ), XMFLOAT2( 0.0f, 0.0f ) },
            { XMFLOAT3( 1.0f, 1.0f, 0.5f ), XMFLOAT2( 1.0f, 0.0f ) },
            { XMFLOAT3( 1.0f, -1.0f, 0.5f ), XMFLOAT2( 1.0f, 1.0f ) },
            { XMFLOAT3( -1.0f, -1.0f, 0.5f ), XMFLOAT2( 0.0f, 1.0f ) },
        };
    

    and in the Vertex Shader do

    VS_OUTPUT RenderSceneVS( VS_INPUT input )
    {
        VS_OUTPUT Output;
    
        Output.Position = input.Position;
    
        Output.TextureUV = input.TextureUV; 
    
        return Output;    
    }
    

    you get a render to full screen as well without having to worry about the viewing frustrum. Using this I never saw any lines between the two triangles.