Solved

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

Posted on 2004-08-30
2
371 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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