problems problems...

Started by koroljov, September 21, 2003, 01:08:06 PM

Previous topic - Next topic

koroljov

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.

EgonOlsen

How slow is "slow"? From the code you presented, i can't find anything wrong with your approach. It's basically the same what i'm doing in jPCT (albeit i'm interpolating v,r,g,b too...just like in the code i've posted in your last thread).
A wild guess would be, that your ZBuffer[] in not int[] but float[] or whatever which would cause an implicit conversion all the time, but i doubt that... :wink:
What VM are you using? Have you tried the (unfinished) demo i posted here: http://www.jpct.net/forum/viewtopic.php?t=75? It's based on a much more recent version of jPCT than the old jPCT-demo. How fast does it run on your machine?

koroljov

I guess I would better use some sorting algorithm to sort the triangles in the z-order.

EgonOlsen

Quote from: "koroljov"I guess I would better use some sorting algorithm to sort the triangles in the z-order.
Yes. Polygon sorting and drawing front to back is good for software engines (only a limited number of polys to sort, but highly fillrate limited). jPCT is doing this too. In addition, it uses some "tricks" to skip the drawing of hidden spans and to avoid zbuffer-reads in some situations.