http://hi.baidu.com/zyb_debug/blog/item/aee40a12b5877c896438db9d.html不多說了,龍書的材質(zhì)燈光函數(shù)。可重用代碼
//=========================================

/**//**
@brief
@param 定義一些顏色常量
@return
*/
//=========================================
const D3DXCOLOR WHITE(D3DCOLOR_XRGB(255,255,255));
const D3DXCOLOR BLACK(D3DCOLOR_XRGB(0,0,0));
const D3DXCOLOR RED(D3DCOLOR_XRGB(255,0,0));
const D3DXCOLOR GREEN(D3DCOLOR_XRGB(0,255,0));
const D3DXCOLOR BLUE(D3DCOLOR_XRGB(0,0,255));
const D3DXCOLOR YELLOW(D3DCOLOR_XRGB(255,255,0));
const D3DXCOLOR CYAN(D3DCOLOR_XRGB(0,255,255));
const D3DXCOLOR MAGENTA(D3DCOLOR_XRGB(255,0,255));


D3DMATERIAL9 InitMtrl(D3DXCOLOR a,D3DXCOLOR d,D3DXCOLOR s,D3DXCOLOR e,float p);

//=========================================

/**//**
@brief
@param 定義一些基本常用材質(zhì)
@return
*/
//=========================================
const D3DMATERIAL9 WHITE_MTRL = InitMtrl(WHITE,WHITE,WHITE,BLACK,8.0f);
const D3DMATERIAL9 RED_MTRL = InitMtrl(RED,RED,RED,BLACK,8.0f);
const D3DMATERIAL9 GREEN_MTRL = InitMtrl(GREEN,GREEN,GREEN,BLACK,8.0f);
const D3DMATERIAL9 BLUE_MTRL = InitMtrl(BLUE,BLUE,BLUE,BLACK,8.0f);
const D3DMATERIAL9 YELLOW_MTRL = InitMtrl(YELLOW,YELLOW,YELLOW,BLACK,8.0f);

//=========================================

/**//**
@brief
@param 燈光
@return
*/
//=========================================
D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color);
D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color);
D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color);

//=========================================

/**//**
@brief
@param 初始化材質(zhì)
@return
*/
//=========================================
D3DMATERIAL9 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;

}

//=========================================

/**//**
@brief
@param 燈光
@return
*/
//=========================================

D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color)


{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));

light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = *color * 0.4f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Direction = *direction;

return light;
}

D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)


{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));

light.Type = D3DLIGHT_POINT;
light.Ambient = *color * 0.4f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Position = *position;
light.Range = 1000.0f;
light.Falloff = 1.0f;
light.Attenuation0 = 1.0f;
light.Attenuation1 = 0.0f;
light.Attenuation2 = 0.0f;

return light;
}

D3DLIGHT9 InitSpotLight(D3DXVECTOR3* position, D3DXVECTOR3* direction, D3DXCOLOR* color)


{
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));

light.Type = D3DLIGHT_SPOT;
light.Ambient = *color * 0.4f;
light.Diffuse = *color;
light.Specular = *color * 0.6f;
light.Position = *position;
light.Direction = *direction;
light.Range = 1000.0f;
light.Falloff = 1.0f;
light.Attenuation0 = 1.0f;
light.Attenuation1 = 0.0f;
light.Attenuation2 = 0.0f;
light.Theta = 0.5f;
light.Phi = 0.7f;

return light;
}

}
