天天看點

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.

繼續閱讀