高級紋理貼圖_第1頁
高級紋理貼圖_第2頁
高級紋理貼圖_第3頁
高級紋理貼圖_第4頁
高級紋理貼圖_第5頁
已閱讀5頁,還剩161頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、高級紋理映射技術(shù)(1) 紋理映射在三維圖形程序設(shè)計(jì)中具有非常重要的作用,三維場景中的許多特殊效果都是通過紋理映射來實(shí)現(xiàn)的。例如通過紋理映射模擬復(fù)雜的光照效果,物體表面對周圍環(huán)境的反射效果等。 多層紋理映射Direct3D最多支持8層紋理,也就是說,在一個三維物體的表面可以同時擁有18張不同的紋理貼圖。Direct3D能夠在一個渲染過程中把 這些紋理顏色依次混合,渲染到同一個物體的表面。每一個紋理層對應(yīng)07的索引序號,多層紋理映射能夠模擬更為真實(shí)的三維世界。例如,要顯示具有周圍景物 倒影的光滑大理石地板,可以把大理石地板貼圖設(shè)置為紋理層0,把具有周圍景物倒影的貼圖設(shè)置為紋理層1,然后通

2、過設(shè)置Direct3D多層紋理混合操作, 把紋理層0和紋理層1相混合,這時繪制出的三維物體就同時具有大理石地板和景物倒影的紋理顏色。利用Direct3D多達(dá)8層的紋理混合,可以在圖形顯示 系統(tǒng)中顯示豐富多彩的圖像。Direct3D多層紋理混合過程如下圖所示:從上圖可以看出8層紋理是逐層混合然后輸出的,也就是說,最對需要8個階段完成紋理映射,而且每一階段都是獨(dú)立進(jìn)行的,針對每一階段都需要設(shè)置相應(yīng)的顏色 和alpha混合方法。所以一個紋理層就相當(dāng)于一個紋理階段,多層紋理映射有時也稱為多階段紋理混合。其中,是否應(yīng)用紋理層07,即是否進(jìn)行07紋理 階段的操作,可由應(yīng)用程序指定,但它們的選擇必須是順序的

3、。也就是說,在沒有使用第n層的情況下,第n+1層不能使用。默認(rèn)情況下,第一個紋理階段操作(階段0)的默認(rèn)操作是D3DTOP_MODULATE,其他紋理操作階段的默認(rèn)操作是D3DTOP_DISABLE。即默認(rèn)情況下Direct3D使用單層紋理映射繪制圖形,除紋理層0外,紋理層17都是禁用的。在使用多層紋理映射之前,應(yīng)先查詢當(dāng)前設(shè)備是否支持紋理混合,以及最多能支持幾層紋理混合:/ check whether device support multi textures renderif(pCaps->MaxTextureBlendStages <= 1)   

4、; return false;MaxTextureBlendStages Maximum number of texture-blending stages supported in the fixed function pipeline. This value is the number of blenders available. In the programmable pixel pipeline, this corresponds to the number of unique texture registers used by pixel shader instructions. 為

5、了將多層紋理映射到物體表面,需要為每層紋理指定使用的紋理坐標(biāo),各層紋理可以使用相同的紋理坐標(biāo),也可以使用不同的紋理坐標(biāo)。紋理坐標(biāo)包含在頂點(diǎn)數(shù)據(jù) 中,如果需要使用不同的紋理坐標(biāo),那么在頂點(diǎn)數(shù)據(jù)中就需要包括多組紋理坐標(biāo),然后通過索引為每層紋理指定使用哪組紋理坐標(biāo):pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);D3DTSS_TEXCOORDINDEX Index of the texture

6、coordinate set to use with this texture stage. You can specify up to eight sets of texture coordinates per vertex. If a vertex does not include a set of texture coordinates at the specified index, the system defaults to the u and v coordinates (0,0). When rendering using vertex shaders, each stage&#

7、39;s texture coordinate index must be set to its default value. The default index for each stage is equal to the stage index. Set this state to the zero-based index of the coordinate set for each vertex that this texture stage uses.Additionally, applications can include, as logical OR with the index

8、 being set, one of the constants to request that Direct3D automatically generate the input texture coordinates for a texture transformation. For a list of all the constants, see D3DTSS_TCI.With the exception of D3DTSS_TCI_PASSTHRU, which resolves to zero, if any of the following values is included w

9、ith the index being set, the system uses the index strictly to determine texture wrapping mode. These flags are most useful when performing environment mapping. 這里指定了兩組紋理坐標(biāo):struct sCustomVertexfloat x, y, z;DWORD color;float u0, v0;float u1, v1;#define D3DFVF_CUSTOM_VERTEX(D3DFVF_XYZ | D3DFVF_DIFFUS

10、E | D3DFVF_TEX2)sCustomVertex vertices = -3.0f, -3.0f, 0.0f, 0xffffffff, 0.0f, 1.0f, 0.0f, 1.0f, -3.0f, 3.0f, 0.0f, 0xffffffff, 0.0f, 0.0f, 0.0f, 0.0f, 3.0f, -3.0f, 0.0f, 0xffffffff, 1.0f, 1.0f, 1.0f, 1.0f, 3.0f, 3.0f, 0.0f, 0xffffffff, 1.0f, 0.0f, 1.0f, 0.0f;這里指定的兩組紋理坐標(biāo)值完全相同,你可以改變其中任何一組坐標(biāo),使兩層紋理使用不同

11、的紋理坐標(biāo)。設(shè)置紋理層混合方法的代碼如下:/ set color blend operation and texture coordinate index for texture stage 0pd3dDevice->SetTexture(0, g_texture_0);pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);pd3dDevice->Se

12、tTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);pd3dDevice->SetSamplerState(0, D3DSAMP

13、_MINFILTER, D3DTEXF_LINEAR);/ set color blend operation and texture coordinate index for texture stage 1pd3dDevice->SetTexture(1, g_texture_1);pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);pd3dDevice->

14、SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);pd3dDevice->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);pd3dDevice->SetSamplerState(1, D3DSA

15、MP_MINFILTER, D3DTEXF_LINEAR);這里先將物體的漫反射顏色和紋理層0的紋理顏色相乘,得到的結(jié)果再與紋理層1的紋理顏色相加后輸出。該示例將物體紋理和光照紋理相混合后輸出:物體紋理光照紋理運(yùn)行效果圖如下:我們可以修改頂點(diǎn)的紋理坐標(biāo)為:sCustomVertex vertices = -3.0f, -3.0f, 0.0f, 0xffffffff, 0.0f, 1.0f, 1.0f, 0.0f, -3.0f, 3.0f, 0.0f, 0xffffffff, 0.0f, 0.0f, 1.0f, 1.0f, 3.0f, -3.0f, 0.0f, 0xffffffff, 1.0f,

16、 1.0f, 0.0f, 0.0f, 3.0f, 3.0f, 0.0f, 0xffffffff, 1.0f, 0.0f, 0.0f, 1.0f;這時的運(yùn)行效果圖如下:主程序:#include "dxstdafx.h"#include "resource.h"#pragma warning(disable : 4127)#define IDC_TOGGLE_FULLSCREEN        1#define 

17、;IDC_TOGGLE_REF                2#define IDC_CHANGE_DEVICE            3#define release_com(p)    do  if(p)  (p)

18、->Release(); (p) = NULL;   while(0)struct sCustomVertex    float x, y, z;    DWORD color;    float u0, v0;    float u1, v1;#define D3DFVF

19、_CUSTOM_VERTEX    (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX2)ID3DXFont*        g_font;ID3DXSprite*    g_text_sprite;bool            g_show_h

20、elp = true;CDXUTDialogResourceManager    g_dlg_resource_manager;CD3DSettingsDlg                g_settings_dlg;CDXUTDialog           

21、60;        g_button_dlg;IDirect3DVertexBuffer9*        g_vertex_buffer;IDirect3DTexture9*            g_texture_0;IDirect3DTexture9*     

22、;       g_texture_1;/-/ Rejects any devices that aren't acceptable by returning false/-bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat,    

23、                               D3DFORMAT BackBufferFormat, bool bWindowed, void* pUserContext )   &#

24、160;/ Typically want to skip backbuffer formats that don't support alpha blending    IDirect3D9* pD3D = DXUTGetD3DObject();     if( FAILED( pD3D->CheckDeviceFormat(

25、0;pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat,                     D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, BackBufferFormat ) )&

26、#160;)        return false;    / check whether device support multi textures render    if(pCaps->MaxTextureBlendStages <= 1)      

27、0; return false;    return true;/-/ Before a device is created, modify the device settings as needed./-bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const

28、0;D3DCAPS9* pCaps, void* pUserContext )    / If video card does not support hardware vertex processing, then uses sofaware vertex processing.    if(pCaps->DevCaps 

29、;& D3DDEVCAPS_HWTRANSFORMANDLIGHT) = 0)        pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;    static bool is_first_time = true;    if(is_first

30、_time)            is_first_time = false;        / if using reference device, then pop a warning message box.     &#

31、160;  if(pDeviceSettings->DeviceType = D3DDEVTYPE_REF)            DXUTDisplaySwitchingToREFWarning();        return true;/-/ Create any D3DPOOL_MANAGED 

32、resources here /-HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice,                                

33、;  const D3DSURFACE_DESC* pBackBufferSurfaceDesc,                                  void* pUserContext

34、0;)    HRESULT    hr;    V_RETURN(g_dlg_resource_manager.OnCreateDevice(pd3dDevice);    V_RETURN(g_settings_dlg.OnCreateDevice(pd3dDevice);    D3DXCreateFont(pd3dDevice, 18, 0, FW_BOLD,

35、 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,                   DEFAULT_PITCH | FF_DONTCARE, L"Arial", &g_font);  

36、60; V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, L"Wall.bmp",  &g_texture_0);    V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, L"light.jpg", &g_texture_1);    / create vertex buffer

37、60;and fill data    sCustomVertex vertices =                  -3.0f, -3.0f,  0.0f,  0xffffffff, 0.0f, 1.0f, 0.0f, 1.0f, 

38、        -3.0f,  3.0f,  0.0f,  0xffffffff, 0.0f, 0.0f, 0.0f, 0.0f,          3.0f, -3.0f,  0.0f,  0xffffffff, 1.0f, 1.0f, 

39、1.0f, 1.0f,          3.0f,  3.0f,  0.0f,  0xffffffff, 1.0f, 0.0f, 1.0f, 0.0f                /*   

40、60;     -3.0f, -3.0f,  0.0f,  0xffffffff, 0.0f, 1.0f, 1.0f, 0.0f,         -3.0f,  3.0f,  0.0f,  0xffffffff, 0.0f, 0.0f, 1.0f, 1.0f,

41、0;         3.0f, -3.0f,  0.0f,  0xffffffff, 1.0f, 1.0f, 0.0f, 0.0f,          3.0f,  3.0f,  0.0f,  0xffffffff, 1.0f, 0.0f

42、, 0.0f, 1.0f        */        pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_MANAGED, &g_vertex_buffer, NULL);    void*

43、0;ptr;    g_vertex_buffer->Lock(0, sizeof(vertices), (void*)&ptr, 0);    memcpy(ptr, vertices, sizeof(vertices);    g_vertex_buffer->Unlock();    return S_OK;/-/ Create a

44、ny D3DPOOL_DEFAULT resources here /-HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice,                           &

45、#160;     const D3DSURFACE_DESC* pBackBufferSurfaceDesc,                                 void* 

46、pUserContext )    HRESULT hr;    V_RETURN(g_dlg_resource_manager.OnResetDevice();    V_RETURN(g_settings_dlg.OnResetDevice();    V_RETURN(g_font->OnResetDevice();    V_RETURN(D3DXCreateSp

47、rite(pd3dDevice, &g_text_sprite);    / set dialog position and size    g_button_dlg.SetLocation(pBackBufferSurfaceDesc->Width - 170, 0);    g_button_dlg.SetSize(170, 170);  

48、  / setup view matrix    D3DXMATRIX mat_view;    D3DXVECTOR3 eye(0.0f, 0.0f, -8.0f);    D3DXVECTOR3  at(0.0f, 0.0f,  0.0f);    D3DXVECTOR3  

49、up(0.0f, 1.0f,  0.0f);    D3DXMatrixLookAtLH(&mat_view, &eye, &at, &up);    pd3dDevice->SetTransform(D3DTS_VIEW, &mat_view);    / set projection matrix  

50、0; D3DXMATRIX mat_proj;    float aspect = (float)pBackBufferSurfaceDesc->Width / pBackBufferSurfaceDesc->Height;    D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4, aspect, 1.0f, 100.0f); &#

51、160;  pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat_proj);    / set color blend operation and texture coordinate index for texture stage 0    pd3dDevice->SetTexture(0, 

52、g_texture_0);    pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP,        D3DTOP_MODULATE);    pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1,    D3DTA_TEXTURE);  

53、60; pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2,    D3DTA_DIFFUSE);    pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP,        D3DTOP_DISABLE);    pd3dDevice->SetTe

54、xtureStageState(0, D3DTSS_TEXCOORDINDEX,    0);    pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);    pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);   

55、0;/ set color blend operation and texture coordinate index for texture stage 1    pd3dDevice->SetTexture(1, g_texture_1);    pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, &#

