﻿<?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++博客-C++优雅之旅-随笔分类-OGRE</title><link>http://www.cppblog.com/eros/category/6681.html</link><description>探索C++</description><language>zh-cn</language><lastBuildDate>Thu, 21 May 2009 08:03:15 GMT</lastBuildDate><pubDate>Thu, 21 May 2009 08:03:15 GMT</pubDate><ttl>60</ttl><item><title>Ogre学习笔记(一)</title><link>http://www.cppblog.com/eros/archive/2009/05/20/83455.html</link><dc:creator>林公子</dc:creator><author>林公子</author><pubDate>Wed, 20 May 2009 03:48:00 GMT</pubDate><guid>http://www.cppblog.com/eros/archive/2009/05/20/83455.html</guid><wfw:comment>http://www.cppblog.com/eros/comments/83455.html</wfw:comment><comments>http://www.cppblog.com/eros/archive/2009/05/20/83455.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/eros/comments/commentRss/83455.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/eros/services/trackbacks/83455.html</trackback:ping><description><![CDATA[<span style="color: red;">本系列文章部分内容来源于</span><a style="color: red;"  href="http://ogre3d.cn/">http://ogre3d.cn
</a><span style="color: red;">的Ogre系列教程的中文翻译版，加上一些我自己学习时的笔记。</span><br><br>Ogre三基石<br><br>SceneManager 场景管理器<br><br>&nbsp;&nbsp;&nbsp; 屏幕上显示的所有东西都有场景管理器来管理。当往场景中添加物体时，场景管理器会记录物体的位置，当添加摄像机来观察某个场景时，场景管理器会记录摄像机的位置。Ogre里有多种场景管理器<br><br>Entity 实体<br><br>&nbsp;&nbsp;&nbsp; 一个实体是可以在场景中渲染的物体之一。可以把实体理解为任何一个3D模型。一个机器人可以使一个实体，一条鱼可以是一个实体，大地草原可以是一个非常大的实体。<span style="color: red;">灯光，摄像机，粒子，广告牌等不能成为实体。</span><br>&nbsp;&nbsp;&nbsp; Ogre中你不能直接将一个实体放入场景中，而是将实体与场景节点绑在一起，这个场景节点则包括了实体的方位信息。<br><br>SceneNode 场景节点<br>&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 场景节点将持续跟踪与它绑在一起的实体的方位。当你创建了一个实体时，它直到与一个场景节点绑定后才会被渲染。同样，一个场景节点也不能单独的在屏幕上显示出来，只有与一个实体绑定后才能在屏幕上显示。<span style="color: red;">一个场景节点可以绑定多个实体，场景节点同样可以和其他场景节点绑定，但是一个实体不能被多个场景节点绑定。</span><br>&nbsp;&nbsp;&nbsp; 每个场景管理器都有一个根节点，而场景节点都是在根节点的基础上建立，其位置总是相对于根节点的。<br><br>实践<br><br>&nbsp;&nbsp;&nbsp; 要创建一个实体，可以调用SceneManager的createEntity方法：<br>&nbsp;&nbsp;&nbsp;
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;"></span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;创建一个实体,&nbsp;加载Athena网格。</span><span style="color: #008000;"></span><span style="color: #000000;"><br>Entity</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;entity1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;mSceneMgr</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">createEntity(</span><span style="color: #000000;">"</span><span style="color: #000000;">athene1</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">athene.mesh</span><span style="color: #000000;">"</span><span style="color: #000000;">);</span></div>
&nbsp;&nbsp;&nbsp; createEntity的第一个参数为实体指定了一个唯一的标识符，第二个参数表示要加载的资源名称，在这里是一个名为athene的网格模型。<br><br>&nbsp;&nbsp;&nbsp; 我们可以为这个实体代表的网格模式指定一个材质：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;设置网格材质。</span><span style="color: #008000;"><br></span><span style="color: #000000;">entity1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">setMaterialName(</span><span style="color: #000000;">"</span><span style="color: #000000;">Examples/Athene/NormalMapped</span><span style="color: #000000;">"</span><span style="color: #000000;">);</span></div>
<br>&nbsp;&nbsp;&nbsp; 创建了一个实体后，还要创建一个场景节点与它绑定起来：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;创建一个子场景节点。</span><span style="color: #008000;"><br></span><span style="color: #000000;">SceneNode</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;node1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;mSceneMgr</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">getRootSceneNode()</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">createChildSceneNode(</span><span style="color: #000000;">"</span><span style="color: #000000;">athene1</span><span style="color: #000000;">"</span><span style="color: #000000;">);</span></div>
&nbsp;&nbsp;&nbsp; 与实体一样，场景节点的名字也是唯一的，在这里是athene1。<br><br>&nbsp;&nbsp;&nbsp; 然后将实体和场景节点绑定：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;将实体绑定到场景节点上。</span><span style="color: #008000;"><br></span><span style="color: #000000;">node1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">attachObject(entity1);</span></div>
&nbsp;&nbsp;&nbsp; 绑定之后实体就会被渲染出来，默认情况下实体会被渲染到世界坐标的原点(0, 0, 0)处。<br><br>&nbsp;&nbsp;&nbsp; 要改变实体在场景中的位置，可以使用与之绑定的场景节点的setPosition方法：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">node1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">setPosition(</span><span style="color: #000000;">30</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">);</span></div>
&nbsp;&nbsp;&nbsp; 这样场景节点就相对于根节点往X轴正方向移动了10个单位。<br><br>&nbsp;&nbsp;&nbsp; 同样，还可以使用场景节点的translate方法达到相同的目的：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">node1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">translate(Vector3(</span><span style="color: #000000;">30</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">));</span></div>
&nbsp;&nbsp;&nbsp; setPosition位置的改变是相对于父节点的,translate则可以设置变换相对于哪个坐标系。<br><br>&nbsp;&nbsp;&nbsp; 要得到场景节点的父节点，可以调用getParentNode:<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">SceneNode* </span><span style="color: #000000;"></span><span style="color: #000000;">parentNode&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;node1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">getParentSceneNode();</span></div>
&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; 要得到场景节点的名称，可以调用getName:<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">String&nbsp;nodeName&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;nod1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">getName();</span></div>
<br>&nbsp;&nbsp;&nbsp; 因为场景节点的移动都是相对于父节点的，所以很容易实现两个节点一起移动：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">Entity&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">entity1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;mSceneMgr</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">createEntity(&nbsp;</span><span style="color: #000000;">"Athene1</span><span style="color: #000000;"></span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Athene.mesh</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;);<br>SceneNode&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">node1&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;mSceneMgr</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">getRootSceneNode()</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">createChildSceneNode(&nbsp;</span><span style="color: #000000;">"Athene</span><span style="color: #000000;">Node1</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;);<br>ode1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">attachObject(&nbsp;entity1 );<br><br>Entity&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">entity2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;mSceneMgr</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">createEntity(&nbsp;</span><span style="color: #000000;">"</span>Athene2<span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">"</span>Athene<span style="color: #000000;">.mesh</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;);<br>SceneNode&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">node2&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">node1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">createChildSceneNode(&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">AtheneNode2</span><span style="color: #000000;">"</span><span style="color: #000000;">,&nbsp;Vector3(&nbsp;</span><span style="color: #000000;">50</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">&nbsp;)&nbsp;);<br>node2</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">attachObject(&nbsp;entity2&nbsp;);</span></div>
&nbsp;&nbsp;&nbsp; 这样一来，node1就成了node2的父节点，移动node1会使node2跟着一起移动，而移动node2不会影响node1。<br><br>缩放<br><br>&nbsp;&nbsp;&nbsp; 使用场景节点可以对网格模型进行缩放：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">node1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">scale(</span><span style="color: #000000;">1.5</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">1.5</span><span style="color: #000000;">,&nbsp;</span><span style="color: #000000;">1.5</span><span style="color: #000000;">);</span></div>
&nbsp;&nbsp;&nbsp; scale的三个参数分别对应x, y, z方向上的缩放比例，上面的例子将网格模型放大到1.5倍。<br><br>旋转<br><br>&nbsp;&nbsp;&nbsp; 还可以对网格模型进行旋转，其中yaw是Y轴的旋转，pitch是X轴的旋转，roll是Z轴的旋转：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">node1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">yaw(Degree(</span><span style="color: #000000;">-</span><span style="color: #000000;">90</span><span style="color: #000000;">));<br>node1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">pitch(Degree(</span><span style="color: #000000;">-</span><span style="color: #000000;">90</span><span style="color: #000000;">));<br>node1</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">roll(Degree(</span><span style="color: #000000;">-</span><span style="color: #000000;">90</span><span style="color: #000000;">));</span></div>
<br><br><br><img src ="http://www.cppblog.com/eros/aggbug/83455.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/eros/" target="_blank">林公子</a> 2009-05-20 11:48 <a href="http://www.cppblog.com/eros/archive/2009/05/20/83455.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SceneNode::setDirection函数问题</title><link>http://www.cppblog.com/eros/archive/2008/12/30/70732.html</link><dc:creator>林公子</dc:creator><author>林公子</author><pubDate>Mon, 29 Dec 2008 17:27:00 GMT</pubDate><guid>http://www.cppblog.com/eros/archive/2008/12/30/70732.html</guid><wfw:comment>http://www.cppblog.com/eros/comments/70732.html</wfw:comment><comments>http://www.cppblog.com/eros/archive/2008/12/30/70732.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/eros/comments/commentRss/70732.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/eros/services/trackbacks/70732.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">mSn</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">translate(mPosition&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;evt.timeSinceLastFrame,&nbsp;Node::TS_WORLD);</span></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="FONT-FAMILY: 微软雅黑">刚开始学习Ogre,做了个很简单的demo,用键盘的上下左右控制一个人物模型四处走动。逻辑很简单，但是实现到想要的效果却费了一番周折。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;一开始用一个Vector3 mPosition变量保存人物的位置，然后Real mWalkSpeed保存人物的移动速度，在上下左右的按键响应代码中分别<br>写上处理人物各方向移动的代码：</span><br>
<div>&nbsp;
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">&nbsp;<span style="COLOR: #000000">1&nbsp;&nbsp;&nbsp; </span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;DemoFrameListener::keyPressed(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;OIS::KeyEvent&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">arg)<br>&nbsp;&nbsp; </span><span style="COLOR: #000000">2</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_64_1144_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_64_1144_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">switch</span><span style="COLOR: #000000">&nbsp;(arg.key)<br><img id=Codehighlighter1_95_1118_Open_Image onclick="this.style.display='none'; Codehighlighter1_95_1118_Open_Text.style.display='none'; Codehighlighter1_95_1118_Closed_Image.style.display='inline'; Codehighlighter1_95_1118_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif">&nbsp;</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_95_1118_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_95_1118_Open_Text><span style="COLOR: #000000">{<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;按下Escape键，退出程序。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;OIS::KC_ESCAPE:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mContinue&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;W、S、A、D控制人物行走。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;OIS::KC_UP:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">11</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;OIS::KC_W:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">12</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mPosition.z&nbsp;</span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000">&nbsp;mWalkSpeed;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">13</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mSn</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">setDirection(Vector3::NEGATIVE_UNIT_Z);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">14</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;取得模型的"Walk"动画集。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">15</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mAnimationState&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;mEntity</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">getAnimationState(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Walk</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">16</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">17</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;OIS::KC_S:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">18</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;OIS::KC_DOWN:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">19</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mPosition.z&nbsp;</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">&nbsp;mWalkSpeed;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">20</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mSn</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">setDirection(Vector3::UNIT_Z);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">21</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;取得模型的"Walk"动画集。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">22</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mAnimationState&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;mEntity</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">getAnimationState(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Walk</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">23</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">24</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;OIS::KC_A:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">25</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;OIS::KC_LEFT:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">26</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;取得模型的"Walk"动画集。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">27</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mAnimationState&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;mEntity</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">getAnimationState(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Walk</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">28</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mSn</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">setDirection(Vector3::UNIT_X);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">29</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">30</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;OIS::KC_D:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">31</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">case</span><span style="COLOR: #000000">&nbsp;OIS::KC_RIGHT:<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">32</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;取得模型的"Walk"动画集。</span><span style="COLOR: #008000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">33</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mAnimationState&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;mEntity</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">getAnimationState(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Walk</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">34</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mSn</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">setDirection(Vector3::NEGATIVE_UNIT_X);<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">35</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"></span><span style="COLOR: #000000">36</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">37</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">38</span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif"></span><span style="COLOR: #000000">39</span><span style="COLOR: #000000">}</span></span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">40</span><span style="COLOR: #000000"><br><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span></div>
然后在每一帧中调用<br><span style="FONT-FAMILY: 微软雅黑">
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">mSn</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">translate(mPosition&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;evt.timeSinceLastFrame,&nbsp;Node::TS_LOCAL);</span></div>
来更新人物位置。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;编译代码运行，出现了奇怪的效果。初始时人物面朝-Z轴方向，按下后应该面朝+Z轴方向并朝这个方向行走，实际却是面朝+Z方向往-Z轴方向移动。以为逻辑代码写错导致移动方向错误，检查了半天发现一点错误都没有。折腾了两天都没有结果，今天在群里和人聊天时谈到SceneNode::setDirection这个函数，人家说调用SceneNode::setDirection这个函数后会改变模型的局部坐标系，顿时脑子高速运转，隐约觉得离答案已经不远了，自己琢磨的一下，终于解决了问题。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;原来，用一个参数(方向向量)调用setDirection函数时，该函数有两个默认值，我遇到的问题就和第二个参数有关，这个参数描述的是setDirection函数操作的参照空间，有Node::TS_LOCAL,Node::TS_PARENT和Node::TS_WORLD三个可选值，分别表示，局部空间，父节点空间和世界空间。在我的例子中，人物的朝向是以整个地图为参照物的，所以，这个参数应该设置成TS_WORLD，如果取其默认值Node::TS_LOCAL，调用setDirection之后，人物的整个局部坐标系的方向都变了，这样一来，当按下方向键时，人物会面向正确的方向，但是移动的参照方向却和原来的相反，这就是为什么会有正确的朝向却背向移动的奇怪现象了。</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;另外<br>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #000000">mSn</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">translate(mPosition&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;evt.timeSinceLastFrame,&nbsp;Node::TS_LOCAL);</span></div>
中的Node::TS_LOCAL也要改成Node::TS_WORLD，使人物相对于世界空间平移。 </div>
<img src ="http://www.cppblog.com/eros/aggbug/70732.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/eros/" target="_blank">林公子</a> 2008-12-30 01:27 <a href="http://www.cppblog.com/eros/archive/2008/12/30/70732.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>