Jump to content
  • Advertisement
Sign in to follow this  
chowarth

DX11 Sprite Rendering Position Problem.

This topic is 1975 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to implement sprites and read through the RasterTek tutorial on rendering sprites. I got everything working fine but I didn't like the idea of using dynamic vertex buffers to make it possible to move a sprite, so I went about changing them to use a matrix instead.

 

I have come across a little problem with sprite positions.

 

I create a few sprites like so:

mpScene->AddSprite( "TestSprite1", "alpha.png", 0,    0,    100,    100 );
mpScene->AddSprite( "TestSprite2", "alpha.png", 100,    0,    100,    100 );
mpScene->AddSprite( "TestSprite3", "alpha.png", 0,      100,    100,    100 );

 

Now my assumption is that if I create a sprite at (0, 0) with dimensions of (100, 100), and then create another at (100, 0) with the same dimensions that the two sprites would be right next to each other, however they seem to be the full width of a sprite apart:

 

sprites_zpse3239e19.png

 

 

This is how I create my sprite buffers:

// Calculate screen space positions of the vertices
float left        = ( ( screenWidth * 0.5f ) * -1 ) + posX;
float right        = left + width;
float top        = ( screenHeight * 0.5f ) + posY;
float bottom    = top - height;

// Populate vertices.
vertices[0].Pos = XMFLOAT3( left, top, 1.0f );
vertices[0].Tex = XMFLOAT2( 0.0f, 0.0f );

vertices[1].Pos = XMFLOAT3( right, top, 1.0f );
vertices[1].Tex = XMFLOAT2( 1.0f, 0.0f );

vertices[2].Pos = XMFLOAT3( right, bottom, 1.0f );
vertices[2].Tex = XMFLOAT2( 1.0f, 1.0f );

vertices[3].Pos = XMFLOAT3( left, bottom, 1.0f );
vertices[3].Tex = XMFLOAT2( 0.0f, 1.0f );

// Create indices array.
indices = new USHORT[numIndices];
indices[0] = 0;    //
indices[1] = 1;    // Triangle 1
indices[2] = 2;    //___________
indices[3] = 0;    //
indices[4] = 2;    // Triangle 2
indices[5] = 3;    //

 

and how I create the sprite matrix:

XMMATRIX p    = XMMatrixTranslation( mPosition.x, mPosition.y, mPosition.z );
XMMATRIX r    = XMMatrixRotationZ( mRotation );
XMMATRIX s    = XMMatrixScaling( mScale.x, mScale.y, 1.0f );

XMStoreFloat4x4( &mMatrix, p * r * s );
 

 

and the rendering of the sprites:

// Disable depth testing when drawing sprite objects.
pDevice->SetDepthStencilState( mpDepthDisabledState );

mpSpriteFX->SetMatrix( "gViewProjMatrix", pCamera->GetOrthoMatrix() );
pDevice->SetInputLayout( mpSpriteFX->GetInputLayout() );

CSprite* pSprite = NULL;
pSpriteList->BeginEnumeration();

while( ( pSprite = pSpriteList->EnumAsset() ) != 0 )
{
    CTexture* pTexture = pTextureList->GetAsset( pSprite->GetTextureUID() );
    mpSpriteFX->SetSRV( "dTexture", pTexture->GetSRV() );

    pDevice->SetVertexIndexBuffer( sizeof( SpriteVertex ), pSprite->GetVB(), pSprite->GetIB() );

    mpSpriteFX->SetMatrix( "gWorldMatrix", pSprite->GetMatrix() );

    UINT numPasses = mpSpriteFX->GetNumPasses();
    for( UINT pass = 0; pass < numPasses; pass++ )
    {
        pDevice->ApplyTechPass( mpSpriteFX->GetTechnique(), pass );

        pDevice->DrawIndexed( mNumIndices, 0, 0 );
    }
}
pSpriteList->EndEnumeration();
    
// Re-enable depth testing for drawing non-sprite objects.
pDevice->SetDepthStencilState( NULL );

 

I'm just wondering if there's something I've done wrong or an explanation as to why the sprites would be positioned the full width of a sprite apart instead of next to each other as I would expect.

 

Thanks in advance.

 

Edit:

Appears the image wasn't working :( Fixed (I hope).

Edited by chowarth

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!