Original Post
Hi,
I need help on this java thing. Yes I know this is in tha java forum and there is another forum for this sort of thing, but I am writing it in java and I need to be aware of any performance increases I could get out of java. It is a function for flat shading polygons and it works perfectly, apart from the speed. It is based on the Fatmap tutorial by Mats Byggmastar, its on this site so people may be familiar with it. Anyways here it is:
public void fillPoly3D(Poly3D p,int color) {
projectPoints(p.verteces,p.nverteces);
// project 3d points to 2d, xpoints[] and ypoints[]
int tempx;
int tempy;
if(ypoints[0] > ypoints[1]) { tempy=ypoints[0]; tempx=xpoints[0]; ypoints[0]=ypoints[1]; xpoints[0]=xpoints[1]; ypoints[1]=tempy; xpoints[1]=tempx; }
if(ypoints[0] > ypoints[2]) { tempy=ypoints[0]; tempx=xpoints[0]; ypoints[0]=ypoints[2]; xpoints[0]=xpoints[2]; ypoints[2]=tempy; xpoints[2]=tempx; }
if(ypoints[1] > ypoints[2]) { tempy=ypoints[1]; tempx=xpoints[1]; ypoints[1]=ypoints[2]; xpoints[1]=xpoints[2]; ypoints[2]=tempy; xpoints[2]=tempx; }
// ^ Sorts the points into height order (0 = top, 1 = mid, 2 = bot)
int polyHeight = ypoints[2] - ypoints[0];
if(polyHeight == 0) // Has zero height so can''t be visible
return;
int temp = ((ypoints[1] - ypoints[0]) << 16) / polyHeight;
int longest = temp * (xpoints[2] - xpoints[0]) + ((xpoints[0] - xpoints[1]) << 16);
if(ypoints[0] == ypoints[1]) ypoints[1]+=1;
// ^ for debugging a problem with the edges
if(longest<0) { // middle point is on the right
ledge_dx[0]=ledge_dx[1]=((xpoints[2]-xpoints[0])<<16) / polyHeight;
if(ypoints[1]!=ypoints[0])
redge_dx[0]=((xpoints[1]-xpoints[0])<<16) / (ypoints[1]-ypoints[0]);
if(ypoints[2]!=ypoints[1])
redge_dx[1]=((xpoints[2]-xpoints[1])<<16) / (ypoints[2]-ypoints[1]);
}
if(longest>0) { // middle point is on the left
redge_dx[0]=redge_dx[1]=((xpoints[2]-xpoints[0])<<16) / polyHeight;
if(ypoints[1]!=ypoints[0])
ledge_dx[0]=((xpoints[1]-xpoints[0])<<16) / (ypoints[1]-ypoints[0]);
if(ypoints[2]!=ypoints[1])
ledge_dx[1]=((xpoints[2]-xpoints[1])<<16) / (ypoints[2]-ypoints[1]);
}
int ledge_x=xpoints[0]<<16,redge_x=xpoints[0]<<16; // Top of polygon
int y=(ypoints[0]<0) ? 0 : ypoints[0]; // Start at the top
int ymax=(ypoints[2]>height) ? height : ypoints[2]; // bottom limit
int ledgedx=ledge_dx[0];
int redgedx=redge_dx[0];
int x;
int xmax;
int scany=ytable[y];
do {
x=(ledge_x>>16<0) ? 0 : ledge_x>>16;
xmax=(redge_x>>16>width) ? width : redge_x>>16;
scany+=width;
do {
pixels[ scany+x ]=color;
} while(++x
Before this I used an edge buffer to draw polygons, but the Fatmap tut says that is slow and should be avoided. But there has only been a few fps if anything added. Anyway thanks in advance for any comments and help,
-Paul