![]() In this case we want the interpolation to occur on a quad not triangle, as pictured below: ![]() Graphics cards can work only on triangles, not quads. This is happening because interpolation occurs on two triangles independent of each other. When we want to render a gradient which starts in one of the corners of a sprite (see the picture below), we can see quite ugly results :( When dealing with triangle meshes, this way of attribute interpolation gives correct results, but when rendering 2D sprites or font glyphs, some disadvantages may occur under specific circumstances. (This functionality can be disabled by using proper Interpolation Qualifier on an attribute in vertex shader source code, like flat). This way not only color can be interpolated, but any vertex attribute. Then the color of the fragment C can be calculated by weighted sum of colors at each vertex C 1, C 2, C 3, and weights are of course barycentric coordinates: C = C 1 * λ 1 + C 2 * λ 2 + C 3 * λ 3 When a triangle is rasterized, for every fragment of the triangle proper barycentric coordinates are calculated. In short, barycentric coordinate system is a set of three points λ 1, λ 2, λ 3 ≥ 0 (for convex polygons) where λ 1 + λ 2 + λ 3 = 1. More on this topic can be read in Rasterization: a Practical Implementation). (Those coordinates not only can be used for interpolation but additionally define if a fragment lies inside the triangle or not. The way to achieve this is by using barycentric coordinates. In the described problem it’s about generating interpolated colors inside the rendered triangle. As we can read in Wikipedia, interpolation in mathematics is a type of estimation, a method that can be used to generate new data points between a discrete set of known data points. How then those rainbow colors are generated as shown on the picture above, while as I said only one color can be set on three vertices of the triangle? The answer is interpolation. Those attributes can be color of the vertex, normal vector, texture coordinate etc. How are colors of the pixels inside the triangle generated? Each vertex can contain not only one set of data - 3D coordinates in the virtual world, but also additional data called attributes. This is very important because in this stage the generation of fragments (pixels) happens, which are lying inside the triangle, as shown on the picture below. After vertex processing, Primitive Assembly follows, then the next stage is Rasterization. ![]() I was describing what is happening with vertices, but at the beginning I mentioned triangles, with three vertices forming one triangle. After primitive clipping, perspective divide, and viewport transform, vertices are projected on the 2D screen, which is drawn to the monitor output.īut this is only half the story. Usually it is a transformation from 3D coordinate system to Normalized Device Coordinates (NDC) (information about NDC can be found in Coordinate Systems). I don’t want to describe how it is done (a lot of information on this topic can be easily found on the Internet) and describe the whole graphics pipeline, but for a short recap: In the first programmable part of the rendering pipeline, vertex shader receives a single vertex (with assigned data about it that I will later refer to) and outputs one vertex which is transformed by the shader. In a nutshell, today’s graphics cards render meshes using only triangles, as shown on the picture below. It has been written by my friend Łukasz Izdebski Ph.D. This is probably first guest post ever on my blog. # How to Correctly Interpolate Vertex Attributes on a Parallelogram Using Modern GPUs?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |