16

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?

  • 3
    Not really. Shadows aren't built in to the pipeline. You have to generate them yourself (there are many ways to do this, each with it's own pros and cons) and render them yourself. – GManNickG Oct 03 '10 at 08:11
  • OpenGL does not make shadows, it renders every point as if there is no hinder between the point and lightsource – Amir Zadeh Oct 11 '10 at 15:03
  • 1
    Unless you want to learn the innards of graphics programming, you might want to use a graphics engine (such as Horde3D) or game engine (Ogre or Irrlicht) instead of pure OpenGL. It would save you from re-inventing the wheel (well, many wheels, probably) and let you jump straight into what you want to achieve. OpenGL is a low-level API. – Kos Oct 21 '10 at 10:08

2 Answers2

30

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.

Calvin1602
  • 9,413
  • 2
  • 44
  • 55
  • If you're trying to get shadows of the mountains with a static sun, lightmaps are the way to go. Export your map into any 3D format, open in Blender, follow any tutorial : you've got your lightmap. Mix with the texture with a shader (or directly in Photoshop if you're lazy), you're done. – Calvin1602 Oct 04 '10 at 17:53
  • Sorry, the pedant in me makes me comment: shadow volumes were first proposed in 1977 by Frank Crow. They're a technique, like BSP trees or that fast square root that you see around, that Carmack has popularised for games but definitely didn't invent. – Tommy Oct 18 '10 at 00:10
  • Thanks for the comment, I didn't know. Message updated accordingly. – Calvin1602 Oct 18 '10 at 11:17
  • I'd just like to point out that in Unreal Engine 4 realtime shadows aren't really "research". Decide if it's next-gen or not for you. – Bartek Banachewicz Jan 03 '13 at 13:44
  • If you're referring to Crassin's paper as used by UE4, voxels are only used for secondary "rays". There is still a shadowmap. – Calvin1602 Jan 04 '13 at 12:06
  • Hi Calvin, very useful list. However I would like to ask you if the render-to-texture shadow (http://download.java.net/media/jogl/www/ts1361.pdf) is also included in one of those points. Is it maybe too old as technique? – elect Apr 05 '13 at 08:02
  • @elect this is called Shadow Maps, still state of the art (at least, variants of it) – Calvin1602 Apr 05 '13 at 11:08
6

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.

Zooba
  • 11,221
  • 3
  • 37
  • 40
  • Oh, the even easier way is to use an engine with it already built in, but I assume that you're not interested in just building models for someone else's engine (fair enough, neither am I). – Zooba Oct 03 '10 at 08:13
  • No, I don't want to build models for someone elses engine. As for the answer above, where could I find out how to do this? –  Oct 03 '10 at 09:12
  • Any basic introduction to multitexturing will cover it. The trick is to set the texture to wrap (with transparent edges) and position it using the UV parameters on the terrain. Alternatively you can make an extra surface for the shadow, which is easy on a flat surface, but a bit more involved on a terrain... – Zooba Oct 04 '10 at 03:37
  • Alternatively, if you want terrain shadows, the easiest way is to turn your heightmap (assuming black is lowest) into a texture and apply it over the whole terrain. I was coming at the answer from a "providing a shadow for a character" POV, rather than adding terrain shadows. – Zooba Oct 04 '10 at 03:38