Thread: my polygon class

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002

    my polygon class

    I'm developing a simple polygon class (using allegro) to use in games in a way similar to level 10 graphics on the snes. Like with one ground polygon and sprites rendered over that all interacting in a 2d way.

    I made this class and it works ok, but it renders these weird artifact things at the edge of the polygon. I thought it was just an off by one error in the rendering code, but I tried that and it didn't work. The class uses two functions to render, a main render function and a conversion function that handles rotation, translation, and screen coords.
    void poly_t::render(BITMAP* buffer) {
      float x, y;
      int bmpx, bmpy, screenx, screeny;
      point_t point;
      //step through poly and save converted screen coords to buffers
      for (x = -w/2; x < w/2; x += w / float(bmp->w)) {
        for (y = -h/2; y < h/2; y += h / float(bmp->h)) {
          //gen interpolated 3d point (object origin)
          //convert 3d point to world origin to screen
          //find location in bitmap
          bmpx = int((x + w/2) / w * float(bmp->w));
          bmpy = int((y + h/2) / h * float(bmp->h));
          if (bmpx < 0 || bmpx >= max || bmpy < 0 || bmpy >= max)
            error("Invalid bmp coords (" + tostring(bmpx) + "," + tostring(bmpy) + ")");
          //save data to buffers
          screenbufferx[bmpx][bmpy] = screenx;
          screenbuffery[bmpx][bmpy] = screeny;
          screenbufferc[bmpx][bmpy] = getpixel(bmp,bmpx,bmpy);
      //use buffers to render poly with triangles
      for (bmpx = 1; bmpx < bmp->w - 2; ++bmpx) {
        for (bmpy = 1; bmpy < bmp->h - 2; ++bmpy) {
    //object origin points in
    void poly_t::conv(point_t& point, int& screenx, int& screeny) {
      point_t x, y, z;
      //rotate about x-axis
      x.x = point.x;
      x.y = cos(th.x) * point.y - sin(th.x) * point.z;
      x.z = sin(th.x) * point.y + cos(th.x) * point.z;
      //rotate about y-axis
      y.x = cos(th.y) * x.x + sin(th.y) * x.z;
      y.y = x.y;
      y.z = sin(th.y) * x.x + cos(th.y) * x.z;
      //rotate about z-axis
      z.x = cos(th.z) * y.x - sin(th.z) * y.y;
      z.y = sin(th.z) * y.x + cos(th.z) * y.y;
      z.z = y.z;
      z.x += pos.x;
      z.y += pos.y;
      z.z += pos.z;
      //to screen
      screenx = SCREEN_W/2 + int((z.x / z.z) * float(SCREEN_W/2));
      screeny = SCREEN_H/2 + int((z.y / z.z) * float(SCREEN_W/2));
    I'm totally stumped. All that code looks good to me, but something's not working just a little tiny bit. I'll attach a pic of the problem in case that might identify this as some standard problem.
    Last edited by ichijoji; 08-02-2004 at 01:10 AM.
    Illusion and reality become impartiality and confidence.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    There is an easier way to draw tri's.

    In a triangle the length of the two smaller sides are equal to the length of the longest side.

    So find the longest side...scan convert, find the smaller sides, scan convert. Draw lines from left to right.

    void ScanConvert (int tx1, int ty1, int tx2, int ty2, int miny, int maxy, int *SideBuffer)
    if (ty1 < miny) miny = ty1;
    if (ty2 < miny) miny = ty2;
    if (ty1 > maxy) maxy = ty1;
    if (ty2 > maxy) maxy = ty2;
    int diffx = tx2 - tx1;
    int diffy = ty2 - ty1;
    float m=0.0f;
    if (diffy == 0)
    } else m = (float)diffx / (float)diffy;
    int ystep=1;
    if (ty1 > ty2) ystep = 1;
    float x = (float)tx1;
    for (int y = ty1;y<ty2;y+=ystep)
    	SideBuffer(y) = x;
    	x = x + m;
    I'll leave the code to figure out which is the longest side up to you.
    Last edited by VirtualAce; 08-02-2004 at 12:15 AM.

  3. #3
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Quote Originally Posted by Bubba
    There is an easier way to draw tri's.

    In a triangle the length of the sum(two smaller sides)^2 are equal to the length^2 of the longest side.

    So find the longest side...scan convert, find the smaller sides, scan convert. Draw lines from left to right.

    I'll leave the code to figure out which is the longest side up to you.

    > , <
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  4. #4
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    I'm guessing you're talking about those lines on the left of the picture? I'm not familiar with Allegro at all, but it seems like there's a pen of sort that is used to draw, and this pen is always set to the top left corner (0, 0) and draws it's path when it moves. This is just speculation, mind you.

  5. #5
    Join Date
    Feb 2003
    > , <

    the formula for finding the length of a leg of a triangle is:

    a^2 + b^2 = c^2

    where a and b are two sides of the triangle, and c is the hypotenuse.
    there used to be something here, but not anymore

  6. #6
    Join Date
    May 2004
    skorman00 might be right.
    try redrawing the background every frame around the polygon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM