CSDN浅墨大神博客专栏:http://blog.csdn.net/column/details/vc-game-programming.html
DirectX提供的快捷几何体绘制:
步骤:
四大光照类型:
1.环境光
2.漫反射光
3.镜面反射光
4.自发光
三大光源类型:
static D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
1.点光源
light.Type = D3DLIGHT_POINT;
light.Ambient = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f);
light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
light.Specular = D3DXCOLOR(0.3f, 0.3f, 0.3f, 1.0f);
light.Position = D3DXVECTOR3(0.0f, 200.0f, 0.0f);
light.Attenuation0 = 1.0f;
light.Attenuation1 = 0.0f;
light.Attenuation2 = 0.0f;
light.Range = 300.0f;
2.方向光
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
light.Specular = D3DXCOLOR(0.3f, 0.3f, 0.3f, 1.0f);
light.Direction = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
3.聚光灯
light.Type = D3DLIGHT_SPOT;
light.Position = D3DXVECTOR3(100.0f, 100.0f, 100.0f);
light.Direction = D3DXVECTOR3(-1.0f, -1.0f, -1.0f);
light.Ambient = D3DXCOLOR(0.3f, 0.3f, 0.3f, 1.0f);
light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
light.Specular = D3DXCOLOR(0.3f, 0.3f, 0.3f, 0.3f);
light.Attenuation0 = 1.0f;
light.Attenuation1 = 0.0f;
light.Attenuation2 = 0.0f;
light.Range = 300.0f;
light.Falloff = 0.1f;
light.Phi = D3DX_PI / 3.0f;
light.Theta = D3DX_PI / 6.0f;
最后通过g_p3dDevice设置开启光源
g_pd3dDevice->SetLight(0, &light);
g_pd3dDevice->LightEnable(0, true);
g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(36, 36, 36));
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, true);
g_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);
g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW)
材质
D3DMATERIAL9 mtrl;
::ZeroMemory(&mtrl, sizeof(mtrl));
mtrl.Ambient = D3DXCOLOR(0.5f, 0.5f, 0.7f, 1.0f);
mtrl.Diffuse = D3DXCOLOR(0.6f, 0.6f, 0.6f, 1.0f);
mtrl.Specular = D3DXCOLOR(0.3f, 0.3f, 0.3f, 0.3f);
mtrl.Emissive = D3DXCOLOR(0.3f, 0.0f, 0.1f, 1.0f);
g_pd3dDevice->SetMaterial(&mtrl);
顶点法线
使用顶点法线目的是使模型在光照效果下质地更加光滑