Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now