首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

o3d 根本的矩阵操作

2012-12-27 
o3d基本的矩阵操作里记录一下矩阵的基本操作和矩阵乘法的顺序问题。1: while( msg.message!WM_QUIT )2: {3

o3d 基本的矩阵操作

里记录一下矩阵的基本操作和矩阵乘法的顺序问题。
   1: while( msg.message!=WM_QUIT )
   2: {
   3:     // check for messages
   4:     if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
   5:     {
   6:         TranslateMessage( &msg );
   7:         DispatchMessage( &msg );
   8:     }
   9:     // this is called when no messages are pending
  10:     else
  11:     {
  12:         // Clear the backbuffer to a black color
  13:         pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );
  14: 
  15:         pd3dDevice->BeginScene();
  16: 
  17:         pd3dDevice->SetStreamSource( 0, vertexBuffer, 0, sizeof(CUSTOMVERTEX) );
  18:         pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  19: 
  20:         // translate the object away from the origin
  21:         D3DXMatrixTranslation(&matTranslate, 64.0f, 0.0f, 0.0f);
  22: 
  23:         // set the rotation
  24:         D3DXMatrixRotationY(&matRotate, timeGetTime()/1000.0f);
  25:        
  26:         // multiple the translation and rotation matrices to create the objMat matrix
  27:         D3DXMatrixMultiply(&objMat, &matRotate, &matTranslate);
  28:         D3DXMatrixMultiply(&objMat, &matTranslate, &matRotate);
  29: 
  30:         // transform the object in world space
  31:         pd3dDevice->SetTransform(D3DTS_WORLD, &objMat);
  32: 
  33:         pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 2 );
  34:         pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  4, 2 );
  35:         pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  8, 2 );
  36:         pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );
  37:         pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );
  38:         pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );
  39: 
  40:         pd3dDevice->EndScene();
  41: 
  42:         // Present the backbuffer contents to the display
  43:         pd3dDevice->Present( NULL, NULL, NULL, NULL );
  44:     }
  45: }
第21行和第23行的顺序其实是没有关系的。关键是第27行和第28行的区别。这里先插一句timeGetTime函数的用法。MSND上是这样说的:
The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows was started.
DWORD timeGetTime(VOID);
The only difference between this function and the timeGetSystemTime function is that timeGetSystemTime uses the MMTIME structure to return the system time. The timeGetTime function has less overhead than timeGetSystemTime.
所以timeGetTime得到的是一个递增的毫秒值。
第27行:matRotate * matTranslate,表示先平移后旋转(顺序与表达式刚好相反),效果就是绕着一根轴旋转,而该轴是Y轴向右(X轴正方向)平移6个单位。
第28行: matTranslate * matRotate,表示先旋转和平移。效果就是绕着Y轴旋转,旋转半径是64。可以在脑海中想一下,是这样叠加而成的:先在源点转一个角度,此时X轴也跟着旋转了,然后再向X轴平移64个单位。
怎样理解这种变换:前一个矩阵变换会影响后一个矩阵变换。据我所知,世界坐标是一个客观存在,那么可以推测以上的矩阵变换中的坐标都是围绕着物体模型本身的坐标。
比如第27行,物体先向X轴平移64个单位,此时物体模型的坐标系也向X轴平移了64个单位,然后再绕Y轴旋转,此时的Y轴不是世界坐标中的Y轴,而是物体模型坐标中经过平移之后的Y轴。这是可以解释的。
同样第28行,物体先绕着物体模型坐标系的Y轴旋转一个角度(此时,物体模型坐标系和世界坐标系是重合的),这时物体模型坐标系像前面一样,也旋转了同样的角度。此时物体模型坐标系的X轴与世界坐标系的X轴已经不再重合,此时再平移,是沿着物体坐标系的X轴向“右”平移64个单位。这也解释得通。
以上的推测,就是基于一点,世界坐标是客观存在的,它不会为其中一个物体的变换而变换。

热点排行