Skip to content

Instantly share code, notes, and snippets.

@Madsy
Last active December 21, 2015 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Madsy/6292380 to your computer and use it in GitHub Desktop.
Save Madsy/6292380 to your computer and use it in GitHub Desktop.
/* naive equation/algorithm */
/*
float b1 = (y2*x - y2*x3 - y3*x + y3*x3) + (x3*y - x3*y3 - x2*y + x2*y3);
float b2 = (y3*x - y3*x3 - y1*x + y1*x3) + (x1*y - x1*y3 - x3*y + x3*y3);
float b3 = 1 - b1 - b2;
*/
//Since the only thing that changes per pixel is 'x' and 'y', we can rewrite it like so:
//Setup (once per triangle) :
float denomInv = 1 / ((y2 - y3)(x1 - x3) + (x3 - x2)(y1 - y3));
float x = startX;
float y = startY;
float b1Term1 = y2*x;
float b1Term2 = y3*x;
float b1Term3 = x3*y;
float b1Term4 = x2*y;
float b1TermConst1 = (-y2*x3) + y3*x3;
float b1TermConst2 = (-x3*y3) + x2*y3;
float b1TermConst = b1TermConst1 + b1TermConst2;
float b2Term1 = y3*x;
float b2Term2 = y1*x;
float b2Term3 = x1*y;
float b2Term4 = x3*y;
float b2TermConst1 = (-y3*x3) + y1*x3;
float b2TermConst2 = (-x1*y3) + x3*y3;
float b2TermConst = b2TermConst1 + b2TermConst2;
//compute s0/w0, s1/w1, s2/w2 as usual..
//compute 1/w0, 1/w1, 1/w2 as usual..
//Loop (for every pixel) :
for(y; y<yEnd; ++y){
for(; x<xEnd; ++x){
float b1 = (b1TermConst + b1Term1 + b1Term3 - b1Term2 - b1Term4) * denomInv;
float b2 = (b2TermConst + b2Term1 + b2Term3 - b2Term2 - b2Term4) * denomInv;
float b3 = 1 - b1 - b2;
float sOverW = s0w0*b1 + s1w1*b2 + s2w2*b3;
float OneOverW = w0*b1 + w1*b2 + w2*b3;
float wFinal = 1 / OneOverW;
float sFinal = sOverW * wFinal;
b1Term1 += y2
b1Term2 -= y3
b2Term1 += y3
b2Term2 -= y1
}
b1Term3 += x3
b1Term4 -= x2
b2Term3 += x1
b2Term4 -= x3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment