Tricks of the Windows Game Programming Gurus:Vector Rasterization and 2D Transformations

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Drawing and Clipping Polygons

Drawing a polygon is as simple as drawing `n` connected line segments. You already know how to draw a line, so all you have to do to draw a polygon is loop on the vertices and connect the dots. Of course, if you want the polygon clipped, you should call the clipping function, which I’ve put a little wrapper around called `Draw_Clip_Line()`. It has the same parameterization as `Draw_Line()`, except it clips to the globally defined clipping region. Here’s a general function to draw a `POLYGON2D` structure:

    int Draw_Polygon2D(POLYGON2D_PTR poly, UCHAR *vbuffer, int lpitch)

    {

    // this function draws a POLYGON2D based on

    // test if the polygon is visible

    if (poly->state)

       {

       // loop thru and draw a line from vertices 1 to n

       for (int index=0; index < poly->num_verts-1; index++)

            {

            // draw line from ith to ith+1 vertex

            Draw_Clip_Line(poly->vlist[index].x+poly->x0,

                           poly->vlist[index].y+poly->y0,

                           poly->vlist[index+1].x+poly->x0,

                           poly->vlist[index+1].y+poly->y0,

                           poly->color,

                           vbuffer, lpitch);

            } // end for

           // now close up polygon

           // draw line from last vertex to 0th

            Draw_Clip_Line(poly->vlist[0].x+poly->x0,

                           poly->vlist[0].y+poly->y0,

                           poly->vlist[index].x+poly->x0,

                           poly->vlist[index].y+poly->y0,

                           poly->color,

                           vbuffer, lpitch);

       // return success

       return(1);

       } // end if

    else

       return(0);

    } // end Draw_Polygon2D

The only weird thing about the function is the use of (x0,y0) as the center of the polygon. This is so you can move the polygon around without messing with the individual vertices. Furthermore, defining the polygon in a relative manner from its center allows you to use what are called local coordinates rather than world coordinates. Take a look at Figure 8.15 to see the relationship between the two.

Figure 8.15 Local coordinates in relation to world coordinates.

It’s always much better to define polygons relative to the center of (0,0) (local coordinates) and then later transform the polygon out to a position (x,y) (world coordinates). You’ll learn about local, world, and camera (the point relative to the viewpoint) coordinates in detail when you get to 3D, but for now, just take note that they exist.

As a demo, I have created `DEMO8_3.CPP|EXE`. It first creates an array of polygon objects, each with eight points. These polygons look like little asteroids. Then the program randomly positions these asteroids and moves (translates) them around the screen. The program works in 640×480×8 and uses page flipping to accomplish the animation. Figure 8.16 is a screen shot from the program.

Figure 8.16 `DEMO8_3.EXE` in action.

All right, Mr. Spock, now you can define a polygon and draw it. The next topic I want to cover is 2D transformations—translating, rotating, and scaling polygons.

### Transformations in the 2D Plane

As I’m sure you can tell, you’re slipping into the world of mathematics—I’ve been sneaking up on you <BG>. Nevertheless, this stuff is fun, and if you understand the basics, there’s really nothing in 3D game programming that you won’t be able to tackle! So let’s hit it!

You’ve seen translation a number of times up to this point, but you haven’t really looked at a mathematical description of it, or for that matter any other transformations, like rotation and scaling. Let’s take a look at each one of these concepts and how they relate to 2D vector images. Then, when you make the leap to 3D graphics, all you’ll need to do is add a variable or two and take into consideration the z-axis—but we’ll get to that.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.