`
yuanjinxiu
  • 浏览: 653482 次
文章分类
社区版块
存档分类
最新评论

3D游戏从入门到精通-22

 
阅读更多
聚光灯
像汽车的远光灯发出的灯光,就是聚光灯。它有位置、方向和颜色。下面的就是聚光的例子显示的效果图:

聚光灯比点光源多了方向,还多了内光圈和外光圈了的角度。因此,初始化灯光的结构时,就需要设置它们才能让聚光灯有效里显示出来。程序初始化如下:

//

//初始化灯光。

//蔡军生 2006/09/03

//

void CCAISpotLight::InitLight(void)

{

//

ZeroMemory( &m_meshMaterialFloor, sizeof(D3DMATERIAL9) );

m_meshMaterialFloor.Diffuse.r = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialFloor.Diffuse.g = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialFloor.Diffuse.b = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialFloor.Diffuse.a = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialFloor.Ambient.r = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialFloor.Ambient.g = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialFloor.Ambient.b = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialFloor.Ambient.a = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_pd3dDevice->SetMaterial( &m_meshMaterialFloor );

//聚光灯

ZeroMemory( &m_ligthSpot, sizeof(D3DLIGHT9) );

m_ligthSpot.Diffuse.r = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_ligthSpot.Diffuse.g = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_ligthSpot.Diffuse.b = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_ligthSpot.Diffuse.a = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_ligthSpot.Type = D3DLIGHT_SPOT;

m_ligthSpot.Position = D3DXVECTOR3( <chmetcnv w:st="on" unitname="F" sourcevalue="0" hasspace="False" negative="False" numbertype="1" tcsc="0">0.0f</chmetcnv>, <chmetcnv w:st="on" unitname="F" sourcevalue="2" hasspace="False" negative="False" numbertype="1" tcsc="0">2.0f</chmetcnv>, <chmetcnv w:st="on" unitname="F" sourcevalue="0" hasspace="False" negative="False" numbertype="1" tcsc="0">0.0f</chmetcnv> );

//

m_ligthSpot.Direction = D3DXVECTOR3( 0, -1.0, 0 );

m_ligthSpot.Attenuation0 = <chmetcnv w:st="on" unitname="F" sourcevalue="0" hasspace="False" negative="False" numbertype="1" tcsc="0">0.0f</chmetcnv>;

m_ligthSpot.Attenuation1 = <chmetcnv w:st="on" unitname="F" sourcevalue=".1" hasspace="False" negative="False" numbertype="1" tcsc="0">0.1f</chmetcnv>;

m_ligthSpot.Attenuation2 = <chmetcnv w:st="on" unitname="F" sourcevalue="0" hasspace="False" negative="False" numbertype="1" tcsc="0">0.0f</chmetcnv>;

m_ligthSpot.Range = <chmetcnv w:st="on" unitname="F" sourcevalue="30" hasspace="False" negative="False" numbertype="1" tcsc="0">30.0f</chmetcnv>;

//

m_ligthSpot.Theta = D3DX_PI/10;

m_ligthSpot.Phi = D3DX_PI/4;

m_ligthSpot.Falloff = 3.0;

m_pd3dDevice->SetLight( 0, &m_ligthSpot );

//

ZeroMemory( &m_meshMaterialSpot, sizeof(D3DMATERIAL9) );

m_meshMaterialSpot.Emissive.r = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialSpot.Emissive.g = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialSpot.Emissive.b = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

m_meshMaterialSpot.Emissive.a = <chmetcnv w:st="on" unitname="F" sourcevalue="1" hasspace="False" negative="False" numbertype="1" tcsc="0">1.0f</chmetcnv>;

}

上面首先设置地板的材料颜色,接着就初始化聚光灯的结构m_ligthSpot,这里选择灯光类型为D3DLIGHT_SPOT,颜色是白色。还设置了方向和位置,还有内圈的角度为D3DX_PI/10,而外圈的角度为D3DX_PI/4,从内光圈到外光圈衰减为3.0

其它渲染部份,同点光源是一样的。

电子书MM3D引擎源程序例子源程序49元一套
联系人:蔡军生
联系方式:
QQ: 9073204
EMAIL: caimouse1976 at sina.com
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics