|
|
发表于 2008-5-13 06:23:00
|
显示全部楼层
Re:请问如何在3D世界坐标里绘制文字,或者怎么把FONT的DRA
Step2. 用D3DXCreateText创建3D文字网格
//
// Get a handle to a device context.
//
HDC hdc = CreateCompatibleDC( 0 );
HFONT hFont;
HFONT hFontOld;
//
// Describe the font we want.
//
LOGFONT lf;
ZeroMemory(&lf, sizeof(LOGFONT));
lf.lfHeight = 25; // in logical units
lf.lfWidth = 12; // in logical units
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 500; // boldness, range 0(light) - 1000(bold)
lf.lfItalic = false;
lf.lfUnderline = false;
lf.lfStrikeOut = false;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = 0;
lf.lfClipPrecision = 0;
lf.lfQuality = 0;
lf.lfPitchAndFamily = 0;
strcpy(lf.lfFaceName, "Times New Roman"); // font style
//
// Create the font and select it with the device context.
//
hFont = CreateFontIndirect(&lf);
hFontOld = (HFONT)SelectObject(hdc, hFont);
//
// Create the text mesh based on the selected font in the HDC.
//
D3DXCreateText(Device, hdc, "hello!world!",
0.001f, 0.4f, &Text, 0, 0);
//
// Restore the old font and free the acquired HDC.
//
SelectObject(hdc, hFontOld);
DeleteObject( hFont );
DeleteDC( hdc );
Step3.渲染3D文字:
//
// Render
//
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);
Device->BeginScene();
Device->SetMaterial(&d3d::WHITE_MTRL);
Text->DrawSubset(0);
Device->EndScene();
Device-> resent(0, 0, 0, 0);
Step4.释放网格对象
void Cleanup()
{
d3d::Release<ID3DXMesh*>(Text);
}
在DXUT中试过,可以绘制中文。其中d3d命名空间的函数如下:
template<class T> void Release(T t)
{
if( t )
{
t->Release();
t = 0;
}
}
template<class T> void Delete(T t)
{
if( t )
{
delete t;
t = 0;
}
}
D3DMATERIAL9 d3d::InitMtrl(D3DXCOLOR a, D3DXCOLOR d, D3DXCOLOR s, D3DXCOLOR e, float p)
{
D3DMATERIAL9 mtrl;
mtrl.Ambient = a;
mtrl.Diffuse = d;
mtrl.Specular = s;
mtrl.Emissive = e;
mtrl.Power = p;
return mtrl;
}
const D3DMATERIAL9 WHITE_MTRL = InitMtrl(WHITE, WHITE, WHITE, BLACK, 2.0f);
const D3DMATERIAL9 RED_MTRL = InitMtrl(RED, RED, RED, BLACK, 2.0f);
const D3DMATERIAL9 GREEN_MTRL = InitMtrl(GREEN, GREEN, GREEN, BLACK, 2.0f);
const D3DMATERIAL9 BLUE_MTRL = InitMtrl(BLUE, BLUE, BLUE, BLACK, 2.0f);
const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW, YELLOW, YELLOW, BLACK, 2.0f);
|
|