本篇是创建游戏内核(6)【OO改良版】的续篇,关于该内核的细节说明请参阅创建游戏内核(7)。
接口:
class MATERIAL
{
public:
MATERIAL();
D3DMATERIAL9* get_material();
void init();
void set_diffuse(unsigned char red, unsigned char green, unsigned char blue);
void get_diffuse(unsigned char* red, unsigned char* green, unsigned char* blue);
void set_ambient(unsigned char red, unsigned char green, unsigned char blue);
void get_ambient(unsigned char* red, unsigned char* green, unsigned char* blue);
void set_specular(unsigned char red, unsigned char green, unsigned char blue);
void get_specular(unsigned char* red, unsigned char* green, unsigned char* blue);
void set_emissive(unsigned char red, unsigned char green, unsigned char blue);
void get_emissive(unsigned char* red, unsigned char* green, unsigned char* blue);
void set_power(float power);
float get_power();
private:
D3DMATERIAL9 m_material;
};
typedef MATERIAL* MATERIAL_PTR;
实现:
//-------------------------------------------------------------------------
// Constructor, zero member data.
//-------------------------------------------------------------------------
MATERIAL::MATERIAL()
{
ZeroMemory(this, sizeof(*this));
}
//-------------------------------------------------------------------------
// set a default material (white).
//-------------------------------------------------------------------------
void MATERIAL::init()
{
set_diffuse(255, 255, 255);
set_ambient(255, 255, 255);
set_specular(255, 255, 255);
set_emissive(255, 255, 255);
set_power(1.0f);
}
//-------------------------------------------------------------------------
// set diffuse color.
//-------------------------------------------------------------------------
void MATERIAL::set_diffuse(unsigned char red, unsigned char green, unsigned char blue)
{
m_material.Diffuse.r = red / 255.0f;
m_material.Diffuse.g = green / 255.0f;
m_material.Diffuse.b = blue / 255.0f;
}
//-------------------------------------------------------------------------
// Get diffuse color.
//-------------------------------------------------------------------------
void MATERIAL::get_diffuse(unsigned char* red, unsigned char* green, unsigned char* blue)
{
if(red != NULL)
*red = (unsigned char)(255.0 * m_material.Diffuse.r);
if(green != NULL)
*green = (unsigned char)(255.0 * m_material.Diffuse.g);
if(blue != NULL)
*blue = (unsigned char)(255.0 * m_material.Diffuse.b);
}
//-------------------------------------------------------------------------
// set ambient color.
//-------------------------------------------------------------------------
void MATERIAL::set_ambient(unsigned char red, unsigned char green, unsigned char blue)
{
m_material.Ambient.r = red / 255.0f;
m_material.Ambient.g = green / 255.0f;
m_material.Ambient.b = blue / 255.0f;
}
//-------------------------------------------------------------------------
// Get ambient color.
//-------------------------------------------------------------------------
void MATERIAL::get_ambient(unsigned char *red, unsigned char *green, unsigned char *blue)
{
if(red != NULL)
*red = (unsigned char)(255.0f * m_material.Ambient.r);
if(green != NULL)
*green = (unsigned char)(255.0f * m_material.Ambient.g);
if(blue != NULL)
*blue = (unsigned char)(255.0f * m_material.Ambient.b);
}
//-------------------------------------------------------------------------
// set specular color.
//-------------------------------------------------------------------------
void MATERIAL::set_specular(unsigned char red, unsigned char green, unsigned char blue)
{
m_material.Specular.r = red / 255.0f;
m_material.Specular.g = green / 255.0f;
m_material.Specular.b = blue / 255.0f;
}
//-------------------------------------------------------------------------
// Get specular color.
//-------------------------------------------------------------------------
void MATERIAL::get_specular(unsigned char* red, unsigned char* green, unsigned char* blue)
{
if(red != NULL)
*red = (unsigned char)(255.0 * m_material.Specular.r);
if(green != NULL)
*green = (unsigned char)(255.0 * m_material.Specular.g);
if(blue != NULL)
*blue = (unsigned char)(255.0 * m_material.Specular.b);
}
//-------------------------------------------------------------------------
// set emissive color.
//-------------------------------------------------------------------------
void MATERIAL::set_emissive(unsigned char red, unsigned char green, unsigned char blue)
{
m_material.Emissive.r = red / 255.0f;
m_material.Emissive.g = green / 255.0f;
m_material.Emissive.b = blue / 255.0f;
}
//-------------------------------------------------------------------------
// Get emmisive color.
//-------------------------------------------------------------------------
void MATERIAL::get_emissive(unsigned char *red, unsigned char *green, unsigned char *blue)
{
if(red != NULL)
*red = (unsigned char)(255.0 * m_material.Emissive.r);
if(green != NULL)
*green = (unsigned char)(255.0 * m_material.Emissive.g);
if(blue != NULL)
*blue = (unsigned char)(255.0 * m_material.Emissive.b);
}
//-------------------------------------------------------------------------
// set power for material, which specifying the sharpness of specular
// highlights.
//-------------------------------------------------------------------------
void MATERIAL::set_power(float power)
{
m_material.Power = power;
}
//-------------------------------------------------------------------------
// Get power for material, which specifying the sharpness of specular
// highlights.
//-------------------------------------------------------------------------
float MATERIAL::get_power()
{
return m_material.Power;
}
//-------------------------------------------------------------------------
// Get material.
//-------------------------------------------------------------------------
D3DMATERIAL9* MATERIAL::get_material()
{
return &m_material;
}