directx初第七卷

光照处理

光照类型

环境光(ambient)

环境光就是场景中物体表面产生的反射光的统一光照,环境光没有位置和方向,并且值为常量。

//设置渲染状态(环境光,颜色)
g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(100, 100, 100));

漫反射光(diffuse)

由光照方向以及顶点法向量计算得到

镜面反射光(specular)

//设置渲染状态(镜面反射,开启)
g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);

自发光(emissive)

通过对物体材质Emissive属性设置来实现的,自发光不会影响到其他物体

光源类型

点光源(point)

D3DLIGHT9 InitPointLight(D3DXVECTOR3* position, D3DXCOLOR* color)
{

D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_POINT; //类型为点光源
light.Ambient = *color*.6f; //环境光颜色值
light.Diffuse = *color; //漫反射颜色值
light.Specular = *color*.6f; //镜面反射颜色值
light.Position = *position; //光源位置
light.Range = 1000; //光照范围
light.Attenuation0 = 1; //这三个参数,表示光强随距离衰减的方式
light.Attenuation1 = 0;
light.Attenuation2 = 0;
return light;
}
D3DXVECTOR3 pos(0, 0, 0);
D3DXCOLOR color = D3DCOLOR_XRGB(255, 255, 255);
D3DLIGHT9 point;
point = InitPointLight(&pos, &color);
//设置光源(选择第一个光源,填充好的灯光类型地址)
g_pd3dDevice->SetLight(0, &point);
//启用光照(选择第一个光源,开灯)
g_pd3dDevice->LightEnable(0, true);
//设置渲染状态(对法线向量进行归一化处理,开启)
g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, false);

方向光(directional)

D3DLIGHT9 InitDirectionalLight(D3DXVECTOR3* direction, D3DXCOLOR* color)
{

D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = *color*.6f;
light.Diffuse = *color;
light.Specular = *color*.6f;
light.Direction = *direction; //光源光照方向
return light;
}
D3DLIGHT9 directional;
D3DXVECTOR3 dir(1, 0, .25f);
D3DXCOLOR color = D3DCOLOR_XRGB(255, 255, 255);
directional = InitDirectionalLight(&dir, &color);
g_pd3dDevice->SetLight(0, &directional);
g_pd3dDevice->LightEnable(0, true);
g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, false);

聚光灯(spot)

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

D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_SPOT;
light.Ambient = *color*.0f;
light.Diffuse = *color;
light.Specular = *color*.6f;
light.Position = *position;
light.Direction = *direction;
light.Range = 1000;
light.Falloff = 1; //用于聚光灯从内锥形到外锥形的衰减方式
light.Attenuation0 = 1;
light.Attenuation1 = 0;
light.Attenuation2 = 0;
light.Theta = .4f; //仅用于聚光灯,内锥形的角度,单位为弧度
light.Phi = .9f; //仅用于聚光灯,外锥形的角度,单位为弧度
return light;
}
D3DLIGHT9 spot;
D3DXVECTOR3 pos(0, 0, -5);
D3DXVECTOR3 dir(0, 0, 1);
D3DXCOLOR color = White;
spot = InitSpotLight(&pos, &dir, &color);
g_pd3dDevice->SetLight(0, &spot);
g_pd3dDevice->LightEnable(0, true);
g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);

没有效果?需要模型和材质才能看得出效果(以后再详细说明)

ID3DXMesh* teapot;
D3DXCreateTeapot(g_pd3dDevice, &teapot, 0);

D3DMATERIAL9 mtrl;
mtrl.Ambient = (D3DXCOLOR)D3DCOLOR_XRGB(255, 0, 0);
mtrl.Diffuse = (D3DXCOLOR)D3DCOLOR_XRGB(255, 0, 0);
mtrl.Specular = (D3DXCOLOR)D3DCOLOR_XRGB(255, 0, 0);
mtrl.Power = 2;
mtrl.Emissive = (D3DXCOLOR)D3DCOLOR_XRGB(0, 0, 0);

g_pd3dDevice->SetMaterial(&mtrl);
teapot->DrawSubset(0);
文章目录
  1. 1. 光照处理
    1. 1.1. 光照类型
    2. 1.2. 光源类型
    3. 1.3. 没有效果?需要模型和材质才能看得出效果(以后再详细说明)