# Direct3D Questions for 2D - Sprite Frames and Screen Coords

This topic is 4969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have two questions for Direct3D for 2D graphics. Please note that I am only just beginning to start 3D programming and Direct3D, so I'm not too savy on many a topic. 1) Is there some way to apply only a section of a texture, instead of having to create multiple textures using locking and unlocking, like IDirectDrawSurface::Blit and source/destination rectangles? I am using the Blit function from the tutorial Dissecting Sprites in Direct3D 2) I thought I remembered (when I had examined some tutorials here) some sort of flag in the initialization of Direct3D that made it to where the coordinates were screen coordinates, instead of having to calculate what a screen coordinate would be in the floating-point coordinate system. Is there actually such a flag that eliminates that extra calculation, or do I have to do it manually? Please correct my errors, numerous as they may be.

##### Share on other sites
1) To apply only portions of a texture simply use fractions for the float value. Like if you wanted to start half way accrossa texture make the left hand x valued 0.5 etc.

2) You have to tell D3D not to transform the vertices by the world matrix and this is done by doing the following:

// SIMPLE 2D COORDINATE
struct _2D_COORD
{
float x, y, z, rhw; // positions, rhw (1 for pretransformed)
float u, v; // texture coordinates;
};
// Appropriate vertex declaration
#define D3DFVF_TRANSTEXT (D3DFVF_XYZRHW|D3DFVF_TEX1)

Thats an example vertex struct.

The coordinates are floats and their values are the locations on the back buffer x-pixels by y-pixels.

Hope this helps,

ace

##### Share on other sites
Thanks for the reply.
So the fractional coordinates would belong in the variables "u" and "v"? This what he (author of the article) has when structuring a quad:
D3DTLVERTEX vertices[4] =	{ // x, y, z, rhw, colour, tu, tv { left,  top, z, rhw, colour, 0, 0 }, //It's the last two, right? { right, top, z, rhw, colour, 1, 0 }, { right, bottom, z, rhw, colour, 1, 1 }, { left,  bottom, z, rhw, colour, 0, 1 } };

Also, for the vertices, the author of "Dissecting Sprites in Direct3D" uses a whole bunch of complex (to me) computations dealing with matrices and stuff like that... Why does it need a matrix when it's only two-dimensional?

Sorry if I'm being somewhat difficult.

EDIT: Ah, you were right about the portion coordinates. Thanks.

EDIT (AGAIN): I'm starting to see, now that I can actually think (yay Dr. Pepper!), what the transformations and such do. Probably need to look at it some more.
Thanks for your help.

However, I have one more question: When exactly do I need to be sure to keep a texture that's dimensions are a power of two? His (the author again) sprite's size is 32x40 and it doesn't seem distorted or fuzzy at all...

##### Share on other sites
From my experience i can tell you there is nothing complicated about gett quads in 2D, u simply set the RHW values to 1 so that the xyz values are not transformed by the world matrix

##### Share on other sites
Thanks. (Rating++)

NOTE: Had a question, then figured out how stupid it was.

##### Share on other sites
Well if you dont understand just ask anyhoo :P :D

ace

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015495
×