microsoft / DirectXTK

The DirectX Tool Kit (aka DirectXTK) is a collection of helper classes for writing DirectX 11.x code in C++

Home Page:https://walbourn.github.io/directxtk/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Additional Clarification on sourceRectangle vs m_origin, m_rotation, m_scale

aaronstackpole opened this issue · comments

In this page you describe how to rotate and scale sprites, and there is one place where you set the origin value based on the size of the entire texture. Can you provide a technical note describing how these values interact with the sourceRectangle in the Draw call? That is to say, are the calculations for rotation, scale, and origin based on the source rectangle you define in a texture atlas, for example? If I use sourceRectangle{ 10, 10, 74, 74 } and I set m_origin { 0.f, 0.f } does this result in the actual origin being 10, 10? And if m_origin is { 1.f, 1.f } is that 74, 74?

https://github.com/microsoft/DirectXTK/wiki/Sprites-and-textures#drawing-a-sprite

Source rectangle describes the location on the texture where to pull the sprite data. This is always axis-aligned. This is used to compute the texture coordinates for the sprite triangles. The primary use for source rectangle is implementing a SpriteSheet.

The origin, scale, and rotation is used to compute the sprite triangle location on the screen. This computation is not affected by the source rectangle at all.

That does not add clarity.

The sourceRectangle determines where within a texture atlas (sprite sheet, you're calling it) to get the pixel data which is then drawn upon a 'sprite triangle'. The origin, scale and rotation are then applied to the triangle, correct?

The request for clarification is that in your sample, you measure the entire size of the texture to get a center point for the origin, it is not clear exactly how this interacts with the 'sprite triangle'.

CD3D11_TEXTURE2D_DESC catDesc;
cat->GetDesc(&catDesc);

m_origin.x = float(catDesc.Width / 2);
m_origin.y = float(catDesc.Height / 2);

If I wanted to get the center of a sprite within a sprite sheet, would it be the sourceRectangle::width/2? I suppose what I'm asking for is clarification on what this value is suppose to mean. Is the sprite triangle the same size as the texture, or the sourceRectangle, or what?

Ah, ok.

Typically in a sprite-sheet/atlas implementation it provides some metadata about each entry like the width/height. For example, in my spritesheet demo I have:

    struct SpriteFrame
    {
        RECT                sourceRect;
        DirectX::XMFLOAT2   size;
        DirectX::XMFLOAT2   origin;
        bool                rotated;
    };

You'd use the size metadata here rather than the raw texture atlas size and the origin is which point in the individual sprite that's considered the 'origin'.