Simple Shadows

Hi guys!
Here I am posting some picture of my projects as I promised so long ago!
I hope you like them :slight_smile:

I got the trees and animated leaves with my particle system (winter picture shows some leaves flowing in the wind), and now trying to add shadows…

Performance is a very big issue for me so I want to implement a solution with the lowest FPS impact, and I was considering a solution similar to shadow maps (but without generating them on runtime everytime). Please tell me what do you think about this:

For each ‘scenario’ bake a shadowmap and save it in a 1024x1024 image file. Then load the image as a blended texture on the terrain using multitexturing. I just need plants to produce static shadows using also the texture data of the plants (the trees are made by polys and contain transparent texture rendered with alpha test, so I would need leaves depicted in the texture also produce static shadows).

If you consider this a good idea (I am looking for something easy to implement too) I would greatly appreciate if you tell me how to do the following:

Could anyone post the code on how to do this:
-project each plant to the terrain in black color to reproduce shadow given a light direction and save to disk into an image that contains all plants ‘shadows’ (I suspect it has something to do with stencil buffer but I am totally lost on how to do it, nor on how to save a texture to disk).

I have read a lot of papers on Shadow Mapping, dynamic shadow mapping, and all seem really difficult as they talk about solutions using plain opengl, others using extensions, and others using shaders (‘hardware shadow mapping’). I read about the two passes that are needed rendering the scene from the light source and then from camera view, the problems about the texture definition, the advantages and disadvantages vs volume shadows, blah, blah. Then I found like thousands of different approaches like Adaptive Shadow Maps (ASM) vs Light Space Perspective (LiSPSM), or Perspective Shadow Maps (PSM), or ‘Adaptive Shadow Maps’… ‘Variance Shadow Maps’… sorry but I am completly lost and I can’t find one plain and SIMPLE example! :confused:
So I would greatly appreciate if you could help me with the code above! Thanks!

Thanks so much in advance!

Since you have textures with alpha test you can’t use shadow volumes if you want these polygons to cast shadows, too.
So you need shadowmaps. For static shadows (not casted by moving objects or by moving lights) you can use lightmaps.
Seems like your knowledge on shadowmaps is very limited - in that case I would suggest you implement the very basic shadowmaps to get some experience and knowledge.
All the modifications of shadowmaps are meant to overcome various limitations of shadowmaps, like limited size or limited precision. Some other are meant to create effect of soft shadows.
If you’re creating outdoor shadows from one static, directional light source (the Sun), then you have the simplest case of all because you only need orthogonal shadow maps and not the perspective ones.

Originally posted by k_szczech:
For static shadows (not casted by moving objects or by moving lights) you can use lightmaps.

Yes! That’s exactly what I wanted… baking lightmaps!

Originally posted by k_szczech:

If you’re creating outdoor shadows from one static, directional light source (the Sun), then you have the simplest case of all because you only need orthogonal shadow maps and not the perspective ones.

… I know it’s supposed to be really simple but I don’t know where to start at all… Could you at least point as to which are the functions that I must use to:

  1. project objects to make shadows
  2. save the resulting texture to disk to then use it as a lightmap

Thanks so much in advance!

There are two major differences between lightmaps and shadowmaps:
-lightmap include both shadow data and lighting data for every light source
-shadowmap include only shadow data for one light source
-lightmaps are one per polygon (every polygon has it’s own lightmap and does not share it with others)
-shadowmaps are one per light source

So, shadowmaps can be simplier to implement, but lightmaps will be faster.

Generating lightmap data is a bit complex, so I won’t describe it here. Lightmapping is related to painting on a mesh. I’ve found this little program recently - perhaps iw would be of interest to you if you plan on creating good lightmaps:

As for shadowmaps - look at these two tutorials:

Thanks k_szczech!
I got it much clearer now… then I know that I what I want is to generate lightmaps!

As you mentioned that baking lightmaps is difficult to implement I searched on the internet and found irrEdit (Irrlicht3d’s Free editor) that generates lightmaps. I am considering using this as my world editor for generating the lightmaps and then adjusting the files to match my current engine, although I am not sure it’s a good idea as I was already with a simple editor in mind using my own engine, and I might have to recreate everything because of the lightmaps…
Anyways, I managed to create some simple lightmaps from this editor, but I am not really sure on how to implement them:

Originally posted by k_szczech:

-lightmaps are one per polygon (every polygon has it’s own lightmap and does not share it with others)

Did you mean one per mesh?! I was thinking I would have for example 1 lightmap texture for my whole terrain, unless what you mean is that the texture contains as many lightmaps as polygons and then the engine does the rest using texture coordinates selecting the appropiate textures for each poly?

Well with Irrlight editor I think that happened… I got a really weird lightmap that I have no clue on how to implement… I suppose the following lightmap contains lighting information for many polys in one single texture that I must then use to blend with multitexture… Am I correct?
The lightmap:

Since I only just need to lightmap the terrain I think that maybe I will be easier to create the lightmap myself instead of decoding IrrEdit? What do you suggest?

Thanks so much in advance!

P.S: By the way… there almost no tutorial that explain lightmap generation. Is it such a difficult topic? I guess that the main reason is that you must have an editor first is that right?

You’re right - you can have many lightmaps in one texture. One mesh can use many textures with lightmaps as well as one texture can be used for many meshes. The only thing that needs to be assured is that every polygon in the scene has it’s own, “private” fragment of lightmap texture just for itself. You can then have illumination and shadows received by that polygon painted on it’s texture.

If you don’t want the trees to be self-shadowed and only cast shadow on the ground then you’re in luck - this will be very easy:

  1. Create one big texture that will cover entire terrain.
  2. Render directly to this texture using EXT_framebuffer_object or just render to screen and then copy to this textur using glCopyTexSubImage2D.
  3. When rendering to texture first clear screen with white color and set ortho projection matrix that covers entire texture (2D rendring).
  4. Then render all your trees using black color - look at them from top

If you want your shadows to be casted at some angle, then rotate everything around, let’s say X axis, before you render trees to texture. Then you only need to adjust texture coordinates for your terrain for such rotated lightmap.
Normally your lightmap texture coordinates would vary from (0.0, 0.0) to (1.0, 1.0), but after rotation you’ll get:
(0.0, 0.5 - cos(angle) * 0.5) to (0.0, 0.5 + cos(angle) * 0.5)

Hi k_szczech!
Thanks so much for everything.
In this time I managed to implement an ASE loader that loads lightmaps created with 3D Studio Max :slight_smile: .

Now that I have more experience I am trying to approach your solution again, as there are some objects I create on runtime and I want them to cast shadows on the flat terrain.

Regairding your solution,
how can I render the trees using black color. Is this a good approach (pseudo-code):

Camera Look from top
Clear Background to white
Draw Plants
Copy Screen 

Another concern: my ground is actually really large and doesn’t fit on the screen so I guess I would have to use EXT_framebuffer_object. Do you still recommend me this approach or should I head for shadowmapping?

I want -static- soft shadows with almost no performance hit that are created at the beggining of scene. I am ready to go for shaders, so if it’s a better approach to use GLSL, please point me in the right direction.

Thanks so much! :slight_smile: