• C

# Need help in goraud shading.

I wrote a grayscale goraud shading,
but it has a problem-white lines
on the edges of the polygons.It maybe
becouse  I'm using 3ds format,where all
objects from triangles polygons.(At
least 3DICA tutorial says something
about it problem with the triangles :))
So I need a good answer,tip,code,
tutorial or anything that will help me
to solve that problem.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
make sure your goraud shading slope is correct, and does not overrun, and neither does your triangle draw routine. A combo of this could be death (or white lines, whichever comes first). If you plot 1 pixel to many for your triangle, your shading slope will probably overrun making the pixel a wrong color in the whole picture. Some people have their triangle drawwer make an extra pixel so adjunct polygons do not have 'holes' between them.

Just some thoughts,

Floris
0
Commented:
/*My gouraud functions...
Very optomized!

Not sure what compiler you are using.. this was written in Borland Turbo C/C++ 3.0 Dos.

/*

#include <conio.h>

#define colmin 128

struct poly
{
float x, y, z;
};

unsigned char far *screen = (unsigned char far*)0xa0000000l;
unsigned char far virt[64001];

//Can be changed to zoom in/out

//Also does simple Z-Buffering... depending on color..
//since depth here is the color :)
void hline(unsigned int y, int x1, int x2, uchar c1, uchar c2)
{
if (x1<0) x1=0;
if (x2>319) x2=319;
unsigned int off=y+x1;
unsigned int ctr;
int tc=(c2-c1), tx=(x2-x1), tc2=tc;
uchar cl2;
for (ctr=x1;ctr<x2;ctr++)
{
cl2=tc/tx+c1;
if (cl2>virt[off])
virt[off]=cl2;
asm inc off;
tc+=tc2;
}
}

void draw(int x1, int y1, uchar c1, int x2, int y2, uchar c2, int x3, int y3, uchar c3)
{
if (y2<y1)
{
swap(y1,y2);
swap(x1,x2);
swap(c1,c2);
}
if (y3<y2)
{
swap(y3,y2);
swap(x3,x2);
swap(c3,c2);
if (y2<y1)
{
swap(y1,y2);
swap(x1,x2);
swap(c1,c2);
}
}
if (y3==y1) return;
int start, fin;
int y;
int my1, my2, mx1, mx2, mc1, mc2, ma1, ma2, mac1, mac2;
unsigned int off=(y1<<6)+(y1<<8);
my2=(y3-y1);
mx2=ma2=(x3-x1);
mc2=mac2=(c3-c1);
if (y1!=y2)
{
my1=(y2-y1);
mx1=ma1=(x2-x1);
mc1=mac1=(c2-c1);
for (y=y1;y<y2;y++)
{
if (y>0&&y<200)
{
start = mx1/my1+x1;
fin = mx2/my2+x1;
if (fin<start)
hline(off,fin,start,mc2/my2+c1,mc1/my1+c1);
else
hline(off,start,fin,mc1/my1+c1,mc2/my2+c1);
}
else if (y==0) off=0;
mx1+=ma1;
mx2+=ma2;
mc1+=mac1;
mc2+=mac2;
off+=320;
}
}
if (y3!=y2)
{
my1=(y3-y2);
mx1=ma1=(x3-x2);
mc1=mac1=(c3-c2);
for (y=y2;y<y3;y++)
{
if (y>0&&y<200)
{
start = mx1/my1+x2;
fin = mx2/my2+x1;
if (fin<start)
hline(off,fin,start,mc2/my2+c1,mc1/my1+c2);
else
hline(off,start,fin,mc1/my1+c2,mc2/my2+c1);
}
else if (y==0) off=0;
mx1+=ma1;
mx2+=ma2;
mc1+=mac1;
mc2+=mac2;
off+=320;
}
}
}

void tri3(poly_s p1, poly_s p2, poly_s p3)
{
uchar c1,c2,c3;
c1=p1.z*4;
c2=p2.z*4;
c3=p3.z*4;
c1 = colmin-c1;
c2 = colmin-c2;
c3 = colmin-c3;
if (p1.z>20 && p2.z>20 && p3.z>20)
draw((p1.x*256)/(p1.z)+160,(p1.y*256)/(p1.z)+100,c1,
(p2.x*256)/(p2.z)+160,(p2.y*256)/(p2.z)+100,c2,
(p3.x*256)/(p3.z)+160,(p3.y*256)/(p3.z)+100,c3);
}

void set_mode(short mode)
{
asm mov ax, mode;
asm int 0x10;
}

int main(void)
{
poly p[7];  //One extra just incase...
p[0].x = -10;
p[0].y = -10;
p[0].z = 0;

p[1].x = 10;
p[1].y = -10;
p[1].z = 0;

p[2].x = 10;
p[2].y = 10;
p[2].z = 0;

p[3].x = -10;
p[3].y = -10;
p[3].z = 0;

p[4].x = -10;
p[4].y = 10;
p[4].z = 0;

p[5].x = 10;
p[5].y = 10;
p[5].z = 0;
_fmemset(virt,0,64000); //Make sure it's all 0!!!
set_mode(0x13);
while (!kbhit())
{
tri3(p[0],p[1],p[2]);
tri3(p[3],p[4],p[5]);
_fmemcpy(screen,virt,64000); //Copy over back buffer.
_fmemset(virt,0,64000);
} //Loop for no reason.. but you can add movement here.
getch();
set_mode(0x3);
return 0;
}
0

Experts Exchange Solution brought to you by