Polygon rendering



Note: these functions are not supported in truecolor video modes!

void polygon3d(BITMAP *bmp, int type, BITMAP *texture, int vc, V3D *vtx[]);
void polygon3d_f(BITMAP *bmp, int type, BITMAP *texture, int vc, V3D_f *vtx[]);
Draw 3d polygons onto the specified bitmap, using the specified rendering mode. Unlike the regular polygon() function, these routines don't support concave or self-intersecting shapes, and they can't draw onto mode-X screen bitmaps (if you want to write 3d code in mode-X, draw onto a memory bitmap and then blit to the screen). The width and height of the texture bitmap must be powers of two, but can be different, eg. a 64x16 texture is fine, but a 17x3 one is not. The vertex count parameter (vc) should be followed by an array containing the appropriate number of pointers to vertex structures: polygon3d() uses the fixed point V3D structure, while polygon3d_f() uses the floating point V3D_f structure. These are defined as:

   typedef struct V3D
   {
      fixed x, y, z;       - position
      fixed u, v;          - texture map coordinates
      int c;               - color
   } V3D;

typedef struct V3D_f { float x, y, z; - position float u, v; - texture map coordinates int c; - color } V3D_f;

How the vertex data is used depends on the rendering mode:

The x and y values specify the position of the vertex in 2d screen coordinates.

The z value is only required when doing perspective correct texture mapping, and specifies the depth of the point in 3d world coordinates.

The u and v coordinates are only required when doing texture mapping, and specify the position of the point in the texture bitmap, for example 0, 0 maps the vertex onto the top left corner of the texture, and if the texture is sized 32x32, setting u=32 and v=16 maps the vertex to the point half way down the right edge of the texture. The u/v coordinates wrap at the edge of the texture, so with a 32x32 texture, u=v=32 is the same as u=v=0. This can be used to tile textures several times across a polygon.

The c value specifies the vertex color, and is interpreted differently by various rendering modes.

The type parameter specifies the polygon rendering mode, and can be any of the values:

POLYTYPE_FLAT:
A simple flat shaded polygon, taking the color from the c value of the first vertex.

POLYTYPE_GCOL:
A single-color gouraud shaded polygon. The colors for each vertex are taken from the c value, and interpolated across the polygon. This is very fast, but will only work if your palette contains a smooth gradient between the colors.

POLYTYPE_GRGB:
A gouraud shaded polygon which interpolates RGB triplets rather than a single color, and uses the global rgb_map table to convert the result to an 8 bit paletted color. This must only be used after you have set up the RGB mapping table! This mode is significantly slower than POLYTYPE_GCOL, but doesn't require the palette to contain all the color gradients you are going to use. The colors for each vertex are taken from the c value, which is interpreted as a 24 bit RGB triplet (0xFF0000 is red, 0x00FF00 is green, and 0x0000FF is blue).

POLYTYPE_ATEX:
An affine texture mapped polygon. This stretches the texture across the polygon with a simple 2d linear interpolation, which is fast but not mathematically correct. It can look ok if the polygon is fairly small or flat-on to the camera, but because it doesn't deal with perspective foreshortening, it can produce strange warping artifacts. To see what I mean, run test.exe and see what happens to the polygon3d() test when you zoom in very close to the cube.

POLYTYPE_PTEX:
A perspective-correct texture mapped polygon. This uses the z value from the vertex structure as well as the u/v coordinates, so textures are displayed correctly regardless of the angle they are viewed from. Because it involves division calculations in the inner texture mapping loop, this mode is a lot slower than POLYTYPE_ATEX, and it uses floating point so it will be very slow on anything less than a Pentium (even with an FPU, a 486 can't overlap floating point division with other integer operations like the Pentium can).

POLYTYPE_ATEX_MASK:
POLYTYPE_PTEX_MASK:
Like POLYTYPE_ATEX and POLYTYPE_PTEX, but zero texture map pixels are skipped, allowing parts of the texture map to be transparent.

POLYTYPE_ATEX_LIT:
POLYTYPE_PTEX_LIT:
Like POLYTYPE_ATEX and POLYTYPE_PTEX, but the global color_map table is used to blend the texture with a light level taken from the c value in the vertex structure. This must only be used after you have set up the color mapping table! These modes cannot be used with texture maps larger than 256x256.

void triangle3d(BITMAP *bmp, int type, BITMAP *tex, V3D *v1, *v2, *v3);
void triangle3d_f(BITMAP *bmp, int type, BITMAP *tex, V3D_f *v1, *v2, *v3);
Draw 3d triangles, using either fixed or floating point vertex structures. These are equivalent to calling polygon3d(bmp, type, tex, 3, v1, v2, v3); or polygon3d_f(bmp, type, tex, 3, v1, v2, v3);

void quad3d(BITMAP *bmp, int type, BITMAP *tex, V3D *v1, *v2, *v3, *v4);
void quad3d_f(BITMAP *bmp, int type, BITMAP *tex, V3D_f *v1, *v2, *v3, *v4);
Draw 3d quads, using either fixed or floating point vertex structures. These are equivalent to calling polygon3d(bmp, type, tex, 4, v1, v2, v3, v4); or polygon3d_f(bmp, type, tex, 4, v1, v2, v3, v4);




Back to Contents