3D Graphics Overview
Provides an overview of 3D graphics. The Microsoft.Xna.Framework.Graphics namespace contains classes to use the graphics device to load and render resources and to apply effects to vertices and pixels. This overview covers the following topics as they apply to 3D graphics.
Getting Started with 3D
In the examples in this section, you must provide the following three items for even the most simple 3D graphics application:
- 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 enables 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 the following:
- Manipulating the vertices you created
- Creating your own effects
- Applying textures
- Improving the performance of your application through such techniques as converting your vertex buffers to indexed vertex streams
The XNA Framework uses a shader-driven programmable pipeline. It 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, the XNA application model initializes a graphics device for you. You can use the GraphicsDeviceManager.GraphicsDevice property to gain access to the graphics device. To determine the capabilities of the graphics device, use the GraphicsDevice.GraphicsDeviceCapabilities property.
Graphics Device Initialization
Handle the PreparingDeviceSettings event on the GraphicsDeviceManager, and change the PreparingDeviceSettingsEventArgs.GraphicsDeviceInformation.PresentationParameters member properties.
When you call Game.Initialize, GraphicsDeviceManager creates and configures GraphicsDevice. You can safely access GraphicsDevice settings such as the backbuffer, depth/stencil buffer, viewport, and render states in Initialize.
After you call Game.Initialize, changes to the PresentationParameters of the GraphicsDevice will not take effect until you call GraphicsDeviceManager.ApplyChanges. Other changes, such as render states, will take effect immediately.
Render States and the Graphics Device
Render states are an important part of the graphics device. They can affect game rendering significantly. When you first create a graphics device, the render states are set to their default values. For more information, see RenderStates.
Commonly, the value of a render state is changed only if it is manually modified. For example, the following code sample sets the cull mode of the current graphics device to
None. This causes all triangle faces to be drawn and differs from the default value of
renderState.CullMode = CullMode.None;
However, there are cases where render states can change automatically. A common example is the rendering of sprites and 3D objects on the same graphics device. In this case, the SpriteBatch object changes various render states when you call End. If you try any 3D rendering after this step, the results can be unpredictable.
For this reason, you should restore several key render states to their former settings before you try to render any 3D objects. The following code demonstrates this approach.
GraphicsDevice.RenderState.DepthBufferEnable = true; GraphicsDevice.RenderState.AlphaBlendEnable = false; GraphicsDevice.RenderState.AlphaTestEnable = false;
In addition, you should set the following render states, depending on the type of 3D content you are rendering.
GraphicsDevice.SamplerStates.AddressU = TextureAddressMode.Wrap; GraphicsDevice.SamplerStates.AddressV = TextureAddressMode.Wrap;
It is also possible to save the render state of the graphics device before rendering sprites by passing SaveStateMode.SaveState your SpriteBatch.Begin method call. However, this is not recommended because it is time intensive, and can slow down the rendering process.
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 Content.
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 mipmap 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 perform 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.