void D3D9Mesh::CreateText(const char *string, char *FontName, int FontHeight, float deviation, float depth, bool bold, bool italic) { //just call the DirectX function to create the text FreeMemory(); LPD3DXMESH TextMesh; HDC hdc = CreateCompatibleDC( NULL ); HFONT hFont; HFONT hFontOld; INT nHeight = -MulDiv( FontHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72 ); hFont = CreateFont(nHeight, 0, 0, 0, bold ? FW_BOLD : FW_NORMAL, italic, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FontName); hFontOld = (HFONT)SelectObject(hdc, hFont); if(FAILED(D3DXCreateText(GetD3DDevice(), hdc, string, deviation, depth, &TextMesh, NULL, NULL))) _asm int 3; SelectObject(hdc, hFontOld); DeleteObject( hFont ); DeleteDC( hdc ); TextMesh->CloneMeshFVF(D3DMeshOptions, D3DMeshFVF, GetD3DDevice(), &_Mesh); TextMesh->Release(); SetColor(RGBColor::White); GenerateNormals(); }
HRESULT CDebug::AddText3D( int _no, D3DXVECTOR3 _pos, LPTSTR _str, bool _isUpdate) { HFONT hFontOld; hFontOld = (HFONT)SelectObject(m_hdc, m_hFont); if( _no >= LOG_COUNT || _no < 0 ) { //로그 최대 갯수안에 들어오지 않거나 0보다 작으면 반환; /*if( m_Text3dCount >= LOG_COUNT ) m_Text3dCount = 0; _no = m_Text3dCount ;*/ AddLog(-1, _T("3D문자열 추가 실패 : %s"), _str); return E_FAIL; } m_Text3dPos[_no] = _pos; if( _no >= m_Text3dCount || _isUpdate ) { if (D3DXCreateText( m_pDevice, m_hdc, _str, 0.5f, 0.4f, &m_Text3D[_no++], NULL, NULL) != D3D_OK) assert(false); m_Text3dCount = _no; } SelectObject(m_hdc, hFontOld); return S_OK; }
//字体 void START::create_text(TEXT_STRUCT *text,wchar_t *name_text,HDC &hdc,int num) { D3DXCreateText(_device, hdc,name_text, //创建字体网格 0.001,0.01, &(text->_text), 0, 0); text->_sphere._radius = 800; //指定半径 text->_sphere._center = D3DXVECTOR3(-1000,1400,0) + (num + 1) * D3DXVECTOR3(0,1500,0); //设置包装盒的世界位置,拾取用 D3DXCreateSphere(_device,text->_sphere._radius,20,20,&(text->_sphere_mesh),NULL); //创建包装球 }
LPD3DXMESH Renderer::CreateD3DXTextMesh( const char* pString, bool bCentered ) { HRESULT hr; LPD3DXMESH pMeshNew = NULL; HDC hdc = CreateCompatibleDC( NULL ); if( hdc == NULL ) return NULL; HFONT newfont=CreateFont(10, //Height 0, //Width 0, //Escapement 0, //Orientation FW_NORMAL, //Weight false, //Italic false, //Underline false, //Strikeout DEFAULT_CHARSET,//Charset OUT_DEFAULT_PRECIS, //Output Precision CLIP_DEFAULT_PRECIS, //Clipping Precision DEFAULT_QUALITY, //Quality DEFAULT_PITCH|FF_DONTCARE, //Pitch and Family "Arial"); HFONT hFontOld; hFontOld = ( HFONT )SelectObject( hdc, newfont ); hr = D3DXCreateText( m_pD3DDevice, hdc, pString, 0.001f, 0.2f, &pMeshNew, NULL, NULL ); SelectObject( hdc, hFontOld ); DeleteDC( hdc ); if( SUCCEEDED( hr ) ) { if( bCentered ) { // Center text D3DXVECTOR3 vMin, vMax; PosNormalVertex* pVertices; pMeshNew->LockVertexBuffer( 0, reinterpret_cast<VOID**>(&pVertices)); D3DXComputeBoundingBox( (D3DXVECTOR3*)pVertices, pMeshNew->GetNumVertices(), sizeof ( PosNormalVertex ), &vMin, &vMax ); D3DXVECTOR3 vOffset; D3DXVec3Subtract( &vOffset, &vMax, &vMin ); D3DXVec3Scale( &vOffset, &vOffset, 0.5f ); for ( unsigned int i = 0; i < pMeshNew->GetNumVertices(); i++) { D3DXVec3Subtract( &pVertices[i].Coord, &pVertices[i].Coord, &vOffset ); } pMeshNew->UnlockVertexBuffer(); } } return pMeshNew; }
bool Setup() { HDC hdc = CreateCompatibleDC(0); HFONT hFont; HFONT hFontOld; LOGFONT lf; ZeroMemory(&lf, sizeof(LOGFONT)); lf.lfHeight = 20; lf.lfWidth = 12; lf.lfWeight = 500; lf.lfCharSet = DEFAULT_CHARSET; strcpy_s(lf.lfFaceName, "Comic Sans MS"); hFont = CreateFontIndirect(&lf); hFontOld = (HFONT)SelectObject(hdc, hFont); D3DXCreateText(Device, hdc, "Christopher King", 0.0001f, 0.4f, &Text, 0,0); SelectObject(hdc, hFontOld); DeleteObject(hFont); DeleteDC(hdc); // position and aim the camera D3DXVECTOR3 position(0.0f, 0.0f, -5.0f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMATRIX V; D3DXMatrixLookAtLH(&V, &position, &target, &up); Device->SetTransform(D3DTS_VIEW, &V); // set projection matrix D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.5f, (float)Width / (float)Height, 1.0f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); // set the render states Device->SetRenderState(D3DRS_FILLMODE, 5); return true; }
// // Framework functions // bool Setup() { // // 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, "Direct3D", 0.001f, 0.4f, &Text, 0, 0); // // Restore the old font and free the acquired HDC. // SelectObject(hdc, hFontOld); DeleteObject( hFont ); DeleteDC( hdc ); // // Lights. // D3DXVECTOR3 dir(0.0f, -0.5f, 1.0f); D3DXCOLOR col = d3d::WHITE; D3DLIGHT9 light = d3d::InitDirectionalLight(&dir, &col); Device->SetLight(0, &light); Device->LightEnable(0, true); Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); Device->SetRenderState(D3DRS_SPECULARENABLE, true); // // Set camera. // D3DXVECTOR3 pos(0.0f, 1.5f, -3.3f); D3DXVECTOR3 target(0.0f, 0.0f, 0.0f); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXMATRIX V; D3DXMatrixLookAtLH( &V, &pos, &target, &up); Device->SetTransform(D3DTS_VIEW, &V); // // Set projection matrix. // D3DXMATRIX proj; D3DXMatrixPerspectiveFovLH( &proj, D3DX_PI * 0.25f, // 45 - degree (float)Width / (float)Height, 0.01f, 1000.0f); Device->SetTransform(D3DTS_PROJECTION, &proj); return true; }