Sign in to follow this  

Line rasterization problem

This topic is 3870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a small software renderer as an experiment. I have made it so far as line rasterisation and i'm already having troubles. Here's an image: http://img515.imageshack.us/img515/4369/problemcg4.jpg It seems like the same vertex doesn't have the same screen position for two or more lines. Obviously, there's an error stacking up (and a big one) but I can't seem to find it. Here's code: steep := abs(fry2 - fry1) > abs(frx2 - frx1);
  if steep then
    begin
      //  Swaps
      aux := frx1; frx1 := fry1; fry1 := aux;
      aux := frx2; frx2 := fry2; fry2 := aux;
    end;

  if (frx1 > frx2) then
    begin
      //  Swaps
      aux := frx1; frx1 := frx2; frx2 := aux;
      aux := fry1; fry1 := fry2; fry2 := aux;
    end;

  rx1 := Round(frx1);
  rx2 := Round(frx2);

  cs := frx2 - frx1;

  deltax := Round(frx2 - frx1);
  deltay := Round(Abs(fry2 - fry1));
  error := -deltax div 2;
  ry := Round(fry1);
  if (fry1 < fry2) then ystep := 1 else ystep := -1;
  for rx := rx1 to rx2 do
    begin
      cs2 := (rx - rx1) / cs;
      cs3 := 1 - cs2;

      if steep then
        PlotFragment(ry, rx)
       else
        PlotFragment(rx, ry);

      error := error + deltay;
      if (error > 0) then
        begin
          ry := ry + ystep;
          error := error - deltax;
        end;
    end;
This is an optimised bresenham's algorithm that I found on wikipedia. Any ideeas ? PS: I seem getting the same error with a DDS algorithm !

Share this post


Link to post
Share on other sites
The problem is the rounding to integer coordinates. Depending on whether the line is x-major or y-major (steep), the coordinates get rounded differently.

One solution is to round all coordinates (x and y) to integers first. That way you're guaranteed that every line uses the same endpoint coordinates.

Once you've fixed that you can further improve quality with sub-pixel accuracy. Instead of rounding the coordinates to integers, round them to fixed-point numbers with about 4 bits below the digit. You can keep using Bresenham's algorithm for rasterization, but you need to compute the correct starting values and step values...

Share this post


Link to post
Share on other sites

This topic is 3870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this