Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

problem with polygon drawing routine

I'm getting to a point with my 3d-engine where I will be able to start programming a game for it.
Problem is a bunch of bugs.
Prior to this post I had a problem with the polygons only drawing when viewing from one side, it was a simple thing.
The main thing preventing me from continuing with my game is that when I look away from my object(s) it start to slow down and then stops.
I known it's because I get bad data to the polygon drawing routine when the objects arent on screen (the y size of the polygon gets really large).


don't know if anyone can understand anything of my routine
but if someone could show me where I'm thinking wrong and tell me how to do it instead.
good links to page's about polygon routines would be highly appriciated.

here's my code:
---------------
#include "polygon.h"
#include "vector.h"
#include "line.h"
#include "define.h"

bool table::ntbl(int ei)
{
if(ei<0)
return 0;
x1=new int[ei];
x2=new int[ei];
for(int i=0; i<ei; i++)
{
x1[i]=0;
x2[i]=0;
}
return 1;
}

void table::dtbl()
{
delete x1;
delete x2;
x1=NULL;
x2=NULL;
}

void edge(int minx, int miny, vector p1, vector p2, table &tbl)
{
int ei=(int)p1.y-miny;
int eminy, emaxy;
double diff, diffx, diffy;
if(p1.y>=p2.y)
{
eminy=(int)p1.y;
emaxy=(int)p2.y;
}
else
{
eminy=(int)p2.y;
emaxy=(int)p1.y;
}
if(p2.x>p1.x)
diffx=p2.x-p1.x;
if(p2.x<=p1.x)
diffx=p1.x-p2.x;
if(p2.y>p1.y)
diffy=p2.y-p1.y;
if(p2.y<=p1.y)
diffy=p1.y-p2.y;
diff=diffx/diffy;
if(p1.x<=p2.x)
{
if(p1.y<=p2.y)
{
for(int i=0; i<(eminy-emaxy); i++)
tbl.x2[(ei+i)]=(int)(i*diff)+(int)p1.x-minx;
}
else
{
for(int i=0; i<(eminy-emaxy); i++)
tbl.x1[(ei-i-1)]=(int)(i*diff)+(int)p1.x-minx;
}
}
else
{
if(p1.y<=p2.y)
{
for(int i=0; i<(eminy-emaxy); i++)
tbl.x2[(ei+i)]=(int)p1.x-minx-(int)(i*diff);
}
else
{
for(int i=0; i<(eminy-emaxy); i++)
tbl.x1[(ei-i-1)]=(int)p1.x-minx-(int)(i*diff);
}
}
}

bool drawpolygon(SDL_Surface *screen, vector *p, Uint32 color)
{
int ip;
int miny=SCREENHEIGHT;
int maxy=0;
int minx=SCREENWIDTH;
int maxx=0;
// ---Wireframe Mode---
/*
for(ip=0; ip<2; ip++)
{
drawline(screen, (int)p[ip].x, (int)p[ip].y, (int)p[ip+1].x, (int)p[ip+1].y, color);
}
drawline(screen, (int)p[ip].x, (int)p[ip].y, (int)p[0].x, (int)p[0].y, color);
*/
// --------------------
// ---Polygon Mode-----
// /*
for(ip=0; ip<3; ip++)
{
if(p[ip].y<miny)
miny=(int)p[ip].y;
}
for(ip=0; ip<3; ip++)
{
if(p[ip].y>maxy)
maxy=(int)p[ip].y;
}
for(ip=0; ip<3; ip++)
{
if(p[ip].x<minx)
minx=(int)p[ip].x;
}
for(ip=0; ip<3; ip++)
{
if(p[ip].x>maxx)
maxx=(int)p[ip].x;
}
int ydiff=maxy-miny;
table tbl;
if(!tbl.ntbl(ydiff))
return 0;

for(ip=0; ip<2; ip++)
edge(minx, miny, p[ip], p[ip+1], tbl);
edge(minx, miny, p[ip], p[0], tbl);

for(int i=0; i<ydiff; i++)
{
if((miny+i)<0)
i=i-miny;
if((miny+i)>=SCREENHEIGHT)
i=ydiff;
if((minx+tbl.x1[i])>=0 && (minx+tbl.x1[i])=0 &&
(minx+tbl.x2[i])=0 && (miny+i)<SCREENHEIGHT)
drawpolyline(screen, minx+tbl.x1[i], minx+tbl.x2[i], miny+i, color);

}
tbl.dtbl();
// --------------------
// */
return 1;
}

void drawpoly(SDL_Surface *screen, polygon p, vector *v)
{
vector w[3];
w[0].x=v[p.a].x;
w[0].y=v[p.a].y;
w[1].x=v[p.b].x;
w[1].y=v[p.b].y;
w[2].x=v[p.c].x;
w[2].y=v[p.c].y;
drawpolygon(screen, w, p.color);
}
-------------------
-~:K.v.a.s.a.r.N.o.m.a.d:~-
Sign In or Register to comment.