Thread: Gouraud Shading problem

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    19

    Gouraud Shading problem

    Hi,

    I've written a single module to rasterize and shade a triangle. Rasterization works fine but I get some errors when I try to bilinearly interpolate between the vertices.

    The module is too big and I rather not post it (unless of course one of you wants me to).

    Assuming that the vertices are in an anticlockwise arrangement:

    Get length of left edge
    Get length of right edge

    For every scanline
    Interpolate between vertex v0 and v1
    Interpolate between vertex v0 and v2

    for every pixel in current scaline
    Interpolate between values of previous two interpolations
    Colour the pixel

    That is basically how I did it. I tried but failed in finding any coded examples that weren't heavily optimized. ( usually heavy optimizations affect clarity i.e. inlined assembly modules)

    Any help would be appreciated( tutorials, code, personal opinions)

    Duetti

  2. #2
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    can you post the code

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    19
    I removed the code fragment
    Last edited by Duetti; 11-22-2003 at 04:57 PM.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    19
    I found the errors in the code fragment above, the most serious of which was that I forgot to call sqrt() for the left and right edges.
    Last edited by Duetti; 11-22-2003 at 04:53 PM.

  5. #5
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Crap, I *might've* been able to help because I've dabbled with softare rasterization crap. Could you send me the code anyway? I'm interested in seeing it.

    Congrats on fixing the problem.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I've posted this a million times here but....

    Code:
    //No assembly
    double BI(double v1,double v2,double v3,double v4,double f1,double f2)
    {
      double val1=v1+f1*(v2-v1);
      double val2=v3+f1*(v4-v3);
      return (val1+f2*(val2-val1));
    }
    
    //Inline assembly
    double BI(double v1,double v2,double v3,double v4,double f1,double f2)
    }
      double val1=0.0,val2=0.0,rval=0.0;
      asm {
        
        fld     [v2]
        fsub  [v1]
        fmul  [f1]
        fadd  [v1]
        fstp   [val1]
    
        fld     [v4]
        fsub  [v3]
        fmul  [f1]
        fadd  [v3]
        fstp   [val2]
    
        fld     [val2]
        fsub  [val1]
        fmul  [f2]
        fadd  [val1]
        fstp   [rval]
       }
      return rval;
    }

    But to do what you want only requires 2 separate linear interpolations rather than one bilinear. Essentially you are doing a bilinear interpolation but its easier to do two singles which is:

    interpolated_value=v1+f1*(v2-v1);

    where f1 is the interpolant.


    Code:
    //Scan conversion
    double m=(x2-x1)/(y2-y1);
    double cm=(c2-c1)/y2-y1);
    int x=x1;
    int c=c1;
    
    for (double y=y1;y<y2;y+=1.0)
    {
      edge[(int)y].x=x;
      edge[(int)y].c=c;
      x+=m;
      c+=cm;
    }
    
    
    //Drawing from left edge to right edge
    double x1=(double)left_edge[y].x;
    double x2=(double)right_edge[y].x;
    double c1=(double)left_edge[y].c;
    double c2=(double)right_edge[y].c;
    
    cm=(c2-c1)/(x2-x1);
    
    double c=(double)c1;
    
    for (double x=x1;x<x2;x++)
    {
      putpixel((int)x,(int)y,(int)c);
      c+=cm;
    }
    So it is a type of bilinear interpolation since you first interpolate on y and then x. The data type conversions might be wrong in my examples but I'm sure you can get it right. Using < and > with doubles is not a good idea.
    Last edited by VirtualAce; 11-23-2003 at 01:27 AM.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    19
    Apologies for the delay Silvercord. I tried to fix a few problems with the module. It's not error free but it works fine for triangles with flat bottom or top. Where shall I send the code?
    Last edited by Duetti; 11-25-2003 at 05:41 PM.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    19
    What about this thread geek?

    :-)

    Duetti

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Stop bumping threads...see rules in the link on my sig

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM