I was trying to write a 3d engine in java.
I wrote the triangle filler , using this algorithm:
-sort the points on the y-axis.
-calculate (x2-x1)/(y2-y1) for each of the 3 lines.
-use that to calculate the start position and the width of each scanline.
-draw the scanline.
This is all in fixed-point.
I use a 1-dimensional texture, so I interpolate 1/z and u/z. (fixed-point, of course).
I calculate the correct U and Z value onley for the start and end point of each scanline.
Then I draw the scanline like this (with linear interpolation):
public void drawScanLine(int start,int stop,int u1,int u2,int[] tex)
{
int dus=(u2-u1)/(stop-start);
for(;start<=stop;start++)
{
u1+=dus;
pixels[start]=tex[u1>>20];
}
}
This works perfectly.
Then I try to add a Z-buffer, like this:
public void drawScanLine(int start,int stop,int u1,int u2,int z1,int z2,int[] tex)
{
int dus=(u2-u1)/(stop-start);
int dzs=(z2-z1)/(stop-start);
for(;start<=stop;start++)
{
if(z1<Zbuffer[start])
{
px[start]=tex[u1>>20];
Zbuffer[start]=z1;
}
z1+=dzs;
u1+=dus;
}
}
This works verry slow.
Is there something wrong with my source? Should I use an other algorithm for the Z-buffer?
The JPCT demo works perfectly and very rapid on the same computer.