[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-08-30
2
Medium Priority
?
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Accepted Solution

by:
davebytes earned 1500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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