XNA 2.0 Game Programming Recipes
Chapter 5: Getting the Most Out of Vertices (144 pages)
Whether youíre creating a 3D game or some visualization software, in many cases youíll want to render primitives such as points, lines, or triangles. All of these primitives are defined by vertices, which are the corners of your triangles or the ends of your lines.
Vertices contain not only a position; they can contain all kinds of data. They can specify which color the corner of your triangle should be rendered in. Or, if youíre using a texture to put over your triangle, the vertices should indicate which point of your texture should be mapped to which corner of your triangle. These are only two examples of an almost endless list of data
that can be contained in your vertices.
This chapter starts by showing you the basics: how to define vertices and how to render points, lines, or triangles from them. Next in line are texture coordinates and normal data, because most vertices usually store this data.
Next, the chapter covers some powerful functionality provided by XNA, including static and dynamic VertexBuffers and IndexBuffers, which let you store your vertices in the memory on the graphics card. This is applied in practice when you create a whole 3D terrain defined solely by vertices. Since a terrain (or a grid in general) is quite commonly used in game programming, this chapter deals with some common challenges such as bilinear height determination as well as surface picking. Using Catmull-Rom interpolation, you can even generate extra
vertices to make any terrain as detailed as you like!
Since vertices are the data that is sent to your graphics card, this chapter explains why vertex formats are the link between your XNA project, your vertices, and your vertex shader on your graphics card.
Using HLSL, youíll learn how to add bump mapping to your object to increase the detail of the pixels in your triangles. In the end, youíll even learn how to add an impressive ocean to your 3D world by letting your graphics card manipulate all of your vertices and pixels.
This chapter also provides some advanced content pipeline examples. Youíll learn how to serialize and deserialize complex objects, such as arrays and objects that require a link to the device. Furthermore, youíll learn how to define your own content importer and how to load an object from an XML file into your XNA project.
5-1. Render Triangles, Lines, and Points in a 3D World
This basic recipe introduces you to vertices, and why you need them to render triangles.
5-2. Apply a Texture to Your Triangles
This recipe shows you how you can cover your triangles with 2D images, and discusses all available texture addressing modes.
5-3. Remove Redundant Vertices Using Indices
Most triangles of 3D objects share some vertices. Learn how to remove redundant vertices in this recipe.
5-4. Store Your Vertices and Indices in the Memory of Your Graphics Card Using a VertexBuffer and an IndexBuffer
Vertices that donít change their data too frequently should be saved on the fast memory of the graphics card. This recipe shows you how this can be done using VertexBuffers and IndexBuffer.
5-5. Store Your Vertices in a DynamicVertexBuffer for Frequently Updated Data
For vertices that are changed frequently, you should use the Dynamic version of the buffers explained in the previous recipe.
5-6. Enable Backface Culling: What It Is and What It Can Do for You
Backface culling is nothing advanced, but you need to understand it purpose and how it works before you can create your own 3D applications.
5-7. Automatically Calculate the Normals for All Vertices in a VertexBuffer
This recipe explains you how you can automatically calculate the normals for your vertices, both in case of a TriangleList and TriangleStrip. The resulting normals are mathematically correct.
5-8. Create a Terrain Based on a VertexBuffer and an IndexBuffer
This recipe shows the preferred way of rendering a terrain as a trianglestrip, based on a 2D heightmap image.
5-9. Calculate the Exact Height of a Terrain Between Vertices Using Bilinear Interpolation
Before you can put Models on your terrain or perform terrain picking, you need to be able to calculate the exact height of your terrain at any point.
5-10. Calculate the Collision Point Between the Pointer and the Terrain: Surface Picking
Using the result of the previous recipe, you learn how to detect the exact 3D location on the terrain where the mouse pointer is over. This is done by narrowing down your searching space using a linear search, after which lock to the exact position using a binary search.
5-11. Extend the TextureProcessor to Generate a Terrain Object from an Image: Advanced DOM Objects
This advanced Content Pipeline recipe shows you how you can serialize more advanced objects, such as arrays and objects that require a link to the graphics device.
5-12. Write a Custom Content Importer: Loading a Terrain from a CSV File
5-13. Load Data from an XML File
This recipe explains how you can read in data from XML files, allowing you to create and adjust game content through XML files.
5-14. Create Your Own Vertex Format
Before moving over to HLSL, you need to understand the link between the vertices defined in your XNA application, and the vertex shader in your HLSL code. The Vertex Format links them together.
5-15. Introducing Bump Mapping: Fixed Normal
Since Bump Mapping can seem difficult at first sight, Iíve split this up into 2 HLSL recipes. This first recipe bump maps a flat surface, adding more per-pixel detail as can be seen in the image below.
5-16. Adding Per-Pixel Detail by Bump Mapping in Tangent Space
This recipe explains full bump mapping in Tangent Space, so it can be applied to any object.
5-17. Add an Ocean to Your 3D World
This is where the image at the top of these pages comes from. Learn how you can add a convincing 3D ocean to your world using this advanced HLSL effect. Best of all, the movements of the water is calculated entirely on your graphics card!
5-18. Apply Catmull-Rom Interpolation in 3D to Generate Additional Vertices
The XNA framework contains a very useful Catmull-Rom implementation, so it should definitely be discussed! CR interpolation can be used to generate details up to a level of your choice.
5-19. Create the Vertices for a Racing Track
Create a complete racing track, starting simply from a couple of 3D coordinates! Catmull-Rom interpolation is used to generate the extra points. The extra implementation even allows for loopings in your track!
Recipe 5-16: The lighting details on this object have been increased using bump mapping.
- Website design & XNA + DirectX code : Riemer Grootjans -
©2003 - 2011 Riemer Grootjans