Author Topic: problems problems...  (Read 5095 times)

koroljov

  • Guest
problems problems...
« on: September 21, 2003, 01:08:06 pm »
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.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
problems problems...
« Reply #1 on: September 22, 2003, 06:26:56 pm »
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

  • Guest
problems problems...
« Reply #2 on: September 25, 2003, 07:05:44 pm »
I guess I would better use some sorting algorithm to sort the triangles in the z-order.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
problems problems...
« Reply #3 on: September 25, 2003, 10:51:42 pm »
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.