青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

天行健 君子當自強而不息

高級紋理映射技術(9)

凹凸紋理映射

關于凹凸映射的原理請參閱凹凸映射(Bump Map)實現原理

凹凸紋理映射是一種紋理混合方法,它可以創建三維物體復雜的紋理外觀表面。普通的紋理映射只能模擬比較平滑的三維物體表面,難以顯示表面高低起伏、凹凸不平的效果。凹凸紋理映射能夠通過一張表示物體表面凹凸程度的高度圖(稱為凹凸紋理),對另一張表示物體表面環境映射的紋理圖的紋理坐標進行相應的干擾,經過干擾的紋理坐標將應用于環境映射,從而產生凹凸不平的顯示效果。凹凸紋理映射通常由三張紋理映射圖組成,第一張紋理圖表示物體表面原始紋理顏色,第二張凹凸紋理圖表示物體表面凹凸的高度起伏值,用來對下一張環境紋理圖坐標進行干擾,第三張紋理圖表示周圍鏡面反射或漫反射光照的環境光照映射圖。凹凸紋理映射的紋理映射流程如下圖所示:

 

檢查硬件設備

在使用凹凸紋理映射之前,應查詢當前的Direct3D設備是否支持D3DTEXOPCAPS_BUMPENVMAP或D3DTEXOPCAPS_BUMPENVMAPLUMINANCE多層紋理混合,以及當前設備是否支持3層紋理映射。

BOOL SupportsBumpMapping()
{
D3DCAPS9 d3dCaps;
d3dDevice->GetDeviceCaps( &d3dCaps );
    // Does this device support the two bump mapping blend operations?
if ( 0 == d3dCaps.TextureOpCaps & ( D3DTEXOPCAPS_BUMPENVMAP | D3DTEXOPCAPS_BUMPENVMAPLUMINANCE ))
return FALSE;
    // Does this device support up to three blending stages?
if( d3dCaps.MaxTextureBlendStages < 3 )
return FALSE;
    return TRUE;
}

如果當前硬件不支持上面的任何一項,程序框架會自動轉而使用參考設備。

 

凹凸紋理生成

Direct3D的凹凸紋理被用來表示物體表面相鄰像素的高度差,它的每個紋理元素由表示水平相鄰像素高度差的Du、表示垂直相鄰像素高度差的Dv以及表示該點亮度的L組成(某些凹凸紋理像素格式可以不包含L)。下表列出了Direct3D支持的凹凸紋理像素格式:

凹凸紋理像素格式 說明
D3DFMT_V8U8 每個像素由16位整數表示,分別由8位整數表示Du和Dv
D3DFMT_L6V5U5 每個像素由16位整數表示,6位整數表示L,分別由5位整數表示Du和Dv
D3DFMT_X8L8V8U8 每個像素由32位整數表示,包括8位保留位、8位L、8位Du、8位Dv
D3DFMT_V16U16 每個像素由32位整數表示,分別由16位整數表示Du和Dv
D3DFMT_Q8W8V8U8 每個像素由32位整數表示,分別由8位整數表示Q、W、V、U
D3DFMT_CxV8U8 壓縮像素格式,每個像素由16位整數表示,即8位Du和8位Dv,另外C = sqrt(1 - Du2 - Dv2  )

通常情況下,可以載入一張表示物體表面圖像高度的紋理圖,通過計算高度圖水平相鄰和垂直相鄰元素的高度差來生成凹凸紋理,也可以通過程序生成凹凸紋理,這里根據紋理圖來生成凹凸紋理,代碼如下:

//--------------------------------------------------------------------------------------
// Create bump texture from height map texture.
//--------------------------------------------------------------------------------------
HRESULT CreateBumpTexture(IDirect3DDevice9* device)
{
HRESULT hr;
	D3DSURFACE_DESC surface_desc;
g_height_map_texture->GetLevelDesc(0, &surface_desc);
	V_RETURN(device->CreateTexture(surface_desc.Width, surface_desc.Height, 1, 0, D3DFMT_X8L8V8U8, D3DPOOL_MANAGED,
&g_bump_map_texture, NULL));
	D3DLOCKED_RECT locked_rect;
g_height_map_texture->LockRect(0, &locked_rect, NULL, 0);
	DWORD src_pitch   = (DWORD) locked_rect.Pitch;
BYTE* src_row_top = (BYTE*) locked_rect.pBits;
BYTE* src_row_cur = src_row_top;
BYTE* src_row_bot = src_row_top + src_pitch * (surface_desc.Height - 1);
	g_bump_map_texture->LockRect(0, &locked_rect, NULL, 0);
	DWORD dest_pitch   = (DWORD) locked_rect.Pitch;
BYTE* dest_row_top = (BYTE*) locked_rect.pBits;
BYTE* dest_row_cur = dest_row_top;
	// iterate through all lines
for(DWORD y = 0; y < surface_desc.Height; y++)
{
BYTE* src_pixel_cur;
BYTE* src_pixel_up;
BYTE* src_pixel_below;
BYTE* dest_pixel;
		src_pixel_cur = src_row_cur;
		if(y == 0)
src_pixel_up = src_row_bot;
else
src_pixel_up = src_row_cur - src_pitch;
		if(y == surface_desc.Height - 1)
src_pixel_below = src_row_top;
else
src_pixel_below = src_row_cur + src_pitch;
		dest_pixel = dest_row_cur;
		// iterate through all columns in current line
for(DWORD x = 0; x < surface_desc.Width; x++)
{
BYTE src_pixel_left, src_pixel_right;
			if(x == 0)
src_pixel_left = *(src_row_cur + (surface_desc.Width - 4));
else
src_pixel_left = *(src_pixel_cur - 4);
			if(x == surface_desc.Width - 1)
src_pixel_right = *src_row_cur;
else
src_pixel_right = *(src_pixel_cur + 4);
			BYTE du = BYTE(src_pixel_left - src_pixel_right);
BYTE dv = BYTE(src_pixel_up - src_pixel_below);
			// the luminance bump value
BYTE u_lumi = (*src_pixel_cur > 1) ? 63 : 127;
			*dest_pixel++ = du;
*dest_pixel++ = dv;
*dest_pixel++ = u_lumi;
*dest_pixel++ = 0;
			// move one pixel to the right
src_pixel_cur += 4;
src_pixel_up += 4;
src_pixel_below += 4;
}
		// move to the next line
src_row_cur += src_pitch;
dest_row_cur += dest_pitch;
}
	g_bump_map_texture->UnlockRect(0);
g_height_map_texture->UnlockRect(0);
	return S_OK;
}

 

凹凸紋理設置

凹凸紋理映射通常使用3層紋理:物體原始紋理、由原始紋理高度圖生成的凹凸紋理、環境紋理,對應于多層紋理混合的0、1、2層。指定當前紋理層狀態為D3DTOP_BUMPENVMAP或D3DTOP_BUMPENVMAPLUMINANCE可設置當前紋理層為凹凸紋理,例如:

pd3dDevice->SetTexture(1, g_bump_map_texture);
pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAP);

pd3dDevice->SetTexture(1, g_bump_map_texture);
pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAPLUMINANCE);

紋理狀態D3DTOP_BUMPENVMAP和D3DTOP_BUMPENVMAPLUMINANCE表示兩種不同的凹凸紋理映射方法。紋理狀態D3DTOP_BUMPENVMAPLUMINANCE表示在凹凸紋理中包含凹凸紋理亮度值L,L將與下一紋理層的紋理顏色相乘作為最后輸出的紋理顏色。紋理狀態D3DTOP_BUMPENVMAP默認亮度值L為1,即不改變下一紋理層的紋理顏色。

這里分別使用了3層紋理貼圖,如下所示:


原始紋理


原始紋理高度圖


環境紋理貼圖

Direct3D可設置凹凸紋理矩陣,對凹凸紋理中的每個紋理元素值(Dv、Du,即對于下一紋理層中的每個紋理坐標的擾動值)進行坐標變換:

Du' = Du * M00 + Dv * M10

Dv' = Dv * M01 + Dv * M11

凹凸紋理的每個紋理元素Dv、Du與一個2 x 2的凹凸矩陣相乘,其結果Dv、Du對下一紋理層中的每個紋理元素的紋理坐標產生該數值代表的坐標擾動。2 x 2的凹凸矩陣值可由函數IDirect3DDevice9::SetTextureStageState()設置,將它的第一個參數設置為紋理層序號,第二個參數設置為D3DTSS_BUMPENVMAT00或D3DTSS_BUMPENVMAT01或D3DTSS_BUMPENVMAT10或D3DTSS_BUMPENVMAT11,分別表示凹凸矩陣的4個矩陣元素,第三個參數設置為該矩陣元素的值。紋理坐標擾動Dv、Du或Dv'、Du'的范圍介于-1 ~ +1之間。

如果使用D3DTOP_BUMPENVMAPLUMINANCE計算凹凸紋理,Direct3D將使用下列方程計算凹凸紋理的亮度值L'(L'為0~255的整數,它將被用來與下一紋理層的顏色相乘,作為最后輸出的紋理顏色值)。

L' = L * S + O

其中,L為凹凸紋理元素中的亮度L值,比例系數S和偏移系數O可改變最終的亮度值。S和O可由函數IDirect3DDevice9::SetTexureStageState()設置,將它的第一個參數設置為紋理層序號,第二個參數設置為D3DTSS_BUMPENVLSCALE或D3DTSS_BUMPENVLOFFSET,分別表示凹凸紋理映射的比例系數S和偏移系數O,將第三個參數設置為相應的系數值。

設置凹凸紋理狀態的代碼如下所示:

// set texture color blend method for stage 0 (base texture)

pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);

pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

// set texture color blend method for stage 1 (bump map texture)

pd3dDevice->SetTexture(1, g_bump_map_texture);

pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);

pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAPLUMINANCE);
pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);

pd3dDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT00, F2DW(0.8f));
pd3dDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT01, F2DW(0.0f));
pd3dDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT10, F2DW(0.0f));
pd3dDevice->SetTextureStageState(1, D3DTSS_BUMPENVMAT11, F2DW(0.8f));

pd3dDevice->SetTextureStageState(1, D3DTSS_BUMPENVLSCALE, F2DW(4.0f));
pd3dDevice->SetTextureStageState(1, D3DTSS_BUMPENVLOFFSET, F2DW(0.0f));

pd3dDevice->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
pd3dDevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

// set texture color blend method for stage 2 (environment map texture)

D3DXMATRIX mat;

mat._11 = 0.5f; mat._12 = 0.0f; mat._13 = 0.0f; mat._14 = 0.0f;
mat._21 = 0.0f; mat._22 = -0.5f; mat._23 = 0.0f; mat._24 = 0.0f;
mat._31 = 0.0f; mat._32 = 0.0f; mat._33 = 1.0f; mat._34 = 0.0f;
mat._41 = 0.5f; mat._42 = 0.5f; mat._43 = 0.0f; mat._44 = 1.0f;

pd3dDevice->SetTransform(D3DTS_TEXTURE2, &mat);

pd3dDevice->SetTexture(2, g_env_map_texture);

pd3dDevice->SetTextureStageState(2, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
pd3dDevice->SetTextureStageState(2, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_SPHEREMAP);
pd3dDevice->SetTextureStageState(2, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pd3dDevice->SetTextureStageState(2, D3DTSS_COLORARG2, D3DTA_CURRENT);
pd3dDevice->SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_ADD);

pd3dDevice->SetSamplerState(2, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
pd3dDevice->SetSamplerState(2, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);


原始紋理圖


經過凹凸紋理映射后

 

主程序:

#include "dxstdafx.h"
#include 
"resource.h"

#pragma warning(disable : 
4127 4995 4996)

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

#define IDC_TOGGLE_FULLSCREEN        1
#define IDC_TOGGLE_REF                2
#define IDC_CHANGE_DEVICE            3

const D3DXCOLOR FONT_COLOR(1.0f0.5f0.25f1.0f);

ID3DXFont
*                    g_font;
ID3DXSprite
*                g_text_sprite;
bool                        g_show_help;

CDXUTDialogResourceManager    g_dlg_resource_manager;
CD3DSettingsDlg                g_settings_dlg;
CDXUTDialog                    g_button_dlg;

ID3DXMesh
*                    g_mesh;
D3DMATERIAL9
*                g_mesh_materials;
IDirect3DTexture9
**            g_mesh_textures;
DWORD                        g_num_materials;

IDirect3DTexture9
*            g_env_map_texture;
IDirect3DTexture9
*            g_height_map_texture;
IDirect3DTexture9
*            g_bump_map_texture;

inline DWORD F2DW(
float f) { return *((DWORD*&f); }

//--------------------------------------------------------------------------------------
// Rejects any devices that aren't acceptable by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsDeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, 
                                  D3DFORMAT BackBufferFormat, 
bool bWindowed, void* pUserContext )
{
    
// Typically want to skip backbuffer formats that don't support alpha blending

    IDirect3D9
* pD3D = DXUTGetD3DObject(); 

    
if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, 
                    D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
        
return false;

    
// check whether device support bump textue mapping
    if((pCaps->TextureOpCaps & (D3DTEXOPCAPS_BUMPENVMAP | D3DTEXOPCAPS_BUMPENVMAPLUMINANCE)) == 0)
        
return false;

    
// check whether device support 3 level texture stage blend
    if(pCaps->MaxTextureBlendStages < 3)
        
return false;

    
return true;
}


//--------------------------------------------------------------------------------------
// Before a device is created, modify the device settings as needed.
//--------------------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, const D3DCAPS9* pCaps, void* pUserContext )
{
    
// If video card does not support hardware vertex processing, then uses sofaware vertex processing.
    if((pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0)
        pDeviceSettings
->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

    
static bool is_first_time = true;

    
if(is_first_time)
    {
        is_first_time 
= false;

        
// if using reference device, then pop a warning message box.
        if(pDeviceSettings->DeviceType == D3DDEVTYPE_REF)
            DXUTDisplaySwitchingToREFWarning();
    }

    
return true;
}

//--------------------------------------------------------------------------------------
// Create bump texture from height map texture.
//--------------------------------------------------------------------------------------
HRESULT CreateBumpTexture(IDirect3DDevice9* device)
{
    HRESULT hr;

    D3DSURFACE_DESC surface_desc;
    g_height_map_texture
->GetLevelDesc(0&surface_desc);

    V_RETURN(device
->CreateTexture(surface_desc.Width, surface_desc.Height, 10, D3DFMT_X8L8V8U8, D3DPOOL_MANAGED,
                                   
&g_bump_map_texture, NULL));


    D3DLOCKED_RECT locked_rect;
    g_height_map_texture
->LockRect(0&locked_rect, NULL, 0);

    DWORD src_pitch   
= (DWORD) locked_rect.Pitch;
    BYTE
* src_row_top = (BYTE*) locked_rect.pBits;
    BYTE
* src_row_cur = src_row_top;
    BYTE
* src_row_bot = src_row_top + src_pitch * (surface_desc.Height - 1);

    g_bump_map_texture
->LockRect(0&locked_rect, NULL, 0);

    DWORD dest_pitch   
= (DWORD) locked_rect.Pitch;
    BYTE
* dest_row_top = (BYTE*) locked_rect.pBits;
    BYTE
* dest_row_cur = dest_row_top;

    
// iterate through all lines
    for(DWORD y = 0; y < surface_desc.Height; y++)
    {
        BYTE
* src_pixel_cur;
        BYTE
* src_pixel_up;
        BYTE
* src_pixel_below;        
        BYTE
* dest_pixel;

        src_pixel_cur 
= src_row_cur;

        
if(y == 0)
            src_pixel_up 
= src_row_bot;
        
else
            src_pixel_up 
= src_row_cur - src_pitch;

        
if(y == surface_desc.Height - 1)
            src_pixel_below 
= src_row_top;
        
else
            src_pixel_below 
= src_row_cur + src_pitch;

        dest_pixel 
= dest_row_cur;

        
// iterate through all columns in current line
        for(DWORD x = 0; x < surface_desc.Width; x++)
        {
            BYTE src_pixel_left, src_pixel_right;

            
if(x == 0)
                src_pixel_left 
= *(src_row_cur + (surface_desc.Width - 4));
            
else
                src_pixel_left 
= *(src_pixel_cur - 4);

            
if(x == surface_desc.Width - 1)
                src_pixel_right 
= *src_row_cur;
            
else
                src_pixel_right 
= *(src_pixel_cur + 4);

            BYTE du 
= BYTE(src_pixel_left - src_pixel_right);
            BYTE dv 
= BYTE(src_pixel_up   - src_pixel_below);

            
// the luminance bump value
            BYTE u_lumi = (*src_pixel_cur > 1? 63 : 127;

            
*dest_pixel++ = du;
            
*dest_pixel++ = dv;
            
*dest_pixel++ = u_lumi;
            
*dest_pixel++ = 0;

            
// move one pixel to the right
            src_pixel_cur   += 4;
            src_pixel_up    
+= 4;
            src_pixel_below 
+= 4;            
        }

        
// move to the next line
        src_row_cur  += src_pitch;
        dest_row_cur 
+= dest_pitch;
    }

    g_bump_map_texture
->UnlockRect(0);
    g_height_map_texture
->UnlockRect(0);

    
return S_OK;
}

//--------------------------------------------------------------------------------------
// Remove path from fullname, and convert filename from multibyte to wchar.
//--------------------------------------------------------------------------------------
void RemovePathFromFileName(LPSTR fullname, LPWSTR wfilename)
{
    WCHAR wbuf[MAX_PATH]  
= {0};
    MultiByteToWideChar(CP_ACP, 
0, fullname, -1, wbuf, MAX_PATH);

    LPWSTR w_last_back_slash 
= wcsrchr(wbuf, '\\');

    
if(w_last_back_slash)
        lstrcpy(wfilename, 
++w_last_back_slash);
    
else
        lstrcpy(wfilename, wbuf);
}

//--------------------------------------------------------------------------------------
// Create any D3DPOOL_MANAGED resources here 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnCreateDevice( IDirect3DDevice9* pd3dDevice, 
                                 
const D3DSURFACE_DESC* pBackBufferSurfaceDesc, 
                                 
void* pUserContext )
{
    HRESULT    hr;

    V_RETURN(g_dlg_resource_manager.OnCreateDevice(pd3dDevice));
    V_RETURN(g_settings_dlg.OnCreateDevice(pd3dDevice));

    D3DXCreateFont(pd3dDevice, 
180, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
                   DEFAULT_PITCH 
| FF_DONTCARE, L"Arial"&g_font);

    V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, L
"HeightMap.bmp"&g_height_map_texture));
    V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, L
"EnvMap.bmp",     &g_env_map_texture));
    V_RETURN(CreateBumpTexture(pd3dDevice));
    
    ID3DXBuffer
* material_buffer;

    V_RETURN(D3DXLoadMeshFromXW(L
"SphereEarth.x", D3DXMESH_MANAGED, pd3dDevice, NULL, &material_buffer, NULL, 
                                
&g_num_materials, &g_mesh));

    D3DXMATERIAL
