﻿<?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++博客-小星星的天空</title><link>http://www.cppblog.com/LittleStar/</link><description>   O(∩_∩)O  小月亮的fans   ^_^</description><language>zh-cn</language><lastBuildDate>Tue, 14 Apr 2026 14:46:19 GMT</lastBuildDate><pubDate>Tue, 14 Apr 2026 14:46:19 GMT</pubDate><ttl>60</ttl><item><title>【转】多态(Polymorphism)的实现机制(上)－－C++篇</title><link>http://www.cppblog.com/LittleStar/archive/2009/10/20/99063.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Tue, 20 Oct 2009 13:36:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/10/20/99063.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/99063.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/10/20/99063.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/99063.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/99063.html</trackback:ping><description><![CDATA[<p>多态(Polymorphism)是面向对象的核心概念，本文以C++为例，讨论多态的具体实现。C++中多态可以分为基于继承和虚函数的动态多态以及基于模板的静态多态，如果没有特别指明，本文中出现的多态都是指前者，也就是基于继承和虚函数的动态多态。至于什么是多态，在面向对象中如何使用多态，使用多态的好处等等问题，如果大家感兴趣的话，可以找本面向对象的书来看看。<br>&nbsp;&nbsp;&nbsp;
为了方便说明，下面举一个简单的使用多态的例子(From [1] )：</p>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">class
Shape<br>{<br>protected:<br>&nbsp; int m_x;&nbsp;&nbsp;&nbsp; // X coordinate<br>&nbsp; int m_y;&nbsp; // Y
coordinate<br>public:<br>&nbsp; // Pure virtual function for drawing<br>&nbsp; virtual
void Draw() = 0;&nbsp;&nbsp;<br><br>&nbsp; // A regular virtual function<br>&nbsp; virtual void
MoveTo(int newX, int newY);<br><br>&nbsp;// Regular method, not overridable.<br>&nbsp;
void Erase();<br><br>&nbsp; // Constructor for Shape<br>&nbsp; Shape(int x, int
y);&nbsp;<br><br>&nbsp;// Virtual destructor for Shape<br>&nbsp; virtual ~Shape();<br>};</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">//
Circle class declaration<br>class Circle : public Shape<br>{<br>private:<br>&nbsp;&nbsp;
int m_radius;&nbsp;&nbsp;&nbsp; // Radius of the circle <br>public:<br>&nbsp;&nbsp; // Override to draw a
circle<br>&nbsp;&nbsp; virtual void Draw();&nbsp;&nbsp;&nbsp;&nbsp;<br><br>&nbsp;&nbsp; // Constructor for Circle<br>&nbsp;&nbsp;
Circle(int x, int y, int radius);<br><br>&nbsp;&nbsp;// Destructor for Circle<br>&nbsp;&nbsp;
virtual ~Circle();<br>};</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">//
Shape constructor implementation<br>Shape::Shape(int x, int y)<br>{<br>&nbsp;&nbsp; m_x =
x;<br>&nbsp;&nbsp; m_y = y;<br>}<br>// Shape destructor
implementation<br>Shape::~Shape()<br>{<br>//...<br>}</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">&nbsp;//
Circle constructor implementation<br>Circle::Circle(int x, int y, int radius) :
Shape (x, y)<br>{<br>&nbsp;&nbsp; m_radius = radius;<br>}<br><br>// Circle destructor
implementation<br>Circle::~Circle()<br>{<br>//...<br>}<br><br>// Circle override
of the pure virtual Draw method.<br>void Circle::Draw()<br>{<br>&nbsp;&nbsp;
glib_draw_circle(m_x, m_y, m_radius);<br>}</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"><br>main()<br>{<br>&nbsp;
// Define a circle with a center at (50,100) and a radius of 25<br>&nbsp; Shape
*pShape = new Circle(50, 100, 25);<br><br>&nbsp; // Define a circle with a center at
(5,5) and a radius of 2<br>&nbsp; Circle aCircle(5,5, 2);<br><br>&nbsp; // Various
operations on a Circle via a Shape pointer<br><strong>&nbsp;
//</strong>Polymorphism<br><strong>&nbsp; pShape-&gt;Draw();</strong><br><strong>&nbsp;
pShape-&gt;MoveTo(100, 100);<br><br></strong>&nbsp; pShape-&gt;Erase();<br>&nbsp; delete
pShape;<br><br>&nbsp;// Invoking the Draw method directly<br>&nbsp;
aCircle.Draw();<br>}&nbsp;&nbsp;&nbsp; </div>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 例子中使用到多态的代码以黑体标出了，它们一个很明显的特征就是通过一个基类的指针（或者引用）来调用不同子类的方法。<br>&nbsp;&nbsp;&nbsp;&nbsp;
那么，现在的问题是，这个功能是怎样实现的呢？我们可以先来大概猜测一下：对于一般的方法调用，到了汇编代码这一层次的时候，一般都是使用 Call funcaddr
这样的指令进行调用，其中funcaddr是要调用函数的地址。按理来说，当我使用指针pShape来调用Draw的时候，编译器应该将Shape::Draw的地址赋给funcaddr，然后Call
指令就可以直接调用Shape::Draw了，这就跟用pShape来调用Shape::Erase一样。但是，运行结果却告诉我们，编译器赋给funcaddr的值却是Circle::Drawde的值。这就说明，编译器在对待Draw方法和Erase方法时使用了双重标准。那么究竟是谁有这么大的法力，使编译器这个铁面无私的判官都要另眼相看呢？<strong>virtual！！<br>&nbsp;&nbsp;&nbsp;&nbsp;
</strong>Clever！！正是virtual这个关键字一手导演了这一出&#8220;乾坤大挪移&#8221;的好戏。说道这里，我们先要明确两个概念：静态绑定和动态绑定。<br>&nbsp;&nbsp;&nbsp;
1、静态绑定（static
bingding），也叫早期绑定，简单来说就是编译器在编译期间就明确知道所要调用的方法，并将该方法的地址赋给了Call指令的funcaddr。因此，运行期间直接使用Call指令就可调用到相应的方法。<br>&nbsp;&nbsp;&nbsp;&nbsp;2、动态绑定（dynamic
binding），也叫晚期绑定，与静态绑定不同，在编译期间，编译器并不能明确知道究竟要调用的是哪一个方法，而这，要知道运行期间使用的具体是哪个对象才能决定。<br>&nbsp;&nbsp;&nbsp;
好了，有了这两个概念以后，我们就可以说，virtual的作用就是告诉编译器：我要进行动态绑定！编译器当然会尊重你的意见，而且为了完成你这个要求，编译器还要做很多的事情：编译器自动在声明了virtual方法的类中插入一个指针vptr和一个数据结构VTable（vptr用以指向VTable；VTable是一个指针数组，里面存放着函数的地址），并保证二者遵守下面的规则：<br>&nbsp;&nbsp;&nbsp;
1、VTable中只能存放声明为virtual的方法，其它方法不能存放在里面。在上面的例子中，Shape的VTable中就只有Draw，MoveTo和~Shape。方法Erase的地址并不能存放在VTable中。此外，如果方法是纯虚函数，如
Draw，那么同样要在VTable中保留相应的位置，但是由于纯虚函数没有函数体，因此该位置中并不存放Draw的地址，而是可以选择存放一个出错处理的函数的地址，当该位置被意外调用时，可以用出错函数进行相应的处理。<br>&nbsp;&nbsp;&nbsp;
2、派生类的VTalbe中记录的从基类中继承下来的虚函数地址的索引号必须跟该虚函数在基类VTable中的索引号保持一致。如在上例中，如果在Shape的VTalbe中，Draw为
1 号， MoveTo 2 号，~Shape为 3 号，那么，不管这些方法在Circle中是按照什么顺序定义的，Circle的VTable中都必须保证Draw为
1 号，MoveTo为 2号。至于 3号，这里是~Circle。为什么不是~Shape啊？嘿嘿，忘啦，析构函数不会继承的。<br>&nbsp;&nbsp;&nbsp;
3、vptr是由编译器自动插入生成的，因此编译器必须负责为其进行初始化。初始化的时间选在对象创建时，而地点就在构造函数中。因此，编译器必须保证每个类至少有一个构造函数，若没有，自动为其生成一个默认构造函数。<br>&nbsp;&nbsp;&nbsp;&nbsp;
4、vptr通常放在对象的起始处，也就是Addr(obj) ==&nbsp;Addr(obj.vptr)。<br>&nbsp;&nbsp;&nbsp;
你看，天下果然没有免费的午餐，为了实现动态绑定，编译器要为我们默默干了这么多的脏话累活。如果你想体验一下编译器的辛劳，那么可以尝试用C语言模拟一下上面的行为，【1】中就有这么一个例子。好了，现在万事具备，只欠东风了。编译，连接，载入，GO！当程序执行到
<strong>pShape-&gt;Draw()</strong>的时候，上面的设施也开始起作用了。。<br>&nbsp;&nbsp;&nbsp;
前面已经提到，晚期绑定时之所以不能确定调用哪个函数，是因为具体的对象不确定。好了，当运行到<strong>pShape-&gt;Draw()</strong>时，对象出来了，它由pShape指针标出。我们找到这个对象后，就可以找到它里面的vptr（在对象的起始处），有了vptr后，我们就找到了VTable，调用的函数就在眼前了。。等等，VTable中方法那么多，我究竟使用哪个呢？不用着急，编译器早已为我们做好了记录：编译器在创建VTable时，已经为每个virtual函数安排好了座次，并且把这个索引号记录了下来。因此，当编译器解析到<strong>pShape-&gt;Draw()</strong>的时候，它已经悄悄的将函数的名字用索引号来代替了。这时候，我们通过这个索引号就可以在VTable中得到一个函数地址，Call
it！<br>&nbsp;&nbsp;&nbsp;
在这里，我们就体会到为什么会有第二条规定了，通常，我们都是用基类的指针来引用派生类的对象，但是不管具体对象是哪个派生类的，我们都可以使用相同的索引号来取得对应的函数实现。<br>&nbsp;&nbsp;&nbsp;&nbsp;
现实中有一个例子其实跟这个蛮像的：报警电话有110，119，120（VTable中不同的方法）。不同地方的人拨打不同的号码所产生的结果都是不一样的。譬如，在三环外的一个人（具体对象）跟一环内的一个人（另外一个具体对象）打119，最后调用的消防队肯定是不一样的，这就是多态了。这是怎么实现的呢，每个人都知道一个报警中心（VTable，里面有三个方法
110，119，120）。如果三环外的一个人需要火警抢险（一个具体对象）时，它就拨打119，但是他肯定不知道最后是哪一个消防队会出现的。这得有报警中心来决定，报警中心通过这个具体对象（例子中就是具体位置了）以及他说拨打的电话号码（可以理解成索引号），报警中心可以确定应该调度哪一个消防队进行抢险（不同的动作）。<br>&nbsp;&nbsp;&nbsp;&nbsp;
这样，通过vptr和VTable的帮助，我们就实现了C++的动态绑定。当然，这仅仅是单继承时的情况，多重继承的处理要相对复杂一点，下面简要说一下最简单的多重继承的情况，至于虚继承的情况，有兴趣的朋友可以看看
Lippman的《Inside the C++ Object
Model》，这里暂时就不展开了。（主要是自己还没搞清楚，况且现在多重继承都不怎么使用了，虚继承应用的机会就更少了）<br>&nbsp;&nbsp;&nbsp;&nbsp;
首先，我要先说一下多重继承下对象的内存布局，也就是说该对象是如何存放本身的数据的。</p>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">class
Cute<br>{<br>public:<br>&nbsp;int i;<br>&nbsp;virtual void cute(){ cout&lt;&lt;"Cute
cute"&lt;&lt;endl; }<br>};</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">class
Pet<br>{<br>public:<br>&nbsp;&nbsp; int j;<br>&nbsp;&nbsp; virtual void say(){ cout&lt;&lt;"Pet
say"&lt;&lt;endl;&nbsp; }<br>};</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">class
Dog : public Cute,public Pet<br>{<br>public:<br>&nbsp;int z;<br>&nbsp;void cute(){
cout&lt;&lt;"Dog cute"&lt;&lt;endl; }<br>&nbsp;void say(){ cout&lt;&lt;"Dog
say"&lt;&lt;endl;&nbsp; }<br>};</div>
<p>&nbsp;&nbsp;&nbsp; 在上面这个例子中，一个Dog对象在内存中的布局如下所示：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p>
<table style="border: medium none ; border-collapse: collapse;" class="MsoTableGrid" border="1" cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td style="border: 1pt solid windowtext; padding: 0cm 5.4pt; width: 59.4pt;" width="79">
            <p style="margin: 0cm 0cm 0pt; text-align: center;" class="MsoNormal" align="center"><strong><span lang="en">Dog</span></strong></p>
            </td>
        </tr>
        <tr>
            <td style="border-style: none solid solid; border-color: #ece9d8 windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; background-color: transparent; width: 59.4pt;" width="79" valign="top">
            <p style="margin: 0cm 0cm 0pt;" class="MsoNormal"><span lang="en">Vptr1</span></p>
            </td>
        </tr>
        <tr>
            <td style="border-style: none solid solid; border-color: #ece9d8 windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; background-color: transparent; width: 59.4pt;" width="79" valign="top">
            <p style="margin: 0cm 0cm 0pt;" class="MsoNormal"><span lang="en">Cute::i</span></p>
            </td>
        </tr>
        <tr>
            <td style="border-style: none solid solid; border-color: #ece9d8 windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; background-color: transparent; width: 59.4pt;" width="79" valign="top">
            <p style="margin: 0cm 0cm 0pt;" class="MsoNormal"><span lang="en">Vptr2</span></p>
            </td>
        </tr>
        <tr>
            <td style="border-style: none solid solid; border-color: #ece9d8 windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; background-color: transparent; width: 59.4pt;" width="79" valign="top">
            <p style="margin: 0cm 0cm 0pt;" class="MsoNormal"><span lang="en">Pet::j</span></p>
            </td>
        </tr>
        <tr>
            <td style="border-style: none solid solid; border-color: #ece9d8 windowtext windowtext; border-width: medium 1pt 1pt; padding: 0cm 5.4pt; background-color: transparent; width: 59.4pt;" width="79" valign="top">
            <p style="margin: 0cm 0cm 0pt;" class="MsoNormal"><span lang="en">Dog::z</span></p>
            </td>
        </tr>
    </tbody>
</table>
<br>&nbsp;&nbsp;&nbsp;&nbsp;
也就是说，在Dog对象中，会存在两个vptr,每一个跟所继承的父类相对应。如果我们要想实现多态，就必须在对象中准确地找到相应的vptr，以调用不同的方法。但是，如果根据单继承时的逻辑，也就是vptr放在指针指向位置的起始处，那么，要在多重继承情况下实现，我们必须保证在将一个派生类的指针隐式或者显式地转换成一个父类的指针时，得到的结果指向相应派生类数据在Dog对象中的起始位置。幸好，这工作编译器已经帮我们完成了。上面的例子中，如果Dog向上转换成Pet的话，编译器会自动计算Pet数据在Dog对象中的偏移量，该偏移量加上Dog对象的起始位置，就是Pet数据的实际地址了。</p>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">int
main()<br>{<br>&nbsp;Dog* d = new Dog();<br>&nbsp;cout&lt;&lt;"Dog object addr :
"&lt;&lt;d&lt;&lt;endl;<br>&nbsp;Cute* c = d;<br>&nbsp;cout&lt;&lt;"Cute type addr :
"&lt;&lt;c&lt;&lt;endl;<br>&nbsp;Pet* p = d;<br>&nbsp;cout&lt;&lt;"Pet type addr :
"&lt;&lt;p&lt;&lt;endl;<br>&nbsp;delete d;<br>}<br></div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">output:<br>Dog
object addr : 0x3d24b0<br>Cute type addr : 0x3d24b0<br>Pet type addr :
0x3d24b8&nbsp;&nbsp; // 正好指向Dog对象的vptr2处，也就是Pet的数据<br></div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
好了，既然编译器帮我们自动完成了不同父类的地址转换，我们调用虚函数的过程也就跟单继承统一起来了：通过具体对象，找到vptr（通常指针的起始位置，因此Cute找到的是vptr1，而Pet找到的是vptr2），通过vptr，我们找到VTable，然后根据编译时得到的VTable索引号，我们取得相应的函数地址，接着就可以马上调用了。<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
在这里，顺便也提一下两个特殊的方法在多态中的特别之处吧：第一个是构造函数，在构造函数中调用虚函数是不会有多态行为的，例子如下：</p>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">class
Pet<br>{<br>public:<br>&nbsp;&nbsp; Pet(){ sayHello(); }<br>&nbsp;&nbsp; void say(){ sayHello();
}<br><br>&nbsp;&nbsp; virtual void sayHello()<br>&nbsp;&nbsp; {<br>&nbsp;&nbsp; &nbsp;&nbsp;cout&lt;&lt;"Pet
sayHello"&lt;&lt;endl;<br>&nbsp;&nbsp; }<br>&nbsp;&nbsp; <br>};</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">class
Dog : public Pet<br>{<br>public:<br>&nbsp;&nbsp; Dog(){};<br>&nbsp;&nbsp; void sayHello()<br>&nbsp;&nbsp;
{<br>&nbsp;&nbsp; &nbsp;&nbsp;cout&lt;&lt;"Dog sayHello"&lt;&lt;endl;<br>&nbsp;&nbsp; }<br>};</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">int
main()<br>{<br>&nbsp;Pet* p = new Dog();<br>&nbsp;p-&gt;sayHello();<br>&nbsp;delete
p;<br>}</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">output:<br>Pet
sayHello //直接调用的是Pet的sayHello()<br>Dog sayHello //多态<br></div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;
第二个就是析构函数，使用多态的时候，我们经常使用基类的指针来引用派生类的对象，如果是动态创建的，对象使用完后，我们使用delete来释放对象。但是，如果我们不注意的话，会有意想不到的情况发生。</p>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">class
Pet<br>{<br>public:<br>&nbsp;&nbsp; ~Pet(){ cout&lt;&lt;"Pet destructor"&lt;&lt;endl;&nbsp;
}<br>&nbsp;&nbsp;//virtual ~Pet(){ cout&lt;&lt;"Pet virtual destructor"&lt;&lt;endl;&nbsp;
}<br>};</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">class
Dog : public Pet<br>{<br>public:<br>&nbsp;&nbsp; ~Dog(){ cout&lt;&lt;"Dog
destructor"&lt;&lt;endl;};<br>&nbsp;&nbsp;&nbsp;//virtual ~Dog(){ cout&lt;&lt;"Dog virtual
destructor"&lt;&lt;endl;&nbsp; }<br>};</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">int
main()<br>{<br>&nbsp;Pet* p = new Dog();<br>&nbsp;delete p;<br>}</div>
<div style="border: 1px solid #cccccc; margin: 5px 20px; padding: 5px; background: #f3f3f3 none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;">output：<br>Pet
destructor&nbsp; //糟了，Dog的析构函数没有调用，memory
leak!<br><br>如果我们将析构函数改成virtual以后，结果如下<br>Dog virtual destructor<br>Pet virtual
destructor&nbsp;&nbsp; // That's OK!</div>
<p>&nbsp;&nbsp;&nbsp; 所以，如果一个类设计用来被继承的话，那么它的析构函数应该被声明为virtual的。</p><img src ="http://www.cppblog.com/LittleStar/aggbug/99063.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-10-20 21:36 <a href="http://www.cppblog.com/LittleStar/archive/2009/10/20/99063.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转】不当使用memset函数带来的麻烦问题</title><link>http://www.cppblog.com/LittleStar/archive/2009/10/20/99061.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Tue, 20 Oct 2009 13:11:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/10/20/99061.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/99061.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/10/20/99061.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/99061.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/99061.html</trackback:ping><description><![CDATA[通常在C的编程中，我们经常使用memset函数将一块连续的内存区域清零或设置为其它指定的值，最近在移植一段java代码到C++的时候，不当使用memset函数花费了我几个小时的调试时间。对于虚函数的底层机制很多<a href="http://action.utops.cc/click.jsp?adsId=249&amp;adsLeagueId=5&amp;adsUserId=188&amp;siteId=57&amp;siteLeagueId=5&amp;siteUserId=47&amp;scId=2&amp;adsType=2&amp;prices=0.8&amp;checkCode=1da859540f&amp;click=1&amp;url=http%3A//www.sasacity.com/sports-brand-60.html/%3Futm_source%3Dhp_banner&amp;v=0&amp;keyword=%u8D44%u6599&amp;s=http%3A//www.xker.com/page/e2009/0215/69251.html&amp;rn=884032" style="font-size: 1em;" id="vad_3" class="vLink9999" onmouseover="if(typeof(showTitle)!='undefined'){this.title='';window.clearTimeout(hideTO);showTitle(event, this, 3,'');}" title="" onmouseout="if(typeof(showTitle)!='undefined'){mouseIsOverLayer = false; mouseoverwhileload =" false;="" hideto=" window.settimeout('checkifmouseoverlayer()',500);}" onclick="" target="_blank">资料</a>都有较详细<a href="http://action.utops.cc/click.jsp?adsId=231&amp;adsLeagueId=5&amp;adsUserId=188&amp;siteId=57&amp;siteLeagueId=5&amp;siteUserId=47&amp;scId=2&amp;adsType=98&amp;prices=1.1&amp;checkCode=c3ac468370&amp;click=1&amp;url=http%3A//www.apple.com.cn/&amp;v=0&amp;keyword=%u9610%u8FF0&amp;s=http%3A//www.xker.com/page/e2009/0215/69251.html&amp;rn=846935" style="font-size: 1em;" id="vad_1" class="vLink9999" onmouseover="if(typeof(showTitle)!='undefined'){this.title='';window.clearTimeout(hideTO);showTitle(event, this, 1,'');}" title="iphone @utops.cc" onmouseout="if(typeof(showTitle)!='undefined'){mouseIsOverLayer = false; mouseoverwhileload =" false;="" hideto=" window.settimeout('checkifmouseoverlayer()',500);}" onclick="" target="_blank">阐述</a>，但对我个人而言，这次的调试让我感触颇深。
<br><br>先来看一段代码，在继承的类Advance之中，有很多属性字段，我<a href="http://action.utops.cc/click.jsp?adsId=240&amp;adsLeagueId=5&amp;adsUserId=188&amp;siteId=57&amp;siteLeagueId=5&amp;siteUserId=47&amp;scId=2&amp;adsType=2&amp;prices=1.32&amp;checkCode=5409038f93&amp;click=1&amp;url=http%3A//www.jiayuan.com/parties/succdjs/&amp;v=0&amp;keyword=%u5E0C%u671B&amp;s=http%3A//www.xker.com/page/e2009/0215/69251.html&amp;rn=192199" style="font-size: 1em;" id="vad_0" class="vLink9999" onmouseover="if(typeof(showTitle)!='undefined'){this.title='';window.clearTimeout(hideTO);showTitle(event, this, 0,'');}" title="" onmouseout="if(typeof(showTitle)!='undefined'){mouseIsOverLayer = false; mouseoverwhileload =" false;="" hideto=" window.settimeout('checkifmouseoverlayer()',500);}" onclick="" target="_blank">希望</a>将其清成0或NULL，于是在构造函数中我通过memset将当前类的所有属性置0。 <br><br>class Base{
<br><br>public: <br><br>virtual void kickoff() = 0; <br><br>};
<br>class Advance:public Base{ <br><br>public: <br><br>Advance(){
<br><br>memset(this, 0, sizeof(Advance)); <br><br>} <br><br>void kickoff(){
<br><br>count++; <br><br>//... do something else; <br><br>} <br><br>private:
<br><br>int attr1, attr2; <br><br>char* label; <br><br>int count; <br><br>//...
other attributes, they should be initiated to 0 or NULL at beginning. <br><br>};
<br><br>int _tmain(int argc, _TCHAR* argv[]) <br><br>{
<br>Base* ptr = new Advance(); <br>ptr-&gt;kickoff();
<br>return 0; <br>} <br><br>这样看似能正常运行，但运行程序时，你会发现类似于下面的错误：
<br><br>TestVirtual.exe 中的 0x00415390 处未处理的异常: 0xC0000005: 读取位置 0x00000000
时发生访问冲突
<br><br>同时断点停留在ptr-&gt;kickoff()处，从错误提示我们可以得知无法调用kickoff方法，这个方法的指针没有被正确初始化，但为什么呢？
<br><br>指出问题之前，先看看这段文献上的关于虚函数机制的说明： <br><br>函数赖以生存的底层机制：vptr +
vtable。虚函数的运行时实现采用了VPTR/VTBL的形式，这项<a href="http://action.utops.cc/click.jsp?adsId=123&amp;adsLeagueId=5&amp;adsUserId=1&amp;siteId=57&amp;siteLeagueId=5&amp;siteUserId=47&amp;scId=2&amp;adsType=2&amp;prices=0.81&amp;checkCode=eb592d2efd&amp;click=1&amp;url=http%3A//www.totole.com.cn/&amp;v=0&amp;keyword=%u6280%u672F&amp;s=http%3A//www.xker.com/page/e2009/0215/69251.html&amp;rn=553884" style="font-size: 1em;" id="vad_2" class="vLink9999" onmouseover="if(typeof(showTitle)!='undefined'){this.title='';window.clearTimeout(hideTO);showTitle(event, this, 2,'');}" title="%u7EC6%u6570%u7231%u4E0A%u53A8%u623F%u7684%u7406%u7531 @utops.cc" onmouseout="if(typeof(showTitle)!='undefined'){mouseIsOverLayer = false; mouseoverwhileload =" false;="" hideto=" window.settimeout('checkifmouseoverlayer()',500);}" onclick="" target="_blank">技术</a>的基础：
<br>①编译器在后台为每个包含虚函数的类产生一个静态函数指针数组（虚函数表），在这个类或者它的基类中定义的每一个虚函数都有一个相应的函数指针。
<br>②每个包含虚函数的类的每一个实例包含一个不可见的数据成员vptr（虚函数指针），这个指针被构造函数自动初始化，指向类的vtbl（虚函数表）
<br>③当客户调用虚函数的时候，编译器产生代码反指向到vptr，索引到vtbl中，然后在指定的位置上找到函数指针，并发出调用。
<br><br>这里的问题，就出在 <br><br>memset(this, 0, sizeof(Advance));
<br><br>上面，虚函数指针应该在进入构造函数赋值体之前自动初始化的，而memset却又将已经初始化好的指针清0了，这就是为什么会产生上面的访问零址的错误。将上面的memset语句去除程序就可以正常运行了。
<br><br>所以，从上面的问题中，我们可以看出在构造函数体内调用memset将整个对象清0是很有风险的，当没有虚函数的时候上面程序可以正常运行（可以试着将Base类的纯虚函数声明改成非虚函数再运行程序）。初始化类的属性对象时，比较稳妥的办法还是手动逐个进行初使化 <img src ="http://www.cppblog.com/LittleStar/aggbug/99061.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-10-20 21:11 <a href="http://www.cppblog.com/LittleStar/archive/2009/10/20/99061.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>shadow map   阴影图算法的思考</title><link>http://www.cppblog.com/LittleStar/archive/2009/10/13/98538.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Tue, 13 Oct 2009 15:34:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/10/13/98538.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/98538.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/10/13/98538.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/98538.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/98538.html</trackback:ping><description><![CDATA[<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shadow map 以前早就研究过，不过一次不小心把以前做的东西都弄丢了，今天重新做了一下，<br>加到了系统里，给大家看下效果：）<br><br><img alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/shadowmap.JPG" width="640" height="480"><br><br>shadow map算法原理很简单,先简单介绍下算法给新人：<br>1.以光源所在位置为观察点渲染场景（可以只渲染需要产生阴影的物体）将渲染后的深度值保存深度图（一张事先准备好的纹理）。<br>在此步需要注意的是 此次渲染用到的模型观视投影矩阵（以后简称mvp）需要保存一下，下一步要用到。<br>2.正常渲染场景，将顶点坐标乘以步骤一时候的mvp，将坐标变换到以光源为观察点的坐标系里，比较z值和从深度图中读出来的<br>值得大小判断遮挡，有遮挡的话将输出颜色减弱或者换成别的随笔你了。其中有个地方需要注意，如何从深度图纹理中读数据，<br>这个我是这么解决的：float2 suv = ((spos.xy/spos.z))//其中spos是变换到光源坐标系下的顶点数据，得到的suv经过处理后可以<br>当做深度图的纹理坐标值，读取方法为float4 shadow = tex2D(t11,(suv+1.0)*0.5)，其中用到一个【-1，1】到【0，1】的变换。<br>剩下的就是比较了&nbsp; ： <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float sz =&nbsp; 1 - spos.z/(gDepthSize)；//将深度值变换到【0，1】区间，gDepthSize是获得深度纹理时渲染场景的最深值 &nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //增加阴影<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if((sz &lt; shadow.x))//sz是就是<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; color = color*(1 - shadow);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; color.w = 1.0;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //color = float4(sz,sz,sz,1);<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br>//----------------------------------------------------------------------------------------------------------------------------<br>关于shadow map 算法的缺点,跟大家讨论一下:<br>永远的困扰shadow map的失真问题，当光源照射场景稍大的时候失真现象就会很严重，有些改进算法，但都觉得治标不治本。<br>如果说我整个场景有很多到处跑的人，那他们的阴影效果要怎样做呢？？？<br><br>感觉shadow map用在生成当前角色的阴影挺好，如果是大范围的不大适合。很多静态的物体可以先把阴影事先计算好，用的时候<br>直接读取，没有必要每帧都重新计算。<br><br>//----------------------------------------------------------------------------------------------------------------------------<br>shadow map 最大的好处是可以处理透明纹理的阴影，以为我的场景的树是用透明纹理画上去的，如果得到的阴影是个矩<br>形那就很怪了，幸好shadow map 没有这个问题！！！<br><br><br><br><img src ="http://www.cppblog.com/LittleStar/aggbug/98538.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-10-13 23:34 <a href="http://www.cppblog.com/LittleStar/archive/2009/10/13/98538.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>vertex shader  texture  访问纹理的方法</title><link>http://www.cppblog.com/LittleStar/archive/2009/10/13/98452.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Tue, 13 Oct 2009 01:02:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/10/13/98452.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/98452.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/10/13/98452.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/98452.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/98452.html</trackback:ping><description><![CDATA[<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 今天考虑程序的优化问题，突然想到说现在vetex shader已经可以访问纹理资源了，可以把访问高度图的操作转移<br>到vetex shader中去做计算，GPU访问纹理的速度要比CPU访问内存快多了吧（我是这么认为的）。不过遇见一个问题。<br>用tex2D访问出来的值怪怪的，不是我要的高度纹理值，后来发现如果fragment shader里面访问了别的纹理会影响到vetex里<br>面的，上网google了一下，好多人都说得用 tex2DLod才行，试了下一点好转的迹象都没有，又有人说纹理得是float的，参照<br>着改了下，还是不行。（很多时候在网上查到的都不好用，当然也有好用的，有点废话，哈哈）<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 后来突然发现我的sampler都没有跟寄存器绑定，绑定了以后就好了，很奇怪，难道说只调用cgGLSetTextureParameter<br>并不能实现纹理的绑定？&nbsp;&nbsp; 现在我把所有sampler都跟一个寄存器绑定后就一切正常了。<br><br>就像这样：<br>//--------------------------------------------------------------------------------<br>sampler2D t10 :TEXUNIT0;//地面高度图,&nbsp;&nbsp;&nbsp; 其他的sampler也需要绑定到寄存器才会好用<br><br>OutPut xS_v(float4 ipos:POSITION,<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; float2 tex:TEXCOORD0,<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; float3 normal:NORMAL)<br>{<br>&nbsp;&nbsp;&nbsp; OutPut OUT;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //out是关键字<br>&nbsp;&nbsp;&nbsp; ipos.y =tex2D(t10,tex).z*256;<br>//---------------------------------------------------------------------------------<br><br><br>cg语言运行出来的结果经常怪怪的，有一次我故意写错了一句话，结果编译也能通过，只是显示出来的完全不<br>是我想要的，有没有高人指点一下，多谢！！<br><br><br>不过把访问高度图改到用GPU访问纹理后，效果还是很明显的，速度提升了差不多一倍。<br><img alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/gaileshude.JPG" width="647" height="487"><br><br><br><br>  <img src ="http://www.cppblog.com/LittleStar/aggbug/98452.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-10-13 09:02 <a href="http://www.cppblog.com/LittleStar/archive/2009/10/13/98452.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>水面效果图  应用了动态纹理，法线扰动，反射纹理</title><link>http://www.cppblog.com/LittleStar/archive/2009/10/12/98336.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Sun, 11 Oct 2009 16:48:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/10/12/98336.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/98336.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/10/12/98336.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/98336.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/98336.html</trackback:ping><description><![CDATA[<br>最近的工作：<br><br>1.实现了刀客的动作控制。（md2文件，GPU实现帧动画）。<br>2.试验了水面效果（有反射纹理映射，水面法线贴图计算光照和扰动）。<br>3.试验了billboard效果。<br>4.修改程序中的一些bug。<br><br>有两个个问题：<br>1.OpenGL如何提高效率，欢迎指教！<br>2.flt文件如何简化，有好用的工具么，我的都是从3ds导过来的，面数太多了，不大适用。<br><br>发几张孬图，贻笑大方！<br><br><img alt="" src="http://www.cppblog.com/images/cppblog_com/littlestar/shuimian.JPG" width="647" height="492"><br><br>感觉水面反射加了树的倒影反而变得怪怪的！！<br><br>再来几张前几天截得图<br><img alt="" src="http://www.cppblog.com/images/cppblog_com/littlestar/kongzhonghuayuan.JPG" width="644" height="487"><br><br>这是我的小花园，大树的纹理烂透了。<br><br><img alt="" src="http://www.cppblog.com/images/cppblog_com/littlestar/xiaohe.JPG" width="642" height="487"><br><br><br>小河流水 哗啦啦&#8230;&#8230;<br><br><br><br><img alt="" src="http://www.cppblog.com/images/cppblog_com/littlestar/meijiadimiande.JPG" width="647" height="487"><br><br>去掉地形的效果，感觉还蛮漂亮的，呵呵<br><br>大家有什么看法尽量留言，您的关注就是对我最大的帮助！！<br><br>  <img src ="http://www.cppblog.com/LittleStar/aggbug/98336.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-10-12 00:48 <a href="http://www.cppblog.com/LittleStar/archive/2009/10/12/98336.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Parallax Mapping 效果图 很烂的效果图 </title><link>http://www.cppblog.com/LittleStar/archive/2009/09/25/97192.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Thu, 24 Sep 2009 19:33:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/09/25/97192.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/97192.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/09/25/97192.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/97192.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/97192.html</trackback:ping><description><![CDATA[<h1 class="title_txt">normal map<br></h1>
<img alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/normalMapping1.JPG" width="646" height="511"><br><br>
<h1 class="title_txt">Parallax Mapping</h1>
<img alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/ParallaxMapping1.JPG" width="647" height="514"><br><br>对应的网格图<br><br><img alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/ParallaxMapping1mesh.JPG" width="645" height="513"><br><br>趁着人少发两张等着挨砖头的效果图。<br><br>看了一天的资料，证实了原来的想法是对的。<br>normal map&nbsp; 是 bump map 的改进，主要是GPU可编程为其创造了条件。<br>parallax map 是 normal map 的改进版本，相当于是对normal map 的修正，没什么心意。<br><br>Displacement Mapping貌似挺牛，明天仔细研究下 ：-）<br><img src ="http://www.cppblog.com/LittleStar/aggbug/97192.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-09-25 03:33 <a href="http://www.cppblog.com/LittleStar/archive/2009/09/25/97192.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Texture Blending &amp;&amp; Phone Model &amp;&amp; Roam Terrain  Utilize the Cg  to realize</title><link>http://www.cppblog.com/LittleStar/archive/2009/09/24/97084.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Wed, 23 Sep 2009 18:09:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/09/24/97084.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/97084.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/09/24/97084.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/97084.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/97084.html</trackback:ping><description><![CDATA[<img alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/roam10000.JPG" width="649" height="515"><br><br>先来张调整了参数的roam网格图片，把面片数约束在1w，这下roam的效果就明显了吧！<br>发现地势如果不是特别平坦的话，很难找到一个满意的参数，既能减少面片数，又能取得很好的显示效果。<br>如果有大片大片的平地，那就简单了。<br><br><img alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/roamtexture.JPG"><br><br>这是使用cg语言实现的 texture blend 使用四张纹理加一个细节纹理混合而成，<br>增加了像素级的 Phone 光照模型。光照的颜色有点怪怪的。<br><br><img src ="http://www.cppblog.com/LittleStar/aggbug/97084.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-09-24 02:09 <a href="http://www.cppblog.com/LittleStar/archive/2009/09/24/97084.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>改进的roam terrain 效果图</title><link>http://www.cppblog.com/LittleStar/archive/2009/09/21/96813.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Sun, 20 Sep 2009 17:00:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/09/21/96813.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/96813.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/09/21/96813.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/96813.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/96813.html</trackback:ping><description><![CDATA[<img alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/roam1.JPG" width="648" height="513"><br><br>面片数跟帧数永远的让人很矛盾。这样以后突变就很不明显了，肉眼几乎发现不了，不过感觉有退化成普通的lod的趋势。<br>当然，远处的网格还是会有跳动现象，不过那已经很远了，一般不会有人注意到发生了什么。<br>可是这样大大增加了网格数量，应用下视景体剪除效果会比这个好很多吧！<br><br><img src ="http://www.cppblog.com/LittleStar/aggbug/96813.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-09-21 01:00 <a href="http://www.cppblog.com/LittleStar/archive/2009/09/21/96813.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于 roam terrain 的疑问</title><link>http://www.cppblog.com/LittleStar/archive/2009/09/20/96742.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Sat, 19 Sep 2009 16:29:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/09/20/96742.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/96742.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/09/20/96742.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/96742.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/96742.html</trackback:ping><description><![CDATA[<img alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/roam.JPG" width="498" height="456"><br><br>这个是我实现出来的 roam terrain 地形<br>问题是，当我在地形上漫游时，网格必然会发生变化。不同层次的网格之间跳跃的很厉害，让人觉得地形很不真实，<br>有没有人有好的办法能解决？<br><br><img src ="http://www.cppblog.com/LittleStar/aggbug/96742.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-09-20 00:29 <a href="http://www.cppblog.com/LittleStar/archive/2009/09/20/96742.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>简易飞行仿真 —— 入侵我的小镇</title><link>http://www.cppblog.com/LittleStar/archive/2009/09/17/96480.html</link><dc:creator>Little Star</dc:creator><author>Little Star</author><pubDate>Wed, 16 Sep 2009 16:35:00 GMT</pubDate><guid>http://www.cppblog.com/LittleStar/archive/2009/09/17/96480.html</guid><wfw:comment>http://www.cppblog.com/LittleStar/comments/96480.html</wfw:comment><comments>http://www.cppblog.com/LittleStar/archive/2009/09/17/96480.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cppblog.com/LittleStar/comments/commentRss/96480.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LittleStar/services/trackbacks/96480.html</trackback:ping><description><![CDATA[1.测试一下 flt文件读取和现实效果。<br>2.测试下引擎框架，寻找bug。<br><img style="width: 890px; height: 506px;" alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/flightsimulation.JPG"><br><br>这张是刚开始时没加载上外部文件时的<br><br><img style="width: 889px; height: 437px;" alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/bomb.JPG"><br><br>这张是爆炸效果图<br><img style="width: 881px; height: 476px;" alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/fire.JPG"><br><br>这张是着火效果<br><br><img style="width: 883px; height: 595px;" alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/daodan.JPG"><br><br>被导弹追击<br><br>最后来张夜色美景<br><br><img style="width: 869px; height: 470px;" alt=""  src="http://www.cppblog.com/images/cppblog_com/littlestar/night.JPG"><br><br><br>诚盼牛人指点不足！<br><img src ="http://www.cppblog.com/LittleStar/aggbug/96480.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LittleStar/" target="_blank">Little Star</a> 2009-09-17 00:35 <a href="http://www.cppblog.com/LittleStar/archive/2009/09/17/96480.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>