3D Graphics Overview
Getting Started with 3D
In the examples in this section, you will see that the most simple 3D graphics application you will need to provide three items:
- A set of world, view, and projection transformation matrices
- A vertex buffer
- An effect that applies the world, view, and projection transformation matrices to the vertex data.
This list of items will allow you to render your first 3D content in an XNA application. As you become comfortable with these ideas, you will want to learn more about manipulating the vertices you have created, creating your own effects, applying textures, and improving the performance of your application through such techniques as converting your vertex buffers to indexed vertex streams.
The XNA Framework utilizes a shader-driven programmable pipeline and requires a graphics card capable of at least Shader Model 1.1. Shader Model 2.0 is recommended. In the programmable pipeline, shaders and effects are used to apply transformations, textures, lighting and materials. However, you do not need to write a custom shader for your first 3D XNA applications. The XNA Framework provides a class called BasicEffect that encapsulates most of these common operations.
The Graphics Device
When you create a game with XNA Game Studio Express, a graphics device is initialized for you by the XNA application model. You can gain access to the graphics device through the GraphicsDeviceManager.GraphicsDevice property. The capabilities of the graphics device may be obtained using the GraphicsDevice.GraphicsDeviceCapabilities property.
A resource is a collection of data stored in memory that can be accessed by the CPU or GPU. Types of resources that an application might use include render targets, vertex buffers, index buffers, and textures.
Based on the resource management mode that was used when a resource is created, it should be reloaded when the device is reset. For more information, see How to: Load Resources.
Vertex and Index Buffers
A vertex buffer contains a list of 3D vertices to be streamed to the graphics device. Each vertex in a vertex buffer may contain data about not only the 3D coordinate of the vertex, but also other information describing the vertex, such as the vertex normal, color, or texture coordinate. The XNA Framework contains several classes to describe common vertex declaration types, such as VertexPositionColor, VertexPositionColorTexture, VertexPositionNormalTexture, and VertexPositionTexture. You may also use the VertexElement class to compose custom vertex types.
For a demonstration of low-level rendering of a 3D object from a vertex buffer, see How to: Draw Points, Lines, and Other 3D Primitives.
Vertex buffers may contain either indexed or non-indexed vertex data.
If a vertex buffer is not indexed, all of the vertices are placed in the vertex buffer in the order they are to be rendered. Because 3D line lists or triangle lists often reference the same vertices multiple times, this can result in a large amount of redundant data.
Index buffers allow you to list each vertex only once in the vertex buffer. An index buffer is a list of indices into the vertex buffer, given in the order that you want the vertices to render.
To render a non-indexed vertex buffer, call the GraphicsDevice.DrawPrimitives Method or GraphicsDevice.DrawUserPrimitives Generic Method. To render an indexed buffer, call the GraphicsDevice.DrawIndexedPrimitives Method or GraphicsDevice.DrawUserIndexedPrimitives Method.
A texture resource is a structured collection of data designed to store texture data. The data in a texture resource is made up of one or more subresources that are organized into arrays and mip chains. Textures can be filtered by texture samplers as they are read by shaders. The type of texture influences how the texture is filtered.
You can apply textures by using the Texture property of the BasicEffect class, or choose to write your own effect methods to apply textures. For a demonstration of applying a texture from a user-created effect, see the example How to: Create Custom Texture Effects.
Shaders and Effects
Shaders are programs that do per-vertex or per-pixel processing of resources at run time. An effect is a combination of vertex and pixel shader code grouped together to encapsulate a particular rendering effect. Because shaders transform vertices and pixels at run time, they are used for lighting, materials, and textures. Unless you are using the BasicEffect class, you must first write shaders in either high-level shader language (HLSL) or assembly shader code (ASM) and then compile your shaders before your application can use the binary code.
For overviews of HLSL and the effect file format, see the HLSL Shaders and Effects DirectX Programming Guides on MSDN. Complete reference documentation for HLSL, shader ASM, and the effect file format is available in the Direct3D API Reference.
For a simple demonstration of how to apply a vertex shader from an effect file, see How to: Create and Apply Custom Effects.