Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am creating own sprite drawer with DX11 on C++. Works fine but I dont know how to apply source rectangle to texture coordinates of rendering surface(for animation sprite sheets)

            //source = (0,0,32,64); //RECT

            D3DXVECTOR2 t0 = D3DXVECTOR2( 1.0f, 0.0f);
    D3DXVECTOR2 t1 = D3DXVECTOR2( 1.0f, 1.0f);
    D3DXVECTOR2 t2 = D3DXVECTOR2( 0.0f, 1.0f);
    D3DXVECTOR2 t3 = D3DXVECTOR2( 0.0f, 1.0f);
    D3DXVECTOR2 t4 = D3DXVECTOR2( 0.0f, 0.0f);
    D3DXVECTOR2 t5 = D3DXVECTOR2( 1.0f, 0.0f);        

    VertexPositionColorTexture vertices[] =
        { D3DXVECTOR3( dest.left+dest.right,,              z),D3DXVECTOR4(1,1,1,1), t0},
        { D3DXVECTOR3( dest.left+dest.right,,  z),D3DXVECTOR4(1,1,1,1), t1},
        { D3DXVECTOR3( dest.left,   ,  z),D3DXVECTOR4(1,1,1,1), t2},
        { D3DXVECTOR3( dest.left,   ,  z),D3DXVECTOR4(1,1,1,1), t3},
        { D3DXVECTOR3( dest.left ,  ,              z),D3DXVECTOR4(1,1,1,1), t4},
        { D3DXVECTOR3( dest.left+dest.right,,              z),D3DXVECTOR4(1,1,1,1), t5},
share|improve this question
I'm not exactly sure what you are asking.It seems like you already store texture coordinates in your vertices? Are you asking how to apply a texture, or how to apply the texture coordinates to the texture? What do you mean by source rectangle? – melak47 Apr 30 '12 at 9:48
I mean howto apply source region to texture coordinates. Source rectangle is a specified region on texture for draw only this region. – ChaosDev Apr 30 '12 at 9:58
So you want to crop your texture? Simply use the coordinates for the crop as the texture coordinates on the vertices, for a quick solution. – melak47 Apr 30 '12 at 10:20
How ? This is a question. I dont know mathematic method for add this to texture coordinates. – ChaosDev Apr 30 '12 at 10:28
it depends, in what form is the cropping selection specified? – melak47 Apr 30 '12 at 12:33

Since you say you want this for animation, I assume you want to modify the cropping at runtime to display different parts of your texture.

One way would be to keep your geometry the same, and adjust the texture coordinates in the shader.

Say you want to to crop your texture to 1/4 the area, on the top left, that would be as simple as multiplying your texture coordinate by 0.5.

for the quarter on the top right, multiply by 0.5, add (0.5, 0). bottom left: multiply by 0.5, add (0, 0.5) bottom right: multiply by 0.5, add (0.5, 0.5)

the vertex shader could look like this for example:

cbuffer inputs {
    float4x4 worldviewprojection;
    int tiles_x; // number of frames in x (and y) direction
    int tile; // which frame is to be displayed

struct vs_in {
    float3 pos : POSITION;
    float3 col : COLOR;
    float2 uv : TEXCOORD;

struct ps_in {
    float3 pos: SV_POSITION;
    float3 col : COLOR;
    float2 uv : TEXCOORD;

ps_in vertexshader(vs_in input) {

    ps_in output;

    output.pos = mul(worldviewprojection, input.pos);
    output.col = input.col;

    float offset = 1.0 / tiles_x;
    output.uv = input.uv * offset  // "zoom"
    output.uv += float2(offset * (tile % tiles_x), offset * floor(tile / tiles_x) ); // offset

    return output;


this is just a simple example, which assumes your texture is of the same x and y dimension and contains 2^n animation frames.

share|improve this answer
hmm. interesting. However i already implement solution(without shader), but it seems more efficient. – ChaosDev Apr 30 '12 at 13:31
it depends, if you do it on the cpu, you may save time as you only have to change the values once every frame, no matter how many sprites you draw with the same geometry. doing it in the shader, it gets done every frame for every vertex you draw. on the other hand, you wont have to update the vertex buffer every frame, only the constants/shader variables. – melak47 Apr 30 '12 at 13:35
I update vertex buffer every frame, because the size of rendering surface depending on texture size of specified sprite, and I must update it anyway. – ChaosDev Apr 30 '12 at 13:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.