56、160;      D3DTOP_ADD);    pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1,    D3DTA_TEXTURE);    pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2,    D3DTA_CURRENT);

57、    pd3dDevice->SetTextureStageState(1, D3DTSS_ALPHAOP,        D3DTOP_DISABLE);    pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX,    1);    pd3dDevice-&g

58、t;SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);    pd3dDevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);    pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);    return S_OK;/-/ 

59、;Release resources created in the OnResetDevice callback here /-void CALLBACK OnLostDevice( void* pUserContext )    g_dlg_resource_manager.OnLostDevice();    g_settings_dlg.OnLostDevice(); &

60、#160;  g_font->OnLostDevice();    release_com(g_text_sprite);/-/ Release resources created in the OnCreateDevice callback here/-void CALLBACK OnDestroyDevice( void* pUserContext )    

61、;g_dlg_resource_manager.OnDestroyDevice();    g_settings_dlg.OnDestroyDevice();        release_com(g_font);    release_com(g_vertex_buffer);    release_com(g_texture_0);    release

62、_com(g_texture_1);/-/ Handle updates to the scene/-void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )/-/ Render the helper information/-voi

63、d RenderText()    CDXUTTextHelper text_helper(g_font, g_text_sprite, 20);        text_helper.Begin();    / show frame and device states    text_helper.SetI

64、nsertionPos(5, 5);    text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 0.475f, 0.0f, 1.0f) );    text_helper.DrawTextLine( DXUTGetFrameStats(true) );    text_helper.DrawTextLine( DXUTGetDeviceStat

65、s() );    / show other simple information    text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) );    text_helper.DrawTextLine(L"Multi Texture Blending");&

66、#160;   / show helper information        const D3DSURFACE_DESC* surface_desc = DXUTGetBackBufferSurfaceDesc();    if(g_show_help)          &

67、#160; text_helper.SetInsertionPos(10, surface_desc->Height - 15 * 6);        text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 0.475f, 0.0f, 1.0f) );        text_he

68、lper.DrawTextLine(L"Controls (F1 to hide):");                text_helper.SetInsertionPos(40, surface_desc->Height - 15 * 4);       &

69、#160;text_helper.DrawTextLine(L"Quir: ESC");        else            text_helper.SetInsertionPos(10, surface_desc->Height - 15 * 4);    &#

70、160;   text_helper.SetForegroundColor( D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) );        text_helper.DrawTextLine(L"Press F1 for help");        text_helper.End()

71、;/-/ Render the scene /-void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )    HRESULT hr;    if(g_settings_dlg.IsActi

72、ve()            g_settings_dlg.OnRender(fElapsedTime);        return;        / Clear the render target and the zbuffer &

73、#160;   V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) );    / Render the scene    if( SUCCEEDED( pd3d

74、Device->BeginScene() ) )            pd3dDevice->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex);        pd3dDevice->SetFVF(D3DFVF_CUSTOM_VERTEX); &#

75、160;      pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);        RenderText();        V(g_button_dlg.OnRender(fElapsedTime);        

76、;V( pd3dDevice->EndScene() );    /-/ Handle messages to the application /-LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,      

77、                     bool* pbNoFurtherProcessing, void* pUserContext )    *pbNoFurtherProcessing = g_dlg_resource_manager.MsgProc(hWnd, uMsg

78、, wParam, lParam);    if(*pbNoFurtherProcessing)        return 0;    if(g_settings_dlg.IsActive()            g_settings_dlg.MsgProc(hWnd, uM

79、sg, wParam, lParam);        return 0;        *pbNoFurtherProcessing = g_button_dlg.MsgProc(hWnd, uMsg, wParam, lParam);    if(*pbNoFurtherProcessing)  

80、;      return 0;    return 0;/-/ Handle keybaord event/-void CALLBACK OnKeyboardProc(UINT charater, bool is_key_down, bool is_alt_down, void* user_context)    i

81、f(is_key_down)            switch(charater)                case VK_F1:            g_show_help

82、60;= !g_show_help;            break;            /-/ Handle events for controls/-void CALLBACK OnGUIEvent(UINT event, int control_i

83、d, CDXUTControl* control, void* user_context)    switch(control_id)        case IDC_TOGGLE_FULLSCREEN:        DXUTToggleFullScreen();       

84、 break;    case IDC_TOGGLE_REF:        DXUTToggleREF();        break;    case IDC_CHANGE_DEVICE:        g_settings_dlg.SetActive(true);        break;    /-/ Initialize dialogs/-void InitDialogs()    g_settings_dlg.Init(&g_dlg_resource_manager);   &#

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論