Optimization of Rasterization method

Source: Internet
Author: User

Before using the bounding box and the center of gravity coordinate method to do the grating implementation, but there is a problem with the method, this time to achieve a more efficient rasterization method.


The problem of bounding box

Before using the bounding box method to determine the scope of rasterization, but the bounding box to determine the scope is a rectangle, to rasterization is a triangle, then each time there will be no need to participate in the operation of pixels, a face of course no problem, but if it is 100 face problem is big.


Improvement methods
In the <3d game Programming Master Skills >, the author uses the method of triangle scan line interpolation, which is very efficient and implements similar methods:
By calculating the equation of the triangle three edges and the two intersections of each scan line to determine the range of the rasterized X-values, the raster calculations can be performed accurately, so that the impossible pixels are not considered.
Implementation method
The line in space has the following parametric equation:p=p0+ (p1-p0) *tP is a point on the line, P0 is the beginning of the line vector, P1 is the end of the line vector, if the t=0 point on the P0 and T=1 point on the P1, so if you want the target point on the segment P0P1, the value range of T should be 0 to 1.Scan lines are traversed from the top of the triangle to the bottom of the triangle, from y0 to Y1, then there are equations like the followingy=nThe values for n range from y0 to Y1. Three-side parametric equations and scanning line equations can be calculated for three T, the left and right boundary of the scan line should be in the line segment, so exclude the t from the 0 to 1 range, leaving the remaining two as the right and left boundary points of T, By substituting the linear equation, X is able to determine the left and right range of the scan line. This is the general situation, there is no discussion of flat bottom, encountered this situation does not require and scanning line parallel to the line equation, the line equation of the other two edges, and then find two T and two X can be, the following is the specific implementation:
if (Scray==scrby&&scray==scrcy)//screen coordinates all y are equal, the line is not rasterized return;int Miny=max (0,min (Scray,min (Scrby,scrcy))); Top Y0int maxy=min (Fb->height-1,max (Scray,max (Scrby,scrcy))); Y1for at the bottom (int scry=miny;scry<=maxy;scry++) {float x1,x2;if (Scray==scrby) {//flat top plain bottom case float paramac= ((float) Scry-scray)/(Scrcy-scray), float parambc= ((float) scry-scrby)/(Scrcy-scrby); x1= (Scrcx-scrax) *paramac+scrax;x2= ( SCRCX-SCRBX) *PARAMBC+SCRBX;} else if (Scray==scrcy) {//flat top case float paramab= ((float) scry-scray)/(Scrby-scray); float parambc= ((float) scry-scrby)/( Scrcy-scrby); x1= (Scrbx-scrax) *paramab+scrax;x2= (SCRCX-SCRBX) *parambc+scrbx;} else if (Scrby==scrcy) {//flat top case float paramab= ((float) scry-scray)/(Scrby-scray); float paramac= ((float) scry-scray)/( Scrcy-scray); x1= (Scrbx-scrax) *paramab+scrax;x2= (Scrcx-scrax) *paramac+scrax;} else {//normal float paramab= ((float) scry-scray)/(Scrby-scray); float paramac= ((float) scry-scray)/(Scrcy-scray); float parambc= (float) scry-scrby)/(Scrcy-scrby); bool Ab= (paramab<=1.0&¶mab>=0.0)? True:false;bool ac= (paramac<=1.0&¶mac>=0.0)? True:false;bool bc= (paramBC<=1.0&¶mBC>= 0.0)? true:false;float xab= (Scrbx-scrax) *paramab+scrax;float xac= (Scrcx-scrax) *paramac+scrax;float xBC= ( SCRCX-SCRBX) *parambc+scrbx;if (!ab) {X1=XAC;X2=XBC;} else if (!AC) {X1=XAB;X2=XBC;} else if (!BC) {x1=xab;x2=xac;} else { X1=xac;x2=xbc;if (X1==X2)//Excluding the intersection of two lines x2=xab;}}  int Minx=max (0,min (X1,X2)-0.5); x value int maxx=min (Fb->width-1,max (x1,x2) +0.5) to the left of the scan line; x value to the right of the scan line

The following rasterization steps are used in the same gravity coordinate method as before:
for (int scrx=minx;scrx<=maxx;scrx++) {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;if (pa<0| | pb<0| | pc<0) Continue;float sum=pa+pb+pc;pa/=sum; Pb/=sum; Pc/=sum; 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.WX,FACE-&GT;CLIPB.WX,FACE-&GT;CLIPC.WX,FRAG.WX); INTERPOLATE3F (PA , Pb,pc,face->clipa.wy,face->clipb.wy,face->clipc.wy,frag.wy); interpolate3f (Pa,pb,pc,face->clipA.wz , FACE-&GT;CLIPB.WZ,FACE-&GT;CLIPC.WZ,FRAG.WZ); interpolate3f (pa,pb,pc,face->clipa.ww,face->clipb.ww,face- &GT;CLIPC.WW,FRAG.WW); 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- &GT;CLIPB.S,FACE-&GT;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);}

Using this method avoids unnecessary rasterization operations and is more efficient than the previous bounding box method.
Reference: <3d Game Programming Master tips >http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-837-computer-graphics-fall-2012/ Lecture-notes/mit6_837f12_lec21.pdf

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

Optimization of Rasterization 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.