How to layout 2d Font with D3DXVector3 in DirectX9

Started by
3 comments, last by SandWind 14 years, 1 month ago
I was developing a HUD in DirectX9. so I used a method of Id3dxfont follow as: INT DrawText( LPD3DXSPRITE pSprite, LPCTSTR pString, INT Count, LPRECT pRect, DWORD Format, D3DCOLOR Color ); I found the position of LPD3DXFONT depending on the varible RECT. But the member left,right,top,bottom of RECT is integer not float. In the 3D space,there was a trouble for tranforming the postion of 2d font I want to know a idear to layout 2d font and tranforming the position of font with matrix. I needed your advice. thanks a lot
Advertisement
if I clearly understand what do you want to do. You should find position of 3-dimensional point onto screen. The pseudocode below.

Vector2D WorldToScreen(Vector3D 3dpos, Matrix ViewProjection){Vector4D pos4(3dpos.x, 3dpos.y, 3dpos.z, 1);Vector4D proj4 = ViewProjection * Pos4;return Vector2D(((proj4.x/proj4.w)+1.0) * 0.5, ((-proj4.y/proj4.w)+1.0) * 0.5);}


Further you may place left-upper corner of text rectangle in computed position.

Oh, forget to add.
The returned coordinates will in [0;1] range. And if proj4.z will be less nil it's a back projection, and doesn't need draw such text (may to do special flag).

[Edited by - RomanAlex on March 2, 2010 6:25:25 AM]
Well, you can't set a transform matrix for the font, only the sprite that it uses to render. So, you can just use (0, 0) as the top left of your RECT, and change the text position using the matrix passed to ID3DXSprite::SetTransform().
Thank RomanAlex and Evil Steve for giving my great help.

Now,there was a new problem in my hud.

The font didn't align with the scale line when they translated together.


The scale line is always faster than the font.

code snippet:

D3DXMatrixTranslation(&world,vx,speed,vz);
world=world*matOrth;
p_Line->SetWidth(1.43f);
p_Line->Begin();
p_Line->DrawTransform(plongVertexlist,2,&world,D3DCOLOR_XRGB(0,255,0));
p_Line->End();

pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE ,true);

D3DXMatrixTranslation(&world,0,speed,0);
p_sprite->SetTransform(&world);

if (p_sprite != NULL)
{
Rt=p_sprite->Begin(D3DXSPRITE_ALPHABLEND);

p_Font->DrawText( p_sprite,speed_buffer,-1,&m_Rect_speed_Font,DT_LEFT,
D3DCOLOR_ARGB(0xcb,0,255,0));
//
Rt= p_sprite->End();
}
}
Quote:Original post by Evil Steve
Well, you can't set a transform matrix for the font, only the sprite that it uses to render. So, you can just use (0, 0) as the top left of your RECT, and change the text position using the matrix passed to ID3DXSprite::SetTransform().


so I used (0, 0) as the top left of the RECT,but font appeared on the left of screen not where i minded.

[Edited by - SandWind on March 10, 2010 10:36:00 AM]

This topic is closed to new replies.

Advertisement