What Is

What Is Color Blending?

XNA Game Studio 3.1

Color blending is the process of mixing two colors together to produce a third color.

The first color is called the source color. This is the new color being added. The second color is called the destination color. This is the color that already exists (in the backbuffer, for example). Each color has a separate blend factor that determines how much of each color is combined into the final product. Once the source and destination colors have been multiplied by their blend factors, the results are combined according to the specified blend function. The normal blend function is simple addition.

The full formula looks like this:

(source × source blend factor) (blend function) (destination × destination blend factor)  

The source blend factor is specified by the SourceBlend.aspx) property, and the destination blend factor is specified by the DestinationBlend.aspx) property. The BlendFunction.aspx) property specifies the blend function to use, normally BlendFunction.Add. In that case the formula looks like this:

(source × SourceBlend) + (destination × DestinationBlend)  

When the AlphaBlendEnable.aspx) property is false, no blending occurs during rendering. In this case, the source pixel overwrites the destination pixel. When AlphaBlendEnable.aspx) is true, you can create a lot of special effects using the SourceBlend.aspx) and DestinationBlend.aspx) properties:

Blend TypeBlend Settings
Alpha Blending(source × Blend.SourceAlpha) + (destination × Blend.InvSourceAlpha)
Additive Blending(source × Blend.One) + (destination × Blend.One)
Multiplicative Blending(source × Blend.Zero) + (destination × Blend.SouceColor)
2X Multiplicative Blending(source × Blend.DestinationColor) + (destination × Blend.SourceColor)

https://i-msdn.sec.s-msft.com/dynimg/IC302966.jpg

Figure 1.  This picture illustrates four common blend modes. From left to right: Alpha blending, Additive blending, Multiplicative blending, and 2X Multiplicative blending. The top image in each column is the source image and below, it's effect when added to the destination.

Alpha blending uses the alpha channel of the source color to create a transparency effect so that the destination color appears through the source color. For example, if you clear your backbuffer to Color.Gray, it will be colored (0.5,0.5,0.5,1). If you then take a Color.White color with a partial alpha value (1,1,1,0.4), the result will be 60 percent of the destination color and 40 percent of the source: (0.5 x 0.6) + (1 x 0.4). The resulting color will be (0.7,0.7,0.7, 1). The alpha values are multiplied as well - (.6 x 1) + .4 gives us an alpha value of 1.

In some cases, alpha blending is automatic. When drawing sprites using the SpriteBatch.aspx) class, choosing SpriteBlendMode.AlphaBlend configures alpha blending for you.

By default, the alpha channel is blended along with the red, green, and blue channels using the SourceBlend.aspx) and DestinationBlend.aspx) properties. You can choose to customize the blending for just the alpha channel by using the AlphaSourceBlend.aspx) and AlphaDestinationBlend.aspx) properties. When you compute the alpha channel, these properties are used as blending factors if the SeparateAlphaBlendEnabled.aspx) property is true.

See Also

Concepts

2D Graphics Overview
3D Graphics Overview

Tasks

How To: Draw a Masked Sprite over a Background

Reference

RenderState
SourceBlend
DestinationBlend
AlphaSourceBlend
AlphaDestinationBlend
Blend
SpriteBlendMode.AlphaBlend


 What Is a Depth Buffer?

XNA Game Studio 3.1

Other Versions )

 

A depth buffer is a buffer that is the same width and height as your render target. This buffer records the depth of each pixel that is rendered.

When a pixel is rendered a second time - such as when one object is rendered behind another - the depth buffer will keep either the previous depth value, or replace it with the depth value for the second pixel. Which depth is preserved and which depth is discarded depends on the depth function you select. For example, if CompareFunction.LessEqual is the current depth function, depth values that are less than or equal to the current value are preserved. Any value greater than the current depth value is discarded. This is called the depth test. The depth test occurs every time a pixel is rendered. When a pixel passes the depth test, its color is written to the render target and its depth is written to the depth buffer.

The depth of a pixel is determined based on the size of the view and projection matrix selected for rendering. A pixel that touches the near plane of the projection has depth 0. A pixel that touches the far plane of the projection has depth 1. As each object in the scene is rendered, normally the pixels that are closest to the camera are kept, as those objects block the view of the objects behind them.

