?
Solved

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

Posted on 2004-08-30
2
Medium Priority
?
376 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

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 …
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

800 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