UI sprites

Recommended Posts

Just wondering if there is a better way of what im doing at the moment. Currently whenever i need to render a sprite of an image, i create a plane via 4 vertices, and render it with a texture set back in z a bit. And the sprite class has a static list container of all the sprites that repeatedly get rendered to the screen, such as health icon, crosshair etc. And then each frame this list is rendered out. The planes are created in a 0,0 manner, like win32 windows. and another quick question, what is the relation ship between the window and vertex location, such as if i have a window 800x600, and render a vertice at 0,0,0 , thats gonna be the center of the screen, but how would i create a texture to fill the screen? other than just moving it till it looks right. (concerning ViewMatrix being set to 0) does the screen size have anything to do with it? i mean the center is is 0 in x, so does having 1 unit either size fill it, and with the width being wider (800) than the height (600), does that have any affect on it. ive been messing with this, but seeing as you always have to move stuff back in z a bit, i didn't want to guess.

Share on other sites
Quote:
 Original post by wforland another quick question, what is the relation ship between the window and vertex location, such as if i have a window 800x600, and render a vertice at 0,0,0 , thats gonna be the center of the screen, but how would i create a texture to fill the screen? other than just moving it till it looks right.

This depends on your projection matrix. If you're using perspective projection, the on-screen point will depend on the field of view and also the z coordinate of the vertex. For orthographic projection, it depends on the width and height of the projection used.

Is suspect that what you want here is to use pre-transformed coordinates. With these you say "I want my vertex to show up at this point on the screen", and the pipeline skips any transformation by world, view, and projection matrices. So you could just specify one vertex at (0,0,0), another at (800,0,0), then (800,600,0), and (0,600,0) and it would cover the whole screen.

If you tell me what language and which exact API you're using (D3D9, D3D10, SlimDX, XNA) I can try to tell you how to use pre-transformed vertices.

Share on other sites
im using dx9 unmanaged, thanks

Share on other sites
You'll want to create your vertex buffer using the D3DFVF_XYZRHW FVF code if you're using FVF vertex buffers, or with the D3DDECLUSAGE_POSITIONT usage code if you're using vertex declarations. Also when you do this your position has to be a 4-component vector in your vertex buffer. You'll just want to set the extra w-component to 1.0.

So your vertex setup could be something like this:

struct SpriteVertex{    D3DXVECTOR4 vPosition;    D3DXVECTOR2 vTexCoord;    static const DWORD FVF = D3DFVF_XYZRHW|D3DFVF_TEX1;};pDevice->CreateVertexBuffer(sizeof(SpriteVertex) * 4, D3DUSAGE_DYNAMIC, SpriteVertex::FVF, D3DPOOL_DEFAULT, &pSpriteVB, NULL);SpriteVertex* pVB;pSpriteVB->Lock(0, (void**)&pVB);// Positions are offset by -0.5 to exactly align texels to screen pixelspVB[0].vPosition = D3DXVECTOR4(-0.5f, -0.5f, 0, 1.0f);pVB[0].vTexCoord = D3DXVECTOR2(0, 0);pVB[1].vPosition = D3DXVECTOR4(-0.5f, -0.5f + 600, 0, 1.0f);pVB[1].vTexCoord = D3DXVECTOR2(0, 1);pVB[2].vPosition = D3DXVECTOR4(-0.5f + 800, -0.5f, 0, 1.0f);pVB[2].vTexCoord = D3DXVECTOR2(1, 0);pVB[3].vPosition = D3DXVECTOR4(-0.5f + 800, -0.5f + 600, 0, 1.0f);pVB[3].vTexCoord = D3DXVECTOR2(1, 1);

Share on other sites
ok, thankyou

so with what you have there, you mean just set the projection/view/world to identity, and then just draw?

dont i need to set up the projection matrix still with my height/width though?

Share on other sites
It doesn't matter what you set the matrices to, they'll be ignored. The vertices will just be placed on the screen at the coordinates specified according to the dimensions of your render-target.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628333
• Total Posts
2982121

• 22
• 9
• 9
• 13
• 11