The depth buffer may also contain stencil bits - for this reason it's often called the depth-stencil buffer. The depth format describes the composition of the depth buffer. The depth buffer is always 32 bits, but those bits can be arranged in different ways, similar to how texture formats can vary. A common depth format is Depth32, where all 32 bits are reserved for depth information. Another common format is DepthFormat.Depth24Stencil8, where 24 bits are used for depth calcuation and 8 bits are used by the stencil buffer. DepthFormat.Depth24Stencil8Single is a more unusual format where the 24 bits for the depth buffer are arranged as a floating point value. Use the AutoDepthStencilFormat.aspx) property on PresentationParameters.aspx) to set the default depth format.

Use RenderState.DepthBufferEnable.aspx) to enable or disable depth buffering. Use RenderState.DepthBufferFunction.aspx) to change the comparison function used for the depth test. The depth buffer can be cleared separately by passing ClearOptions.DepthBuffer to the GraphicsDevice.Clear.aspx) method. Use the DepthStencilBuffer.aspx) class to create your own depth buffer. How To: Create a Depth Texture.aspx) provides an example of how to create a custom DepthStencilBuffer.aspx).

 See Also

Concepts

3D Graphics Overview
What Is a Depth Texture?
What Is a Stencil Buffer?

Tasks

How To: Create a Depth Texture

Reference

DepthStencilBuffer
GraphicsDevice.Clear
RenderState.DepthBufferEnable
RenderState.DepthBufferFunction
DepthFormat


What Is a Depth Texture?

XNA Game Studio 3.1

Other Versions )

 

A depth texture is a texture that contains the data from the depth buffer.aspx) for a particular scene.

The color of each pixel in the texture represents the depth of the polygon under that pixel, taken from the depth buffer. You can use depth textures to render accurate shadows using a technique called shadow mapping. For this reason, depth textures are also known as shadow maps.

There are two common ways to encode the depth information into a texture. The first is to use a standard RGBA surface format for your texture, and treat all four colors as one 32-bit buffer and copy the depth into that buffer (depth formats normally assign 24 or 32 bits for depth data). The second is to use a floating-point surface format for your texture combined with a floating-point depth buffer. Depth buffers are normally fixed-point. When using the floating-point approach, it is more accurate to subtract the depth from 1.0 before storing it, due to the way floating-point numbers are encoded.

Both of these techniques require a custom vertex and pixel shader. How To: Create a Depth Texture.aspx) provides an example of a depth texture shader and the code necessary to use it.

See Also

Concepts

3D Graphics Overview.aspx)

Tasks

How To: Create a Depth Texture.aspx)
How To: Implement Shadow Mapping.aspx)

 


What Is a Model Bone?

XNA Game Studio 3.1

Other Versions )

A model bone is a matrix that represents the position of a mesh relative to other bones in a 3D model.

img

In the XNA Framework, the Model.aspx) class represents the whole model. The Model.aspx) contains a ModelMesh.aspx) for each separate mesh in the model. Each ModelMesh.aspx) contains a ParentBone.aspx), which controls the mesh's position and orientation relative to the model. The Model.aspx) has a Root.aspx) bone, which determines the model's position and orientation. Every ModelBone.aspx) can have one parent and many children. The Root.aspx) bone on the Model.aspx) object is the ultimate parent. Its children are bones on ModelMesh.aspx) objects—objects which might have other ModelMesh.aspx) bones as their children, and so on. In any given family of bones, rotating the parent bone also rotates the children, and their children, and so on.

Every bone has a transformation matrix (called Transform.aspx)) that defines its position and rotation relative to the position of the parent bone. This rotation and translation applies to all the vertices in the ModelMesh.aspx) (for example, all the vertices that connect to that bone). To animate a bone, you multiply the default bone transform by a new matrix. When you draw the ModelMesh.aspx), you then base your world matrix on the bone's transform.

The easiest way to incorporate transformed bones into drawing is to use the CopyAbsoluteBoneTransformsTo.aspx) method. This method takes the bone transforms, which are relative to each other, and iterates over them to make them relative to the Root.aspx) bone of the Model.aspx). Then it returns a copy of these transforms. When you draw each ModelMesh.aspx), you can use the absolute bone transform as the first part of your world matrix. This way you won't have to worry about parent bones and their relationships.

See Also

Concepts

3D Graphics Overview.aspx)

Tasks

How To: Render a Model.aspx)

Reference

ModelBone.aspx)
Model.aspx)
Model.CopyAbsoluteBoneTransformsTo Method.aspx)
ModelMesh.aspx)

 


What Is a Render Target?

XNA Game Studio 3.1

Other Versions )

 

A render target is a buffer where the video card draws pixels for a scene that is being rendered by an Effect Class.aspx).

The default render target is called the back buffer - this is the part of video memory that contains the next frame to be drawn. You can create other render targets with the RenderTarget2D.aspx) class - in effect, reserving new regions of video memory for drawing. Most games render a lot of content to other render targets besides the back buffer ("offscreen"), then assemble the different graphical elements in stages, combining them to create the final product in the back buffer.

A render target has a width and height. The width and height of the back buffer are the final resolution of your game (although on Xbox 360 that final result is scaled to match the user's screen). An offscreen render target does not need to have the same width and height as the back buffer. Small parts of the final image can be rendered in small render targets, then copied to another render target later. A render target also has a surface format, which describes how many bits are allocated to each pixel and how they are divided between red, green, blue, and alpha. For example, SurfaceFormat.Bgr32 allocates 32 bits per pixel: 8 bits for each color and 8 bits for the alpha channel. As an option, render targets can perform antialiasing on all the images that are rendered into them.

To use a render target, create a RenderTarget2D.aspx) object with the width, height, and other options you prefer. Then call GraphicsDevice.SetRenderTarget.aspx) to make your render target the current render target. From this point on, any Draw calls you make will draw into your render target. When you are finished with the render target, call GraphicsDevice.SetRenderTarget.aspx) to a new render target (or null for the back buffer). Then at any time you can call RenderTarget2D.GetTexture.aspx) to get the contents of the render target for further processing.

Render targets work in conjunction with the depth-stencil buffer. If you set a new render target, it will use the existing depth-stencil buffer. If the new render target has different multisampling settings than the depth-stencil buffer, or a larger width and height, you will need a new depth-stencil buffer to match. You must also use a depth format in your depth-stencil buffer that is compatible with the surface format of your render target.

You can sometimes render to more than one render target at the same time. The number of simultaneous render targets your graphics device supports is given by the MaxSimultaneousRenderTargets.aspx) property. There are numerous caveats for using multiple render targets. For more information, see Render Targets.aspx).

See Also

Concepts

3D Graphics Overview.aspx)
Displays, Client Bounds, Viewports, and Back Buffers.aspx)
What Is a Depth Buffer?.aspx)
What Is a Stencil Buffer?.aspx)

Tasks

How To: Apply a Pixel Shader to Sprites.aspx)
How To: Create a Depth Texture.aspx)
How To: Implement Shadow Mapping.aspx)

Reference

RenderTarget.aspx)
RenderTarget2D.GetTexture.aspx)
GraphicsDevice.SetRenderTarget.aspx)

 


What Is a Stencil Buffer?

XNA Game Studio 3.1

Other Versions )

 

A stencil buffer is similar to a depth buffer.aspx). In fact, it uses part of the depth buffer (for this reason, the depth buffer is often called the depth-stencil buffer). The stencil buffer allows the programmer to set a stencil function that will test the "reference" stencil value - a global value - against the value already in the stencil buffer each time a pixel is rendered.

The outcome of that stencil test determines if the color value of the pixel is written to the render target, and if the depth value of that pixel is written to the depth buffer.

For example, if you render some objects to a scene while the reference stencil is 0, and the stencil buffer has been cleared to 1, the stencil buffer will contain a cut-out pattern of zeros where those objects were rendered. If the reference value was then set to 1, and the StencilFunction.aspx) was set to CompareFunction.LessEqual.aspx), the only pixels that could be rendered to the scene are those that are not in the same location where the stencil value was set to 0. This is a basic way to use the stencil buffer to create an area that is off-limits to the current rendering pass.

The stencil buffer can be used in more sophisticated ways. It is possible to specify StencilOperations.aspx) that go beyond replace or discard, to increment or decrement the stencil buffer after each successful stencil test. This can be combined with the StencilMask.aspx) value to ensure that the stencil test only operates on a part of the stencil buffer.

To use the stencil buffer, the DepthFormat.aspx) must reserve some bits for the stencil buffer. The DepthFormat.Depth24Stencil8 depth format allows 8 bits for a stencil buffer. When you combine this with the RenderState.StencilMask Property.aspx), this can provide eight different stencil buffers for the programmer. The Depth24Stencil4 depth format uses 4 bits for the stencil buffer, while the DepthFormat.Depth15Stencil1 format only allows one bit. The stencil buffer can be cleared separately by passing ClearOptions.StencilBuffer to the GraphicsDevice.Clear.aspx) method.

You can use the DepthStencilBuffer class to create your own depth-stencil buffer. You may need to create your own depth-stencil buffer when you create a custom render target

See Also

Concepts

3D Graphics Overview.aspx)
What Is a Depth Buffer?.aspx)
What Is a Render Target?.aspx)

Tasks

How To: Draw a Shadow.aspx)
How To: Create a Depth Texture.aspx)

Reference

DepthStencilBuffer.aspx)
StencilFunction.aspx)
StencilMask.aspx)
StencilOperation.aspx)
DepthFormat.aspx)


What Is Texture Mapping?

XNA Game Studio 3.1

Other Versions )

 

Texture mapping is the process of determining where in a particular space a texture will be applied.

A texture consists of a series of pixels (also called texels), each occupying a texture coordinate determined by the width and height of the texture. These texture coordinates are then mapped into values ranging from 0 to 1 along a u and v axes (u is width, v is height). This process is called UV mapping. The resulting coordinates are UV coordinates.

Whenever you render a polygon, a UV coordinate is calculated for each vertex in the triangle. These three UV coordinates dictate how a texture is applied to the polygon by the pixel shader. When the u or v values move below 0 or above 1, the polygon is applied according to the texture address mode that has been set. When TextureAddressMode.Border is in effect, any pixels where the UV mapping is outside the 0-1 range are colored with the border color. When TextureAddressMode.Clamp is in effect, the color of the nearest pixel is used. TextureAddressMode.Wrap indicates that the texture should repeat across the triangle. When you specify TextureAddressMode.Mirror, the texture wraps, but reverses itself when it crosses a UV boundary. Texture-addressing modes are specified for each axis separately, so you could wrap along the u-axis while clamping the v-axis.

If the texture is too large or too small for the polygon, the texture must be filtered to fit the space. There are two types of filtering that can be applied to textures: magnification and minification. A magnification filter enlarges a texture to fit a polygon. A minification filter reduces the texture to fit into a smaller area. Texture magnification is normally straightforward, and results in a blurrier image. Texture minification is more complicated, and improper minification can result in aliasing – that is, jagged edges.

The most popular approach to minification is to create mipmaps for each texture. A mipmap is a pre-shrunk texture, normally half the size of the original. The mipmap itself then gets mipmapped, and this process continues until a 1x1 texture is created. This is the final mipmap for the texture. You can think of mipmaps as a chain, starting with the original texture and becoming smaller and smaller until the 1 texel texture is reached. When minification is needed, first the appropriate mipmapped texture is chosen, then that mipmap is applied to the object, with real-time texture filtering if needed. The default Texture processor for the Content Pipeline has an option to generate mipmaps automatically.

XNA supports five texture filters. TextureFilter.Point uses the nearest corresponding point on the texture, with no filtering. TextureFilter.Linear uses bilinear interpolation to sample four neighboring texels and create an average value. TextureFilter.PyramidalQuad also uses four samples and favors texels nearer to the center of the resulting pixel when calculating the final result. The TextureFilter.GaussianQuad works the same way, using a different mathematical weighting - similar to a bell curve where the top of the curve is the center of the resulting pixel. TextureFilter.Anisotropic is a filtering method designed for surfaces that are not facing the camera (such as the ground near a horizon).

See Also

Concepts

3D Graphics Overview.aspx)
2D Graphics Overview.aspx)

Tasks

How To: Tile a Sprite.aspx)

Reference

TextureAddressMode.aspx)
TextureFilter.aspx)

 

标签: none

添加新评论