* xmaterials = (D3DXMATERIAL*) material_buffer->GetBufferPointer();
    g_mesh_materials 
= new D3DMATERIAL9[g_num_materials];
    g_mesh_textures  
= new IDirect3DTexture9*[g_num_materials];

    
for(DWORD i = 0; i < g_num_materials; i++)
    {
        g_mesh_materials[i] 
= xmaterials[i].MatD3D;

        
// .x file do not save ambient data, so set it here.
        g_mesh_materials[i].Ambient = g_mesh_materials[i].Diffuse;         

        WCHAR wfilename[MAX_PATH];        
        RemovePathFromFileName(xmaterials[i].pTextureFilename, wfilename);

        g_mesh_textures[i] 
= NULL;

        
if(xmaterials[i].pTextureFilename != NULL && lstrlen(wfilename) > 0)        
            V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, wfilename, 
&g_mesh_textures[i]));
    }

    material_buffer
->Release();

    
return S_OK;
}


//--------------------------------------------------------------------------------------
// Create any D3DPOOL_DEFAULT resources here 
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnResetDevice( IDirect3DDevice9* pd3dDevice, 
                                
const D3DSURFACE_DESC* pBackBufferSurfaceDesc, 
                                
void* pUserContext )
{
    HRESULT hr;

    V_RETURN(g_dlg_resource_manager.OnResetDevice());
    V_RETURN(g_settings_dlg.OnResetDevice());
    V_RETURN(g_font
->OnResetDevice());
    V_RETURN(D3DXCreateSprite(pd3dDevice, 
&g_text_sprite));

    
// set dialog position and size

    g_button_dlg.SetLocation(pBackBufferSurfaceDesc
->Width - 1700);
    g_button_dlg.SetSize(
170170);

    
// setup view matrix

    D3DXMATRIX mat_view;
    D3DXVECTOR3 eye(
0.0f-3.0f0.0f);
    D3DXVECTOR3  at(
0.0f0.0f,  0.0f);
    D3DXVECTOR3  up(
0.0f0.0f,  1.0f);

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

    
// set projection matrix
    D3DXMATRIX mat_proj;
    
float aspect = (float)pBackBufferSurfaceDesc->Width / pBackBufferSurfaceDesc->Height;
    D3DXMatrixPerspectiveFovLH(
&mat_proj, D3DX_PI/4, aspect, 1.0f1000.0f);
    pd3dDevice
->SetTransform(D3DTS_PROJECTION, &mat_proj);    

    
// set texture color blend method for stage 0 (base texture)
    
    pd3dDevice
->SetTextureStageState(0, D3DTSS_COLORARG1,        D3DTA_TEXTURE);        
    pd3dDevice
->SetTextureStageState(0, D3DTSS_COLOROP,            D3DTOP_SELECTARG1);        
    
    pd3dDevice
->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    pd3dDevice
->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

    
// set texture color blend method for stage 1 (bump map texture)

    pd3dDevice
->SetTexture(1, g_bump_map_texture);

    pd3dDevice
->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);

    pd3dDevice
->SetTextureStageState(1, D3DTSS_COLOROP,           D3DTOP_BUMPENVMAPLUMINANCE);
    pd3dDevice
->SetTextureStageState(1, D3DTSS_COLORARG1,       D3DTA_TEXTURE);
    pd3dDevice
->SetTextureStageState(1, D3DTSS_COLORARG2,       D3DTA_CURRENT);

    pd3dDevice
->SetTextureStageState(1, D3DTSS_BUMPENVMAT00,   F2DW(0.8f));
    pd3dDevice
->SetTextureStageState(1, D3DTSS_BUMPENVMAT01,   F2DW(0.0f));
    pd3dDevice
->SetTextureStageState(1, D3DTSS_BUMPENVMAT10,   F2DW(0.0f));
    pd3dDevice
->SetTextureStageState(1, D3DTSS_BUMPENVMAT11,   F2DW(0.8f));

    pd3dDevice
->SetTextureStageState(1, D3DTSS_BUMPENVLSCALE,  F2DW(4.0f));
    pd3dDevice
->SetTextureStageState(1, D3DTSS_BUMPENVLOFFSET, F2DW(0.0f));

    pd3dDevice
->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    pd3dDevice
->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

    
// set texture color blend method for stage 2 (environment map texture)

    D3DXMATRIX mat;

    mat._11 
= 0.5f; mat._12 =  0.0f; mat._13 = 0.0f; mat._14 = 0.0f
    mat._21 
= 0.0f; mat._22 = -0.5f; mat._23 = 0.0f; mat._24 = 0.0f
    mat._31 
= 0.0f; mat._32 =  0.0f; mat._33 = 1.0f; mat._34 = 0.0f
    mat._41 
= 0.5f; mat._42 =  0.5f; mat._43 = 0.0f; mat._44 = 1.0f

    pd3dDevice
->SetTransform(D3DTS_TEXTURE2, &mat);

    pd3dDevice
->SetTexture(2, g_env_map_texture);

    pd3dDevice
->SetTextureStageState(2, D3DTSS_TEXTURETRANSFORMFLAGS,    D3DTTFF_COUNT2);
    pd3dDevice
->SetTextureStageState(2, D3DTSS_TEXCOORDINDEX,            D3DTSS_TCI_SPHEREMAP);
    pd3dDevice
->SetTextureStageState(2, D3DTSS_COLORARG1,                D3DTA_TEXTURE);
    pd3dDevice
->SetTextureStageState(2, D3DTSS_COLORARG2,                D3DTA_CURRENT);
    pd3dDevice
->SetTextureStageState(2, D3DTSS_COLOROP,                    D3DTOP_ADD);    

    pd3dDevice
->SetSamplerState(2, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    pd3dDevice
->SetSamplerState(2, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

    
return S_OK;
}

//--------------------------------------------------------------------------------------
// Release resources created in the OnResetDevice callback here 
//--------------------------------------------------------------------------------------
void CALLBACK OnLostDevice( void* pUserContext )
{
    g_dlg_resource_manager.OnLostDevice();
    g_settings_dlg.OnLostDevice();
    g_font
->OnLostDevice();

    release_com(g_text_sprite);
}


//--------------------------------------------------------------------------------------
// Release resources created in the OnCreateDevice callback here
//--------------------------------------------------------------------------------------
void CALLBACK OnDestroyDevice( void* pUserContext )
{
    g_dlg_resource_manager.OnDestroyDevice();
    g_settings_dlg.OnDestroyDevice();    

    delete[] g_mesh_materials;
    g_mesh_materials 
= NULL;

    
if(g_mesh_textures)
    {
        
for(DWORD i = 0; i < g_num_materials; i++)
            release_com(g_mesh_textures[i]);

        delete[] g_mesh_textures;
        g_mesh_textures 
= NULL;
    }

    release_com(g_font);
    release_com(g_mesh);
    release_com(g_env_map_texture);
    release_com(g_height_map_texture);
    release_com(g_bump_map_texture);
}

//--------------------------------------------------------------------------------------
// Handle updates to the scene
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameMove( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
    D3DXMATRIX mat_world;
    D3DXMatrixRotationZ(
&mat_world, timeGetTime()/1000.0f);
    pd3dDevice
->SetTransform(D3DTS_WORLD, &mat_world);
}

//--------------------------------------------------------------------------------------
// Render the helper information
//--------------------------------------------------------------------------------------
void RenderText()
{
    CDXUTTextHelper text_helper(g_font, g_text_sprite, 
20);
    
    text_helper.Begin();

    
// show frame and device states
    text_helper.SetInsertionPos(55);
    text_helper.SetForegroundColor(FONT_COLOR);
    text_helper.DrawTextLine( DXUTGetFrameStats(
true) );
    text_helper.DrawTextLine( DXUTGetDeviceStats() );

    
// show helper information
    
    
const D3DSURFACE_DESC* surface_desc = DXUTGetBackBufferSurfaceDesc();

    
if(g_show_help)
    {
        text_helper.SetInsertionPos(
10, surface_desc->Height - 18 * 5);
        text_helper.SetForegroundColor(FONT_COLOR);
        text_helper.DrawTextLine(L
"Controls (F1 to hide):");
        
        text_helper.SetInsertionPos(
40, surface_desc->Height - 18 * 4);
        text_helper.DrawTextLine(L
"Quit: ESC");
    }
    
else
    {
        text_helper.SetInsertionPos(
10, surface_desc->Height - 15 * 4);
        text_helper.SetForegroundColor( D3DXCOLOR(
1.0f1.0f1.0f1.0f) );
        text_helper.DrawTextLine(L
"Press F1 for help");
    }

    text_helper.End();
}

//--------------------------------------------------------------------------------------
// Render the scene 
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
    HRESULT hr;

    
if(g_settings_dlg.IsActive())
    {
        g_settings_dlg.OnRender(fElapsedTime);
        
return;
    }

    
// Clear the render target and the zbuffer 
    V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0000), 1.0f0) );

    
// Render the scene
    if( SUCCEEDED( pd3dDevice->BeginScene() ) )
    {
        
for(DWORD i = 0; i < g_num_materials; i++)
        {
            pd3dDevice
->SetMaterial(&g_mesh_materials[i]);
            pd3dDevice
->SetTexture(0, g_mesh_textures[i]);
            g_mesh
->DrawSubset(i);
        }

        RenderText();

        V(g_button_dlg.OnRender(fElapsedTime));

        V( pd3dDevice
->EndScene() );
    }
}


//--------------------------------------------------------------------------------------
// Handle messages to the application 
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, 
                          
bool* pbNoFurtherProcessing, void* pUserContext )
{
    
*pbNoFurtherProcessing = g_dlg_resource_manager.MsgProc(hWnd, uMsg, wParam, lParam);
    
if(*pbNoFurtherProcessing)
        
return 0;

    
if(g_settings_dlg.IsActive())
    {
        g_settings_dlg.MsgProc(hWnd, uMsg, wParam, lParam);
        
return 0;
    }

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

    
return 0;
}


//--------------------------------------------------------------------------------------
// Handle keybaord event
//--------------------------------------------------------------------------------------
void CALLBACK OnKeyboardProc(UINT charater, bool is_key_down, bool is_alt_down, void* user_context)
{
    
if(is_key_down)
    {
        
switch(charater)
        {
        
case VK_F1:
            g_show_help 
= !g_show_help;
            
break;
        }
    }
}

//--------------------------------------------------------------------------------------
// Handle events for controls
//--------------------------------------------------------------------------------------
void CALLBACK OnGUIEvent(UINT eventint control_id, CDXUTControl* control, void* user_context)
{
    
switch(control_id)
    {
    
case IDC_TOGGLE_FULLSCREEN:
        DXUTToggleFullScreen();
        
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);
    g_button_dlg.Init(
&g_dlg_resource_manager);

    g_button_dlg.SetCallback(OnGUIEvent);

    
int x = 35, y = 10, width = 125, height = 22;

    g_button_dlg.AddButton(IDC_TOGGLE_FULLSCREEN, L
"Toggle full screen", x, y,         width, height);
    g_button_dlg.AddButton(IDC_TOGGLE_REF,          L
"Toggle REF (F3)",     x, y += 24, width, height);
    g_button_dlg.AddButton(IDC_CHANGE_DEVICE,      L
"Change device (F2)", x, y += 24, width, height, VK_F2);    
}

//--------------------------------------------------------------------------------------
// Initialize everything and go into a render loop
//--------------------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
{
    
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF 
| _CRTDBG_LEAK_CHECK_DF );
#endif

    
// Set the callback functions
    DXUTSetCallbackDeviceCreated( OnCreateDevice );
    DXUTSetCallbackDeviceReset( OnResetDevice );
    DXUTSetCallbackDeviceLost( OnLostDevice );
    DXUTSetCallbackDeviceDestroyed( OnDestroyDevice );
    DXUTSetCallbackMsgProc( MsgProc );
    DXUTSetCallbackFrameRender( OnFrameRender );
    DXUTSetCallbackFrameMove( OnFrameMove );
    DXUTSetCallbackKeyboard(OnKeyboardProc);
   
    
// TODO: Perform any application-level initialization here
    InitDialogs();

    
// Initialize DXUT and create the desired Win32 window and Direct3D device for the application
    DXUTInit( truetruetrue ); // Parse the command line, handle the default hotkeys, and show msgboxes
    DXUTSetCursorSettings( truetrue ); // Show the cursor and clip it when in full screen
    DXUTCreateWindow( L"Bump Texture Mapping" );
    DXUTCreateDevice( D3DADAPTER_DEFAULT, 
true640480, IsDeviceAcceptable, ModifyDeviceSettings );

    
// Start the render loop
    DXUTMainLoop();

    
// TODO: Perform any application-level cleanup here

    
return DXUTGetExitCode();
}

 


下載示例工程


posted on 2008-05-21 21:15 lovedday 閱讀(2974) 評論(0)  編輯 收藏 引用

公告

導航

統計

常用鏈接

隨筆分類(178)

