How do you get an object to cast a shadow?

I am drawing a sun the earth and the moon.
I want the earth to cast a shadow on the moon and vise versa.

I placed a spotlight in the center of the sun, turned off the lights, rendered the sun, then turned on the lights and rendered the earth and moon. The earth and moon are shaded properly, i.e. there is night and day on the earth and moon, yet neither is casting any shadows…

Any ideas?

OpenGL computes “local illumination” which is only a function of the vertex being lit and its properties (like position and normal) and the light source and its properties.

Other renderers (like ray tracers) compute “global illumination” which include not just the surface being lit and the light source properties, but also the stuff in between.

If you want shadows in OpenGL, you have to do them yourself. For the case you describe, though, they’re not too hard.

Thanks -
Cass

Despite Cass’s pessimism, go to the nVidia
web site and check out the article on shadow
volumes using the stencil buffer. Or do a
google search for the same name.

Basically, you project each object from your
light source and render these projections
into the stencil buffer, then you render the
actual scene with the light on in the places
where the stencil isn’t set, and finally you
render the scene with the light off in the
places where the stencil is set.

(yeah, I know, it’s slightly more involved
than that, but that’s the basic idea)

Sorry if I was unclear about this - you can do shadows in OpenGL, but OpenGL built-in per-vertex lighting does not.

i think when your planets/moons are detailed enough (enough vertices), you just have to calculate for every vertex, if the line between it and the lightsource ( sun, i suppose ) hits another object… search about ray->sphere intersection, and you should success…

I checked out the nVidia site as that seems to be promising. However the developer page where I believe articles are stored seems to be broken… Any other links?

[QUOTE]Originally posted by bgl:
[b]Despite Cass’s pessimism, go to the nVidia
web site and check out the article on shadow
volumes using the stencil buffer. Or do a
google search for the same name.

Originally posted by davepermen:
i think when your planets/moons are detailed enough (enough vertices), you just have to calculate for every vertex, if the line between it and the lightsource ( sun, i suppose :slight_smile: ) hits another object… search about ray->sphere intersection, and you should success…

Yikes. For the special case you’re dealing with, where both potential shadowers are (as near as makes no difference) spheres, this is overkill though. If you want to try the stencil volume approach then the shadow volume will always be a cut-off cone; you don’t need to process all the vertices.

I think you’d find it a lot easier, though, to use a projective texture with a big round shadow in the middle and the rest of it transparent.

(If you want to get really fancy, you could even implement the shadowing body as a spotlight with a negative colour (“darklight”); this should give you the nice penumbra soft-shadows you see in real lunar eclipses.)