﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-帅哥帅-随笔分类-dx</title><link>http://www.cppblog.com/lll109383670/category/18663.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 14 Mar 2012 16:11:09 GMT</lastBuildDate><pubDate>Wed, 14 Mar 2012 16:11:09 GMT</pubDate><ttl>60</ttl><item><title>图形学资源（期刊与实验室）</title><link>http://www.cppblog.com/lll109383670/archive/2012/03/08/167402.html</link><dc:creator>帅哥帅</dc:creator><author>帅哥帅</author><pubDate>Thu, 08 Mar 2012 07:29:00 GMT</pubDate><guid>http://www.cppblog.com/lll109383670/archive/2012/03/08/167402.html</guid><wfw:comment>http://www.cppblog.com/lll109383670/comments/167402.html</wfw:comment><comments>http://www.cppblog.com/lll109383670/archive/2012/03/08/167402.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lll109383670/comments/commentRss/167402.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lll109383670/services/trackbacks/167402.html</trackback:ping><description><![CDATA[<div><div align="center"><span>国内外图形学研究机构</span></div></div><div id="freenr"><ul><li>中国科学院深圳先进技术研究院：<a href="http://www.siat.cas.cn/gkjj/jgjj/">http://www.siat.cas.cn/gkjj/jgjj/</a><br />&nbsp;</li><li>微软亚洲研究院：<a href="http://www.msra.cn/">http://www.msra.cn/</a><br />&nbsp;</li><li>Carnegie Mellon School of Computer Science：<a href="http://www.cs.cmu.edu/">http://www.cs.cmu.edu/</a><br />&nbsp;</li><li>Visualization &amp; Interface Design Innovation：<a href="http://vidi.cs.ucdavis.edu/">http://vidi.cs.ucdavis.edu/</a><br />&nbsp;</li><li>Graduate School of Frontier Sciences，The University of Tokyo：<a href="http://www.k.u-tokyo.ac.jp/complex/index-e.html">http://www.k.u-tokyo.ac.jp/complex/index-e.html</a><br />&nbsp;</li><li>University of Illinois at Urbana-Champaign &#8226; College of Engineering Department of Computer Science：<a href="http://cs.illinois.edu/">http://cs.illinois.edu/<br /><br /><br /><div><div align="center"><span>国际会议列表</span></div></div><div id="freenr"><div><p><span><strong>国际会议</strong></span></p><ul><li>ACM Symposium on Solid and Physical Modeling&nbsp;&nbsp; (ACM SPM)<br />&nbsp;</li><li>Intl Conf on Machine Learning&nbsp;&nbsp; (ICML)<br />&nbsp;</li><li>ACM SIGGRAPH/Eurographics Symposium on Computer Animation(SCA)<br />&nbsp;</li><li>International Conference on Computer Vision (Theory and Applications) (ICCV)<br />&nbsp;</li><li>IEEE Computer Society Conference on Computer Vision and Pattern Recognition(CVPR)<br />&nbsp;</li><li>IEEE Visualization<br />&nbsp;</li><li>ACM International Conference for Human-computer Interaction(ACM HCI)<br />&nbsp;</li><li>ACM SIGIR Special Interest Group on Information Retrieval(SIGIR)<br />&nbsp;</li><li>ACM Special Interest Group Conf on Knowledge Discovery in Data and Data Mining(SIGKDD)<br />&nbsp;</li><li>International Symposium on Computer Architecture(ISCA)<br />&nbsp;</li><li>IEEE Virtual Reality(VR)</li></ul></div></div><br /><br /><br /><br /><div><div align="center"><span>期刊杂志列表</span></div></div><div id="freenr"><div><h3><span><span>Computer Graphics Related Journals</span></span></h3><ul><li>ACM Computer Graphics(SIGGRAPH)<br />&nbsp;</li><li>Communications of the ACM<br />&nbsp;</li><li>ACM Transactions on Graphics<br />&nbsp;</li><li>IEEE Transactions on Visualization and Computer Graphics<br />&nbsp;</li><li>IEEE Computer Graphics and Applications<br />&nbsp;</li><li>Graphical Models<br />&nbsp;</li><li>Computer Aided Design<br />&nbsp;</li><li>Computer Aided Geometric Design<br />&nbsp;</li><li>Computer Animation and Virtual Worlds<br />&nbsp;</li><li>Presence: Teleoperators &amp; Virtual Environments<br />&nbsp;</li><li>The Visual Computer<br />&nbsp;</li><li>Computer Graphics Forum<br />&nbsp;</li><li>Computers &amp; Graphics<br />&nbsp;</li><li>Computational Geometry-Theory and Applications<br />&nbsp;</li><li>International Journal of Computational Geometry and Applications<br />&nbsp;</li><li>Journal of Visualization<br />&nbsp;</li><li>Design Studies<br />&nbsp;</li><li>Engineering with Computers<br />&nbsp;</li><li>Computer Graphics World<br />&nbsp;</li><li>ACM Computers in Entertainment<br />&nbsp;</li><li>journal of graphics tools<br />&nbsp;</li><li>The International Journal of Visual Computing<br />&nbsp;</li><li>Virtual Reality<br />&nbsp;</li><li>International Journal of Shape Modeling<br />&nbsp;</li><li>International Journal of Virtual Reality<br />&nbsp;</li><li>Virtual Reality: Research Development and Applications<br />&nbsp;</li><li>Machine Graphics and Vision<br />&nbsp;</li><li>International Journal of Image and Graphics<br />&nbsp;</li><li>International Journal of CAD/CAM</li></ul><p><span><strong>Image Processing Journals Perhaps Related to Computer Graphics</strong></span></p><ul><li>IEEE Transactions on Pattern Analysis and Machine Intelligence<br />&nbsp;</li><li>IEEE Transactions on Image Processing<br />&nbsp;</li><li>Computer Vision and Image Understanding<br />&nbsp;</li><li>Image and Vision Computing<br />&nbsp;</li><li>Pattern Recognition<br />&nbsp;</li><li>Pattern Recognition Letters<br />&nbsp;</li><li>Journal of Visual Communication and Image Representation<br />&nbsp;</li><li>Real Time Imaging</li></ul></div></div><br /><br /><br /><br /></a></li></ul></div><img src ="http://www.cppblog.com/lll109383670/aggbug/167402.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lll109383670/" target="_blank">帅哥帅</a> 2012-03-08 15:29 <a href="http://www.cppblog.com/lll109383670/archive/2012/03/08/167402.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>地形制作全攻略（转载）</title><link>http://www.cppblog.com/lll109383670/archive/2012/02/22/166290.html</link><dc:creator>帅哥帅</dc:creator><author>帅哥帅</author><pubDate>Wed, 22 Feb 2012 15:16:00 GMT</pubDate><guid>http://www.cppblog.com/lll109383670/archive/2012/02/22/166290.html</guid><wfw:comment>http://www.cppblog.com/lll109383670/comments/166290.html</wfw:comment><comments>http://www.cppblog.com/lll109383670/archive/2012/02/22/166290.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lll109383670/comments/commentRss/166290.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lll109383670/services/trackbacks/166290.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: GameRes游戏开发资源网http://www.gameres.com &nbsp;地形制作全攻略作者：程东哲&nbsp;&nbsp;前言零．地形简介一．地形生成算法1．Fault Formation2．Midpoint Displacement二．小纹理生成大地形纹理1．地形纹理介绍2．小纹理插值生成大纹理。3．添加细节纹理4．添加光...&nbsp;&nbsp;<a href='http://www.cppblog.com/lll109383670/archive/2012/02/22/166290.html'>阅读全文</a><img src ="http://www.cppblog.com/lll109383670/aggbug/166290.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lll109383670/" target="_blank">帅哥帅</a> 2012-02-22 23:16 <a href="http://www.cppblog.com/lll109383670/archive/2012/02/22/166290.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Direct3D中实现图元的鼠标拾取（转）</title><link>http://www.cppblog.com/lll109383670/archive/2012/02/22/166211.html</link><dc:creator>帅哥帅</dc:creator><author>帅哥帅</author><pubDate>Wed, 22 Feb 2012 02:27:00 GMT</pubDate><guid>http://www.cppblog.com/lll109383670/archive/2012/02/22/166211.html</guid><wfw:comment>http://www.cppblog.com/lll109383670/comments/166211.html</wfw:comment><comments>http://www.cppblog.com/lll109383670/archive/2012/02/22/166211.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/lll109383670/comments/commentRss/166211.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lll109383670/services/trackbacks/166211.html</trackback:ping><description><![CDATA[<span style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: #000000; text-indent: 0px; white-space: normal; letter-spacing: normal; orphans: 2; widows: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="center"><strong>文章来自：<a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm">http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm</a><br />Direct3D<span style="font-family: 宋体">中实现图元的鼠标拾取</span></strong></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="center">　</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="center">BY 重剑，2004.5.28&nbsp;<a href="http://www.heavysword.com/">重剑空间</a>&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><strong>索引：</strong></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#1、什么是拾取，拾取能做什么？">1、什么是拾取，拾取能做什么？</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#2、拾取操作的步骤和实现">2、拾取操作的步骤和实现</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#2.1．&nbsp; 变换并获得通过视点和屏幕上点击点的射线矢量（Dir）">&nbsp;&nbsp;&nbsp; 2.1．&nbsp; 变换并获得通过视点和屏幕上点击点的射线矢量（Dir）</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">　</p>
<blockquote>
<blockquote>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#2.1.1 确定鼠标选取点的屏幕坐标">2.1.1 确定鼠标选取点的屏幕坐标</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">　</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#2.1.2 得到Dir在观察坐标空间内的表示">2.1.2 得到Dir在观察坐标空间内的表示</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">　</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#2.1.3 转换Dir到世界坐标空间，并得到观察点在世界坐标系中的坐标">2.1.3 转换Dir到世界坐标空间，并得到观察点在世界坐标系中的坐标</a></p></blockquote></blockquote>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">　</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#2.2 &nbsp; 使用射线矢量对场景中的所有三角形图元求交，获得三角形索引值和重心坐标。">&nbsp;&nbsp;&nbsp; 2.2 &nbsp; 使用射线矢量对场景中的所有三角形图元求交，获得三角形索引值和重心坐标。</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">　</p>
<blockquote>
<blockquote>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#2.2.1 D3D扩展函数实现求交">2.2.1 D3D扩展函数实现求交</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#2..2.2射线三角面相交的数学算法">2.2.2射线三角面相交的数学算法</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#2.2.3&nbsp; 拾取完成根据获得的中心坐标计算我们关心的常见量，。">2.2.3&nbsp; 拾取完成根据获得的中心坐标计算我们关心的常见量</a></p></blockquote></blockquote>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#3、结束及声明">3、结束及声明</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#4、参考文献">4、参考文献</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">　</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><a href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm#补充：重心坐标的概念">补充：重心坐标的概念</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">　</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'">　</p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'">　</p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'">3D<span style="font-family: 宋体">交互图形应用程序中，常常要用鼠标去选择图形，其实现的机制基于鼠标拾取算法。本文主要讲述如何在</span>D3D<span style="font-family: 宋体">中实现图元的鼠标拾取。为了讨论简单，本文假定读者理解</span>D3D&nbsp;<span style="font-family: 宋体">坐标变换流程和基本的图形学知识，如果阅读有困难请参考相关资料。</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="1、什么是拾取，拾取能做什么？">1、什么是拾取，拾取能做什么？</a></strong></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">首先，拾取操作指当我们在屏幕上用鼠标点击某个图元应用程序能返回该图元的一个标志和某些相关信息。有图形程序设计经验的人都知道，有这些信息就表示我们有了对该图元的控制权，我们可以删除，可以编辑，可以任意对待该图元，至于你到底想干什么，就是阁下自己的事了</span>^_^<span style="font-family: 宋体">。</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="2、拾取操作的步骤和实现">2、拾取操作的步骤和实现</a></strong></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'" align="left"><span style="font-family: 宋体">拾取算法的思想很简单：得到鼠标点击处的屏幕坐标，通过投影矩阵和观察矩阵把该坐标转换为通过视点和鼠标点击点的一条射入场景的光线，该光线如果与场景模型的三角形相交（本文只处理三角形图元），则获取该相交三角形的信息。本文讲述的方法除可以得到三角形的一个索引号以外还可以得到相交点的重心坐标。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 宋体">从数学角度来看，我们只要得到射线的方向矢量和射线的出射点，我们就具备了判断射线与空间一个三角面是否相交的条件，本文主要讨论如何获得这些条件，并描述了射线三角面相交判断算法和</span>D3D<span style="font-family: 宋体">的通常实现方法。</span>&nbsp;&nbsp;&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">根据拾取操作的处理顺序，大概可以依次分为以下几个步骤</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><a name="2.1．&nbsp; 变换并获得通过视点和屏幕上点击点的射线矢量（Dir）"><strong>2.1</strong><strong>．</strong><span style="font-weight: 700; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;</span><strong><span style="font-family: 宋体">变换并获得通过视点和屏幕上点击点的射线矢量（</span>Dir<span style="font-family: 宋体">）</span></strong></a></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">详细介绍之前，为了大家方便理解，我们要先简单说一下</span>d3d<span style="font-family: 宋体">坐标转换的大概流程，如下图</span>:</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; font-family: 'Times New Roman'"><img height="58" alt="" src="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.files/image002.jpg" width="448" border="0" v:shapes="_x0000_s1032" /></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">所以我们要通过一系列的反变换，得到我们关心的值在世界坐标中的表示。</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="2.1.1 确定鼠标选取点的屏幕坐标">2.1.1 确定鼠标选取点的屏幕坐标</a></strong></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">这一步是非常简单的</span>Windows<span style="font-family: 宋体">给我们提供了</span>API<span style="font-family: 宋体">来完成屏幕坐标的获取，使用</span>GetCursorPos<span style="font-family: 宋体">获得鼠标指针位置，然后再利用</span>ScreenToClient<span style="font-family: 宋体">转换坐标到客户区坐标系</span>(<span style="font-family: 宋体">以窗口视区左上角为坐标原点，单位为像素</span>)<span style="font-family: 宋体">，设该坐标为（</span>POINT screenPt<span style="font-family: 宋体">）。</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="2.1.2 得到Dir在观察坐标空间内的表示">2.1.2 得到Dir在观察坐标空间内的表示</a></strong></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">在观察坐标系中，</span>Dir<span style="font-family: 宋体">是一条从观察坐标原点出发的射线，所以我们只需要再确定一个该射线经过的点，就可以得到它在观察坐标系中的表示。假设我们要求的射线上的另外一点为该射线与透视投影平截头体近剪切面的交点，针对最普遍的透视投影而言，透视投影平截头体经投影变换后，变成一个</span>1/2<span style="font-family: 宋体">立方体（请允许我这么叫</span>^_^<span style="font-family: 宋体">，因为它的大小为一个正方体的一半，</span>x,y<span style="font-family: 宋体">方向边长为</span>2<span style="font-family: 宋体">，</span>z<span style="font-family: 宋体">方向为</span>1<span style="font-family: 宋体">）如图：</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'" align="center"><img height="118" alt="" src="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.files/image004.jpg" width="139" border="0" v:shapes="_x0000_s1031" /></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">投影坐标系以近剪切面中心为坐标原点，该立方体从</span>z<span style="font-family: 宋体">轴负向看过去与图形程序视区相对应，最终近剪切面（前剪切面）上一点与屏幕坐标之间的对应关系如下图所示：</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><img height="149" alt="" src="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.files/image006.jpg" width="448" border="0" v:shapes="_x0000_s1030" /></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'" align="center">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><strong><span style="font-family: 宋体">根据比例关系，</span>screenPt<span style="font-family: 宋体">与投影空间上的点</span>projPt<span style="font-family: 宋体">之间的关系为</span></strong></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">假设图形程序窗口的宽为</span>screenWidth,<span style="font-family: 宋体">高为</span>screenHeight,</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">projPt.x = (screenPt.x-screenWidth/2)/screenWidth*2; （公式1）</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">projPt.y = (screenPt.y-screenHeight/2)/screenHeight*2; （公式2）</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">projPt.z =0;（实际该值可任意取，不影响最终结果。为了处理简单，我们取改值为0，表示该点取在近剪切面上）</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><strong><span style="font-family: 宋体">得到</span>projPt<span style="font-family: 宋体">后，我们需要做的是把该点坐标从投影空间转换到观察空间</span>(view space),</strong></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">根据透视投影的定义，可假设点</span>(projPt.x<span style="font-family: 宋体">，</span>projPt.y<span style="font-family: 宋体">，</span>projPt.z)</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">对应的其次坐标为</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">(projPt.x*projPt.w<span style="font-family: 宋体">，</span>projPt.y*projPt.w<span style="font-family: 宋体">，</span>projPt.z*projPt.w<span style="font-family: 宋体">，</span>projPt.w)</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">我们可以通过</span>&nbsp;GetTransform(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;D3DTS_PROJECTION,&nbsp;&nbsp;&nbsp;&nbsp;&amp;ProjMatrix)<span style="font-family: 宋体">函数获得投影矩阵</span>ProjMatrix,<span style="font-family: 宋体">则根据观察空间到投影空间的变换关系则</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">(projPt.x*projPt.w<span style="font-family: 宋体">，</span>projPt.y*projPt.w<span style="font-family: 宋体">，</span>projPt.z*projPt.w<span style="font-family: 宋体">，</span>projPt.w)</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">&nbsp;= (viewPt.x<span style="font-family: 宋体">，</span>viewPt.y<span style="font-family: 宋体">，</span>viewPt.z, 1)*pProjMatrx;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">根据定义和图形学原理</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">ProjMatrix =&nbsp;<img height="108" alt="" src="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.files/image008.jpg" width="135" border="0" v:shapes="_x0000_s1029" />=<img height="114" alt="" src="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.files/image010.jpg" width="138" border="0" v:shapes="_x0000_s1028" /></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">所以</span>,</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">(projPt.x*projPt.w<span style="font-family: 宋体">，</span>projPt.y*projPt.w<span style="font-family: 宋体">，</span>projPt.z*projPt.w<span style="font-family: 宋体">，</span>projPt.w)</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">= ( viewPt.x*ProjMatrix._m11,</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; text-indent: 15.75pt; font-family: 'Times New Roman'">viewPt.y*ProjMatrix._m22,</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; text-indent: 15.75pt; font-family: 'Times New Roman'">viewPt.z*Q-QZn,</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; text-indent: 15.75pt; font-family: 'Times New Roman'">viewPt.z)</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; text-indent: 15.75pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">所以</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">projPt.x*projPt.w = viewPt.x*ProjMatrix._m11</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">projPt.y*projPt.w = viewPt.y*ProjMatrix._m22</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 21pt; font-family: 'Times New Roman'">projPt.z*projPt.w = viewPt.z*Q-QZn&nbsp;<span style="color: red; font-family: 宋体">（注意</span><span style="color: red">projPt.z = 0</span><span style="color: red; font-family: 宋体">）</span></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'">projPt.w = viewPt.z;</p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">解得</span></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'">viewPt.x = projPt.x*Zn/ ProjMatrix._m11;</p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'">viewPt.y = projPt.y*Zn/ ProjMatrix._m22;</p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'">viewPt.z = Zn;</p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">好了，到这里为止我们终于求出了射线与近剪切面交点在观察坐标系中的坐标，现在我们拥有了射线的出发点</span>(0,0,0)<span style="font-family: 宋体">和射线方向上另外一点</span>(viewPt.x,viewPt.y,viewPt.z),<span style="font-family: 宋体">则该射线的方向矢量在观察空间中的表示可确定为（</span>viewPt.x-0,viewPt.y-0,viewPt.z-0<span style="font-family: 宋体">）</span>,<span style="font-family: 宋体">化简一下三个分量同除近剪切面</span>z<span style="font-family: 宋体">坐标</span>Zn<span style="font-family: 宋体">，该方向矢量可写作</span></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'">DIRview = (projPt.x/projMatrix._m11,projPt.y/projMatrix._m22,1)</p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">代入公式</span>1<span style="font-family: 宋体">，公式</span>2</p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">DIRview.x = (2*screenPt.x/screenWidth-1)/projMatrix._m11;</span></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">DIRview.y = (2*screenPt.y/screenHeight-1)/projMatrix._m22;</span></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">DIRview.z = 1;</span></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">其中screenWidth和screenHeight可以通过图像显示的backBuffer的目标表面（D3DSURFACE_DESC）来获得，该表面在程序初始化时由用户创建。</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="2.1.3 转换Dir到世界坐标空间，并得到观察点在世界坐标系中的坐标">2.1.3 转换Dir到世界坐标空间，并得到观察点在世界坐标系中的坐标</a></strong></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><strong><span style="font-size: 9pt; font-family: 新宋体">由于最终的运算要在世界坐标空间中进行，所以我们还需要把矢量DIRview从观察空间转换为世界坐标空间中的矢量DIRworld。</span></strong></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">因为</span></p>
<p style="font-size: 10.5pt; text-indent: 21pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">DIRview = DIRworld*ViewMatrix;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">其中ViewMatrix为观察矩阵，在D3D中可以用函数GetTransform( D3DTS_VIEW, &amp;ViewMatrix )得到。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">所以DIRworld = DIRview * inverse_ViewMatrix,其中inverse_ViewMatrix为</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">ViewMatrix的逆矩阵。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp; 观察点在观察坐标系中坐标为OriginView（0，0，0，1），所以其在世界坐标系中的坐标同样可以利用ViewMatrix矩阵，反变换至世界坐标系中，事实上我们可以很简单的判断出,其在世界坐标系中的表示为:</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">OriginWorld = (inverse_ViewMatrix._41,</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 63pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">inverse_ViewMatrix._42,</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 63pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">inverse_ViewMatrix._43,</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 63pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">1);</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">到这里为止，判断射线与三角面是否相交的条件就完全具备了。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="2.2 &nbsp; 使用射线矢量对场景中的所有三角形图元求交，获得三角形索引值和重心坐标。">2.2 &nbsp; 使用射线矢量对场景中的所有三角形图元求交，获得三角形索引值和重心坐标。</a></strong></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">这一步骤地实现由两种途径</span>:</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">第一种方法非常简单，利用</span>D3D<span style="font-family: 宋体">提供的扩展函数</span>D3DXIntersect<span style="font-family: 宋体">可以轻松搞定一切。见</span>2.1</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">第二种方法就是我们根据空间解析几何的知识，自己来完成射线三角形的求交算法。一般来讲，应用上用第一种方法就足够了，但是我们如果要深入的话，必须理解相交检测的数学算法，这样才能自由的扩展，面对不同的需求，内容见</span>2.2</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">下面分别讲解两种实现途径：</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="2.2.1 D3D扩展函数实现求交">2.2.1 D3D扩展函数实现求交</a></strong></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">这种方法很简单也很好用，对于应用来说应尽力是用这种方式来实现，毕竟效率比自己写得要高得多。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">实际上其实没什么好讲的，大概讲一下函数</span>D3DXIntersect<span style="font-family: 宋体">吧</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">D3D SDK<span style="font-family: 宋体">该函数声明如下</span></p><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">HRESULT&nbsp;D3DXIntersect(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;LPD3DXBASEMESH&nbsp;<em>pMesh</em>,</span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;CONST D3DXVECTOR3&nbsp;*<em>pRayPos</em>,</span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;CONST D3DXVECTOR3&nbsp;*<em>pRayDir</em>,</span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;BOOL&nbsp;*<em>pHit</em>,</span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;*<em>pFaceIndex</em>,</span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;FLOAT&nbsp;*<em>pU</em>,</span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;FLOAT&nbsp;*<em>pV</em>,</span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;FLOAT&nbsp;*<em>pDist</em>,</span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;LPD3DXBUFFER&nbsp;*<em>ppAllHits</em>,</span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">&nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;*<em>pCountOfHits</em></span></pre><pre style="font-size: 12pt; font-family: 宋体"><span style="font-size: 9pt">);</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 42pt; text-indent: -21pt; font-family: 宋体"><span style="font-size: 9pt; font-family: Wingdings">l<span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt">pMesh指向一个ID3DXBaseMesh的对象，最简单的方式是从.x文件获得，描述了要进行相交检测的三角面元集合的信息，具体规范参阅direct9 SDK</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 42pt; text-indent: -21pt; font-family: 宋体"><span style="font-size: 9pt; font-family: Wingdings">l<span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt">pRayPos 指向射线发出点</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 42pt; text-indent: -21pt; font-family: 宋体"><span style="font-size: 9pt; font-family: Wingdings">l<span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt">pRayDir 指向前面我们辛辛苦苦求出的射线方向的向量</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 42pt; text-indent: -21pt; font-family: 宋体"><span style="font-size: 9pt; font-family: Wingdings">l<span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt">pHit 当检测到相交图元时，指向一个true,不与任何图元相交则为假</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 42pt; text-indent: -21pt; font-family: 宋体"><span style="font-size: 9pt; color: red; font-family: Wingdings">l<span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: red">pU 用于返回重心坐标U分量</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 42pt; text-indent: -21pt; font-family: 宋体"><span style="font-size: 9pt; color: red; font-family: Wingdings">l<span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: red">pV返回重心坐标V分量</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 42pt; text-indent: -21pt; font-family: 宋体"><span style="font-size: 9pt; color: red; font-family: Wingdings">l<span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt; color: red">pDist 返回射线发出点到相交点的长度</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 21pt; font-family: 宋体"><span style="font-size: 9pt; color: red">注意：以上红色字体部分均指最近的一个返回结果（即*pDist最小）</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 42pt; text-indent: -21pt; font-family: 宋体"><span style="font-size: 9pt; font-family: Wingdings">l<span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt">ppAllHits用于如果存在多个相交三角面返回相交的所有结果</span></pre><pre style="font-size: 12pt; margin: 0cm 0cm 0pt 42pt; text-indent: -21pt; font-family: 宋体"><span style="font-size: 9pt; font-family: Wingdings">l<span style="font-weight: normal; font-size: 7pt; font-style: normal; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 9pt">pCountOfHits 返回共有多少个三角形与该射线相交</span></pre>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><strong>&nbsp;</strong></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; color: green; font-family: 新宋体"><a name="补充：重心坐标的概念">补充：重心坐标的概念</a></span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; color: green; font-family: 新宋体">其中pU和pV用到了重心坐标的概念，下面稍作描述</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; color: green; font-family: 新宋体">一个三角形有三个顶点，在迪卡尔坐标系中假设表示为V1(x1,y1,z1),V2(x2,y2,z2),V3(x3,y3,z3),则三角形内任意一点的坐标可以表示为 pV = V1 + U(V2-V1) + V(V3-V1),所以已知三个顶点坐标的情况下，任意一点可用坐标(U,V)来表示，其中 参数U控制V2在结果中占多大的权值，参数V控制V3占多大权值，最终1－U－V控制V1占多大权值，这种坐标定义方式就叫重心坐标。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="2..2.2射线三角面相交的数学算法">2..2.2射线三角面相交的数学算法</a></strong></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 宋体">使用</span>d3d<span style="font-family: 宋体">扩展函数，毕竟有时不能满足具体需求，掌握了该方法，我们才能够获得最大的控制自由度，任意修改算法。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; color: green; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><strong><span style="font-family: 宋体">已知条件</span></strong>:&nbsp;<span style="font-family: 宋体">射线源点</span>orginPoint,<span style="font-family: 宋体">三角形三个顶点</span>&nbsp;v1,v2,v3,<span style="font-family: 宋体">射线方向</span>&nbsp;Dir</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">（均以三维坐标向量形式表示）</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><strong><span style="font-family: 宋体">算法目的</span></strong>:&nbsp;<span style="font-family: 宋体">判断射线与三角形是否相交，如果相交求出交点的重心坐标</span>(U,V)<span style="font-family: 宋体">和射线原点到交点的距离</span>T<span style="font-family: 宋体">。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">我们可先假设射线与三角形相交则交点</span>(<span style="font-family: 宋体">注以下均为向量运算，</span>*<span style="font-family: 宋体">数乘，</span>dot(X,Y) X<span style="font-family: 宋体">，</span>Y&nbsp;<span style="font-family: 宋体">点乘，</span>cross<span style="font-family: 宋体">（</span>X<span style="font-family: 宋体">，</span>Y<span style="font-family: 宋体">）</span>X<span style="font-family: 宋体">，</span>Y<span style="font-family: 宋体">叉乘；</span>U<span style="font-family: 宋体">，</span>V<span style="font-family: 宋体">，</span>T<span style="font-family: 宋体">为标量</span>)</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">则：</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">IntersectPoint = V1 + U*(V2-V1) + V*(V3-V1) ;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">IntersectPoint = originPoint + T*Dir<span style="font-family: 宋体">；</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">所以</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">orginPoint + T*Dir = V1 + U*(V2-V1) + V*(V3-V1);</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">整理得：</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;<img height="161" alt="" src="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.files/image012.jpg" width="410" border="0" v:shapes="_x0000_s1027" /></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">这是一个简单的线性方程组，若有解则行列式</span><img height="21" alt="" src="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.files/image014.jpg" width="154" border="0" v:shapes="_x0000_s1026" /><span style="font-family: 宋体">不为</span>0<span style="font-family: 宋体">。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">根据</span>T,U,V<span style="font-family: 宋体">的含义当</span>T&gt;0, 0&lt;U&lt;1,0&lt;V&lt;1,0&lt;U+V&lt;1<span style="font-family: 宋体">时该交点在三角形内部，</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">解此方程组即可获得我们关心的值</span>,<span style="font-family: 宋体">具体解法不再赘述，克莱姆法则就够了（详细见线性代数）</span>:<span style="font-family: 宋体">射线原点到相交点的距离</span>T,<span style="font-family: 宋体">和交点的中心坐标</span>(U,V)<span style="font-family: 宋体">。</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">下面给出</span>Direct 9 SDK<span style="font-family: 宋体">示例程序中的实现代码</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">IntersectTriangle(&nbsp;<span style="color: blue">const</span>&nbsp;D3DXVECTOR3&amp; orig,</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">const</span>&nbsp;D3DXVECTOR3&amp; dir, D3DXVECTOR3&amp; v0,</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;D3DXVECTOR3&amp; v1, D3DXVECTOR3&amp; v2,</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FLOAT* t, FLOAT* u, FLOAT* v )</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">{</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">// 算出两个边的向量</span></span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; D3DXVECTOR3 edge1 = v1 - v0;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; D3DXVECTOR3 edge2 = v2 - v0;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;<br /></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">//补充说明:<br />&nbsp;<img src="http://www.cppblog.com/images/cppblog_com/lll109383670/1213131.jpg" width="444" height="178" alt="" /><br />//分解为一向量点乘另它二向量的叉乘,本代码中Cross,再dot的结果就是得到上面所说行阵式的值。<br /><strong>//&lt;使用方向向量之前，最好标准化D3DXVec3Normalize,因为根据上面公式T*dir,dir必须为单位化向量</strong><br /><br />&nbsp;&nbsp;&nbsp; D3DXVECTOR3 pvec;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; D3DXVec3Cross( &amp;pvec, &amp;dir, &amp;edge2 );</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">// 如果det为0，或接近于零则射线与三角面共面或平行，不相交</span></span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; color: green; font-family: 新宋体">//此处det就相当于上面的</span><img height="21" alt="" src="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.files/image014.jpg" width="154" border="0" v:shapes="_x0000_s1025" /><span style="font-family: 宋体">，</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; FLOAT det = D3DXVec3Dot( &amp;edge1, &amp;pvec );</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; D3DXVECTOR3 tvec;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if</span>( det &gt; 0 )</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; {</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tvec = orig - v0;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; }</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">else</span></span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; {</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tvec = v0 - orig;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; det = -det;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; }</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if</span>( det &lt; 0.0001f )</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">return</span>&nbsp;FALSE;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">// 计算u并测试是否合法（在三角形内）</span></span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; *u = D3DXVec3Dot( &amp;tvec, &amp;pvec );</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if</span>( *u &lt; 0.0f || *u &gt; det )</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">return</span>&nbsp;FALSE;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">// Prepare to test V parameter</span></span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; D3DXVECTOR3 qvec;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; D3DXVec3Cross( &amp;qvec, &amp;tvec, &amp;edge1 );</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//计算u并测试是否合法（在三角形内）</span></span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; *v = D3DXVec3Dot( &amp;dir, &amp;qvec );</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if</span>( *v &lt; 0.0f || *u + *v &gt; det )</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">return</span>&nbsp;FALSE;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 27pt; text-indent: -27pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">/*计算t,并把t,u,v放缩为合法值（注意前面的t,v,u不同于算法描述中的相应量，乘了一个系数det）,</span><span style="color: red">注意：由于该步运算需要使用除法，所以放到最后来进行，避免不必要的运算，提高算法效率</span><span style="color: green">*/</span></span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; *t = D3DXVec3Dot( &amp;edge2, &amp;qvec );</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; FLOAT fInvDet = 1.0f / det;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; *t *= fInvDet;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; *u *= fInvDet;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp; *v *= fInvDet;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="left"><span style="font-size: 9pt; font-family: 新宋体">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">return</span>&nbsp;TRUE;</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-size: 9pt; font-family: 新宋体">}</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="2.2.3&nbsp; 拾取完成根据获得的中心坐标计算我们关心的常见量，。">2.2.3&nbsp; 拾取完成根据获得的中心坐标计算我们关心的常见量，。</a></strong></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">根据重心坐标（</span>U,V<span style="font-family: 宋体">）</span>,<span style="font-family: 宋体">我们可以很容易的算出各种相关量比如纹理坐标和交点的差值颜色，假设以纹理坐标为例设</span>V1,V2,V3<span style="font-family: 宋体">的纹理坐标分别为</span>T1(tu1,tv1),T2(tu2,tv2),T3(tu3,tv3)<span style="font-family: 宋体">则交点的坐标为</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'" align="center">IntersectPointTexture = T1 + U(T2-T1) + V(T3-T1)</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="3、结束及声明">3、结束及声明</a></strong></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">Ok,&nbsp;<span style="font-family: 宋体">到这里为止关于拾取的相关知识就介绍完了，小弟第一次写这种文章，不知道有没有把问题说清楚，希望对大家有所帮助，有任何问题可以给我发</span>email:&nbsp;<a style="color: blue; text-decoration: underline" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#106;&#122;&#104;&#97;&#110;&#103;&#49;&#64;&#109;&#97;&#105;&#108;&#46;&#120;&#105;&#100;&#105;&#97;&#110;&#46;&#101;&#100;&#117;&#46;&#99;&#110;">jzhang1@mail.xidian.edu.cn</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">或者到我的网站留言：</span>&nbsp;<a style="color: blue; text-decoration: underline" href="http://www.heavysword.com/">www.heavysword.com</a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">声明：</span></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'"><span style="font-family: 宋体">本文写作的目的是为了广大</span>D3D<span style="font-family: 宋体">学习者方便学习服务，文中算法为作者参考相关文献总结，作者无意把这些据为自己的成果，所有权原算法提出者所有（参阅参考文献），文中代码为</span>D3d SDK<span style="font-family: 宋体">的示例内容，由笔者进行了必要的解释，代码版权归</span>microsoft<span style="font-family: 宋体">所有。</span></p>
<p style="font-size: 10.5pt; margin: 0cm 0cm 0pt 18pt; text-indent: -18pt; font-family: 'Times New Roman'"><strong><a name="4、参考文献">4、参考文献</a></strong></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">【1】Microsoft DirectX 9.0 SDK,microsoft</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">【2】fast,Minimun Storage Ray/Triangle Intersection,Tomas Moler,Ben Trumbore</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">　</p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">&nbsp;BY<a href="http://www.heavysword.com/"><img height="31" alt="" src="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.files/linklogo.gif" width="88" border="0" /></a></p>
<p style="font-size: 10.5pt; font-family: 'Times New Roman'">(转载请注明出处）</p>
<div id="maiku-toolbar" style="padding-right: 0px; display: block; padding-left: 25px; left: 1186px; background-image: url(chrome-extension://doobhdlahhejeholdpmecpdenofkhnnc/images/toolbar/logo.png); padding-bottom: 0px; width: 50px; padding-top: 0px; position: absolute; top: -24px; height: 22px; background-color: #ffffff; border-image: initial; "><a title="保存文章到麦库" style="display: inline-block;background-image: url(chrome-extension://doobhdlahhejeholdpmecpdenofkhnnc/images/toolbar/save.png); margin: 4px 5px; width: 14px; text-indent: -9999px; height: 14px" href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm##">保存文章到麦库</a><a title="关闭工具条" style="display: inline-block;background-image: url(chrome-extension://doobhdlahhejeholdpmecpdenofkhnnc/images/toolbar/close.png); margin: 4px 5px; width: 14px; text-indent: -9999px; height: 14px" href="http://dev.gameres.com/Program/Visual/3D/pick_2004_529.htm##">关闭工具条</a></div></span><a href="http://dev.gameres.com/"></a><img src ="http://www.cppblog.com/lll109383670/aggbug/166211.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lll109383670/" target="_blank">帅哥帅</a> 2012-02-22 10:27 <a href="http://www.cppblog.com/lll109383670/archive/2012/02/22/166211.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>3D图形学编程指南笔记</title><link>http://www.cppblog.com/lll109383670/archive/2012/02/22/166190.html</link><dc:creator>帅哥帅</dc:creator><author>帅哥帅</author><pubDate>Wed, 22 Feb 2012 01:33:00 GMT</pubDate><guid>http://www.cppblog.com/lll109383670/archive/2012/02/22/166190.html</guid><wfw:comment>http://www.cppblog.com/lll109383670/comments/166190.html</wfw:comment><comments>http://www.cppblog.com/lll109383670/archive/2012/02/22/166190.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lll109383670/comments/commentRss/166190.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lll109383670/services/trackbacks/166190.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: #000000; text-indent: 0px; white-space: normal; letter-spacing: normal; orphans: 2; widows: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px">
<div style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; text-indent: 30px; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none"><strong id="internal-source-marker_0.8663704113569111" style="font-weight: normal">
<p dir="ltr"><span style="font-weight: bold; font-size: 48px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">3D图形学编程指南笔记</span></p><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">第二章</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">自由度：</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">前提：约束对象体，使所有的点之间的距离不变</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">平面物体有三个自由度，如位于x轴上的线段，则在沿x,y方向上有二个自由度，再沿原点旋转有一个自由度。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">3D物体有六个自由度，三轴，三旋转。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">自由度主要是有关平移和旋转。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none"></span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">有关世界到屏幕的过程。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">1.得到世界矩阵。 &nbsp;</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;首先物体有物体空间，我们要把物体变换到世界空间来，我们要预先知道物体在世界坐标系中的translate,rotate,所以DX里创建世界矩阵是可以根据平移、旋转来创建世界矩阵的。有了这几个参数，就可以把物体从物体坐标系变成世界坐标系中来。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;具体方法可以采用原点重合的方法，就是把物体坐标的原点通过一系列的平移旋转变化与世界坐标系重合，这过程中用到的联合矩阵，就是世界矩阵。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">2.得到视口矩阵，观察矩阵。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把物体变换到世界坐标系后，还应该变换到视口坐标系，也就是观察坐标系，因为我们是通过视口坐标系来观察的，我们眼睛看向的正前方只会是视线所在的方向，也就是z轴所在。所以得把物体从世界坐标系变化到我们的视口坐标系来。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;同样是通过坐标系变换的方法，我们可以得到观察矩阵。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">3.得到屏幕坐标，投影矩阵。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把物体变换到视口坐标系中后，我们要从屏幕上观察到，就必须把3D的视口坐标系，转换成我们可以通过屏幕看到的2D坐标，也就是屏幕坐标系，设备坐标系，在DX中设备坐标系是，正常的笛卡儿坐标第，原点在正中心，X正方向向右，Y正方向向上，单元为1。从视口坐标系到设备坐标系的过程，就可以得到投影坐标系。<br /></span>注意，如果屏幕是长方形的，有高宽比，那么对于投影矩阵来说，在垂直方向上的最终的y还要乘以宽高比.相当于缩短了垂直方向的视野，达到与屏幕的高宽比相同的视野，具体到投影矩阵，就是视距d还要乘以一个宽高比（垂直方向上）。<br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">要完成物体的拾取，其实就是一个屏幕到世界的反过程。</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">举例来说：</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">如果在屏幕上的拾取点是x,y（窗口客户区坐标，640*480）</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">那么屏幕到世界的流程是</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">1.窗口客户区坐标-》设备坐标系 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">先进行坐标系缩放（2*x/640）再进行平移-1</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">x(设) = (2*x/640) - 1</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">2.到视口坐标系</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">x(视) = x(设)/proj_matrix._11 &nbsp;&nbsp;&nbsp;&nbsp;(proj_maxtrix._11是投影矩阵的第一个元素)</span><br /><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">同理到y<br />3.到世界坐标系，乘以视图和世界矩阵的逆矩阵，得到最终拾取点在世界坐标系中的位置<br /></span><span style="font-weight: normal; font-size: 15px; vertical-align: baseline; color: #000000; font-style: normal; font-family: Arial; background-color: transparent; font-variant: normal; text-decoration: none">这样，就完成了屏幕到世界的坐标系变换，然后通过观察点与搭取点决定的射线，就可以找到交点了。<br /><br />主要流程<br /><span class="Apple-style-span" style="word-spacing: 0px; font: 14px/18px Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-transform: none; color: #000000; text-indent: 0px; white-space: normal; letter-spacing: normal; background-color: #ffffff; text-align: left; orphans: 2; widows: 2; webkit-text-decorations-in-effect: none; webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px"> 
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial">0) Obtain your mouse coordinates within the client area</p>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial">1) Get your Projection matrix and View matrix if no Model matrix required.</p>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial">2) Multiply View * Projection</p>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial">3) Inverse the results of multiplication</p>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial">4) Construct a vector4 consisting of</p>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial"><strong style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: bold; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; background-origin: initial; background-clip: initial">x</strong><span class="Apple-converted-space">&nbsp;</span>=<span class="Apple-converted-space">&nbsp;</span><code style="border-top-width: 0px; padding-right: 5px; padding-left: 5px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 1px; margin: 0px; vertical-align: baseline; padding-top: 1px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; background-color: #eeeeee; border-right-width: 0px; background-origin: initial; background-clip: initial">mouseposition.x</code><span class="Apple-converted-space">&nbsp;</span>within a range of window x - transform to values between -1 and 1</p>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial"><strong style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: bold; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; background-origin: initial; background-clip: initial">y</strong><span class="Apple-converted-space">&nbsp;</span>=<span class="Apple-converted-space">&nbsp;</span><code style="border-top-width: 0px; padding-right: 5px; padding-left: 5px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 1px; margin: 0px; vertical-align: baseline; padding-top: 1px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; background-color: #eeeeee; border-right-width: 0px; background-origin: initial; background-clip: initial">mouseposition.y</code><span class="Apple-converted-space">&nbsp;</span>within a range of window y - transform to values between -1 and 1 - remember to invert mouseposition.y if needed</p>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial"><strong style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: bold; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; background-origin: initial; background-clip: initial">z</strong><span class="Apple-converted-space">&nbsp;</span>=<span class="Apple-converted-space">&nbsp;</span><code style="border-top-width: 0px; padding-right: 5px; padding-left: 5px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 1px; margin: 0px; vertical-align: baseline; padding-top: 1px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; background-color: #eeeeee; border-right-width: 0px; background-origin: initial; background-clip: initial">the depth value</code><span class="Apple-converted-space">&nbsp;</span>( this can be obtained with glReadPixel) - you can manually go from -1 to 1 ( zNear, zFar )</p>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial"><strong style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: bold; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; background-origin: initial; background-clip: initial">w</strong><span class="Apple-converted-space">&nbsp;</span>=<span class="Apple-converted-space">&nbsp;</span><code style="border-top-width: 0px; padding-right: 5px; padding-left: 5px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 1px; margin: 0px; vertical-align: baseline; padding-top: 1px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; background-color: #eeeeee; border-right-width: 0px; background-origin: initial; background-clip: initial">1.0</code></p>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial">5) Multiply the vector by inversed matrix created before 6) Divide result vector by it's w component after matrix multiplication ( perspective division )</p>
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #000000">&nbsp;&nbsp;POINT&nbsp;mousePos;<br /></span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetCursorPos(</span><span style="color: #000000">&amp;</span><span style="color: #000000">mousePos);<br /></span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ScreenToClient(&nbsp;</span><span style="color: #0000ff">this</span><span style="color: #000000">-&gt;</span><span style="color: #000000">GetWindowHWND(),&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">mousePos&nbsp;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CMatrix4x4&nbsp;matProjection&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;m_pCamera</span><span style="color: #000000">-&gt;</span><span style="color: #000000">getViewMatrix()&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;&nbsp;m_pCamera</span><span style="color: #000000">-&gt;</span><span style="color: #000000">getProjectionMatrix()&nbsp;;<br /></span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CMatrix4x4&nbsp;matInverse&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;matProjection.inverse();<br /></span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">float</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">[</span><span style="color: #000000">4</span><span style="color: #000000">];<br /></span><span style="color: #008080">10</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">float</span><span style="color: #000000">&nbsp;winZ&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1.0</span><span style="color: #000000">;<br /></span><span style="color: #008080">11</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">12</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">13</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">(</span><span style="color: #000000">2.0f</span><span style="color: #000000">*</span><span style="color: #000000">((</span><span style="color: #0000ff">float</span><span style="color: #000000">)(mousePos.x</span><span style="color: #000000">-</span><span style="color: #000000">0</span><span style="color: #000000">)</span><span style="color: #000000">/</span><span style="color: #000000">(</span><span style="color: #0000ff">this</span><span style="color: #000000">-&gt;</span><span style="color: #000000">GetResolution().x</span><span style="color: #000000">-</span><span style="color: #000000">0</span><span style="color: #000000">)))</span><span style="color: #000000">-</span><span style="color: #000000">1.0f</span><span style="color: #000000">,<br /></span><span style="color: #008080">14</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">[</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">1.0f</span><span style="color: #000000">-</span><span style="color: #000000">(</span><span style="color: #000000">2.0f</span><span style="color: #000000">*</span><span style="color: #000000">((</span><span style="color: #0000ff">float</span><span style="color: #000000">)(mousePos.y</span><span style="color: #000000">-</span><span style="color: #000000">0</span><span style="color: #000000">)</span><span style="color: #000000">/</span><span style="color: #000000">(</span><span style="color: #0000ff">this</span><span style="color: #000000">-&gt;</span><span style="color: #000000">GetResolution().y</span><span style="color: #000000">-</span><span style="color: #000000">0</span><span style="color: #000000">)));<br /></span><span style="color: #008080">15</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">[</span><span style="color: #000000">2</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">2.0</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;winZ&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">1.0</span><span style="color: #000000">;<br /></span><span style="color: #008080">16</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">[</span><span style="color: #000000">3</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">1.0</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">17</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">18</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CVector4&nbsp;vIn&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;CVector4(</span><span style="color: #0000ff">in</span><span style="color: #000000">[</span><span style="color: #000000">0</span><span style="color: #000000">],</span><span style="color: #0000ff">in</span><span style="color: #000000">[</span><span style="color: #000000">1</span><span style="color: #000000">],</span><span style="color: #0000ff">in</span><span style="color: #000000">[</span><span style="color: #000000">2</span><span style="color: #000000">],</span><span style="color: #0000ff">in</span><span style="color: #000000">[</span><span style="color: #000000">3</span><span style="color: #000000">]);<br /></span><span style="color: #008080">19</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;vIn&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;matInverse;<br /></span><span style="color: #008080">20</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">21</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos.w&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1.0</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">/</span><span style="color: #000000">&nbsp;pos.w;<br /></span><span style="color: #008080">22</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">23</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos.x&nbsp;</span><span style="color: #000000">*=</span><span style="color: #000000">&nbsp;pos.w;<br /></span><span style="color: #008080">24</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos.y&nbsp;</span><span style="color: #000000">*=</span><span style="color: #000000">&nbsp;pos.w;<br /></span><span style="color: #008080">25</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos.z&nbsp;</span><span style="color: #000000">*=</span><span style="color: #000000">&nbsp;pos.w;<br /></span><span style="color: #008080">26</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">27</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sprintf(strTitle,</span><span style="color: #000000">"</span><span style="color: #000000">%f&nbsp;%f&nbsp;%f&nbsp;/&nbsp;%f,%f,%f&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">,m_pCamera</span><span style="color: #000000">-&gt;</span><span style="color: #000000">m_vPosition.x,m_pCamera</span><span style="color: #000000">-&gt;</span><span style="color: #000000">m_vPosition.y,m_pCamera</span><span style="color: #000000">-&gt;</span><span style="color: #000000">m_vPosition.z,pos.x,pos.y,pos.z);<br /></span><span style="color: #008080">28</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" /><br /></span><span style="color: #008080">29</span><span style="color: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetWindowText(</span><span style="color: #0000ff">this</span><span style="color: #000000">-&gt;</span><span style="color: #000000">GetWindowHWND(),strTitle);</span></div>
<p style="border-top-width: 0px; clear: both; padding-right: 0px; padding-left: 0px; border-left-width: 0px; font-size: 14px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px 0px 1em; vertical-align: baseline; padding-top: 0px; background-color: transparent; border-right-width: 0px; word-wrap: break-word; background-origin: initial; background-clip: initial">参考文章：<a href="http://stackoverflow.com/questions/7692988/opengl-math-projecting-screen-space-to-world-space-coords-solved?answertab=active#tab-top">http://stackoverflow.com/questions/7692988/opengl-math-projecting-screen-space-to-world-space-coords-solved?answertab=active#tab-top</a><br /><br /></p></span><br /></span></strong></div></span><img src ="http://www.cppblog.com/lll109383670/aggbug/166190.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lll109383670/" target="_blank">帅哥帅</a> 2012-02-22 09:33 <a href="http://www.cppblog.com/lll109383670/archive/2012/02/22/166190.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Material 和 Shader</title><link>http://www.cppblog.com/lll109383670/archive/2012/02/21/166183.html</link><dc:creator>帅哥帅</dc:creator><author>帅哥帅</author><pubDate>Tue, 21 Feb 2012 15:28:00 GMT</pubDate><guid>http://www.cppblog.com/lll109383670/archive/2012/02/21/166183.html</guid><wfw:comment>http://www.cppblog.com/lll109383670/comments/166183.html</wfw:comment><comments>http://www.cppblog.com/lll109383670/archive/2012/02/21/166183.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lll109383670/comments/commentRss/166183.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lll109383670/services/trackbacks/166183.html</trackback:ping><description><![CDATA[<div>关于Material和Shader的概念纠结很久了，一直没找到合适的答案。看起来是一个东西，但总感觉应该不同，有所区别。<br />今天用FX Composer终于找到感觉了。<br />Shader可以说是代码段，是没有输入参数的，如纹理<br />Material是实例化的Shader就是说Material是包括了输入的纹理的，所以不同的纹理就有不同的Material，但用的Shader是相同的。<br />理解了这个概念就在设计类的时候就可以把Shader和Material分开来设计，Shader负责脚本的加载，而Material负责脚本参数的传入，这样就可以复用了。<br />可以参考：<a href="http://www.leadwerks.com/files/Tutorials/CPP/Materials_And_Shaders.pdf">http://www.leadwerks.com/files/Tutorials/CPP/Materials_And_Shaders.pdf</a></div><img src ="http://www.cppblog.com/lll109383670/aggbug/166183.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lll109383670/" target="_blank">帅哥帅</a> 2012-02-21 23:28 <a href="http://www.cppblog.com/lll109383670/archive/2012/02/21/166183.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>dxut 学习笔记</title><link>http://www.cppblog.com/lll109383670/archive/2012/02/20/166060.html</link><dc:creator>帅哥帅</dc:creator><author>帅哥帅</author><pubDate>Mon, 20 Feb 2012 06:18:00 GMT</pubDate><guid>http://www.cppblog.com/lll109383670/archive/2012/02/20/166060.html</guid><wfw:comment>http://www.cppblog.com/lll109383670/comments/166060.html</wfw:comment><comments>http://www.cppblog.com/lll109383670/archive/2012/02/20/166060.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lll109383670/comments/commentRss/166060.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lll109383670/services/trackbacks/166060.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: dxut框架用于调试关键代码和算法&nbsp;用于线程安全访问成员变量的宏&lt; 临界区的访问锁定相关API函数：EnterCriticalSection结构：LPCRITICAL_SECTION使用方法:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www...&nbsp;&nbsp;<a href='http://www.cppblog.com/lll109383670/archive/2012/02/20/166060.html'>阅读全文</a><img src ="http://www.cppblog.com/lll109383670/aggbug/166060.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lll109383670/" target="_blank">帅哥帅</a> 2012-02-20 14:18 <a href="http://www.cppblog.com/lll109383670/archive/2012/02/20/166060.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>