What follows assumes you have a good understanding of texture coordinate space And bilinear texture filtering. The details of why the rasterized output only bears slight resemblance to the input texture are directly related to the way Direct3D addresses and samples textures. (The last illustration in this topic shows the quad at the corrected coordinates.) 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. 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 want the solution, here it is: The edges of the input quad need to lie upon the boundary lines between pixel cells. The quad drawn in the preceding illustration shows the textured output (with a linear filtering mode and a clamp addressing mode) with the superimposed rasterized outline. The following illustration shows how a quad between (0, 0) and (4, 4) is displayed after being rasterized and textured. However, this is not the case even slight changes in position influence how the texture is displayed. The following illustration shows the 4 x 4 texture you'll map directly onto the quad.īecause the texture is 4 x 4 texels and the quad is 4 x 4 pixels, you might expect the textured quad to appear exactly like the texture regardless of the location on the screen where the quad is drawn. However, the difference becomes clear when the quad is given a texture. If you assume 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. (See the Antialias Sample for thorough coverage of multi-sampling.) Be aware that if the rasterizer filled every cell the quad crossed, the resulting area would be of dimension 5 x 5 instead of the desired 4 x 4. However, except for multi-sampling techniques, this is the best possible approximation to the quad. You can see that what the display actually renders is the correct size, but has been shifted by -0.5 cells in the x and y directions. Compare the preceding two illustrations for rendering differences. Note that the quad passed to Direct3D has corners at (0, 0) and (4, 4), but the rasterized output (the preceding illustration) has corners at (-0.5,-0.5) and (3.5,3.5). For this particular case, the rasterized quad is shown in the following illustration. This process is called rasterization, and is detailed in Rasterization Rules (Direct3D 9). Instead, the graphics hardware is tasked with determining which pixels should be filled to approximate the quad. In other words, because each pixel can only display a single color, 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 would need to show two distinct colors: blue where covered by the quad and white where only the background is visible. In fact, it is impossible for a raster display to fill the quad exactly as shown because the edges of the quad do not coincide with the boundaries between pixel cells. The preceding illustration shows where the mathematical quad is in relation to the display, but does not show what the quad will look like once Direct3D rasterizes it and sends it to the display. Direct3D will render a quad with corners at (0, 0) and (4, 4), as shown in the following illustration. The top-left corner of the display is therefore at (-0.5, -0.5) because it is 0.5 cells to the left and 0.5 cells up from the top-left pixel. 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 preceding diagram correctly shows each physical pixel as a point in the center of each cell. A better illustration of the same display is shown in the following diagram. This distinction, though seemingly small, is important. Each square in the preceding diagram indicates the area lit by the pixel, but a pixel is always just a dot at the center of a square. In reality, however, pixels are dots, not squares. The preceding diagram shows pixels that are modeled as squares. By understanding the basics of the process that Direct3D follows when rasterizing and texturing triangles, you can ensure your Direct3D application correctly renders 2D output. 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.
0 Comments
Leave a Reply. |