Solved

Direct3D: how can I fill a object using only a section of a texture?

Posted on 2004-08-30
2
373 Views
Last Modified: 2013-12-08
Hi,

Here the code I use to draw a 2D sprite using D3D 9.  I was wondering if I can use only a part of the texture (let say we add a source rectangle into parameters).

void D3D9Device::Blit(IDirect3DTexture9* texture, RECT* rDest, float rotate)
{
    float X;
    float Y;
 
    D3DXMATRIX matTranslation;
    D3DXMATRIX matScaling;
    D3DXMATRIX matTransform;
 
    //Get coordinates
    X = rDest->left - (float)(d3dPresent.BackBufferWidth) / 2;
    Y = -rDest->top + (float)(d3dPresent.BackBufferHeight) / 2;
 
    //Setup translation and scaling matrices
    D3DXMatrixScaling (&matScaling, (float)(rDest->right - rDest->left),
        (float)(rDest->bottom - rDest->top), 1.0f);
    D3DXMatrixTranslation (&matTranslation, X, Y, 0.0f);
    matTransform = matScaling * matTranslation;
 
    //Check if quad is rotated
    if (rotate)
    {
        D3DXMATRIX matRotate;
 
        //Create rotation matrix about the z-axis
        D3DXMatrixRotationZ (&matRotate, rotate);
 
        //Multiply matrices together
        matTransform *= matRotate;
    }
 
    //Draw the quad
    d3dDevice->SetTransform (D3DTS_WORLD, &matTransform);
    d3dDevice->SetTexture (0, texture);
    d3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
};

I think that ID3DXSprite class provides this feature using its Draw method but, can I handle it that way?  As I have to draw the same object many time, I was thinking to load the texture once and then display them all.


Otherwise, can I define a source rectangle when I load the bitmap to only retrieve a part of the image?  Here the code I use to load a texture:

IDirect3DTexture9 *D3D9Device::LoadTexture(char *fileName)
{
    IDirect3DTexture9 *d3dTexture;
    D3DXIMAGE_INFO SrcInfo;
 
    //Use a magenta colourkey
    D3DCOLOR colorkey = 0xFFFF00FF;
 
    // Load image from file
    if( FAILED(D3DXCreateTextureFromFileEx (d3dDevice, fileName, 0, 0, 1, 0,
          D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
          colorkey, &SrcInfo, NULL, &d3dTexture)) )
    {
        return NULL;
    }
 
 //Return the newly made texture
    return d3dTexture;
};

Thanks, seg.
segmend@hotmail.com
0
Comment
Question by:ubiwebdev
2 Comments
 
LVL 17

Accepted Solution

by:
davebytes earned 500 total points
ID: 11965593
If you use DrawPrimitive, just change the texture U,V coordinates at each corner of the quad to 'index' into it differently.  You are probably using 0.0->1.0, but could use any smaller section.

Note that of course you can end up with some 'bleed' depending on filtering mode active and the precision of your rectangle placement.

-d
0
 

Author Comment

by:ubiwebdev
ID: 12005373
To change the U,V coordinates of the quad, I'll need to lock the device?
I was affraid to do that because I read that it's such ressource consuming.
Thanks for your help!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question