• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當(dāng)自強(qiáng)而不息

            Direct3D中的字體與文本顯示(2)

            創(chuàng)建三維文本網(wǎng)格模型

            在Direct3D中,三維物體的顯示是通過(guò)網(wǎng)格模型來(lái)實(shí)現(xiàn)的,顯示三維物體的關(guān)鍵在于生成該網(wǎng)格模型。三維文本也不例外,顯示三維文本同樣需要該文本所對(duì)應(yīng)的網(wǎng)格模型。 Direct3D為此提供了功能庫(kù)函數(shù)D3DXCreateText(),通過(guò)它可以方便地創(chuàng)建一個(gè)包含具體文本的網(wǎng)格模型,該函數(shù)的聲明如下:

            Creates a mesh containing the specified text, using the font associated with the device context.

            HRESULT D3DXCreateText(
            LPDIRECT3DDEVICE9 pDevice,
            HDC hDC,
            LPCTSTR pText,
            FLOAT Deviation,
            FLOAT Extrusion,
            LPD3DXMESH * ppMesh,
            LPD3DXBUFFER * ppAdjacency,
            LPGLYPHMETRICSFLOAT pGlyphMetrics
            );

            Parameters

            pDevice
            [in] Pointer to the device that created the mesh.
            hDC
            [in] Device context, containing the font for output. The font selected by the device context must be a TrueType font.
            pText
            [in] Pointer to a string that specifies the text to generate. If the compiler settings require Unicode, the data type LPCTSTR resolves to LPCWSTR. Otherwise, the string data type resolves to LPCSTR. See Remarks.
            Deviation
            [in] Maximum chordal deviation from TrueType font outlines.
            Extrusion
            [in] Amount to extrude text in the negative z-direction.
            ppMesh
            [out] Pointer to the returned mesh.
            ppAdjacency
            [out] Pointer to a buffer containing adjacency information. May be NULL.
            pGlyphMetrics
            [out] Pointer to an array of GLYPHMETRICSFLOAT structures that contain the glyph metric data. Each element contains information about the position and orientation of the corresponding glyph in the string. The number of elements in the array should be equal to the number of characters in the string. Note that the origin in each structure is not relative to the entire string, but rather is relative to that character cell. To compute the entire bounding box, add the increment for each glyph while traversing the string. If you are not concerned with the glyph sizes, set this parameter to NULL.

            Return Values

            If the function succeeds, the return value is D3D_OK. If the function fails, the return value can be one of the following: D3DERR_INVALIDCALL, D3DXERR_INVALIDDATA, E_OUTOFMEMORY.

            Remarks

            The compiler setting also determines the function version. If Unicode is defined, the function call resolves to D3DXCreateTextW. Otherwise, the function call resolves to D3DXCreateTextA because ANSI strings are being used.

            This function creates a mesh with the D3DXMESH_MANAGED creation option and D3DFVF_XYZ | D3DFVF_NORMAL flexible vertex format (FVF).

            Deviaton指定弦偏差的最大值。

            Extrusion指定文本在z軸負(fù)方向突出的總量。

            GLYPHMETRICSFLOAT

            The GLYPHMETRICSFLOAT structure contains information about the placement and orientation of a glyph in a character cell.

            typedef struct _GLYPHMETRICSFLOAT { // gmf   
            FLOAT gmfBlackBoxX;
            FLOAT gmfBlackBoxY;
            POINTFLOAT gmfptGlyphOrigin;
            FLOAT gmfCellIncX;
            FLOAT gmfCellIncY;
            } GLYPHMETRICSFLOAT;

            Members

            gmfBlackBoxX
            Specifies the width of the smallest rectangle (the glyph's black box) that completely encloses the glyph.
            gmfBlackBoxY
            Specifies the height of the smallest rectangle (the glyph's black box) that completely encloses the glyph.
            gmfptGlyphOrigin
            Specifies the x and y coordinates of the upper-left corner of the smallest rectangle that completely encloses the glyph.
            gmfCellIncX
            Specifies the horizontal distance from the origin of the current character cell to the origin of the next character cell.
            gmfCellIncY
            Specifies the vertical distance from the origin of the current character cell to the origin of the next character cell.

            Remarks

            The values of GLYPHMETRICSFLOAT are specified as notional units.

            示例代碼如下:

            	HDC hdc = CreateCompatibleDC(NULL);
            if(hdc == NULL)
            return false;
            	HFONT hfont = CreateFont(0, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
            CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, L"Arial");
            	SelectObject(hdc, hfont);
            	D3DXCreateText(g_device, hdc, L"三維字體", 0.001f, 0.4f, &g_text_mesh, NULL, NULL);
            	DeleteObject(hfont);
            DeleteDC(hdc);

            The CreateCompatibleDC function creates a memory device context (DC) compatible with the specified device.

            HDC CreateCompatibleDC(
            HDC
            hdc // handle to DC
            );

            Parameters

            hdc
            [in] Handle to an existing DC. If this handle is NULL, the function creates a memory DC compatible with the application's current screen.

            Return Values

            If the function succeeds, the return value is the handle to a memory DC.

            If the function fails, the return value is NULL.

            Windows NT/2000/XP: To get extended error information, call GetLastError.

            Remarks

            A memory DC exists only in memory. When the memory DC is created, its display surface is exactly one monochrome pixel wide and one monochrome pixel high. Before an application can use a memory DC for drawing operations, it must select a bitmap of the correct width and height into the DC. To select a bitmap into a DC, use the CreateCompatibleBitmap function, specifying the height, width, and color organization required.

            When a memory DC is created, all attributes are set to normal default values. The memory DC can be used as a normal DC. You can set the attributes; obtain the current settings of its attributes; and select pens, brushes, and regions.

            The CreateCompatibleDC function can only be used with devices that support raster operations. An application can determine whether a device supports these operations by calling the GetDeviceCaps function.

            When you no longer need the memory DC, call the DeleteDC function.

            Windows 2000 and later: If hdc is NULL, the thread that calls CreateCompatibleDC owns the HDC that is created. When this thread is destroyed, the HDC is no longer valid. Thus, if you create the HDC andpass it to another thread, then exit the first thread, the second thread will not be able to use the HDC.

             

            繪制三維文本網(wǎng)格模型

            創(chuàng)建好文本的網(wǎng)格模型之后,就可以使用ID3DXMesh的接口函數(shù)DrawSubset()將其繪制出來(lái),在繪制之前,需要注意設(shè)置合適的世界矩陣,這時(shí)雖然是繪制三維文本,但實(shí)質(zhì)上就是繪制一個(gè)三維物體,所以為三維文本設(shè)置世界矩陣是必不可少的。示例代碼如下:

            void setup_world_matrix()
            {
            D3DXMATRIX mat_translation;
            D3DXMatrixTranslation(&mat_translation, -1.75f, 0.0f, 0.0f);
            	D3DXMATRIX mat_world;
            D3DXMatrixRotationY(&mat_world, timeGetTime()/1000.0f);
            	mat_world = mat_translation * mat_world;
            	g_device->SetTransform(D3DTS_WORLD, &mat_world);
            }
            void render()
            {
            g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_X#050505, 1.0f, 0);
            	g_device->BeginScene();
            	setup_world_matrix();
            g_text_mesh->DrawSubset(0);
            	g_device->EndScene();
            	g_device->Present(NULL, NULL, NULL, NULL);
            }

            使用D3DXCreateText()函數(shù)為文本創(chuàng)建網(wǎng)格模型時(shí),網(wǎng)格模型的原點(diǎn)在左下方,所以需要對(duì)文本網(wǎng)格模型進(jìn)行平移,使其基本上顯示在窗口的中央。

             

            源程序:

            #include <D3DX9.h>

            #pragma warning(disable : 
            4127)

            #define CLASS_NAME    "GameApp"

            #define release_com(p)    do { if(p) { (p)->Release(); (p) = NULL; } } while(0)

            IDirect3D9
            *                g_d3d;
            IDirect3DDevice9
            *        g_device;
            ID3DXMesh
            *                g_text_mesh;

            void setup_world_matrix()
            {
                D3DXMATRIX mat_translation;
                D3DXMatrixTranslation(
            &mat_translation, -1.75f0.0f0.0f);

                D3DXMATRIX mat_world;
                D3DXMatrixRotationY(
            &mat_world, timeGetTime()/1000.0f);

                mat_world 
            = mat_translation * mat_world;

                g_device
            ->SetTransform(D3DTS_WORLD, &mat_world);
            }

            void setup_view_proj_matrices()
            {
                D3DXVECTOR3 eye(
            0.0f0.0f,-8.0f);
                D3DXVECTOR3  at(
            0.0f0.0f0.0f);
                D3DXVECTOR3  up(
            0.0f1.0f0.0f);

                D3DXMATRIX mat_view;
                D3DXMatrixLookAtLH(
            &mat_view, &eye, &at, &up);
                g_device
            ->SetTransform(D3DTS_VIEW, &mat_view);

                D3DXMATRIX mat_proj;
                D3DXMatrixPerspectiveFovLH(
            &mat_proj, D3DX_PI/41.0f1.0f100.0f);
                g_device
            ->SetTransform(D3DTS_PROJECTION, &mat_proj);
            }

            void setup_material_light()
            {
                D3DMATERIAL9 material;
                ZeroMemory(
            &material, sizeof(D3DMATERIAL9));

                material.Diffuse.r 
            = material.Ambient.r = 1.0f;
                material.Diffuse.g 
            = material.Ambient.g = 1.0f;
                material.Diffuse.b 
            = material.Ambient.b = 1.0f;
                material.Diffuse.a 
            = material.Ambient.a = 1.0f;

                g_device
            ->SetMaterial(&material);
                
                D3DLIGHT9 light;
                ZeroMemory(
            &light, sizeof(D3DLIGHT9));

                light.Type       
            = D3DLIGHT_DIRECTIONAL;
                light.Diffuse.r  
            = 0.0f;
                light.Diffuse.g  
            = 1.0f;
                light.Diffuse.b  
            = 1.0f;
                light.Range      
            = 1000.0f;

                D3DXVECTOR3 light_dir(
            111);
                D3DXVec3Normalize((D3DXVECTOR3
            *)&light.Direction, &light_dir);
                
                g_device
            ->SetLight(0&light);
                g_device
            ->LightEnable(0, TRUE);
                g_device
            ->SetRenderState(D3DRS_LIGHTING, TRUE);
                
                g_device
            ->SetRenderState(D3DRS_AMBIENT, 0xffFF50FF);
            }

            bool init_d3d(HWND hwnd)
            {
                g_d3d 
            = Direct3DCreate9(D3D_SDK_VERSION);

                
            if(g_d3d == NULL)
                    
            return false;

                D3DPRESENT_PARAMETERS d3dpp;
                ZeroMemory(
            &d3dpp, sizeof(d3dpp));

                d3dpp.Windowed            
            = TRUE;
                d3dpp.SwapEffect        
            = D3DSWAPEFFECT_DISCARD;
                d3dpp.BackBufferFormat    
            = D3DFMT_UNKNOWN;

                
            if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                              
            &d3dpp, &g_device)))
                {
                    
            return false;
                }

                HDC hdc 
            = CreateCompatibleDC(NULL);
                
            if(hdc == NULL)
                    
            return false;

                HFONT hfont 
            = CreateFont(0000, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
                                         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH 
            | FF_DONTCARE, L"Arial");

                SelectObject(hdc, hfont);

                D3DXCreateText(g_device, hdc, L
            "三維字體"0.001f0.4f&g_text_mesh, NULL, NULL);

                DeleteObject(hfont);
                DeleteDC(hdc);

                g_device
            ->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
                g_device
            ->SetRenderState(D3DRS_LIGHTING, TRUE);
                    
                setup_view_proj_matrices();
                setup_material_light();

                
            return true;
            }

            void cleanup()
            {
                release_com(g_text_mesh);
                release_com(g_device);
                release_com(g_d3d);
            }

            void render()
            {
                g_device
            ->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(555), 1.0f0);

                g_device
            ->BeginScene();

                setup_world_matrix();
                g_text_mesh
            ->DrawSubset(0);

                g_device
            ->EndScene();

                g_device
            ->Present(NULL, NULL, NULL, NULL);
            }

            LRESULT WINAPI WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
            {
                
            switch(msg)
                {
                
            case WM_KEYDOWN:
                    
            if(wParam == VK_ESCAPE)
                        DestroyWindow(hwnd);
                    
            break;

                
            case WM_DESTROY:        
                    PostQuitMessage(
            0);
                    
            return 0;
                }

                
            return DefWindowProc(hwnd, msg, wParam, lParam);
            }

            int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR, INT)
            {
                WNDCLASSEX wc;

                wc.cbSize            
            = sizeof(WNDCLASSEX);
                wc.style            
            = CS_CLASSDC;
                wc.lpfnWndProc        
            = WinProc;
                wc.cbClsExtra        
            = 0;
                wc.cbWndExtra        
            = 0;
                wc.hInstance        
            = inst;
                wc.hIcon            
            = NULL;
                wc.hCursor            
            = NULL;
                wc.hbrBackground    
            = NULL;
                wc.lpszMenuName        
            = NULL;
                wc.lpszClassName    
            = L"GameApp";
                wc.hIconSm            
            = NULL;

                
            if(! RegisterClassEx(&wc))
                    
            return -1;

                HWND hwnd 
            = CreateWindow(L"GameApp", L"Direct3D App", WS_OVERLAPPEDWINDOW, 200100600500,
                                         NULL, NULL, wc.hInstance, NULL);

                
            if(hwnd == NULL)
                    
            return -1;

                
            if(init_d3d(hwnd))
                {
                    ShowWindow(hwnd, SW_SHOWDEFAULT);
                    UpdateWindow(hwnd);

                    MSG msg;
                    ZeroMemory(
            &msg, sizeof(msg));

                    
            while(msg.message != WM_QUIT)
                    {
                        
            if(PeekMessage(&msg, NULL, 00, PM_REMOVE))
                        {
                            TranslateMessage(
            &msg);
                            DispatchMessage(
            &msg);
                        }
                            
                        render();
                        Sleep(
            10);
                    }
                }

                cleanup();
                UnregisterClass(L
            "GameApp", wc.hInstance);    

                
            return 0;
            }

             

            posted on 2008-05-11 20:48 lovedday 閱讀(1833) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評(píng)論

            久久av高潮av无码av喷吹| 久久国产三级无码一区二区| 久久av无码专区亚洲av桃花岛| 国产免费福利体检区久久| 波多野结衣AV无码久久一区| 777米奇久久最新地址| 2021最新久久久视精品爱| 国内精品久久久久久久影视麻豆| 精品久久久久成人码免费动漫| AV色综合久久天堂AV色综合在| 亚洲精品无码专区久久同性男| 久久综合九色综合欧美狠狠| 无码精品久久久天天影视| 久久亚洲AV成人无码软件| 久久精品成人免费看| 99久久99久久精品国产| 精品久久久久久久| 亚洲中文字幕无码一久久区| 欧美久久天天综合香蕉伊| 无码八A片人妻少妇久久| 91久久精品国产91性色也| 久久久久久久久无码精品亚洲日韩 | 青青草国产精品久久久久| 久久久无码精品亚洲日韩蜜臀浪潮 | 久久精品国产亚洲精品2020| 亚洲欧美国产精品专区久久| 久久久久久A亚洲欧洲AV冫| 99久久99久久精品国产| 伊人久久免费视频| 亚洲国产精品久久久久婷婷软件 | 亚洲人成电影网站久久| 中文字幕久久波多野结衣av| 中文字幕无码久久人妻| 中文字幕无码av激情不卡久久| 亚洲伊人久久成综合人影院 | 久久午夜电影网| 香蕉久久一区二区不卡无毒影院| 国产欧美久久久精品| 青青青青久久精品国产h| 久久国产成人精品国产成人亚洲| 亚洲精品无码久久毛片|