今天写了个简单的摄像机类,重要的一个函数就是获得视图矩阵。
其代码:
1 void Ccamera::GetViewMatrix( D3DXMATRIX& View )
2 {
3 D3DXVec3Normalize( &m_look, &m_look );
4 D3DXVec3Cross( &m_up, &m_look, &m_right );
5 D3DXVec3Normalize( &m_up, &m_up);
6 D3DXVec3Cross( &m_right, &m_look, &m_up);
7 D3DXVec3Normalize(&m_right, &m_right);
8
9 //建立视图矩阵
10 float fx = -D3DXVec3Dot( &m_right, &m_position);
11 float fy = -D3DXVec3Dot( &m_up, &m_position);
12 float fz = -D3DXVec3Dot( &m_look, &m_position);
13
14 View(0,0) = m_right.x; View(1,0) = m_right.y;
15 View(2,0) = m_right.z; View(3,0) = fx;
16
17 View(0,1) = m_up.x; View(1,1) = m_up.y;
18 View(2,1) = m_up.z; View(3,1) = fy;
19
20 View(0,2) = m_look.x; View(1,2) = m_look.y;
21 View(2,2) = m_look.z; View(3,2) = fz;
22
23 View(0,3) = 0.0f; View(1,3) = 0.0f;
24 View(2,3) = 0.0f; View(3,3) = 1.0f;
25 }
但是却不能正常工作,在排除了其他原因后,把问题确定在了这个函数,
到底是什么原因呢?通过最郁闷的调试重要发现了这句代码有问题:
D3DXVec3Cross( &m_right, &m_look, &m_up);
其用意是使得m_right 向量垂直于m_look 和 m_up向量,但是却没注意其方向性。所以这样的运算使得m_right获得了个与想要方向的反向量。正确的写法应该是:
D3DXVec3Cross( &m_right, &m_up, &m_look);
写代码时思维不够紧密呀,只是想到要修正这些向量使得其正交却忽略了叉乘的方向,回头再看看3D数学基础才行。
posted on 2010-07-06 23:05
木华 阅读(3223)
评论(0) 编辑 收藏 引用