3D游戲編程相關鏈接

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久婷婷一区| 最新中文字幕一区二区三区| 香蕉乱码成人久久天堂爱免费 | 国产偷国产偷精品高清尤物| 国产精品久久久久aaaa樱花| 国产精品久久久久7777婷婷| 国产精品福利久久久| 国产精品推荐精品| 精品99一区二区三区| 亚洲欧洲视频| 亚洲伊人网站| 久久精品国产综合| 欧美国产精品日韩| 亚洲毛片在线看| 亚洲综合视频一区| 裸体女人亚洲精品一区| 欧美人体xx| 国产专区一区| 亚洲麻豆视频| 久久久国产视频91| 亚洲精品免费在线播放| 亚洲欧洲另类| 欧美一区免费视频| 欧美日韩的一区二区| 国产午夜精品久久久| 99视频精品| 免费久久久一本精品久久区| 中文av一区二区| 欧美+日本+国产+在线a∨观看| 国产精品二区二区三区| 亚洲欧洲日韩在线| 久久夜色精品一区| 亚洲视频免费| 欧美日韩午夜在线视频| 午夜精品一区二区三区在线视| 久久久免费av| 国产日产亚洲精品| 亚洲四色影视在线观看| 欧美aⅴ99久久黑人专区| 亚洲一区二区久久| 欧美日韩一区二区精品| 亚洲人成免费| 免费看黄裸体一级大秀欧美| 亚洲一区免费看| 欧美日韩国产一区二区| 亚洲欧洲日本国产| 欧美国产一区二区在线观看| 欧美一乱一性一交一视频| 国产精品麻豆成人av电影艾秋| 亚洲美女中文字幕| 亚洲国产成人在线播放| 久久久五月天| 尹人成人综合网| 美女网站久久| 久久久久久久一区二区三区| 国产日韩欧美一区二区三区四区| 亚洲欧美卡通另类91av| 一区二区三区视频在线| 国产精品啊啊啊| 亚洲一区视频| 亚洲午夜激情| 国产精品免费一区豆花| 亚洲综合丁香| 亚洲综合日韩在线| 国产有码一区二区| 免费成人你懂的| 欧美成人官网二区| 亚洲乱码视频| 亚洲视频图片小说| 国产久一道中文一区| 久久久99国产精品免费| 久久久久久亚洲精品中文字幕| 在线观看一区二区视频| 欧美激情久久久| 欧美日韩国产电影| 欧美一区二区日韩一区二区| 欧美一级在线视频| 亚洲欧洲日本在线| 一本一本久久a久久精品综合妖精| 欧美日韩一区二区精品| 久久国产精品99久久久久久老狼| 久久精品人人做人人爽| 亚洲日韩欧美一区二区在线| 亚洲伦理在线| 国产午夜精品视频| 亚洲国产精品va| 欧美私人网站| 久久一区二区三区av| 欧美激情综合| 久久精品成人欧美大片古装| 另类激情亚洲| 午夜精品久久久久久久久久久| 久久精品一区二区三区不卡| 9i看片成人免费高清| 欧美在线地址| 亚洲深夜影院| 久久最新视频| 久久精品亚洲| 欧美日本高清| 久久精品论坛| 欧美另类视频| 久久久久久亚洲综合影院红桃| 久久综合久久久久88| 亚洲天堂偷拍| 嫩模写真一区二区三区三州| 亚洲免费视频在线观看| 久久久久免费| 午夜精品久久久久久久久久久久| 美女久久一区| 久久久久久久一区| 欧美日韩一区二区精品| 免费一级欧美片在线观看| 国产精品成人一区二区三区吃奶| 乱码第一页成人| 国产精品一级在线| 亚洲卡通欧美制服中文| 亚洲电影免费在线| 久久精品毛片| 久久久综合网| 国产欧美日韩免费| 一区二区三区四区五区视频 | 亚洲图片欧美日产| 99天天综合性| 欧美电影免费观看高清| 久久天天躁狠狠躁夜夜av| 国产精品入口夜色视频大尺度 | 国产精品美女主播| 亚洲高清资源| 亚洲片国产一区一级在线观看| 久久精品国产99| 久久激情综合网| 国产日韩一区二区三区在线| 亚洲视频 欧洲视频| 亚洲天堂av电影| 欧美日韩亚洲网| 一区二区欧美精品| 一区二区高清视频| 欧美色另类天堂2015| 亚洲精品小视频| 亚洲一区欧美激情| 国产久一道中文一区| 亚洲欧美怡红院| 久久久噜噜噜久久人人看| 国产视频亚洲精品| 久久婷婷丁香| 亚洲国产高清在线| 国产精品99久久久久久白浆小说| 欧美日韩精选| 亚洲影院一区| 裸体素人女欧美日韩| 亚洲欧洲在线一区| 欧美日韩国产在线播放| 亚洲天堂av图片| 久久婷婷蜜乳一本欲蜜臀| 亚洲国产精彩中文乱码av在线播放| 麻豆精品视频在线观看| 亚洲国产美女精品久久久久∴| 一区二区三区国产在线观看| 国产精品福利av| 午夜一级在线看亚洲| 亚洲欧美综合国产精品一区| 欧美主播一区二区三区美女 久久精品人| 欧美日韩免费视频| 亚洲欧美日韩视频一区| 欧美不卡在线视频| 亚洲一区二区免费在线| 狠狠色狠狠色综合日日五| 欧美国产另类| 午夜日韩电影| 亚洲毛片av在线| 另类激情亚洲| 亚洲宅男天堂在线观看无病毒| 黄色国产精品一区二区三区| 欧美精品一区二| 亚洲欧美在线免费| 欧美激情一区三区| 欧美一区国产一区| 亚洲免费黄色| 黄色日韩精品| 国产精品久久久久国产精品日日| 久久久久高清| 亚洲一级二级在线| 亚洲第一主播视频| 久久精品成人欧美大片古装| 日韩亚洲欧美精品| 国内精品视频在线播放| 欧美日韩喷水| 欧美福利在线观看| 久久久久免费| 欧美一区二区三区在线观看视频 | 香蕉国产精品偷在线观看不卡 | 亚洲三级视频| 国产一区香蕉久久| 欧美色综合网| 欧美国产精品va在线观看| 香港成人在线视频| 一本一本久久a久久精品牛牛影视| 欧美高清在线播放| 久久久久高清| 欧美在线视频一区|