矩阵中的数学旋转公式 转换到 C++中函数 替换DirectX 9.0中D3DXMatrixRotationAxis函数(四)
returnMatrix->_22 = ay*axis.y + c;
returnMatrix->_23 = ay*axis.z + axis.x*s;
returnMatrix->_24 = 0.0f;
returnMatrix->_31 = az*axis.x + axis.y*s;
returnMatrix->_32 = az*axis.y - axis.x*s;
returnMatrix->_33 = az*axis.z + c;
returnMatrix->_34 = 0.0f;
returnMatrix->_41 = 0.0f;
returnMatrix->_42 = 0.0f;
returnMatrix->_43 = 0.0f;
returnMatrix->_44 = 1.0f;
}
//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
// 旋转
D3DXMATRIXA16 matWorld;
//自定义旋转的轴
//D3DXVECTOR3 axis(1.0f/sqrt(3), 1.0f/sqrt(3), 1.0f/sqrt(3));
D3DXVECTOR3 axis(1.0f, 1.0f, 0.0f);
D3DXMatrixIdentity( &matWorld );
/*DirectX 官方API*/
//D3DXMatrixRotationY( &matWorld, timeGetTime()/500.0f );
/*使用自定义旋转函数1*/
//setupRotate(&matWorld,2,timeGetTime()/500.0f);
/*使用自定义旋转函数2*/
setupRotate(axis,&matWorld,timeGetTime()/500.0f);
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 );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
// 设置视锥体大小
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------------------------------------------
// Name: SetupLights()
// Desc: Sets up the Lights and materials for the scene.
//-----------------------------------------------------------------------------
VOID SetupLights()
{
// 初始化一个材料
D3DMATERIAL9 mtrl;
ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );
mtrl.Diffuse = mtrl.Ambient = D3DXCOLOR(1.0f,1.0f,0.0f,1.0f); //材质有漫射和环境光的设置,都为黄色
g_pd3dDevice->SetMaterial( &mtrl );
// 初始化一个白色的方向光
D3DXVECTOR3 vecDir;
D3DLIGHT9 light;
ZeroMemory( &light, sizeof(D3DLIGHT9) );
light.Type = D3DLIGHT_DIRECTIONAL;//方向光
light.Diffuse = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);//设置光源的漫射光颜色为白色
light.Direction = D3DXVECTOR3(0.0f,0.0f,1.0f);//光的传播方向平行z轴
light.Range = 1000.0f;
g_pd3dDevice->SetLight( 0, &light );
g_pd3dDevice->LightEnable( 0, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
// Finally, turn on some ambient light.
g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
// 背景为蓝色
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER