Directly mapping texels to pixels

Source: Internet
Author: User

Http://blogs.msdn.com/jsteed/articles/209220.aspx

 

When rendering 2D output using pre-transformed vertices, care must be taken to ensure that each Texel area correctly corresponds to a single pixel area, otherwise texture distortion can occur. this article describes the process that direct3d follows when rasterizing and texturing triangles. by understanding the basics of this process you can ensure your direct3d application correctly renders 2D output.

Let's assume we a 6x6 display, as shown in Figure 1 :

In this digoal, the pixels are modeled as squares, but this is somewhat misleading. pixels usually aren't squares, they're little dots. each square indicates the area allocated by the pixel, but a pixel is always just a dot at the center of a square. the distinction may seem small, but it's important. A better authentication of the same display is shown in Figure 2 :

This districorrectly shows each physical pixel as a point in the center of a grid cell. the screen space coordinate (0, 0) is located directly at the top-left pixel, and therefore at the center of the top-left cell. the top-left corner of the display is therefore at (-0.5,-0.5) since it's. 5 cells to the left and. 5 cells up from the top-left pixel. now, let's ask direct3d to render a quad With corners at (0, 0) and (4, 4 ):
Figure 3 Shows where our mathematical quad is in relation to the display, but doesn't show what the Quad will look like once direct3d rasterizes it and outputs it to the display. in fact, before moving on Make sure you agree that it's impossible for a raster display to fill the quad exactly as shown because the edges of the Quad don't coincide with the boundaries between pixel cells.

Stated another way: each pixel can only output a single color so each pixel cell is filled with only a single color; if the display were to render the quad exactly as shown, the pixel cells along the quad's edge wowould need to show 2 distinct colors: Blue where covered by the quad and white where only the background is visible.

Instead, the graphics hardware is tasked with determining which pixels shold be filled to approximate the quad. This process is called Rasterization , And is detailed in the DirectX SDK documentation under the headingRasterization rules . For this special case, the rasterized quad is shown in Figure 4 :

Jump back Figure 3 To see how the quad we told direct3d to draw and what was finally rendered are different. you can see that what the display actually outputs is the correct size, but has been shifted by-0.5 cells in the X and Y directions ctions. however, this is the best possible approximation to the Quad (well, actually multi-sampling techniques compute each pixel's output by averaging color calculations taken at multiple points within the pixel cell, which makes for a better approximation but has the effect of blurring the edges. multi-sampling is an advanced topic and is outside the scope of this article. see Antialias sample Supported with the DirectX SDK for thorough coverage). Realize that if the Rasterizer filled every cell the quad crossed, the resulting area wocould be of dimension 5x5 instead of the desired 4x4.

To the unsuspecting individual who assumes that screen coordinates originate at the top-left corner of the display grid instead of the top-left pixel, the quad appears exactly as expected, but the difference becomes clear when the quad is given a texture. here's the 4x4 texture We'll map directly onto the quad:

Since the texture is 4x4 texels and the quad is 4x4 pixels, we might expect CT the textured quad to appear exactly like the texture regardless of where on the screen the quad is drawn. however, this is not the case; even slight changes in position will influence how the texture is displayed. Figure 6 Shows how a quad between (0, 0) and (4, 4) is displayed after being rasterized and textured:
The rest of this article explains exactly why the output looks the way it does instead of looking like the texture, but for those who are already bored and want the solution, here it is: the edges of the input quad need to lie upon the boundary lines between pixel cells. by simply shifting the X and Y quad coordinates by-0.5 units Texel cells will perfectly cover pixel cells and the quad can be perfectly recreated on the screen. skip ahead Figure 9 To see the quad at the corrected coordinates.

The details of why the rasterized output only bears slight resesponence to the input texture are directly related to the way direct3d addresses and samples textures. what follows assumes you have a good understanding of texture coordinate space and bilinear filtering. see the companion article "bilinear texture filtering and You" if you 'd like a zesty review.

Getting back to our investigation of the strange pixel output, it makes sense to trace the output color back to the pixel shader: the pixel shader is called for each pixel selected to be part of the rasterized shape. the solid blue quad we rendered in the beginning cocould have a special simple shader:

Float4 solidblueps (): Color
{
Return float4 (0, 0, 1, 1 );
}

For the textured quad, the pixel shader has to be changed slightly:

Texture mytexture;
Sampler mysampler =
Sampler_state
{
Texture = <mytexture>;
Minfilter = Linear;
Magfilter = Linear;
Addressu = clamp;
Addressv = clamp;
};
Float4 texturelookupps (float2 vtexcoord: texcoord0): Color
{
Return tex2d (mysampler, vtexcoord );
}

That Code assumes the 4x4 texture Figure 5 Is stored in the mytexture parameter. as shown, the mysampler texture sampler is set to perform bilinear filtering on mytexture. the pixel shader gets called once for each rasterized pixel, and each time the returned color is the sampled texture color at vtexcoord. each time the pixel shader is called, The vtexcoord argument is set to the texture coordinates At that pixel . That means the shader is asking the texture sampler for the filtered texture color at the exact location of the pixel, as detailed in Figure 8 :
In that digoal, the black dots show where the Rasterization pixels are. the texture coordinates at each pixel are easily determined by interpolating the coordinates stored at each vertex: The pixel at (0, 0) coincides with the vertex at (0, 0 ), therefore the texture coordinates at that pixel are simply the texture coordinates stored at that vertex, UV (0.0, 0.0 ). for the pixel at (3, 1), the interpolated coordinates are UV (0.75, 0.25) because that pixel is located at 3/4th the texture's width and 1/4th its height. these interpolated coordinates are what get passed to the pixel shader.

As you can see, in this example the texels don't line up with the pixels; each pixel (and therefore each sampling point) is positioned at the corner of 4 texels. since the filtering mode is setLinear, The sampler will average the colors of the 4 texels sharing that corner. this explains why the pixel we had expected to be red is actually 3/4th gray + 1/4 red, the pixel expected to be Green is 1/2 gray + 1/4 red + 1/4 green, etc.

To fix this problem, all we need to do is correctly map the quad to the pixels it will be rasterized to, and thereby correctly map the texels to pixels.Figure 9Shows the results of drawing the same quad between (-0.5,-0.5) and (3.5, 3.5), which is the quad we wanted all along:

In summary, pixels and texels are actually points, not solid blocks. screen Space originates at the top-left pixel, but texture coordinates originate at the top-left corner of the texture's grid. most importantly, remember to subtract 0.5 units from the X and Y components of your vertex positions when working in transformed screen space in order to correctly align texels with pixels.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.