Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Sprite Rendering Position Problem.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 chowarth   Members   -  Reputation: 516

Like
0Likes
Like

Posted 24 February 2013 - 04:18 AM

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, 26 February 2013 - 01:12 PM.

Ramblings: www.chowarth.co.uk

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS