实用级反主动防御rootkit设计思
作者:白远方 (ID: baiyuanfan, baiyuanfan@163.com, baiyuanfan@hotmail.com)June 18, 2007
关键字:rootkit,反主动防御,网络监控,ring0,mcafee8.5i,KIS6,ZoneAlarm Pro,实用级产品测试
目录:
反主动防御rootkit的产生背景及其必要性
反网络访问主动防御
反API钩子进程行为主动防御
反系统Notify进程行为主动防御
绕过监控进入ring0安装驱动
实用级反主动防御rootkit的通用性问题
反主动防御rootkit的产生背景及其必要性
当前随着新型木马,病毒,间谍软件对网络安全的威胁日益加重,传统的特征查杀型的安全产品和简单的封包过滤型防火墙已不能有效保护用户,因此各大安全公司纷纷推出自己的主动防御型安全产品,例如卡巴斯基kis6,mcafee8.5i,ZoneAlarm Pro等,这些产品应对未知的病毒木马都有很好的效果,若非针对性的作过设计的木马和rootkit,根本无法穿越其高级别防御。因此,反主动防御技术,作为矛和盾的另一方,自然被渗透者们提上日程;由于主动防御安全产品的迅速普及,为了不使后门木马被弹框报警,具有反主动防御能力的rootkit成为了一种必然选择。
反网络访问主动防御
几乎现在每个防火墙都具有应用程序访问网络限制功能。一个未知的程序反弹连接到外网,或者是在本地监听端口,基本上都会引起报警。而且对系统进程的行为也有了比较严格的审查,原先的注射代码到winlogon等系统进程,在向外反弹连接的方法,很多主动防御软件都会阻止了。
很多防火墙的应用程序访问网络限制,都可以通过摘除tcpip.sys上面的过滤驱动,并还原tcpip.sys的Dispatch Routines来绕过。据称这是因为在ndis层次取得进程id不方便而导致的。但是如果在一个实用级的rootkit里应用此方法则是不智之举,因为存在部分防火墙,如ZoneAlarm,其ndis过滤层必须和tdi过滤层协同工作,才会放行网络连接。至于ndis层次的中间层驱动的摘除,和NDIS_OPEN_BLOCK的还原,则是一项不太可能完成的任务,因为无法从原始文件中读取的方法,获得NDIS_OPEN_BLOCK的原始值;即使能够成功恢复ndis钩子,也不能保证系统可以正常运行,很可能会出现各种不明症状。
到现在为止,绕过应用程序访问网络限制最好的选择,还是那两个:简单的一个,注射代码到一个ie进程,用它反弹连接出来,访问外网;复杂的选择则是应用内核驱动,如ndis hook/添加新的ndis protocol,来实现端口复用,或者使用tdi client driver反弹连接。已经有很多木马和rootkit使用前者,因其简单易行,在实际开发中工程量小,出现问题的可能性也少得多,产品成熟的时间代价也小。但是目前很多的主动防御已经注意到这一点,并且在程序行为监控中严密防范了其他程序对ie的感染行为。
反API钩子进程行为主动防御
接下来是主动防御系统的很重要的一部分:进程行为监控。该部分主动防御软件一般通过两种解决方案来执行,一是API钩子,二是windows支持的notify routine。
大量的主动防御安全软件,如KIS6,ZoneAlarm Pro,使用API钩子来监控进程的危险行为。如注射远程线程,启动傀儡IE,加载驱动,注册服务,修改敏感系统注册表键值等。但是作为一个rootkit,完全绕过这些操作,基本上是不可能的;于是摆放在面前的任务,就是如何击败这种主动防御。
对于特定种类的监控,总是有特定的方法可以绕过。比如注射远程线程,如果常用的CreateRemoteThread被监控了,可以尝试采用Debug API, SetThreadContext的方法绕过,也可以尝试采用hook其ntdll!ZwYieldExecution等频繁调用的函数来装载自己的DLL模块。 注册表监控,我的朋友xyzreg曾经写过系列文章,提出了很多种方法,包括RegSaveKey, Hive编辑等方法绕过卡巴斯基的注册表监控,其Hive编辑的方法目前仍未能有任何主动防御系统拦截。
但是从一个通用型,为实战设计的实用型rootkit来说,采用这些特定的技术并不是一个非常好的选择;因为这些技术可以保证对付一个主动防御软件,却不能保证通用,甚至通用性很差。而且针对每一个可能被主动防御拦截的行为,都采用一套特定的绕过技术,从工程代价上来讲,太过巨大,开发耗时,等其成熟更是不知道要多少时间来测试和更改。因此我们需要的一个相对涵盖范围广,能够解决绝大多数主动防御技术的解决方案。
针对API钩子实现的进程行为监控,一个较好的通用解决方案就是卸载所有安全软件所安装的API钩子。为兼容性和稳定起见,几乎所有的安全软件在安装API钩子时都会选择hook SSDT表,例如KIS6,ZoneAlarm Pro。我们如果能够进入ring0,就可以使用一个驱动程序,读取系统文件ntoskrnl.exe/ntkrnlpa.exe/ntkrpamp.exe,从中提出我们所希望的SSDT表的原始函数地址,替换被安全软件hook的地址,用此方法可以通用性很好的解决绝大多数的API钩子实现的进程行为监控。不过此方法有一个前提,就是事先必须绕过监控进入ring0。关于如何实现此前提,请阅读第五部分,“绕过监控进入ring0安装驱动”。 ZoneAlarm Pro更改了大量的SSDT函数地址来监控程序行为。
反系统Notify进程行为主动防御
部分主动防御安全软件不仅仅是用API钩子,同时使用了微软提供的Notify Routine,来监视进程的行为。使用该技术的安全软件不是太多,但是也不至于少到一个实用级别rootkit可以忽略的程度。
以下几个微软DDK函数,PsSetCreateProcessNotifyRoutine,PsSetCreateThreadNotifyRoutine,PsSetLoadImageNotifyRoutine,被用作支持主动防御软件监控新进程的建立,新线程的建立,和一个新的模块被加载。处理该种类型的防御不能简单的清空NotifyRoutine就完事,因为系统本身,还有一些第三方正常模块和驱动,可能添加和使用该链表。
解决方案,一是可以先将使用了该技术的主动防御系统的驱动程序模块做一个列表出来,然后遍历这三条链表,找出地址指向这些驱动模块的项,再将这些项删除脱链。但是这需要对大量主动防御系统的研究和测试,并且通用型也不好。第二种方法,由于Notify Routine的监控力度要远弱于API钩子,因此在纯ring3将程序做一些小的改动,就可以越过这种类型的监控。
另外还有几个SDK函数,可以提供对文件和注册表的更改的notify。不能排除也有部分主动防御软件使用了它们。例如国产的超级巡警(AST.exe),使用了RegNotifyChangeKeyValue,做了对注册表敏感键值修改的事后警告提示。如果仅仅使用了API钩子清除技术,那么在此时就会被AST报警。和以上介绍的三个内核notify类似的也是,有不少正常的notify在被使用,不分青红皂白的全部卸载,会导致系统异常。
因此可见,Notify类监控虽然使用的不多,但是其对付的难度和需要的工程量,比API监控还要大。已经处理了API钩子监控的rootkit仍然被notify方式的AST报警。
绕过监控进入ring0安装驱动
这部分是重中之重。由于几乎每个主动防御系统都会监控未知驱动的加载和试图进入ring0的举动, 而我们在第一,第二和第三部分绕过主动防御要做的处理,都必须需要ring0权限。因此监控进入ring0,是一个独立的话题,也是我们实现前三个部分需要的条件。
直接添加注册表项,ZwLoadDriver安装驱动,是几乎要被任何主动防御系统报警。必须要采用一些隐蔽的或者是为人不知的方法。总结目前已经公布出来的进入ring0的办法,有以下几种:
感染文件,例如win32k.sys,添加自己的代码到里面,启动的时候就会被执行。这种方法的优点是简单易行,稳定度和兼容性很好。但是最大的缺点就是必须重新启动以后,才能进入ring0,这是一个产品级别的后门所不能容忍的。而且微软自己的系统文件保护容易绕过,mcafee和卡巴斯基的文件监控可就不是那么容易了。
利用物理内存对象,来写入自己的代码到内核,并添加调用门来执行。这个是最早被人提出的不用驱动进入ring0的办法。因为出来的时间太长了,所以有以下一些问题:更新的操作系统内核不支持,如2003SP1;很多的主动防御系统会拦截,例如KIS6。所以这个办法也不理想。 利用ZwSystemDebugControl。这个代码在国外有人放出来过,利用它写内存,挂钩NtVdmControl,进入ring0。此法缺陷在于老的windows2000不被支持,最新的windows2003sp1上也取消了这个函数的此能力。不过好处在于,这个方法用的人少,基本上没有主动防御会注意到它,并进行拦截。
利用ZwSetSystemInformation的SystemLoadAndCallImage功能号加载一个模块进入ring0。这个方法提出来比较久了,但是因为用的人少,仍未被主动防御软件所重视。用得少的原因是,它不好用。它只能加载一个普通的模块到内核并且调用,却不是加载一个驱动,因此没有一个DriverObject。这导致了非常多的麻烦。因为要想使用这个办法,必须先用这个办法安装一个简单的内核模块,再用这个模块添加调用门等方式,执行代码清除主动防御的监视驱动安装的钩子,安装一个正常的驱动,才能最终完成任务。而且这个方法似乎对windows2003sp1以上的系统也无效。
因此,要想有一个相对完美的进入ring0解决方案,最好是寻找别人不知道或者使用很少的方法,或者将上面的有缺陷的方法做一个综合,用多种方法通过判断情况来选择使用。我在这里有一个新的思路提供给大家,微软新公布了一部分文档,关于HotPatch的使用。HotPatch可以在执行中修改系统中存在的用户态公用dll的内容,甚至是修改内核模块的内容。具体代码和细节,在这里我不能多说。
要想开发一个好的反主动防御rootkit,绕过监控进入ring0是必不可少的,然而这部分也是使用不成熟技术最多的,最容易出现严重问题的部分。作为一个负责任的实用级产品,一定要对这个部分作做详细的测试,来保证自己的产品不会在某些特殊的环境,比如64位CPU运行32位系统,多核处理器,HyperThread处理器上面,出现故障或者蓝屏。
实用级反主动防御rootkit的通用性问题
前文已述,本文的宗旨在于讨论一种实用级别rootkit开发的可行性。因此,工程量的大小,需要投入的人力,时间和金钱,也是我们需要考虑的内容。必须要考虑更好的兼容性通用性,和工程上的开发代价和稳定成熟周期不能无限大。因此,对于部分新技术,例如BiosRootkit,VirtualMachine-Rootkit,本文不做讨论,因为那些都属于如果要想做稳定通用,工程代价非常大,以至于他们只拥有技术上面的讨论价值,而不具备作为一个产品开发的可选解决方案的可能性。至少是目前来看是如此。
每个主动防御软件的原理和构造都是不相同的,因此不可能指望有某一种方法,从工程上可以解决一个主动防御系统,就可以无需测试的,保证无误的解决其他系统。因为这个原因,开发一个成熟稳定的反主动防御rootkit,必然要在兼容各种主动防御的系统的通用性上面下大功夫。按照不同的主动防御系统,在程序里switch case,应该是非常必要的,尽管绝大多数反主动防御代码原理上可以通用。基本上,在测试程序通用型的时候,常用的主动防御软件,是每种都要安装一个并且仔细测试的。
以下举例说明,几个常用主动防御系统各自需要注意的特点,这都是笔者在实际开发中遇到的比较典型的例子。Mcafee8.5,该主动防御软件在最大化功能时会禁止在系统目录下创建可执行文件,光这一点就会让几乎全部rootkit安装失败,若非针对它做了设计。在这个系统下面,也不可能使用感染文件的方法来进入ring0。KIS6,该系统会自动列举运行的隐藏进程,并且弹框警告。因此在这系统下,不太可能把自己的进程隐藏。而且它列举隐藏进程的手段很底层,很难绕过。ZoneAlarm Pro,该系统下,如果一个其它的进程启动IE并且访问网络,安全报警仍然会以该进程本身访问网络为准执行,另外还会弹框警告,除非将自己的僵尸IE进程的父进程更改,或者不用IE来反弹连接。国产的瑞星,总体来说这个系统的主动防御弱于国外产品,但是它特殊在于,会对IE作出非常严格的限制,默认不允许IE装载任何非系统的dll。因此在这个系统下基本不可能利用IE反弹。
其他的特殊情况还有很多。作为一个成熟产品开发者,这些都是必须要考虑的。
感谢:VXK(郭宏硕), xyzreg(张翼)。
----------------------------
差不多3年没有接触网络安全领域了,3年来经典的东西还是经典,新的东西也层出不穷,国内依旧是泛滥着DDOS,国外不少黑客跑去破解新主机/掌机。
虽然不希望再干网络安全。但是,保持自己的安全意识是非常必要的。所以,继续吧。。
反正重新来过。。。。
2008-01-18
【转载】实用级反主动防御rootkit设计思路
2008-01-14
Physics in Games: A New Gameplay Frontier
From:http://www.gamasutra.com/view/feature/2798/physics_in_games_a_new_gameplay_.php By Pascal Luban
The question is now to know what the possible applications in terms of gameplay are. How are we going to provide the gamer with new experiences?
The purpose of this article is to offer some answers and share my experience which I gained while designing the CTF-Tornado multiplayer map, one of the maps available in an Unreal Tournament 3 mod designed to take advantage of the processor developed by Ageia. The map was developed by the talented Gameco Graphics studio.
Part One: Introduction
Definitions
Let's start with a few definitions. The scope of physics is not limited to gravity simulation and collisions between objects. Physics also includes the following applications:
- Fluid dynamics, either liquid or gaseous
- Distortion of soft objects such as fabric or of hard objects such as metal
- plate or even solid hollow objects
- Simulation of friction and viscosities
- Changes in the state of matter such as the passage of water from the liquid state to the solid state
- Breaking of materials
Thus physics covers a wide scope of issues, some of which have never been used in games. The potential is huge but the issues faced by the developers are equally challenging.
Challenges arising from the use of physics in games
There are several challenges:
- The requirements in terms of computing capacity
- The challenge in multiplayer games
- The incompatibility between the scripted dimension of certain games and the chaotic nature of physics
Let's take a closer look at each of them.
The requirements in terms of computing capacity
Physics management requires a huge computing capacity. It is not by accident that physics has not been widely used in games so far, despite the fact that good software physics engines like Havok have been available for several years.
In the current games, only a very small portion of the CPU power is allotted to physics, between 10 and 25% of a single core. Yet, physics requires considerable resources. Let's consider the simplest case, that of collision management.
The movement of each dynamic object is managed by many parameters (direction of movement, speed, rotation on multiple axes) which must be recalculated for each image. Then, interactions among dynamic objects must be calculated. Ten dynamic objects that are packed against each other require far more calculations than if they did not touch each other. Two technical solutions are available:
- The arrival of multi-core CPUs will provide a massive amount of CPU power. Dual-cores are still too weak but quad-cores will begin to bring the power needed for massive applications of physics. Will it be enough? We don't know for sure. Quad-cores will provide much more power than we currently have, but game engine requirements will increase accordingly and there is no guarantee that there will be enough extra power left for physics applications. Furthermore, programming multiple cores is more complex and generates its own overhead. We might have to wait for eight--core processors.
- The second solution is the use of dedicated physics cards such as the one manufactured by Ageia. Like any new hardware, it will pick up when enough exciting applications will be available, which in turn will be developed if the installed base is large enough.
Physics also makes more work for the CPU outside of the physics in itself
- The use of physics in one level makes the pathfinding far more complex. In fact, it must adapt on-the-fly to a changing environment. If a large block falls down in the middle of a road, the artificial intelligence of the game must take it into account, so that the NPCs controlled by the AI move around the object or use it for cover.
- The lights and the shadows must be dynamic. If objects move or walls are destroyed, pre-calculated shadows or lights cannot be used. Thus, the use of dynamic lighting becomes essential. This can be avoided by choosing environments with little contrast, but the graphical quality of the map suffers.
The challenge posed in multiplayer gaming
The use of physics in a multiplayer game offers very interesting perspectives from the gameplay point of view, as the gamer can then modify the topology to his or her advantage. However, when compared to a single player game, the multiplayer game offers additional constraints as well: the available bandwidth for sessions played on the internet, and the latency. The challenge is to synchronize the events that have a direct impact of the gameplay on all machines.
Until now, in most games, only the position of the gamers and their projectiles had to be synchronized. If physics is to have an impact on gameplay, all physics events should be synchronized as well: physical objects, clouds of particles that are likely to block the view, damage zones etc.
Unfortunately, this constraint is largely independent of the computing power of the game machine, but several solutions enable the use of physics in a multiplayer session. The strategy to follow is to have a physics implementation whose impact is focused on the client machines and which requires little synchronization. Thus, the number of large objects that have an impact on the gameplay should be limited, as they have to be synchronized on all machines.
On the other hand, a cloud made up of particles or small objects does not require perfect synchronization on all machines. Synchronizing the position of the cloud and its possible damage volume would suffice. Thus, if synchronizing the effects of the fall of a watchtower is desired, what should be synchronized on all machines is the damage volume associated with the fall of debris and not the debris itself, of which there is far too much. Physics locally manages the collisions between debris.
The level design should take this constraint into account from the very beginning by allowing that a maximum number of physical effects should be managed locally, and by providing a "budget" of objects to synchronize.
The incompatibility between the scripted dimension of certain games and the chaotic nature of physics
All games that offer a single experience aim to provide the gamer with the best sensations. For game development cost reasons, such games offer a linear level design, thus ensuring that the gamer takes advantage of the projected special effects, cutscenes and scripted combat situations. The linear level design also allows controlling the rhythm of the game. If the gamer starts to roam in a place where nothing interesting happens, they might get bored.
Of course there are many exceptions that offer open game environments such as most of the RPGs or certain action games that take place outdoors, such as Far Cry, but the majority of action/adventure games provide linear or semi-linear architecture.
In such a context, the use of physics may lead to situations that are unpredicted by the level designer. This is called emergent gameplay.
This type of gameplay is usually welcome, but the use of physics provides for the possibility to change the topology of a level and therefore create situations like blocked movements, disturbance of the characters' AI or disappearance of key items for the script. These kinds of problems may also occur in the multiplayer maps.
Thus, in CTF-Tornado we quickly became aware that the movement of large objects by the tornado or gamers could block the access to the bases and therefore prevent them from getting back or laying down the captured flags. This option had to be offered, as it opens new tactical possibilities, but care had to be taken not to block play on the map.
Consequently, various solutions were developed: the number of access paths was increased and we offered gamers the opportunity to get rid of the possible obstacles themselves by using the Impact Hammer, one of the Unreal Tournament 3 weapons that provide a repelling effect.
Part Two: Gameplay Applications of Physics
Except for games where physics provide the main gameplay mechanism, physics has been essentially used in games for cosmetic purposes. Even today very few games use physics to improve their gameplay. The reason for this is simple. Mass-market games like action games are already very demanding in terms of computing power and we have seen that physics is especially demanding in that area, and strongly impacts other parameters such as real-time display.
Today, a more intensive use of physics is conceivable, regardless of the kind of game -- but for what purpose?
In my opinion, there are four groups of application:
- To give the player new ways to handle the challenges he or she will face in the game
- To create mobile game environments
- To develop powerful learning mechanisms
- To allow the player to build his own tools
Providing the gamer with new tools
For most games, the idea behind the gameplay is:
1) offer the gamer a challenge to overcome, such as defeating a group of enemies and
2) provide him or her with tools to succeed: weapons, animations, background elements etc. The gamer's objective is to learn how to master such tools and use them wisely to overcome the challenge.
From this perspective, any new "tool" that is offered to the gamer should enable him or her to achieve this objective. The same is true for physics. If it does not provide new ways for the gamer to overcome the challenge, it is completely useless from the gameplay perspective.
This tough reality should always be taken into account, for the use of physics remains highly demanding in terms of resources and implies significant decisions at the beginning of the project. Let's now look at a few ways to really take advantage of physics to improve gameplay.
Action games represent a major genre, so let's look at the possible uses of physics in action gameplay. In this kind of game, the gameplay lies essentially on the challenges of combat and movement. Physics should therefore provide the gamer with new tools to respond to the following problems:
- Weaken or eliminate opponents
- Protect himself or herself
- Open or close a passage
- Detect opponents
- Hide from opponents
When the gamer's purpose is understood, numerous practical applications come to mind:
- The destruction of background elements enables the gamer to reach an opponent from an advantageous point, gain protection by building a cover, open new paths or on the contrary, close existing paths.
- Fluid management offers a totally new scope. Fluids may be either liquid or gaseous. They allow the player to light up a fire or let the wind blow it in right direction. The smoke generated by the player's actions may impair his opponents' vision, thus offering new tactical opportunities. A moving or expanding liquid may drastically affects the physics of the other bodies: certain bodies will sink, while other will float to the surface. Filling up or, on the contrary, emptying an area may also drastically change the combat and movement conditions and provide a totally new gameplay in an area previously already exploited. A moving fluid may also serve to carry an odor, thus widening the means of detection available for the gamer… and his or her opponents.
Non-static game environments
Today, almost all game levels are totally static. It is the opponents that animate them. Let's imagine games where the game environment itself is the opponent, or at least provides changing conditions. Physics allows for changing environments, with all the consequences: falling objects, loss of balance, objects more or less difficult to move according to the slope, etc.Imagine an action game that takes place on a sinking ship. Compartments fill with water and change the movement of characters by blocking certain passageways, but also by putting out fires. The angle of the hull itself changes. Large objects can slide along the decks and be used as obstacles or even weapons. The complete change of the ship's angle, such as a turn around or passage to vertical, could lead to a drastic change of the circulation in the hull.
Other situations are imaginable: Fire, earthquake, tidal wave, bombing, sea storm, changes in gravity or space pressurization etc.It is in fact what we did with CTF-Tornado. The tornado behaves as the "third team", by helping or putting at a disadvantage one of the two sides. The tornado changes the circulation in the map by blocking or opening numerous passageways. Its simple presence prevents the gamers from taking the shortest passageways. It also changes the defense conditions of the map by tearing off pieces of walls or roofs. It can even sidetrack gamers' projectiles!
Providing the gamer with a powerful learning mechanism
Playing is not an activity reserved to mankind. It is a mechanism of discovery and learning that had been developed by nature well before Homo Sapiens descended from their trees. Playing is learning. Of course this mechanism applies to our own games. The gamers learn in a FPS to anticipate their opponents' reactions and to evaluate the power of a weapon, for instance.How do they do it? By exploring their game environment and by testing it, for example by using the available weapons and evaluating the results. It is thus possible to teach the gamers how to use tools they do not know. But this mechanism becomes much less efficient when the cause-effect relationship is not obvious. You've seen this in certain puzzles that do not respond to our logic but rather to that of the game designer that created them.
Physics is especially suitable for this kind of self-learning mechanism, since we know how it works in the real world. Thus, we instinctively know how the forces of gravity and fluid physics will influence a spilled liquid. Providing the gamers with gameplay that relies on physics enables them to find their own solutions to complex problems.Think this is just hazy theory? Let's look at a few applications. In Half-Life 2 several moving puzzles lie on a well-known mechanism: buoyancy -- or its absence -- of certain objects. The gamer thus uses the positive buoyancy of barrels to make a heavy object float or, on the contrary, takes advantage of the mass of the metal pieces to make a floating object sink.
In the PC version of GRAW 2 developed by the Swedish studio Grin, gamers can get rid of opponents entrenched in a tower by causing its fall. Finally, let's consider the example of Switchball. In this puzzle game, some of the puzzles are solved by using balls of different densities. A player quickly discovers that too light a ball does not have enough energy to make its way through some obstacles.
A physical game environment would allow the player to use his or her initiative and spirit. The player can find solutions by using his or her own real-life experience.
Let's project ourselves ahead into the future and figure out what a level designer with a physical game environment available could accomplish. Imagine that a gamer is blocked by a group of opponents solidly entrenched behind a barricade. What solutions could he or she come up with to pass?
The falling of a tree or a building could crush opponents, a large cylindrical object could be moved by the player who would then be able to push it to make a mobile rampart, a vehicle could be freed along a slope in order to be used as a ram, a vehicle could explode so that the smoke column mask the player's movements. All these solutions derive from the simple observation of the environment and from our innate understanding of what is possible in the real world.
Letting players build their own tools
Physics enables the distortion of objects such as plates, hoses or beams. It is therefore imaginable to allow the gamer to shape them according to his or her needs. Thus, by distorting metal sheets, a player can build watercourses or ball paths. A skillfully cut canvas could act as roofing or veil within a survival simulation. The frailness of certain materials such as branches enables the building of traps by covering a hole with the respective materials. The flexibility features of a steel blade or board enable the building of a primitive catapult.
Such applications of physics would probably be limited to certain kinds of games such as puzzles or survival games. In fact, the interface would have to be adapted. But the fun potential of the game could be huge. Imagine a building game where each brick is designed with physical features. The gamer could build a totally unique game environment.
Conclusion
Physics is extremely demanding in terms of resources and some of the ideas that I have developed here are not currently achievable -- but the advances in the tools and technologies are foreseeable, giving us the power in the future. From now on, gameplay can be improved with uses that are not just cosmetic. The development of dynamic game environments that the player can change on the fly is already a trend in today's level design. Physics makes this evolution possible.
Near future technologies will astonish us and provide us with the power increase. The ball will then be in the court of the game and level designers who will then have to take this advantage we need to bring new experiences to the gamer.
Acknowledgements
I would like to thank the following persons for their contribution:
David Black
Hervé Vazeilles
James Dolan
Jean-Pierre Bordes
Majdi Kraiem
Monier Maher
Philippe Geldard
物理加速卡介绍
Ageia的物理加速卡发布已经有2年多了,也许,在这两年中Ageia不断发展,物理卡也渐渐进入我们眼中,虽然有点晚,但是我们还是有必要详细的了解一下物理加速卡的相关信息。
另外,如果对物理加速有兴趣可以参见我转的一篇物理加速的文章:
[3D加速后又一全新理念]
3D显卡的诞生令人们可以通过显示设备领略到“十分真实”的画面,在1996年,几乎所有市面上出现的显卡产品都宣称拥有一定的3D处理能力,不过当时它们大多只是通过显卡芯片完成常规画面渲染工作,而3D场景模型构建和光照效果运算都是通过专门的软件算法由CPU来处理。包括当时的API也没有统一的标准,实际的3D效果可谓十分有限。不过1997年,随着第一块3D加速卡Voodoo的问世,人们才终于领略到真正的3D游戏画面。在那个只靠CPU来完成大量图形计算的时代,Voodoo让人们见识到了3D加速技术的魅力,各种滤镜效果、性线过滤、抗锯齿等3D特效技术把3D游戏的画面提高到了一个全新的层次,人们也才清楚3D加速的概念。1998年第一款GPU芯片GeForce256出现后,其内置的硬件T&L(几何转换和光源处理)引擎,可以直接完成场景构建和光照运算,大幅提升了显卡3D性能并降低了CPU的负担,从此人们有了完整的3D加速引擎的概念。这一次3D加速革命,也为图像处理技术的发展指明了方向,同时也造就今天的nVIDIA、ATI等著名厂商。
但是,从此之后显卡就再也没有什么革命性的进展。在这漫长的10年之中,一代又一代的显卡芯片除了频率不断提升、制造工艺改进、渲染管线增加之外,并没有任何质的改变。只是从架构上来看,即便是目前最高端的显卡,在架构上和他们的老前辈也并没有什么区别。(当然,G92,G200肯定和这些不同了。)
而现在的大型3D游戏中,为了使游戏的画面更加的真实,开发人员就会在游戏中设计许多接近现实的物理计算,比如:自由落体,物体运动,空气流动,力的反弹以及各种物体间碰撞等等。但是在传统的计算机中,游戏的物理运算基本上是通过物理引擎加上CPU处理后的物理参数后再反馈到中游戏之中,这种方式往往在遇到大规模的物理运算时就会出现运算瓶颈,这也就造成了游戏中一旦出现大量物理运算时,帧数就会明显下降。那么如果来解决这个问题呢,在Game Developers Conference (GDC)2005游戏开发者大会上,一家来自美国的半导体生产厂商AGEIA给我们一个非常好的答案——PPU。
[PPU——物理加速引擎]
PPU——Physics Processing Unit(物理运算处理器),如果CPU是为了达到更快的运算速度,GPU是为了达到更好的图像效果,那么PPU就是为了使游戏更加真实。在以前的3D游戏中,大多数的背景和物体都只是一些静态的贴图效果,真实感较差。而在FPS游戏大作Half Life 2(半条命2)中,Havok物理引擎带给了玩家们一个非常真实的物理世界。你可以在游戏中捡起一个废纸盒,然后把它抛向远处,然后可以看见它撞击到墙壁以后反弹到上地面上,溅起一阵灰尘。接近于真实物理效果使得游戏中任何物体都是可移动的,它们都遵守着游戏中的物理参数,组成了一个“真实”的游戏世界。
PPU的出现可以说为图像处理技术的指明了新的方向,显卡并不再是像以前一样只是单一的工艺进步、频率提升、管线堆叠,而是需要采用不同方式的处理技术。加入了PPU负责单独的物理运算后,GPU便可以专心负责进行图像的渲染和显示,CPU则进行两者之间的综合协调。PPU的出现将物理计算和图像处理分开运算可以创造出更加生动的和真实游戏环境,这可能引领新一轮的3D技术革命。而玩家也将体验到像电影中的一样的壮观场面,各种物体爆炸、撞击、建筑物倒塌等等。有了PPU的辅助以后,可以很轻松的实现各种复杂的碰撞或者运动效果。这种小到一粒沙也会因为空气的流动而移动的场面,令游戏的可玩性、真实性是以前传统的任何一款3D游戏难以比拟的。
但是同时PPU有着和GPU完全不同的内部构架,PPU将拥有软、固质体动力,泛用碰撞侦测,有限元素分析,流体动力,毛发模拟,布料模拟等技术特效。PPU的这些处理技术和GPU是完全不同的两个运算概念,因为物理运算需要十分强大的整数及浮点运算能力,GPU的性能则是将重点放在了画面的渲染效果上,追求单纯的视觉效果,而PPU所强调的是“真实性”。
如此真实的表现真是令人期待,下面我们就先来了解一下全球首款硬件加速物理运算处理器,来自AGEIA的PhysX。
[PhysX横空出世]
2005年3月19日,美国无晶圆半导体厂商AGEIA于游戏开发者论坛(Game Developers Conference, GDC)上发表了全球第一款采用硬件加速的物理运算处理器(PHYsics Processing Unit, PPU),同时将其命名为PhysX。AGEIA称它将会出现在下一代包括PC和游戏主机中。
AEGIA在发布PPU时这样解释:以90年代末被称为GPU的图形处理器为例,早期复杂的3D图形往往需要通过软件配合CPU的运算才能得以实现,而GPU的出现正好接过了CPU的这部分工作,我们的PC开始通过GPU完成对3D图形的运算,当然它并没有完全脱离CPU。AEGIA认为GPU的出现降低了CPU的工作负担,同时为用户提供了更加完美视觉效果和图形效果,而PPU的出现也是如此,它将原本必须依赖软件技术并通过CPU的运算才能完成的工作交给专门负责物理运算的硬件来完成,这样,PPU开始向CPU、GPU一样进入我们的PC。
游戏性能三种重要因素:CPU、GPU和PPU(物理运算处理器)。
PhysX在游戏画面上带来一些较为显著的改变包括:
- 由于爆炸所引起的烟尘和碎片的效果
- 通过制造更加复杂、节点更多的几何图形来形成更为逼真、交互性更强的动作
- 包括人物的服装、眼泪等细节都会和真实的场景一模一样
- 当风吹过衣物、或者树叶之类时,物体会呈现出自然的摆动
- 环绕在物体周围的浓烟&尘雾将会形成一种运动的效果
在今年的E3大展上,AGEIA也首次公布了PhysX的PPU Demo,在第一个Demo场景中,约有4200个多边型石块从一个山坡上滚下来,在运动的过程中,每块石头都以不同的物理条件自由下落,石头会因为高度、速度、重力及撞击而改变运动规则,它创造的完全是一个“真实”的客观世界,不再是以前游戏中每个物体的移动都是按照计算好的路线进行运动。既然需要拥有如此强大的物理运算能力,那么拥有强大性能的双核心CPU是否会更少的依赖PPU的工作呢? AGEIA则认为PPU的作用是无法替代的。因为目前的CPU在游戏中可以处理1000个左右的可自由移动的多边物体,而目前的PPU可以达到32000个,如果驱动程序升级改进以后,可以达到40000~50000个。所以说PPU在游戏中物理运算的重要地位是难以替代的。
[关于AGEIA]
在05年GDC上,一家名不见经传的美国半导体设计厂商Ageia发表了世界上第一颗采硬件加速物理运算处理器(Physics Processing Unit, PPU)“PhysX”,很快在世界上引起了一场物理加速的轩然大波。
AGEIA的计划主要推广该公司的软件产品physics API,该软件起初被称之为Novodex,但是后来被更名为PhysX。
根据AGEIA公司的设想,PPU和CPU以及GPU的关系成为一个相辅相成,共同运算的关系由于PPU能够代替CPU进行大部分的物理计算任务,因此可以使得处理器能够更加快速得与GPU进行数据交换从而使得游戏运行更加平稳。
为了给游戏开发商提供便利,AGEIA公司准备了NovodeX SDK开发包来完成这个使命。该软件不但包含了业界先进的Novodex物理模拟引擎,而且它还可以作为现在主流的的3D建模开发工具以及微软的XNA开发工具的插件运行,开发人员只要通过拖放和点击就能完成对目标施加物理模拟效果,这对于任何专业的开发人员将都不是什么难事。 Ageia早先表示只要游戏开发商支持PhysX物理卡,就可以免费使用PhysX物理引擎和其它相关的技术支持。这吸引了许多一线游戏包括幽灵行动3、X战警2-天启降临、City of Villians等的开发商,虚幻3也将支持PhysX物理卡并使用PhysX引擎,这意味着将来众多的虚幻3引擎游戏都将同时支持PhysX。
Ageia公司的创始人一共有五位,他们是在美国一家名为MinMax technologies的半导体设计公司们共事相识的。当时团队研发了业界第一块基于网络处理器的大型交换机,当完成这个项目后,他们几个人聚在一起决定开创自己事业,于是就有了现在的Ageia公司。新公司首先研发了业界首款三芯片10 Gps数据处理能力的网络处理器,接下来他们决定尝试一些能带来更大影响力和革命性的东西,于是就有了今天的“PhysX”。
[物理加速的全盘解决方案]
随着游戏画面的发展、电脑AI的更加复杂化,再加上物理运算已经使CPU不堪重负,因此业界除了AGEIA外,其它一些有实力的厂商当然不会作壁上观。目前业界的解决方案有3种:物理引擎专业商Ageia制造独立的PhysX物理加速卡; Nvidia同Havok协力研究的“SLI物理卡”加速技术;ATI则在加紧研究提高其显卡的物理运算能力,三种方案虽然都能够提供物理加速,但是在技术上又有很大不同。
Ageia推出革命性的PhysX物理卡并制订了长远的计划来同时发展软件的PhysX物理引擎和硬件的PhysX物理卡。软件的PhysX物理引擎在PC、PS3和XBOX360上都有广泛运用,例如半条命2中运用的rag dolls(布娃娃)物理引擎等。
硬件的PhysX物理卡则是IT业界的一项革命,独立的物理卡要比CPU和GPU模拟物理运算更快更专业,先前Ageia用PhysX物理卡展示了一款8人对战游戏,游戏中有数百个目标你可以把它们移动、毁灭,每一个物体的位置、外型并不是事先设计好,而是实时运算出来的。同时计算数千个物体的活动轨迹和破碎画面,过去是CPU和显卡所无法负担的。
[物理加速——NVIDIA篇]
NVIDIA提出了“SLI物理卡”的概念,这项技术由NVIDIA和Havok共同研发。Havok是一家专业的物理引擎设计公司,许多著名的游戏如帝国3、半条命2、F.E.A.R、光晕2等都使用了其Havok物理引擎。去年10月,Havok宣布最新的物理引擎Havok FX即将完成,这也正是NVIDIA “SLI物理卡”的技术基础。
Havok FX物理引擎,其是可以让任何支持Shader Model 3.0的显卡极大提高物理运算能力的开发软件。NVIDIA首席科学家David Kirk称:让GPU负责物理计算是适合的,目前GPU的速度和高可编程性完全可以兼顾物理运算。游戏开发商通过GPU上各种高级的可编程资源,在GPU上进行并行模拟运算得到了良好的结果。
[物理加速——ATi篇]
Havok FX只需要支持Shader Model 3.0的显卡,而不是仅为NVIDIA显卡可用。所以ATI将来也可以同样提出基于CrossFire的互连方案。ATI称其在动态渲染管线分配技术的领先、与Microsoft在DX10技术的紧密联系等优势会让ATI比竞争对手更强大。
ATI称其R520和R580架构本身具备物理处理功能,该功能可以通过软件方式达到。不久ATI就会给各软件开发商提供新的API以改进Pixel shader,新的DPP(并行数据处理)技术将使显示芯片绕过Direct3D API,同步处理物理数据和pixel shader数据。ATI称这一改进将在让GPU加强物理运算能力的同时还可以极大的提高GPU运算所有浮点指令的速度,这一多用途的改进方案将使ATI显卡在流体处理、视频处理等多方面受益。
Havok FX将物理数据转换为Direct3D数据,从而使任何Shader Model 3.0显卡都可以直接运算。而ATI的最新API可以让开发商使用GPU来加强运算多种物理或浮点数据,从而直接运算物理数据。开发人员可以开发出多种软件使显卡加强不同能力来应对不同运用。这种高自由的开放性API使它受到了欢迎。但是ATI的这一方案也需要专门的开发团队支持,如果是拥有Havok FX的厂商会比较方便,而没有Havok FX的厂商就只有自己设计复杂的转换程序了。
这是一张,开启和未开启物理加速的游戏对比图:
2008-01-13
The Inconvenient Truth
First time I watch the movie
Just as he said , people always believe themselves know a lot about the problem.but in fact, not.
Even if I'm not interested in join environment protected organizations ,I still feel the responsibility to pass on the information about it.
Now is the time,we take a change.
Now is the time,we begin from ourselves.
I post the words on the end of the movie
========================================
Are you ready to change the way you live?
The climate crisis can be solved.
Here's how to start go to www.climatecrisis.net
In fact,you can reduce your carbon emissions to zero.
Buy energy efficient appliances and lightbulbs.
Change your thermostant(and use clock thermostats)to reduce energy for heating and cooling.
Weatherize your house,increase insulation,get an energy audit.
Recycle.
If you can buy a hybrid car.
When you can,walk or ride a bicycle.
Where you can,use light rail and mass transit.
Tell your parents not to ruin the world that you will live in.
If you are a parent, join with your children to save the world they will live in.
Switch to renewable sources of energy.Call your power company to see if they offer green energy.
If they don't, ask them why not.
Vote for leaders who pledge to solve this crisis.
Write to congress.if they don't listen,run for congress.
Plant trees,lots of trees.
Speak up in your community.
Call radio shows and write newspapers.
Insist that American freeze CO2 emissions and join international efforts to stop global warming.
Reduce our dependence on foreign oil;
help farmers grow alcohol fuels.
Raise fuel economy standards;
require lower emissions from automobiles.
If you believe in prayer,pray that people will find the strength to change.
In the words of the old African proverb,
when you pray,move your feet.
Encourage everyone you know to see this movie.
Learn as much as you can about the climate crisis.
Then put your knowledge into action
=====================================
2008-01-12
从课程阅读材料看中美大学课程的差别。
周 | 课程单元 | 阅读资料 |
---|---|---|
1 | 介绍及概要 Introduction and Overview | |
2 | 在制造中学习 Learning Through Making | 〈PIE网络:通过有趣的发明和发现用数码技术促进科学调查和工程学〉,由国家科学基金会提出,2000年。 "The PIE Network: Promoting Science Inquiry and Engineering through Playful Invention and Exploration with New Digital Technologies." Proposal to the National Science Foundation, 2000. Resnick, M., R. Berg和 M. Eisenberg. 〈黑盒子之外:把透明和美带回科学调查中〉,《学习科学杂志9》, 第1期,(2000):7-30。 Resnick, M., R. Berg, and M. Eisenberg. "Beyond Black Boxes: Bringing Transparency and Aesthetics Back to Scientific Investigation." Journal of the Learning Sciences 9, no. 1 (2000): 7-30. |
3 | 从别人身上学习 Learning from One Another | Gardner, H. 《非校园思维:孩子如何想和学校怎样教》,Basic Books,1989,第6,10,11章,第200-210页。 Gardner, H. The Unschooled Mind: How Children Think and How Schools Should Teach. Basic Books, 1989. Chapters 6, 10, 11, pp. 200-210. |
4 | 学徒式学习 Apprenticeship Learning | Brown,J. S., A. Collins和P. Duguid. 〈情境认识和文化学习〉,《教育研究者18》,第1期(1989):32-42。 Brown, J. S., A. Collins, and P. Duguid. "Situated Cognition and the Culture of Learning." Educational Researcher 18, no. 1 (1989): 32-42. |
5 | 有力的想法和表达的流畅度 Powerful Ideas and Fluency | Papert, S. 〈什么是大点子:对于点子力量的教育〉, 《IBM 系统杂志39》,第3-4期 (1991) Papert, S. "What's the Big Idea: Towards a Pedagogy of Idea Power." IBM Systems Journal 39, no. 3-4 (1991). |
6 | “独立” 学习 Learning "On Your Own" | 选读材料出自《在学校教育之外成长》的时事通讯。 Selected readings from the Growing Without Schooling newsletter. Holt, John.《任何时间都学习》。AddisonWesley 出版公司,1990年8月1日。ISBN:0201550911。 Holt, John. Learning All the Time. Addison Wesley Publishing Company, August 1, 1990. ISBN: 0201550911. |
7 | 选择你的工具(并熟练地使用工具) Choosing Your Tools (and Fluency with Tools) | Martin, F. 〈乐高设计的艺术〉,《机器人学实验者:机器人建造者杂志 1》, 第2 期(1995)。 Martin, F. "The Art of LEGO Design." The Robotics Practitioner: The Journal for Robot Builders 1, no. 2 (1995). Resnick, M., A. Bruckman和F. Martin. 〈钢琴不是立体声:创造会计算的工具箱〉,《Interactions 3》,第 6期 (1996): 64-71。 Resnick, M., A. Bruckman, and F. Martin. "Pianos Not Stereos: Creating Computational Construction Kits." Interactions 3, no. 6 (1996): 64-71. Martin,F.B.Mikhak和B.Silverman. 〈MetaCricket:用于制作计算装置的设计师工具箱〉,《IBM系统杂志39》, 第 3-4期 (2001)。 Martin, F., B. Mikhak, and B. Silverman. "MetaCricket: A Designer's Kit for Making Computational Devices." IBM Systems Journal 39, no. 3-4 (2001). |
8 | 纪录及评估活动 Documenting and Evaluating Activities | Duckworth, E. 〈拥有绝佳的点子〉,《哈佛教育评论42》, 第2 期(1972):第217-231页。 Duckworth, E. "The Having of Wonderful Ideas." Harvard Educational Review 42, no. 2 (1972): 217-231. 选读文章出自于Reggio Emilia学派的系列作品: Selected readings from a collection of books on the Reggio Emilia Approach: Katz,L. G.,和B. Cesarone编辑,《 Reggio Emilia方法的反思》,Redleaf出版社,1994。ISBN: 8886277687。 Katz, L. G., and B. Cesarone, eds. Reflections on the Reggio Emilia Approach. Redleaf Press, 1994. ISBN: 8886277687. Ceppi, G.和M. Zini,《 孩子,空间,关系:关于幼儿环境的新项目》,Reggio:Reggio S.R.L., 1998。 Ceppi, G., and M. Zini, eds. Children, Spaces, Relations: Metaproject for an Environment for Young Children. Reggio: Reggio S.R.L., 1998. 《泉水:孩子未被听到的声音》,Reggio: Reggio S.R.L.,1995。 The Fountains: The Unheard Voice of Children. Reggio: Reggio S.R.L., 1995. Edwards,Carolyn,George Forman及Lella Gandini,《孩子的上百种语言》, Ablex出版社,1997,ISBN: 156750311X。 Edwards, Carolyn, George Forman, and Lella Gandini, eds. The Hundred Languages of Children. Ablex Publishing, 1997. ISBN: 156750311X. |
9 | Diane Willow:学习 的空间、地点和环境 Diane Willow: Learning as Space, Place and Environment | Tuan,Yi-Fu. 〈Topophilia第九章〉,《环境理解,态度和价值研究》。纽约:哥伦比亚大学出版社,1990年11月1日。ISBN: 023107395X。 Tuan, Yi-Fu. Chapter 9 in Topophilia; A Study of Environmental Perception, Attitudes, and Values. New York: Columbia University Press, November 1, 1990. ISBN: 023107395X. Orr,David W. 《生态学叙事法第2章:教育和向后现代世界的过渡》。纽约:SUNY 出版社,1992年1月1日。ISBN:0791408744。 Orr, David W. Chapter 8 in Ecological Literacy: Education and the Transition to a Postmodern World. New York: SUNY Press, January 1, 1992. ISBN: 0791408744. Hiss,Tony. 《地点的经验第2章:一种新方法,考虑和安排我们正在发生根本变革的城市和乡村》。Vintage出版社,1991年10月1日。 ISBN:0679735941。 Hiss, Tony. Chapter 2 in The Experience of Place: A New Way of Looking at and Dealing With our Radically Changing Cities and Countryside. Vintage Press, October 1, 1991. ISBN: 0679735941. 在线阅读: Readings Online: Chawla,Louise.〈心醉神迷的地方〉儿童环境季刊,1990,7 (4):18-23。 Chawla, Louise. "Ecstatic Places." Children's Environments Quarterly, 1990, 7 (4): 18-23. Vecchi,Vea.〈在学校的什么地方能过得最好?〉,G. 和 M. Zini编辑,《孩子,空间,关系:关于幼儿环境的新项目》,Reggio:Reggio S.R.L.,1998。 Vecchi, Vea. "What kind of space for living well in school?" in Ceppi, G., and M. Zini, eds. Children, Spaces, Relations: Metaproject for an Environment for Young Children. Reggio: Reggio S.R.L., 1998. |
10 | 设计与工艺的对比 Design vs. Craft | |
11 | 讨论第一轮专题讨论会的计划 Discussion of Plans for First-round Workshops | |
12 | 第一及第二轮专题讨论会的报告 Reports on First and Second Round Workshops | |
13 | 给彼此的专题讨论会 Workshops for One Another | |
14 | 上交期末设计的报告,媒体实验室的专题讨论会 Final Project Reports Due, and Workshops for the Media Lab |
2008-01-09
美国人的商务午餐
美国人的这种商务午餐被称作Power Lunch, 也就是所谓的“实力午餐”,每个行业里有头有脸的人物每天固定到有数的几家餐馆用餐。共餐对象不仅有生意上的伙伴,也有竞争对手。他们边吃边谈,同时密切关注还有谁在那里用餐,和谁在一起。吃什么并不重要,餐桌上的话题和餐桌上的人才是最重要的。
在纽约,几乎每个行业都有自己的实力餐据点。四季酒店优雅的烧烤屋(The Grill Room)是华尔街和实业界巨头锺爱的午餐场所;政界人物一般喜欢在Lowes Regency酒店与商界人士共进早餐;而纽约著名的牛排店则是实力晚餐的首选场合,这也是为什么这些店虽然价格不菲却总要提前几周甚至几个月预定才能有座的原因。
之所以称作Power,是因为这些餐桌上的话题通常都有相当大的份量,动辙就是几亿、几十亿美元的并购交易。对于很多中国人而言,这或许有点匪夷所思,因为我们喜欢关着门讨论问题,不论是国事还是家事。但是在美国,很多重要的生意都是在这些公开的餐桌上谈成的。2003年底华纳唱片公司易手就是买家和卖家在曼哈顿的四季酒店共进午餐时敲定的。据说当时买方和卖方两人低头谈了很久,就连周围就餐的几位商界巨头--包括露华浓公司(Revlon Inc.)老板、亿万富翁Ronald Perelman和雅诗兰黛(Estee Lauder)家族继承人Ronald S. Lauder--也不时朝他们的餐桌瞥上一眼。几天后就传来了华纳被卖的消息。此外,历史上最大的并购案、价值1,010亿美元的荷兰银行(ABN Amro)三方分拆草案也是去年年初三家收购方总裁和美林的一位投资银行家在日内瓦四季酒店一边喝着葡萄酒一边在午餐桌上拟就的。
此外,这类餐厅的食物都价格不菲,不过,就餐的客人们并不在乎这个,他们最在意的可能是他们坐哪张桌子。这类餐厅里的每张桌子暗中都有级别。就迈克尔来说,近门而又靠墙的桌子是最好的,因为那里最利于看人和被人看。有名的人一般都坐那里,认识的人进来时都得跟你打招呼,同时因为靠墙,它还带有一点私密性。
Power Lunch at Michael's
2008-01-08
在Blogger blogspot中帖代码的一种方法
参考:http://klcintw4.blogspot.com/2006/11/blog-post_03.html
在css中加入如下代码CODE {
display: block; /* fixes a strange ie margin bug */
font-family: Courier New;
font-size: 8pt;
overflow:auto;
background: #f0f0f0 url(http://klcintw.images.googlepages.com/Code_BG.gif) left top repeat-y;
border: 1px solid #ccc;
padding: 10px 10px 10px 21px;
max-height:200px;
line-height: 1.2em;
}
在文章中将要粘贴的代码贴入 <code> </code>中
效果.comment-body-author {
margin:0;
padding:0 0 0 20px;
background-color:#C0C0C0;
}
注意: "<"和">"要用 "&lt;"和" &gt;"替换
标签: blogger使用技巧
The First Chapter :Some basic knowledge of Programming
首先,介绍部分C++的预定义字:
C++ defines the following reserved words:
此外,C++也能识别C类型的注释:“ /* ……*/
在C++中,它自身定义的注释为“//……”
-------------------------------------------------------------
定义:
identifier(标识符)是一个用户自定义的名称,比如变量名。标识符不可以与以上reserved words同名。
C string literal(C字符串直接量)是表示可变数量字符的文字值,用""括起来,如"This is a Hello world test."
block(块)是与语句有同样作用的代码块。
数据类型:
C++规定了三种基本的数据类型:
- 整型(integer):用来表示整数值分别有short,int,long.在32位CPU的计算机上long和int长度相同.
- 浮点(float):用来表示小数值,有float,double,long double.
- 字符(character):用来存储字符.
具体信息如下:
char :字符类型
int : 整型
float :单精度实型(浮点型)
double : 双精度实型
unsigned : 无符号类型
signed : 有符号类型
bool : 布尔类型
true : 布尔类型的真值
false : 布尔类型的假值
void : 无类型
sizeof : 取得指定类型的所占用的范围
typedef : 为某种类型取一别名
--------------------------------
类型标识 类型说明 长度 (字节) 范围 备注
char 字符型 1 -128 ~ 127 -27 ~ (27 -1)
unsigned char 无符字符型 1 0 ~ 255 0 ~ (28 -1)
short int 短整型 2 -32768 ~ 32767 -215 ~ (215 - 1)
unsigned short int 无符短整型 2 0 ~ 65535 0 ~ (216 - 1)
int 整型 4 -2147483648 ~ 2147483647 -231 ~ (231 - 1)
unsigned int 无符整型 4 0 ~ 4294967295 0 ~ (232-1)
float 实型(单精度) 4 -1.18*1038 ~ 3.40*1038 7位有效位
double 实型(双精度) 8 -2.23*10308 ~ 1.79*108308 15位有效位
long double 实型(长双精度) 10 -3.37*104932 ~ 1.18*104932 19位有效位
---------------------------------------
注:
unsigned 用于修饰 int 和 char 类型。它使int 或 char 类型成为无符号类型。
signed 是 unsigned 反义词,如 signed int 表示有符号类型,不过signed可以省略,所以上面列出char,short int,int 都是有符号类型。
由于32位计算机下long int 同int同样是4个字节,所以一般省略直接写int。
布尔型(bool)和无类型(void)
除字符型,整型,实型以外,布尔型和无类型也是较常用的两种数据类型。
布尔型(bool)
布尔类型是C++的内容,C语言没有这一类型。
布尔类型的数据只有两种值:true(真) 数值为非0或 false(假)数值为0。
无类型(void)
这个类型比较怪“无”类型。是的,没有类型的类型。或者我们这样认为比较好接受:在不需要明确指定类型的时候,我们可能使用 void 来表示。
为了容易表示,我们用下面这段代码来演示这几种数据类型:
#include<iostream>
#include <limits>
using namespace std;
int main()
{
short SHORT;
unsigned short USGSHORT;
int INT;
unsigned int USGINT;
float FLOAT;
double DOUBLE;
long double LONGDOUBLE;
cout << "\n类型\t\t\t字节数(bytes)\t精度\t最大值\t\t最小值";
cout << "\nshort\t\t\t" << sizeof(SHORT);
cout<<"\t\t"<<numeric_limits<short>::digits10;
cout<<"\t"<<numeric_limits<short>::max();
cout<<"\t\t"<<numeric_limits<short>::min();
cout << "\nunsigned short\t\t" << sizeof(USGSHORT);
cout<<"\t\t"<<numeric_limits<unsigned short>::digits10;
cout<<"\t"<<numeric_limits<unsigned short>::max();
cout<<"\t\t"<<numeric_limits<unsigned short>::min();
cout << "\nint\t\t\t" << sizeof(INT);
cout<<"\t\t"<<numeric_limits<int>::digits10;
cout<<"\t"<<numeric_limits<int>::max();
cout<<"\t"<<numeric_limits<int>::min();
cout << "\nunsigned int\t\t" << sizeof(USGINT);
cout<<"\t\t"<<numeric_limits<unsigned int>::digits10;
cout<<"\t"<<numeric_limits<unsigned int>::max();
cout<<"\t"<<numeric_limits<unsigned int>::min();
cout << "\nfloat\t\t\t" << sizeof(FLOAT);
cout<<"\t\t"<<numeric_limits<float>::digits10;
cout<<"\t"<<numeric_limits<float>::max();
cout<<"\t"<<numeric_limits<float>::min();
cout << "\ndouble\t\t\t" << sizeof(DOUBLE);
cout<<"\t\t"<<numeric_limits<double>::digits10;
cout<<"\t"<<numeric_limits<double>::max();
cout<<"\t"<<numeric_limits<double>::min();
cout << "\nlong double\t\t" << sizeof(LONGDOUBLE);
cout<<"\t\t"<<numeric_limits<long double>::digits10;
cout<<"\t"<<numeric_limits<long double>::max();
cout<<"\t"<<numeric_limits<long double>::min();
cout <<endl;
return 0;}
注意:其中\t等价与你在VC里面按TAB键的效果
numeric_limits<double>::digits10; 是返回该类型精度的。
用法:
sizeof(数据类型)
sizeof(变量)
可以返回数据类型占用的字节数。
运行结果如下:
prologue——序章
我是一个业余学习人员,也就是说,编程与计算机科学不是我的本行也不是我学习的专业,但是我的自我规划中安排了学习C或者C++这一项,于是开始学习,实际上,我几个月前就开始学习C++,很明显,由于没有做过任何学习笔记导致现在整体编写能力还是十分不令人满意,所以现在我开始从头做过笔记,也重新学习一遍。
这里我无意介绍C++的历史,有兴趣的话google it。
首先,从C++的定位开始:
在Bjarne Stroustrup的一个网页上介绍了这些:
C++ is a general purpose programming language with a bias towards systems programming that
- is a better C
- supports data abstraction
- supports object-oriented programming
- supports generic programming.
另外,还有一些必要的介绍:
C++ supports
+ Classes, instances, and methods
+ Polymorphism, encapsulation, and inheritance
C++ does not support
- Automatic garbage collection
- dynamic creation of classes
The main forte of C++ is code reuse and encapsulation. Since most modern programming enterprises involve thousands of lines of code, these features have given C++ its popularity, and, to many, the language constitutes the last "... piece of wood to hang in the middle of the ocean ..."
The following points will be covered for C++:
• Programming Style in C++ (differences between C and C++)
• C++ enhancements over C.
• References and their relation to pointers
• Classes
• Memory Allocation
• More features of classes (static members, friends, arrays of instances, new and delete operators)
• Inheritance, polymorphism, virtual functions
• Operator overloading
• Templates
• Run Time Exception Handling