`
phinecos
  • 浏览: 343519 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

使用矩阵和四元数实现三维模型的空间定位

 
阅读更多
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Czjua%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} &#64;font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} &#64;font-face {font-family:"/&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ &#64;page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

通过矩阵变换实现绕三个坐标轴的特定角度的旋转:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->//-----------------------------------------------------------------------------
//Desc:设置世界矩阵
//-----------------------------------------------------------------------------
VOIDSetWorldMatrix()
{
staticlongcurTime=0;
staticfloatelapsetime=0;
elapsetime
=(timeGetTime()-curTime)/1000.0f;
curTime
=timeGetTime();

//创建并设置世界矩阵
floatfRoll,fPitch,fYaw;
fRoll
=fPitch=fYaw=0.0f;

if(m_bKey['D'])fRoll-=3*elapsetime;
if(m_bKey['A'])fRoll+=3*elapsetime;

if(m_bKey['S'])fPitch-=3*elapsetime;
if(m_bKey['W'])fPitch+=3*elapsetime;

if(m_bKey['Q'])fYaw-=3*elapsetime;
if(m_bKey['E'])fYaw+=3*elapsetime;

//更新网格模型姿态
staticD3DXVECTOR3vRight,vUp,vLook,vPos;

vRight.x
=g_matWorld._11;
vRight.y
=g_matWorld._12;
vRight.z
=g_matWorld._13;
vUp.x
=g_matWorld._21;
vUp.y
=g_matWorld._22;
vUp.z
=g_matWorld._23;
vLook.x
=g_matWorld._31;
vLook.y
=g_matWorld._32;
vLook.z
=g_matWorld._33;
vPos.x
=g_matWorld._41;
vPos.y
=g_matWorld._42;
vPos.z
=g_matWorld._43;

D3DXVec3Normalize(
&vLook,&vLook);
D3DXVec3Cross(
&vRight,&vUp,&vLook);

D3DXVec3Normalize(
&vRight,&vRight);
D3DXVec3Cross(
&vUp,&vLook,&vRight);

D3DXVec3Normalize(
&vUp,&vUp);

staticD3DXMATRIXmatPitch,matYaw,matRoll;

D3DXMatrixRotationAxis(
&matYaw,&vUp,fYaw);
D3DXVec3TransformCoord(
&vLook,&vLook,&matYaw);
D3DXVec3TransformCoord(
&vRight,&vRight,&matYaw);

D3DXMatrixRotationAxis(
&matRoll,&vLook,fRoll);
D3DXVec3TransformCoord(
&vRight,&vRight,&matRoll);
D3DXVec3TransformCoord(
&vUp,&vUp,&matRoll);

D3DXMatrixRotationAxis(
&matPitch,&vRight,fPitch);
D3DXVec3TransformCoord(
&vLook,&vLook,&matPitch);
D3DXVec3TransformCoord(
&vUp,&vUp,&matPitch);

g_matWorld._11
=vRight.x;
g_matWorld._12
=vRight.y;
g_matWorld._13
=vRight.z;
g_matWorld._21
=vUp.x;
g_matWorld._22
=vUp.y;
g_matWorld._23
=vUp.z;
g_matWorld._31
=vLook.x;
g_matWorld._32
=vLook.y;
g_matWorld._33
=vLook.z;

//向前移动
if(m_bKey['F'])
{
g_matWorld._41
+=30*elapsetime*vLook.x;
g_matWorld._42
+=30*elapsetime*vLook.y;
g_matWorld._43
+=30*elapsetime*vLook.z;
}


//向后移动
if(m_bKey['V'])
{
g_matWorld._41
-=30*elapsetime*vLook.x;
g_matWorld._42
-=30*elapsetime*vLook.y;
g_matWorld._43
-=30*elapsetime*vLook.z;
}

g_pd3dDevice
->SetTransform(D3DTS_WORLD,&g_matWorld);
}

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Czjua%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} &#64;font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} &#64;font-face {font-family:"/&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ &#64;page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

通过四元数实现绕三个坐标轴的特定角度的旋转:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
//-----------------------------------------------------------------------------
//Desc:设置世界矩阵
//-----------------------------------------------------------------------------
VOIDSetWorldMatrix()
{
staticlongcurTime=0;
staticfloatelapsetime=0;
elapsetime
=(timeGetTime()-curTime)/1000.0f;
curTime
=timeGetTime();

//创建并设置世界矩阵
floatfRoll,fPitch,fYaw;
fRoll
=fPitch=fYaw=0.0f;

if(m_bKey['D'])fRoll-=3*elapsetime;
if(m_bKey['A'])fRoll+=3*elapsetime;
if(m_bKey['S'])fPitch-=3*elapsetime;
if(m_bKey['W'])fPitch+=3*elapsetime;
if(m_bKey['Q'])fYaw-=3*elapsetime;
if(m_bKey['E'])fYaw+=3*elapsetime;

//更新网格模型姿态
D3DXQUATERNIONqR;
D3DXMATRIXmatRot;
D3DXQuaternionRotationYawPitchRoll(
&qR,fYaw,fPitch,fRoll);
D3DXMatrixRotationQuaternion(
&matRot,&qR);
D3DXMatrixMultiply(
&g_matWorld,&matRot,&g_matWorld);

//获取网格模型前向量
staticD3DXVECTOR3vLook;
vLook.x
=g_matWorld._31;
vLook.y
=g_matWorld._32;
vLook.z
=g_matWorld._33;

//向前移动
if(m_bKey['F'])
{
g_matWorld._41
+=10*elapsetime*vLook.x;
g_matWorld._42
+=10*elapsetime*vLook.y;
g_matWorld._43
+=10*elapsetime*vLook.z;
}


//向后移动
if(m_bKey['V'])
{
g_matWorld._41
-=10*elapsetime*vLook.x;
g_matWorld._42
-=10*elapsetime*vLook.y;
g_matWorld._43
-=10*elapsetime*vLook.z;
}


g_pd3dDevice
->SetTransform(D3DTS_WORLD,&g_matWorld);
}



分享到:
评论

相关推荐

    基于图像的三维模型重建C++源代码+文档说明(高分课程设计)

    基于图像的三维模型重建 core—提供了工程项目需要的所有的基础数据结构,包括image、depthmap、mesh、view以及数据的输入输出等结构和功能。 math—提供矩阵,向量,四元数等基本的数学运算操作。 features—提供...

    OpenSceneGraph三维渲染引擎设计与实践

    1.1.2 具体实现:三维渲染引擎 2 1.1.3 主流渲染引擎介绍 3 1.2 openscenegraph概述 4 1.2.1 诞生与发展 4 1.2.2 优势与不足 5 1.3 openscenegraph的组成结构 6 1.3.1 核心结构 6 1.3.2 资源获取 8 1.3.3 ...

    OpenSceneGraph三维渲染引擎编程指南.pdf

    6.1.1 三维模型文件格式 188 6.1.2 图片及视频文件格式 189 6.1.3 打包及网络传输格式 189 6.1.4 字体文件格式 190 6.1.5 伪插件文件格式 190 6.1.6 .osg文件和.ive文件 190 6.2 文件读取的流程 191 ...

    基于四元数的三维空相似变换解算 (2009年)

    提出一种基于四元数构造旋转矩阵、解算三维空间相似变换模型的平差算法。模拟数据试验表明,该算法是稳健的,摄影测量中用四元数构造旋转矩阵具有明显的优越性。

    OpenSceneGraph三维渲染引擎编程指南

    6.1.1 三维模型文件格式 188 6.1.2 图片及视频文件格式 189 6.1.3 打包及网络传输格式 189 6.1.4 字体文件格式 190 6.1.5 伪插件文件格式 190 6.1.6 .osg文件和.ive文件 190 6.2 文件读取的流程 191 6.2.1 ...

    精通DirectX.3D图形与动画程序设计.pdf

    7.5 三维模型旋转 7.5.1 通过矩阵实现模型旋转 7.5.2 通过四元数实现模型旋转 7.6 使用文件模型的几点提示 7.7 小结 第8章 深度测试与alpha混合 8.1 深度测试 8.1.1 深度缓冲区与深度测试 8.1.2 使用深度测试 8.2 半...

    基于激光雷达的运动补偿方法

    激光雷达运动补偿是智能车动态背景目标检测中不可避免的过程。提出了一种基于激光雷达的运动补偿算法。...经过实验验证,该方法成功地对背景运动进行了估计和补偿,适合应用于三维环境下实时运动目标的检测。

    《精通direct3d图形及动画程序设计》附书源代码

    一个综合性的示例程序,演示了摄影机类和场景漫游的实现,以及静态网格模型、动画网格模型、粒子系统的进一步封装和使用。 第18章 HLSL顶点渲染 1、HLSLTransform 演示使用HLSL渲染语言进行GPU编程实现坐标变换、...

    线状特征约束下基于四元数描述的 LiDAR点云配准方法 (2013年)

    利用四元数法来表达旋转矩阵,进而形成线状特征约束下基于四元数描述的LiDAR点云配准方法,给出了线状特征约束下三维相似变换的相似性测度表达方法,推导并论证了以线状特征作为配准基元时同名线状特征需要满足的...

    3D游戏卷2:动画与高级实时渲染技术——2

    附录5.1 使用和探索着色器 附录5.2 NVIDIA GeForce 3上的顶点编程 附录5.3 NVIDIA寄存结合器操作 第6章 几何处理 6.1 简介 6.2 推动因素和定义 6.2.1 离线和实时阶段 6.2.2 拓扑因素 6.2.3 离散简化与连续简化 6.2.4...

    3D游戏卷2:动画与高级实时渲染技术——1

    附录5.1 使用和探索着色器 附录5.2 NVIDIA GeForce 3上的顶点编程 附录5.3 NVIDIA寄存结合器操作 第6章 几何处理 6.1 简介 6.2 推动因素和定义 6.2.1 离线和实时阶段 6.2.2 拓扑因素 6.2.3 离散简化与连续简化 6.2.4...

    bo:用于图像处理、分割、对象分析和表面重建的方法和结构的集合

    基于四元数或变换矩阵的 3D 空间变换。 多维向量和点的通用表示。 2D 图像的通用表示。 二维图像的线性过滤。 矩阵运算(例如矩阵求逆、实矩阵的特征向量和特征值)。C++11 当前没有使用 C++11 特性来支持旧的...

    ImageBasedModellingEdu

    ImageBasedModellingEdu2.0是用于深蓝学院基于图像的三维模型重建课程配套的代码。该代码著名的开源三维重建开源系统MVE( 我们其基础之上该工程项目采用CMake管理,可与方便的进行跨平台的编译。代码包含特征提取...

Global site tag (gtag.js) - Google Analytics