tailieunhanh - Building XNA 2.0 Games- P13

Building XNA Games- P13: I would like to acknowledge John Sedlak, who saved this book from certain doom, as well as all of the great guys in the XNA community and Microsoft XNA team, who helped me with all of my stupid programming questions. (That is actually the term used—“stupid programming question”—and it is a question that one should not have to ask if one has been approached to write a book about the subject.) | 348 CHAPTER 11 POSTPROCESSING EFFECTS technique Water pass P0 PixelShader compile ps_2_0 Water It s a bit of ugly trig but here s the gist we tell our shader where the water horizon is denoted by the dotted line in Figure 11-6 and then feed it two values delta and theta which are floats that range between 0 and 2 . Since we can t update variables from one to the next in a shader we ll be updating these values from Gamel and feeding them into our water shader. For the technique function we set as horizon - resulting in a flipped image where the line of symmetry is 0 panel 2 of Figure 11-6 . Then applying some trig functions which honestly were reached by trial and error we nudge our texture coordinates around a bit resulting in the rippling effect. Finally we smoothly fade out the top 20 of the image. As for implementing this in the game we have a problem. The way we have our map set up it would look really nice to have the water drawn between the main draw map characters particles and so on and the foreground. However if we kept everything else intact and introduced water between the main draw and the foreground we would need to either introduce another render target or draw the foreground twice. Let s see how the solutions would pan out. Adding a new render target goes like this Set render target to mainTarg Draw background and main characters particles and so on Set render target to waterTarg Draw with water effect Set render target to auxTarg DrawmainTarg andwaterTarg to auxTarg Set render target to bloomTarg 0 Draw auxTarg with bloom effect Set render target to bloomTarg 1 Draw auxTarg with bloom effect Set render target to gameTarg Draw auxTarg Draw bloomTarg 0 Draw bloomTarg 1 Set render target to backbuffer Draw gameTarg CHAPTER 11 POSTPROCESSING EFFECTS 349 Draw HUD Present And here s what we would need to do to draw the foreground twice Set render target to mainTarg Draw the game Set render target to waterTarg Draw with water effect Set render