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

#### 2D Polygon Functions

The next set of functions make up the 2D polygon system. This is by no means advanced, fast, or cutting-edge, but just your work up to this point. The functions do the job. There are better ways to do all of this stuff, but that’s why you’re glued to the book, right? <BG>

Function Prototype(s):

    void Draw_Triangle_2D(int x1,int y1, // triangle vertices

                      int x2,int y2,

                      int x3,int y3,

                      int color,  // 8-bit color index

                      UCHAR *dest_buffer, // destination buffer

                      int mempitch); // memory pitch

    // fixed point high speed version, slightly less accurate

    void Draw_TriangleFP_2D(int x1,int y1,

                            int x2,int y2,

                            int x3,int y3,

                            int color,

                            UCHAR *dest_buffer,

                            int mempitch);

Purpose:

`Draw_Triangle_2D()` draws a filled triangle in the given memory buffer with the sent color. The triangle will be clipped to the current clipping region set in the globals, not by the DirectDraw clipper. This is because the function uses software, not the blitter, to draw lines. Note: `Draw_TriangleFP_2D()` does the exact same thing, but it uses fixed-point math internally, is slightly faster, and is slightly less accurate. Both functions return nothing.

Example:

    // draw a triangle (100,10) (150,50) (50,60)

    // with color index 50 in the back buffer surface

    Draw_Triangle_2D(100,10,150,50,50,60,

                     50, // color index 50

                     back_buffer,

                     back_lpitch);

Function Prototype:

    inline void Draw_QuadFP_2D(int x0,int y0, // vertices

                int x1,int y1,

                int x2,int y2,

                int x3,int y3,

                int color, // 8-bit color index

                UCHAR *dest_buffer, // destination video buffer

                int mempitch); // memory pitch of buffer

Purpose:

`Draw_QuadFP_2D()` draws the sent quadrilateral as a composition of two triangles. Returns nothing.

Example:

    // draw a quadrilateral, note vertices must be ordered

    // either in cw or ccw order

    Draw_QuadFP_2D(0,0, 10,0, 15,20, 5,25,

                   100,

                   back_buffer, back_lpitch);

Function Prototype:

    void Draw_Filled_Polygon2D(

              POLYGON2D_PTR poly, // poly to render

              UCHAR *vbuffer, // video buffer

              int mempitch);  // memory pitch

Purpose:

`Draw_Filled_Polygon2D()` draws a general filled polygon with n sides. The function simply takes the polygon to render, a pointer to the video buffer, and the pitch, and that’s it! Note: The function renders relative to the poly’s (x0,y0), so make sure these are initialized. Returns nothing.

Example:

    // draw a polygon in the primary buffer

    Draw_Filled_Polygon2D(&poly,

                          primary_buffer,

                          primary_lpitch);

Function Prototype:

    int Translate_Polygon2D(

               POLYGON2D_PTR poly, // poly to translate

               int dx, int dy); // translation factors

Purpose:

`Translate_Polygon2D()` translates the given polygon’s origin (x0,y0). Note: The function does not transform or modify the actual vertices making up the polygon. Returns `TRUE` if successful.

Example:

    // translate polygon 10,-5

    Translate_Polygon2D(&poly, 10, -5);

Function Prototype:

    int Rotate_Polygon2D(

                     POLYGON2D_PTR poly, // poly to rotate

                     int theta); // angle 0-359

Purpose:

`Rotate_Polygon2D()` rotates the sent polygon in a counterclockwise fashion about its origin. The angle must be an integer from 0-359. Returns `TRUE` if successful.

Example:

    // rotate polygon 10 degrees

    Rotate_Polygon2D(&poly, 10);

Function Prototype:

    int Scale_Polygon2D(POLYGON2D_PTR poly, // poly to scale

                        float sx, float sy); // scale factors

Purpose:

`Scale_Polygon2D()` scales the sent polygon by scale factors sx and sy in the x- and y-axes, respectively. Returns nothing.

Example:

    // scale the poly equally 2x

    Scale_Polygon2D(&poly, 2,2);

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.