`
cloudtech
  • 浏览: 4610320 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Carmack's Reverse(John Carmack on shadow volumes...)(珍贵历史资料)

 
阅读更多

John Carmack on shadow volumes...

I recieved this in email from John on May 23rd, 2000.

- Mark Kilgard


I solved this in a way that is so elegant you just won't believe it. Here
is a description that I posted to a private mailing list:

----------------------------------------------------------

I first implemented stencil shadow volumes over two years ago in the
post-Q2 research period. They looked great until you flew the viewpoint
into one of the volumes, and depending on the exact test you used, either
most of the screen went into negative shadow, or most of the shadows
disappeared.

The classic shadow volume works that stencil shadows are derived from
usually suggest "inverting the test when the view is inside a shadow
volume". That is not a robust solution, because a non-zero near clip plane
will give situations where the plane is not cleanly on one side or the
other of the view point. It is also non-trivial to make the "inside a
shadow volume" determination, especially after silhouette optimizations.

The conventional wisdom has been that you will need to clip the shadow
volumes to the view plane and cap with triangles, treating the shadow
volumes as if they were polyhedrons.

I implemented the easy cases of this, choosing to project the silhouette
points to either the far plane of the light's effect or the view plane.
For the clear-cut cases, this worked fine, allowing you to walk in front of
a shadowed object, or look directly at it with the light behind it.
Intermediate cases, where some of the vertexes should project onto the
light plane and some should project onto the view plane could also be
handled, but the cost of all the testing was starting to pile up.

Unfortunately, there are cases when an occluding triangle projects a shadow
volume that will clip to something other than a triangular prism. There
are cases where real, honest volume clipping must take place.

Anything that requires finding convex hulls in realtime is starting to
sound like a Bad Idea.

I sweated over this for a while, with the code getting grosser and grosser,
but then I had an idea for a different direction.

It should be possible to let the shadow volumes get clipped off at the view
plane like they always do, then find the clipped off areas in image space
and correct them.

The way to find if a volume has been clipped off is to render the shadow
volume with depth testing disabled, incrementing for the front faces and
decrementing for the back faces. If the stencil buffer ends up with the
original value, the shadow volume is well formed in front of the view volume.

My first attempt to utilize this involved a whole bunch of passes to
determine if it was well formed and combine it with the standard volume
stencil operations. It was an interesting experiment with masking and
anding in the stencil buffer to perform two operations, but it turned out
that, while it worked for simple shapes, complex shapes needed more
information from the volume clipping than just "well formed" or not.

The next iteration involved attempting to "preload" the standard stencil
shadow algorithm by the number of clipped away planes. I first drew the
shadow volumes with depth test disabled, incrementing for back sides and
decrementing for front sides. This finishes with a positive value in the
stencil buffer for each plane that is clipped away at the view plane. The
normal depth tested shadow volume is drawn next, with the change polarity
reversed, decrementing for back sides and incrementing for front sides.
The areas not equal to the initial clear value are in shadow.

That works all the time.

Later, I realized something else. The algorithm was now basically:

Draw back sides, incrementing both with depth pass and depth fail.
Draw front sides, decrementing both with depth pass and depth fail.
Draw back sides, decrementing with depth pass and doing nothing with depth
fail.
Draw front sides, incrementing both with depth and doing nothing with depth
fail.

Rearrange the passes and you get:
Draw back sides, incrementing both with depth pass and depth fail.
Draw back sides, decrementing with depth pass and doing nothing with depth
fail.
Draw front sides, decrementing both with depth pass and depth fail.
Draw front sides, incrementing both with depth and doing nothing with depth
fail.

It is then obvious that they partially cancel each out and can be combined
into:

Draw back sides, doing nothing with depth pass and incrementing with depth
fail.
Draw front sides, doing nothing with depth pass and decrementing with depth
fail.

I was shocked. I went from feeling pretty clever with my unbalanced
preloading algorithm (which I would only apply on surfaces that were likely
to intersect the view plane) to just feeling dumb that I had never seen the
trivial solution before. Thinking about operating on depth test fails is a
bit non-intuitive, but if you work it through a couple times, what is going
on makes pretty good sense.

Shadows done this way have none of the "fragile" feel that geometric
algorithms tend to give. You can use them for major occluders in the world
and noclip fly right through them without any problems at all.

Stencil shadows still aren't cheap by any means. It can cost 3x the
triangle count of the source model (although <2x with some optimizations is
reasonable) per shadowing light, and it can have pathological fill rate
utilization in some cases, like a light shining out horizontally through a
jail cell door. Still, they are quick operations even if there are a lot
of them. The vertexes are just bare xyz points without texcoords or color,
and the fill rate is only to the depth/stencil buffer.

There are lots of subtleties to actually using this, like making sure your
shadow volumes are capped on both ends if they need to be (you can often
optimize away the caps based on culling information), making sure that none
of the shadow volumes get clipped off by your far clipping plane (which
would unbalance the count), and all the normal picky silhouette
optimization issues.

Depth buffer based shadows still sound like they have a lot of advantages:

Not much in the way of coding subtleties required.

The performance is more level (fixed fill rate overhead) and theoretically
somewhat faster (only one extra drawing of the surface into the shadow
buffer) in most cases.

They avoid the silhouette finding work that still needs to be done with the
shadow volumes (a per-face dot product and some copying), and don't require
any connectivity information.

Unfortunately, the quality just isn't good enough unless you use extremely
high resolution shadow maps (or possibly many offset passes with a lower
resolution map, although the bias issues become complex), and you need to
tweak the biases and ranges in many scenes. For comparison, Pixar will
commonly use 2k or 4k shadow maps, focused in on a very narrow field of
view (they assume projections outside the map are NOT in shadow, which
works for movie sets but not for architectural walkthroughs), along with 16
jittered samples of the shadow map for each pixel and occasional hand
tweaking of the bias.

I still want to research the options for cropping and skewing shadow depth
buffer projection planes, but I am now positive that the stencil shadow
architecture works out.


John Carmack


分享到:
评论

相关推荐

    doom3源代码

    This comes after John Carmack had to work-around Carmack's Reverse due to legal issues. The source drop is right in line with what we were expecting. More details to come soon on the open-source ...

    carmack卷轴算法

    NULL 博文链接:https://wayfarer.iteye.com/blog/417646

    [OpenGL(C++)] - (开源)3D 游戏界的大牛人 John Carmack 终于放出quakeIII(雷神之锤3)的源代码

    [OpenGL(C++)] - (开源)3D 游戏界的大牛人 John Carmack 终于放出quakeIII(雷神之锤3)的源代码

    传说中的doom启示录中文版

    本书首先是为众多的游戏玩家,尤其是John Carmack和id software 的忠实拥趸们准备的,而对更多的人,无论你是否玩DOOM游戏,无论你是否崇拜John Carmack,都会从John Carmack和id software的成功中获得启示。...

    《DOOM启示录》又名: Masters of Doom:How Two Guys Created an Empire and Transformed Pop Culture

     本书首先是为众多的游戏玩家,尤其是John Carmack和id software 的忠实拥趸们准备的,而对更多的人,无论你是否玩DOOM游戏,无论你是否崇拜John Carmack,都会从John Carmack和id software的成功中获得启示。...

    Quake II .NET Source.Quake2的C#版本

    This is the complete source code for Quake 2, version 3.21, buildable with visual C++ 6.0. The linux version should be buildable, but we haven t tested it for the release. ...John Carmack Id Software

    雷神之锤3 VC++完整源码(Quake 3 Arena)

    3D 游戏界的大牛人 John Carmack 终于放出了 Q3 的源代码,John Carmack向我们兑现了他公开《Quake 3 Arena》游戏源代码的承诺,这不但另业界震惊,也让VC的FASN们异常兴奋,这套Quake 3 Arena Full Source Code v...

    quake3代码

    事实上早在90年代初DOS时代,只要能在PC上搞个小动画都能让人惊叹一番的时候,John Carmack就推出了石破天惊的Castle Wolfstein, 然后再接再励,doom, doomII, Quake...每次都把3-D技术推到极致。他的3D引擎代码资...

    Doom启示录

    本书首先是为众多的游戏玩家,尤其是John Carmack和id software 的忠实拥趸们准备的,而对更多的人,无论你是否玩DOOM游戏,无论你是否崇拜John Carmack,都会从John Carmack和id software的成功中获得启示。

    卡马克开源大作QuakeII

    This is the complete source code for winquake, glquake, quakeworld, and glquakeworld. The projects have been tested with visual C++ 6.0, but masm is also required ...John Carmack Id Software

    卡马克早年大作Quake I

    This is the complete source code for winquake, glquake, quakeworld, and glquakeworld. The projects have been tested with visual C++ 6.0, but masm is also required ...John Carmack Id Software

    quakeIII 源码终于发布啦

    3D 游戏界的大牛人 John Carmack 终于放出了 Q3 的源代码,借贵宝地,将其发上来。希望对有志于 3D game 的朋友带来更多帮助代码结构很清晰,今天只看了一下就上了隐,要不早就传上来啦 @_@ ..........

    finger.farm:Finger.Farm现代手指协议托管... Node中的一种手指实现

    从前,您可以指责老板,自动售货机,天气,约翰·卡马克(John Carmack)等。用户可以共享.project和.plan文件,显示其当前的项目和计划。 原始的网真。 原始物联网。 原来的微博。 尽管它已经过时了,但是手指...

    DOOM启示录.chm

    DOOM启示录chm版,讲述carmack的传奇

    id_tech5_clipmap_.rar_3d tech3d 継父_Clipmap d3d_[Tech3d] 継父_dyna

    ID TECH5是PC 3D游戏之父和DOOM之父John Carmack最新推出的一项新的游戏制作技术.核心内容为一种命名为"magatexture"的动态贴图管理技术.实际上是一种动态卸载和装载渲染资源技术的统称. 本文主要对引擎中几种技术做...

    MyICQ是一套公开源代码的即时通讯软件

    MyICQ 0.8 alpha1测试版 ==================================================== --------------- 1. MyICQ是什么? --------------- ... ----- John Carmack 什么叫专业,这就... ----- 张勇:-)

    即时通myicq源代码

    MyICQ 0.8 alpha1测试版 ==================================================== --------------- 1. MyICQ是什么? --------------- ... ----- John Carmack 什么叫专业,这就... ----- 张勇:-)

    Quake3 Arena 源代码

    在QuakeCon 2005大会上,John Carmack曾表示,《Quake 3》源代码将在近期发布,任何有兴趣的人都可以下载。现在,id Software兑现了自己的诺言:《Quake 3 Arena》完整源代码1.32b版(Quake 3 Arena Full Source Code...

    游戏Quake3源代码

    在QuakeCon 2005大会上,John Carmack曾表示,《Quake 3》源代码将在近期发布,任何有兴趣的人都可以下载。 现在,id Software兑现了自己的诺言:《Quake 3 Arena》完整源代码1.32b版(Quake 3 Arena Full Source ...

    doom启示录(txt)

    程序员的故事--《毁灭战士》和《雷神之锤》的创作者:约翰·卡马克(John Carmack)和约翰·罗梅洛(John Romero)

Global site tag (gtag.js) - Google Analytics