顶点格式中只有D3DFVF_DIFFUSE时,顶点的颜色值所呈现的是制定的颜色值。
当使用D3DFVF_DIFFUSE | D3DFVF_NORMAL,顶点的颜色值,完全由材质、光照计算。
//-----------------------------------------------------------------------------
// Desc: 顶点结构
//-----------------------------------------------------------------------------
struct CUSTOMVERTEX
{
D3DXVECTOR3 position; //顶点位置
D3DXVECTOR3 normal; //顶点法线
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
//-----------------------------------------------------------------------------
// Desc: 设置变换矩阵
//-----------------------------------------------------------------------------
VOID SetMatrices()
{
//建立并设置世界矩阵
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
//建立并设置观察矩阵
D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
//建立并设置投影矩阵
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------------------------------------------
// Desc: 初始化Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
//创建Direct3D对象, 该对象用于创建Direct3D设备对象
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
//设置D3DPRESENT_PARAMETERS结构, 准备创建Direct3D设备对象
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
//创建Direct3D设备对象
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
//设置剔出模式为不剔出任何面(正面和方面)
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
//启用深度测试
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
//启用镜面反射光照模型
g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
//设置变换矩阵
SetMatrices();
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: 创建场景图形
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
//创顶点缓冲区
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 50*2*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}
//填充顶点缓冲区
CUSTOMVERTEX* pVertices;
if( FAILED( g_pVB->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
return E_FAIL;
for( DWORD i=0; i<50; i++ )
{
FLOAT theta = (2*D3DX_PI*i)/(50-1);
pVertices[2*i+0].position = D3DXVECTOR3( sinf(theta),-1.0f, cosf(theta) );
pVertices[2*i+0].normal = D3DXVECTOR3( sinf(theta), 0.0f, cosf(theta) );
pVertices[2*i+1].position = D3DXVECTOR3( sinf(theta), 1.0f, cosf(theta) );
pVertices[2*i+1].normal = D3DXVECTOR3( sinf(theta), 0.0f, cosf(theta) );
}
g_pVB->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: 设置材质和灯光
//-----------------------------------------------------------------------------
VOID SetupLights()
{
//设置材料属性, 只反射红光分量和绿光分量, 整体上看该物体材料反射黄色光
D3DMATERIAL9 mtrl;
ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
mtrl.Ambient.r = 1.0f;
mtrl.Ambient.g = 1.0f;
mtrl.Ambient.b = 0.0f;
mtrl.Ambient.a = 1.0f;
mtrl.Diffuse.r = 1.0f;
mtrl.Diffuse.g = 1.0f;
mtrl.Diffuse.b = 0.0f;
mtrl.Diffuse.a = 0.5f;
mtrl.Specular.r = 1.0f;
mtrl.Specular.g = 1.0f;
mtrl.Specular.b = 1.0f;
mtrl.Specular.a = 1.0f;
g_pd3dDevice->SetMaterial( &mtrl );
//设置一号光源为方向光,颜色为绿色
D3DXVECTOR3 vecDir;
D3DLIGHT9 light1;
ZeroMemory( &light1, sizeof(D3DLIGHT9) );
light1.Type = D3DLIGHT_DIRECTIONAL;
light1.Ambient.r = 0.0f;
light1.Ambient.g = 1.0f;
light1.Ambient.b = 0.0f;
vecDir = D3DXVECTOR3(-10, 0 ,10); //方向光方向
D3DXVec3Normalize( (D3DXVECTOR3*)&light1.Direction, &vecDir );
g_pd3dDevice->SetLight( 0, &light1 );
//二号光源为点光源,颜色为红色
D3DXVECTOR3 vecPos2;
D3DLIGHT9 light2;
ZeroMemory( &light2, sizeof(D3DLIGHT9) );
light2.Type = D3DLIGHT_POINT;
light2.Diffuse.r = 0.5f;
light2.Diffuse.g = 0.0f;
light2.Diffuse.b = 0.0f;
light2.Position= D3DXVECTOR3(10*sinf(timeGetTime()/350.0f) ,
0,
10*cosf(timeGetTime()/350.0f) );
light2.Range = 100.0f;
light2.Attenuation0 = 1.0f;
g_pd3dDevice->SetLight( 1, &light2 );
//四号光源为点光源,颜色为红色
D3DXVECTOR3 vecPos4;
D3DLIGHT9 light4;
ZeroMemory( &light4, sizeof(D3DLIGHT9) );
light4.Type = D3DLIGHT_POINT;
light4.Ambient.r = 0.f;
light4.Ambient.g = 0.5f;
light4.Ambient.b = 0.0f;
light4.Position= D3DXVECTOR3(10*sinf(timeGetTime()/350.0f) ,
0,
10*cosf(timeGetTime()/350.0f) );
light4.Range = 100.0f;
light4.Attenuation0 = 1.0f;
g_pd3dDevice->SetLight( 3, &light4 );
//三号光源为方向光,颜色为绿色, *镜面反射*
D3DXVECTOR3 vecDir3;
D3DLIGHT9 light3;
ZeroMemory( &light3, sizeof(D3DLIGHT9) );
light3.Type = D3DLIGHT_DIRECTIONAL;
light3.Specular.r = 0.0f;
light3.Specular.g = .0f;
light3.Specular.b = 1.0f;
light3.Specular.a = 1.0f;
vecDir3 = D3DXVECTOR3(-10,0,10);
D3DXVec3Normalize( (D3DXVECTOR3*)&light3.Direction, &vecDir3 );
g_pd3dDevice->SetLight( 2, &light3 );
//设置一定的环境光
g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00505050);
}
效果:
1. 只有D3DFVF_DIFFUSE, 顶点呈现制定颜色。
2. 有D3DFVF_DIFFUSE,物体材质全部设为1.f
a. 只开全局环境光,全局环境光将D3DFVF_DIFFUSE值覆盖
b. 全局环境光、单个光源的相同光照模型的光照值累加覆盖
c. 不含全局环境光,相同光源的不同光照模型的光照值累加覆盖
d. 不含全局环境光,不同光源的不同光照模型的光照值累计覆盖
注: a. 点光源:有位置,需要为之设置关照范围以及关照的衰减率。点光源没有方向属性,而在进行漫反射和镜面反射光照计算时需要使用光线的方向向量,原因是点光源
向四周发射光线,显然方向不固定,因此就不可能在光源属性中像方向光那样用一个方向向量表示,而在具体计算某个顶点的光照颜色时,根据顶点和光源的位置,
不难计算出光线的方向。
b. 光照的矢量以世界坐标为参照,而物体的顶点法向量以三角形的顶点顺序所构造的法向量为标准。
结论: 当不同的光源的不同光照模型进行累加,物体最终所呈现出来的颜色比例有材质决定。但是总的颜色值还是为各种光源的不同光照模型的总和。