# OpenGL How are sprites implemented in DirectX?

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

## Recommended Posts

Ok, this is probably the most noobish question I've asked on a forum, but for the life of me I just can't work it out or find anything to the depth I'm after on blogs / documentation site etc. The question is, how are sprites implemented in managed (ugh!!) direct x? And is is representative of other graphics APIs? The problem I'm having is that the Sprite class I'm using (Microsoft.DirectX.Direct3D.Sprite) doesn't have a width or height property, so am I to assume that the only way to resize a sprite is via scaling with a matrix? What I'm trying to achieve is using a sprite to display a texture on a 2D surface, but I want to set the size of the surface explicity (i.e. width=x etc) and have the texture resize to fit. I know this can be done using a mesh, but isn't that a bit overkill? My second question about Sprites (in this case exclusive to managed direct x) is why does Sprite.Draw have a position vector, when the class have a Transform matrix which I'm assuming should be used for the transforms, amongst which would be the position? Is it just for convenience? Finally (so sorry for the long post) should I not just be setting the world matrix to transform the coordinates of the sprite? I'm pretty sure this is what you do in normal direct x and open gl, or is the world matrix just for meshes and sprites are *special*? :s Agh! I just don't know why sprites a stumping me all of a sudded, I guess I just assumed from previous work (OpenGL maybe?) that they were re-sizable!! If any one could shed light on how they are implemented I'd really appreciate it! I'm starting to really loose it! lol Thanks, Andy.

##### Share on other sites
The D3DX sprite class is just a convenient utility so you don't have to construct your own dynamic vertex buffer. In raw D3DX9 it works somewhat like following:

-Begin method collects certain render states for sprite drawing, and saves the current states to its internal storage
-Draw method adds a quad to the sprite's internal vertex buffer (and possibly index buffer); this quad is transformed according to the parameters of the method as well as the matrix given in SetTransform.
-Flush method does the actual uploading and draw call to submit all geometry collected by the Draw method for rendering
-End method calls Flush and restores the render states set on the device prior to Begin.

##### Share on other sites
Hey, thanks a lot for the reply. That seems to makes sense!

So if its essentially a vertex buffer, why don't they offer methods or properties to size it? I.e. set the x/y coords of the vertexes? Would this not resize the texture to fit or is there more work involved?

Also, I've tried adding text using the same x,y coords as the meshes (as far as I'm aware the z coord of the meshes are at 0) and I've noticed that the meshes are drawn fine, but the text (on sprite) seems to be in a difference place. Is this because the sprites are only 2D and drawn in the projection space (i.e. just drawn to the screen coordinates)?

.. Really thought I understood all this! :s Any good links are also appricated! :)

##### Share on other sites
Quote:
 Original post by FunkiemonkeySo if its essentially a vertex buffer, why don't they offer methods or properties to size it? I.e. set the x/y coords of the vertexes? Would this not resize the texture to fit or is there more work involved?
That would stretch/shrink the texture, yes. It's not done that way simply because it's more often that you want your sprite to be shown with a 1:1 ratio of texels to pixels so it doesn't look distorted on screen. You can still do it by setting a scaling transform on the sprite (Although there's some weird caveats about that, have a search in these forums to see what I mean).

Quote:
 Original post by FunkiemonkeyAlso, I've tried adding text using the same x,y coords as the meshes (as far as I'm aware the z coord of the meshes are at 0) and I've noticed that the meshes are drawn fine, but the text (on sprite) seems to be in a difference place. Is this because the sprites are only 2D and drawn in the projection space (i.e. just drawn to the screen coordinates)?
Yup, exactly. Your mesh is in world space/coordinates, the sprite is drawn in screen space/coordinates.

##### Share on other sites
If you don't specifically set the sprite transform, it defaults to a scale of 1 along each axis with no translation. Your texture will be displayed without resizing.

If you want your texture to be displayed a specific size, you'll need to set a scaling transform with the scale being a ratio of the desired size to the original size.

Note: if you set a scaling transform for the sprite, it will also scale the translation (the position where the sprite is drawn) when the texture is rendered.

E.g., suppose you want to render a texture at twice its size, at a screen position of ( 200, 150 ).
D3DXMATRIX scale;float xscale = 2.0f, yscale = 2.0f; // want texture 2x size// generate scaling matrix for sizingD3DXMatrixScaling(&scale, xscale, yscale, 1 );sprite->SetTransform( &scale ); // set the scale for the textureD3DXVECTOR3 position( 200, 150, 0 ); // want position at 200, 150 onscreen// compensate for scaling to maintain position where desiredposition.x /= xscale;position.y /= yscale;sprite->Begin(...); // options as desiredsprite->Draw( texture, NULL, NULL, &position, desired_color );sprite->End();// to avoid confusion if you want to reuse the sprite for another texture, etc.D3DXMatrixIdentity(&scale);sprite->SetTransform(&scale);

##### Share on other sites
Excellent, that all makes sense now, thanks a lot for the explanations!

I really hate it when something you used to understand slips in your mind and gets lost somewhere....

Probably in the same place as the missing socks!

Thanks again anyway!

Andy.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633299
• Total Posts
3011262
• ### Who's Online (See full list)

There are no registered users currently online

×