Wednesday, 17 April 2013

Assignment 12: Shadows

Requirements
  • Create a new "Shadow" technique for your opaque Effects
    • This technique needs to write depth in the "view" space of the light; I would advise using the same shaders that you used for the Depth technique.
    • You should write to the depth buffer and do a less-than-or-equal depth test
  • Generate a Shadow Map before rendering anything else in the frame
    • You will need to create a new render target. This will be just like the view depth one that you already have.
      • In a real game you would choose a different resolution, but I advise using the same resolution as all of your other render targets to keep things simple (this means you can re-use the same hardware depth buffer)
    • Set the shadow map as the render target, and clear "color" and depth
      • Remember that "color" is what you will be storing your depth in
    • Render all of your opaque objects from the light's perspective
      • You must use a directional light for this to work
      • Instead of getting the world-to-view transform from the camera you will need to set a world-to-light transform. If you have re-used the depth shaders for your Shadow technique then you will still be setting the variables named "world-to-view", but you will be sending different data.
      • Instead of getting the view-to-projected transform from the camera you will need to set a light-to-projected transform.
      • In the fragment shader you should output the position in light space (not projected space). This is analogous to using view space in the Depth technique; you want a nice linear value in world units.
        • (You should normalize this to [0,1] using the light's far plane value)
  • Now render the rest of your Techniques like you did before
    • Depth Technique
      • After setting your view depth as the new render target you will want to clear "color" again (which should be obvious), but you will also want to clear depth again (which may not be as obvious). This is because the depth buffer will currently be filled with depth from the light's perspective, and you now want to render things from the camera's perspective
    • Opaque and Translucent Techniques
      • Everything about these will be the same, except that you can now add shadows (for those shaders that are lit)
      • Set the shadow map as a sampler
        • Do this the same way that you've set the view depth and color buffers. The big difference here is that you can also set it for the Opaque Techniques. Hurray!
      • Calculate whether something is in shadow
        • You will need the position in light space
          • In your vertex shader you will need the world-to-light transform
          • Use that to calculate the fragment's position in light space and pass it on to the fragment shader
        • Compare the fragment's position with the position stored in the shadow map
          • Make sure to convert the shadow map lookup from [0,1] to light space
          • If the fragment's position is less-than to the shadow map value it should be lit, and otherwise it is in shadow
            • You will probably need to add a so-called "epsilon" value (something small) to the comparison to deal with precision problems. My calculation looks like this: §  shadowModifier = depth_current < depth_previous + epsilon;
      • That "shadowModifier" value will be 0 or 1, and I can then multiply it by the lightingamount just like I would use the dot product and the intensity.
  • I must be able to move the light so that I can make sure the shadows are working correctly
    • (Moving a directional light is not as intuitive as a point light. As long as you come up with something I will be ok with it; make sure to document your controls in your writeup)
  • You must have a floor plane and at least two objects above it that cast shadows
    • I should be able to see the objects' shadows on the floor plane
    • I should be able to see one of the object cast a shadow on the other one
  • Your writeup must include a screenshot from PIX of the shadow map (you may need to adjust the histogram to make it easy to see)
Details

World-To-Light Transform
  • I advise still using D3DXMatrixLookAtLH()
  • Set the the Look At point to the origin (0, 0, 0)
  • Set the Up as (0, 1, 0) (this can cause problem if the light direction gets parallel with it, but should be ok for our purposes :)
  • Set the Position using the negative light's direction and an offset. For example:
D3DXVECTOR3 offset = -m_direction * 10.0f;
  • The offset (the hardcoded 10 in my example above) should be outside of all of the scene so that no objects end up in back of the light. Assuming you have a small scene like I do then 10 or thereabouts will be ok. Don't make it too big because then you will run into problems with the projection and near/far planes
Light-To-Projected Transform
  • I advise using D3DXMatrixOrthoLH()
    • This is a so-called "orthographic" projection. Unlike using perspective like we've been used to objects don't get smaller as they get further away with an orthographic projection. This maps onto the concept of a directional light, whose rays are parallel
  • You can probably use the same near and far plane settings you've used for perspective. In general, you want the range of these to be as small as possible (the smaller the range is the better your shadows will look). You should try to make the far plane big enough so that none of your objects get clipped by being too far away from the light, but no bigger.
  • For the width and height you want to maintain the same aspect ratio of your shadow map, so start with the width and height of it, and then divide by some constant value. The bigger these numbers are the more things will fit into the shadow map but the worse it will look (because more things fit meaning they get less resolution), and the smaller these numbers are the fewer this will fit but the better it will look. Try experimenting, but as a point of reference I am dividing by 10 in my example program which works fairly well.

2 comments:

  1. ka astrella kapan lagi posting novelnya , aku suka banget sama novel kakak di IMAGINATIVE WONDERLAND , aku masih nunggu kakak posting novelnya kakak :) - putri -

    ReplyDelete
  2. ka astrella kapan lagi posting novelnya , aku suka banget sama novel kakak di IMAGINATIVE WONDERLAND , aku masih nunggu kakak posting novelnya kakak :) - putri -

    ReplyDelete