Заливка Гуро, страница 2

Мы также должны делать scanedge подпрограмму непосредственно, и следовательно мы нуждаемся в другом наклоне, давайте назовем его zslope. zslope - это как z изменяется относительно y. Следовательно, zslope вычисляется как (z2-z1) / (y2-y1). Это значение мы добавляем к другому значению, которое берем из zpos массива. Подобно этому:

for (y=y1;y<y2;y++)

{

x += xslope;

z += zslope;

xpos[y][side] = x;

zpos[y][side] = z;

}

The routine would look something like this:

Программа выглядела бы так:

void drawedge(int x1, int y1, int z1, int x2, int y2, int z2, char color)

{

int   side = 0;

float temp = x1;

float xslope = (x2-x1)/(y2-y1);

float zslope = (z2-z1)/(y2-y1);

if (y1 >= y2)

{

side = 1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

temp = z1;

z1 = z2;

z2 = temp;

}

for (y=y1;y<=y2;y++)

{

xpos[y][side]=x;

x1 += xslope;

zpos[y][side]=z;

z1 += zslope;

}

}

Now we need to rewrite our hline routine. We need here to make the same changes as we did with the scanedge routine, to include the z value for each line and we also need a zslope like in the scan edge, since we need to know how much z changes per x value. Therefore our new way to calculate zslope should be (z2-z1)/(x2-x1). This value we increase our color value every time we change x. Now for the routine:

Теперь мы должны перезаписать нашу hline подпрограмму. Мы должны здесь делать те же самые изменения, какие мы делали в scanedge подпрограмме, для того чтобы включить значение z для каждой строки, и мы также нуждаемся в zslope подобно в scanedge , поскольку мы должны знать как z изменяется относительно х. Следовательно наш новый способ вычислять zslope должен быть (z2-z1) / (x2-x1). Это значение увеличивает наше значение цвета каждый раз, когда изменяется x. Теперь для подпрограммы:

void drawline(int x1,int x2,int y,char c)

{

int i;

float zinc;

float z1,z2;

char  ch;

z1 = zpos[y][0];

z2 = zpos[y][1];

if (x1>x2)

{

i=x1;

x1=x2;

x2=i;

i=z1;

z1=z2;

z2=i;

}

zinc = (z2-z1)/(x2-x1);

for (i=x1;i<=x2;i++)

{

BufPixel(i,y,z1+c);

z1 += zinc;

}

}

But we are still not done. We need the main routine to be changed to. Since this is very little changes we are making, we'll throw it in with the complete example, and wont get any further into that. But now for the entire z-gouraud routine:

Но мы не все еще сделали. Мы нуждаемся в основной подпрограмме, которая будет изменена. Так как здесь очень немного изменений, которые мы делаем, мы бросим это с полным примером, и это станет привычкой в дальнейшем. Но теперь для всей z-gouraud подпрограммы:

void drawedge(int x1, int y1, int z1, int x2, int y2, int z2, char color)

{

int   side = 0;

float temp = x1;

float xslope = (x2-x1)/(y2-y1);

float zslope = (z2-z1)/(y2-y1);

if (y1 >= y2)

{

side = 1;

x1 = x2;

x2 = temp;

temp = y1;

y1 = y2;

y2 = temp;

temp = z1;

z1 = z2;

z2 = temp;

}

for (y=y1;y<=y2;y++)

{

xpos[y][side]=x;

x1 += xslope;

zpos[y][side]=z;

z1 += zslope;

}

}

void hline(int x1,int x2,int y,char c)

{

int i;

float zinc;

float z1,z2;

char  ch;

z1 = zpos[y][0];

z2 = zpos[y][1];

if (x1>x2)

{

i=x1;

x1=x2;

x2=i;

i=z1;

z1=z2;

z2=i;

}

zinc = (z2-z1)/(x2-x1);

for (i=x1;i<=x2;i++)

{

BufPixel(i,y,z1+c);

z1 += zinc;

}

}

void gpoly(int x1,int y1,int z1,int x2,int y2,int z2,int x3,int y3,int z3,char c)

{

if (y1<=0)

y1=0;

if (y2<=0)

y2=0;

if (y3<=0)

y3=0;

if (y1>=200)

y1=199;

if (y2>=200)

y2=199;

if (y3>=200)

y3=199;

if (x1<=0)

x1=0;

if (x2<=0)

x2=0;

if (x3<=0)

x3=0;

if (x1>=320)

x1=319;

if (x2>=320)

x2=319;

if (x3>=320)

x3=319;

drawedge(x1,y1,z1,x2,y2,z2);

drawedge(x2,y2,z2,x3,y3,z3);

drawedge(x3,y3,z3,x1,y1,z1);

miny=y1;

if (miny > y2)

miny=y2;

if (miny > y3)

miny=y3;

maxy=y1;

if (maxy < y2)

maxy=y2;

if (maxy < y3)

maxy=y3;

for (y=miny;y<=maxy;y++)

hline(xedge[y][0],xedge[y][1],y,c);

};