问题
I recently created some landscape code and added some diffuse lighting to the scene, however, to my disappointment, there are no shadows. I looked around the web for hours looking for ways to get shadows in OpenGL, however they all seemed terribly complicated; and very unique to their own demo programs.
Are there any simple ways to make shadows?
回答1:
No. Rasterization is very bad at this (even recent AAA games have noticeable shadow artefacts), but everybody lives with it.
Solutions include (approx. from easiest/poorest to best/hardest) :
- No shadows. Simply account for occlusion with darker colors. xNormal, Blender.
- If you want an approximate shadow for a character, a simple flat polygon on the ground with a transparent and blurry texture will do. See Zelda screenshots, for instance. Even some recent games still use this.
- Lightmaps. Static geometry only, but perfect lighting (precomputed). Reasonnably simple to implement. Lots of tools exist.
- Shadow volumes, popularised by Carmack. Pixel perfect, reasonnably simple to implement, quite slow. Good for a few objects. No soft shadows.
- Shadow maps. A little hard to implement if you never made any openGL. Hard to get right. Pixellated shadows. Deals with lots of polygons. Doesn't deal with big worlds.
- Myriads of Shadow maps variants. Lots of research these recent years. Current best is Cascaded Shadow Maps : Difficult, still hard to make it look good, but fast, deals with loads of polygons and huge worlds.
- Raytraced shadows : This may be the next-gen. Nobody really uses that except for some research papers. Very complicated, doesn't do well with dynamic worlds (yet), huge static scenes ok. Pixel perfect or soft shadows, depending on how much spare GPU you have. Several variants; as of 2014 this still didn't make in any game for performance reasons.
So the usual trick is to mix beautiful-but-static-only approaches with dynamic-but-not-that-good approaches. For instance, see my tutorials on lightmapping and shadowmapping.
回答2:
No.
The easiest way I know of involves using a pregenerated shadow texture that is overlaid onto the terrain using multitexturing. The complicated part is generating this texture, but if you don't use directional lighting, a simple "big blurry dot" is usually better than nothing.
来源:https://stackoverflow.com/questions/3849052/is-there-an-easy-way-to-get-shadows-in-opengl