材质和灯光的辅助函数

http://hi.baidu.com/zyb_debug/blog/item/aee40a12b5877c896438db9d.html
不多说了,龙书的材质灯光函数。可重用代码
// =========================================
/**/ /**
@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 定义一些基本常用材质
   @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 初始化材质
@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;
}


}
Posted on 2009-08-15 17:15 zyb_debug 阅读(961) 评论(0)   编辑  收藏 引用

你可能感兴趣的:(材质和灯光的辅助函数)