天天看点

3D Graphics OverviewGetting Started with 3DThe Graphics DeviceResourcesShaders and Effects

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
  • The Graphics Device
  • Resources
  • Shaders and Effects

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

最简单的3D游戏也要涉及到的几个items:

1.世界,视图和投影的转换矩阵。

2.多边形定点缓冲

3.应用到世界,视图,投影矩阵的effect,实在不知道怎么翻译好

This list of items enables you to render your first 3D content in an XNA application.

这些items可以帮助我们渲染3D内容。

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.

使用XNA的BasicEffect,可以直接调用封装好的Effect。

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

The GraphicsDeviceManager initializes the GraphicsDevice before you call Game.Initialize. Before you call Initialize, there are three ways to change the GraphicsDevice settings:

注意:在调用Intialize之前,graphic device就被初始化了。如果想要改变设置,有下面的方式:

  1. Set the appropriate properties (e.g. PreferredBackBufferHeight, PreferredBackBufferWidth) on the GraphicsDeviceManager in your game's constructor. 在构造器中设置。
  2. Handle the PreparingDeviceSettings event on the GraphicsDeviceManager, and change the PreparingDeviceSettingsEventArgs.GraphicsDeviceInformation.PresentationParameters member properties. 相应PreparingDeviceSettings事件。
  3. Any changes made to the PreparingDeviceSettingsEventArgs will override the GraphicsDeviceManager preferred settings.修改PreparingDeviceSettingsEventArgs。
  4. Handle the DeviceCreated event on the GraphicsDeviceManager, and change the PresentationParameters of the GraphicsDevice directly. 响应DeviceCreated事件。

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.

关于GraphicDevice的设置,例如:backbuffer,depth/stencil buffer, viewport,render states.

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.

注意:我们修改PresentationParameters时,需要调用GraphicsDeviceManager.ApplyChanges,否则不会生效。

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.

Render states是graphic device的重要组成。

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

CullMode.CounterClockwise

.

renderState.CullMode = CullMode.None;      

通常,RenderStates需要手动设置。

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.

一个自动设置RenderStates的例子。就是在End之后,XNA会自动设置graphic device的属性来渲染3D物体。

 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[0].AddressU = TextureAddressMode.Wrap;
        GraphicsDevice.SamplerStates[0].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.

我承认,上面的话不是很懂。以后再来补充。

Resources

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.

一个vertex数据包含的内容。除了3D坐标,还有vertex normal,颜色,纹理坐标。

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.

XNA中描述vertex数据的类。

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.

vertex缓冲包含indexed和非indexed vertex数据。

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.

非indexed vertex数据的问题。

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.

indexed vertex数据可以保证不出现数据冗余。

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.

针对indexed和non-indexed vertex缓冲的渲染,调用方法也不一样。

Textures

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.

shaders是一组程序,用于在运行时处理每个vertex和pixel.通过不同的组合,可以使用shader来实现不同效果的渲染。

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.

如果我们不使用XNA提供的shader,我们需要自己编写shader来渲染。

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.

继续阅读