矩阵中的数学旋转公式 转换到 C++中函数 替换DirectX 9.0中D3DXMatrixRotationAxis函数(二)

2014-11-24 08:28:21 · 作者: · 浏览: 1
的是3维矩阵,可以看下下面的转换,因为要设计到矩阵相乘,故只能4维矩阵跟4维矩阵相乘,故把3维的转换成4维的了。
具体使用上面两个函数的例子:
[cpp]
// 旋转
D3DXMATRIXA16 matWorld;
//自定义旋转的轴
D3DXVECTOR3 axis(1.0f/sqrt(3), 1.0f/sqrt(3), 1.0f/sqrt(3));
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 );
旋转效果:
我稍微修改了下自定义旋转的轴,改成一个非单位向量,(1.0f, 1.0f, 0.0f),旋转效果(变形的飞起,哈哈):
完整代码(看的麻烦的同学,可以拉到底 下载整个项目来看):
[cpp]
//-----------------------------------------------------------------------------
// File: Lights.cpp
// Copyright (c) Microsoft Corporation & Waitingfy.com. All rights reserved.
//-----------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
ID3DXMesh* Objects;//茶壶
// A structure for our custom vertex type. We added a normal, and omitted the
// color (which is provided by the material)
struct CUSTOMVERTEX
{
D3DXVECTOR3 position; // The 3D position for the vertex
D3DXVECTOR3 normal; // The surface normal for the vertex
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice. Since we are now
// using more complex geometry, we will create a device with a zbuffer.
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;
// Create the D3DDevice
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
// Turn off culling
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn on the zbuffer
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
return S_OK;
}