Rasterization interpolation method

Source: Internet
Author: User

Rasterization is an important step in generating images on a computer, but the Rasterization method is encapsulated in both OpenGL and DirectX or other graphical interfaces. I made a grating myself, and then I said how to achieve rasterization.


Why Rasterization?

The input of the graphics pipeline is an element vertex, the output is a pixel (pixel), there is an intermediate product called fragment (fragment), a fragment corresponds to a pixel, but the fragment is more than the pixel for the computed properties, such as: depth value and normal vector. By using the fragment, we can calculate the color value of the final pixel to be generated, and the process of calculating the fragment of the input vertex is called rasterization. Why rasterization? Because you want to generate a fragment to calculate the final color.


What are the inputs and outputs of rasterization?

Like normal functions, rasterization functions also need input and output, from the previous definition of the function input is composed of the vertex structure of the element, the output is the fragment structure, why is it a structure? Because these can be described in C language struct.


What step does rasterization take place?

Vertex handlers and fragment handlers are often exposed in the graphics interface (the sense shader sounds foggy as a handler), but in this way the GPU does rasterization interpolation calculations, This is why the fragment handler input is the value that the vertex handler's output is interpolated from. Since Rasterization is a step that occurs after a vertex handler, the input vertex structure is after the vertex processing, that is, the MVP transformation, multiplied by the perspective matrix after the vertex, Note: This step has not done perspective division, rasterization interpolation occurs in the clipping space, is not a standardized space, so the vertex position is Sivizi secondary coordinates are not three-dimensional coordinates!


How to implement Rasterization method?

The first thing we can determine is what the rasterized inputs and outputs are. And you should know what data you can use on your hands.

The input vertices are processed to screen coordinates, and the vertex coordinates of the clipping space are converted to the normalized space, like this:

ndca.x=clipa.x/clipa.w;ndca.y=clipa.y/clipa.w;ndcb.x=clipb.x/clipb.w;ndcb.y=clipb.y/clipb.w;ndcc.x=clipc.x/ CLIPC.W;NDCC.Y=CLIPC.Y/CLIPC.W;

The viewport transformation is then performed on the standard coordinates of the vertex:

Viewporttransform (Face->ndca.x,face->ndca.y,fb->width,fb->height,scrax,scray); ViewPortTransform ( Face->ndcb.x,face->ndcb.y,fb->width,fb->height,scrbx,scrby); Viewporttransform (face->ndcC.x, Face->ndcc.y,fb->width,fb->height,scrcx,scrcy);

And then get three two-dimensional coordinates that represent the final position of the three vertices on the screen, and they can form a two-dimensional triangle to obtain the bounding box of the triangle:

int Minx=max (0,min (Scrax,min (SCRBX,SCRCX)); int Maxx=min (Fb->width-1,max (Scrax,max)); int SCRBX,SCRCX (0,min (Scray,min (Scrby,scrcy))); int Maxy=min (Fb->height-1,max (Scray,max));
Be careful not to exceed the screen range, and the points outside the screen range are cropped out.


Traverse this bounding box to get the screen position of the potentially possible fragment:

for (int scrx=minx;scrx<=maxx;scrx++) {for (int scry=miny;scry<=maxy;scry++) {             ...}}

The normalized spatial coordinates of the fragments are obtained separately:

Invviewporttransform (SCRX,SCRY,FB->WIDTH,FB->HEIGHT,NDCX,NDCY);
With the inverse viewport transform, the viewport transform and the inverse viewport transform are convenient, as long as the coordinates are scaled and moved.


So we get the x and y coordinates of the normalized space of possible fragments, why is it possible to fragment it? Because it is not yet certain that these fragments will be inside or outside of the rasterized triangle, we only calculate fragments inside the triangle.

But what's the use of knowing this?

Here's a formula that calculates the proportion of three vertices that affect a fragment, also called a weight value:


The formula a B C represents the three vertices of the triangle, and ax ay aw is the x Y w value of vertex A in the homogeneous coordinates of the clipping space (four-dimensional), which is useless to Z-value, because Z is also calculated by this weight.

How do you deduce this formula?

It is known that the three vertices of the triangulated ABC are in the homogeneous coordinates of the clipping space, and the weight Alpha Beta Gamma is set to the PA PB pc, so that the clipping space of each fragment can be aligned in the following coordinates:

X=pa*ax+pb*bx+pc*cx

Y=pa*ay+pb*by+pc*cy

Z=pa*az+pb*bz+pc*cz

W=pa*aw+pb*bw+pc*cw


The coordinate values of the fragment in the normalized coordinate system are then calculated as:

nx=x/w

ny=y/w

nz=z/w

Nw=1


Can be pushed to:

X=w*nx

Y=w*ny

W=w


Because:

X=pa*ax+pb*bx+pc*cx

Y=pa*ay+pb*by+pc*cy

W=pa*aw+pb*bw+pc*cw

Converting to a 3x3 matrix is

AX BX CX PA w*nx

Ay by cy * PB = W*ny

AW BW CW PC W


Where NX and NY are the X y values of the previously obtained fragments in the normalized coordinate system, and since the PA PB PC is the ratio, w can be removed, so that the value of the PA PB PC can be obtained only by finding the inverse of the 3x3 matrix.

But pay attention to pa+pb+pc=1, so calculate the value after the following processing:

float sum=pa+pb+pc;pa/=sum; Pb/=sum; Pc/=sum;


Then discard the fragments with a ratio less than 0:

if (pa<0| | pb<0| | PC<0) continue;


You can then use these three weights to interpolate the vertex attributes.


The specific rasterization function is this:

void Rasterize (framebuffer* fb,depthbuffer* db,fragmentshader fs,face* face) {float Ndcx=0,ndcy=0,clipw=0;int ScrAX, Scray,scrbx,scrby,scrcx,scrcy;viewporttransform (Face->ndca.x,face->ndca.y,fb->width,fb->height, Scrax,scray); Viewporttransform (Face->ndcb.x,face->ndcb.y,fb->width,fb->height,scrbx,scrby); Viewporttransform (face->ndcc.x,face->ndcc.y,fb->width,fb->height,scrcx,scrcy); int MinX=max (0,min ( Scrax,min (SCRBX,SCRCX))), int maxx=min (Fb->width-1,max (Scrax,max)), int scrbx,scrcx (Miny=max (0,min ( Scrby,scrcy)); int Maxy=min (Fb->height-1,max (Scray,max (Scrby,scrcy))); for (int scrx=minx;scrx<=maxx;scrx++) {for (int scry=miny;scry<=maxy;scry++) {invviewporttransform (scrx,scry,fb->width,fb->height,ndcx,ndcy); VECTOR4D Ndcpixel (ndcx,ndcy,1,0); VECTOR4D proportion4d=face->clipmatrixinv*ndcpixel; Vector3D proportionfragment (proportion4d.x,proportion4d.y,proportion4d.z); float pa=proportionfragment.x;float pb= Proportionfragment.y;float pc=Proportionfragment.z;float sum=pa+pb+pc;pa/=sum; Pb/=sum; Pc/=sum;if (pa<0| | pb<0| | PC&LT;0) continue; Fragment frag;interpolate3f (PA,PB,PC,FACE-&GT;CLIPA.W,FACE-&GT;CLIPB.W,FACE-&GT;CLIPC.W,CLIPW); INTERPOLATE3F (PA , Pb,pc,face->clipa.z,face->clipb.z,face->clipc.z,frag.ndcz); Frag.ndcz/=clipw;if (frag.ndcZ<-1| | frag.ndcz>1) continue;if (db!=null) {float storez=readdepth (db,scrx,scry), if (STOREZ&LT;FRAG.NDCZ) continue; Writedepth (DB,SCRX,SCRY,FRAG.NDCZ);} interpolate3f (PA,PB,PC,FACE-&GT;CLIPA.X,FACE-&GT;CLIPB.X,FACE-&GT;CLIPC.X,FRAG.NDCX); Frag.ndcX/=clipW; interpolate3f (pa,pb,pc,face->clipa.y,face->clipb.y,face->clipc.y,frag.ndcy); Frag.ndcY/=clipW; interpolate3f (PA,PB,PC,FACE-&GT;CLIPA.NX,FACE-&GT;CLIPB.NX,FACE-&GT;CLIPC.NX,FRAG.NX); interpolate3f (PA,PB,PC, Face->clipa.ny,face->clipb.ny,face->clipc.ny,frag.ny); interpolate3f (pa,pb,pc,face->clipa.nz,face- >clipb.nz,face->clipc.nz,frag.nz); interpolate3f (pa,pb,pc,face->clipa.s,face->clipb.s,face-> Clipc.s,FRAG.S); interpolate3f (pa,pb,pc,face->clipa.t,face->clipb.t,face->clipc.t,frag.t); Fragmentout Outfrag;fs (Frag,outfrag);d rawpixel (fb,scrx,scry,outfrag.r,outfrag.g,outfrag.b);}}

Rasterization is complete, so you can implement OpenGL and DirectX yourself!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Rasterization interpolation method

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.