[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
GL3.2 Version 4.1 for AIX: Programming Concepts

Drawing with Move-Draw Style Subroutines

This section discusses the following aspects of move-draw style subroutines:

List of GL Move-Draw Style Subroutines

The following GL Move-Draw Style Subroutines are found in GL3.2 Version 4 for AIX: Graphics Library (GL) Technical Reference.

draw
                          Draws a line.
getgpos
                          Gets the current graphics position.
move
                          Moves the current graphics position to a specified point.
pclos
                          Closes a filled polygon.
pdr
                          Specifies the next point in a polygon.
pmv
                          Specifies the starting point for a polygon.
pnt
                          Draws a point.
rdr
                          Draws a relative line.
rmv
                          Moves the current graphics position to a point relative to the current point.
rpdr
                          Draws a relative polygon.
rpmv
                          Moves the current graphics position to the starting point for a filled polygon relative to the current point.

Move-Draw Introduction

Except for polygons, the figures drawn by the move-draw subroutines are the same as those drawn by the begin-end style ones. For example, points are drawn as a single pixel. However, the subroutines draw polygons only similar to point-sampled polygons; they draw lines connecting the vertices, effectively point-sampled polygons with an outline. For many polygons, the drawing time is approximately doubled when both the polygon and its outline are drawn.

The display list subroutines described in the section entitled "Working with Objects (Display Lists)" (makeobj, closeobj, callobj, and so on) are not supported in the begin-end style subroutines.

The move-draw style subroutines used most often are the three-dimensional ones. Therefore, the naming convention assigns the shortest name (for example, the pnt subroutine) to the most common, 3-D form. The less-used, 2-D form is assigned the longer name, as in the pnt2 subroutine. The 2-D versions are assumed to lie in the z=0 plane, but the 2-D primitives can be transformed out of that plane by the various transformation subroutines.

Current Graphics Position

In GL, the graphical figures are sent together: a set of points, a polyline, and a polygon are sent bracketed by a call to the bgn<type> and end<type> subroutines. The rendering of the figure does not start until the end<type> subroutine is received.

The system automatically maintains the current graphics position, so very few applications need to access it directly, although the getgpos subroutine does return the current graphics position. Its parameters include four pointers to floating point numbers in which the homogeneous coordinates of the current transformed point are returned. The form is getgpos(&fx, &fy, &fz, &fw).

For compatibility, the current graphics position is maintained in exactly the same way for all the move-draw style subroutines. All other graphics subroutines do not depend on the current graphics position, and in fact, leave it in an unpredictable state.

Points

The six versions of the pnt subroutine are shown in the following table.

Versions of the Point Subroutine
Parameter type 2-D 3-D
short integer pnt2s pnts
long integer pnt2i pnti
floating point pnt2 pnt

The parameter lists are: pnt2(x, y) and pnt(x, y, z). In addition to drawing a point, the pnt subroutine updates the current graphics position to its location. The syntax is as follows:

void pnt(Coord x, Coord y, Coord z) 

The following example program draws 100 points in a square area of the window:

#include <gl/gl.h>
main()
{
   int i, j;
   prefposition(100, 500, 100, 500);
   winopen("pointsquare");
   color(BLACK);
   clear();
   color(BLUE);
   for (i = 0; i < 10; i++)
   for (j = 0; j < 10; j++)
      pnti(i*5, j*5, 0);
   sleep(3);
}

Lines and Relative Lines

Lines can be drawn using either the move or draw subroutine. Similar subroutines draw relative lines.

move and draw Subroutines

Lines can be drawn using either of two subroutines, move and draw. The syntax for the move and draw subroutines is as follows:

void move(Coord x, Coord y, Coord z) 
void draw(Coord x, Coord y, Coord z) 

The move subroutine sets the current graphics position to the specified point. The draw subroutine draws from the current graphics position to the specified point and then updates the current graphics position to that point. The parameters and types of the move and draw subroutines are the same as for the point subroutines. The following table is a complete list of the move and draw subroutines.

The move and draw Subroutines
Parameter type 2-D 3-D
short integer move2s moves
long integer move2i movei
floating point move2 move
short integer draw2s draws
long integer draw2i drawi
floating point draw2 draw

The following example program draws the outline of a blue box on the screen using the move and draw subroutines:

#include <gl/gl.h>
main()
{
   prefposition(100, 500, 100, 500);
   winopen("bluebox");
   color(BLACK);
   clear();
   color(BLUE);
   move2i(200, 200);
   draw2i(200, 300);
   draw2i(300, 300);
   draw2i(300, 200);
   draw2i(200, 200);
   sleep(3);
}

Relative Line Subroutines

The relative line-drawing subroutines are similar to the move and draw subroutines, except that their parameters are interpreted as motions relative to the current graphics position. The relative move and draw subroutines are the rmv and rdr subroutines, respectively. After each call to a relative move or draw subroutine, the current graphics position is updated to the specified position. The syntax for the rmv and rdr subroutines is as follows:

void rmv(Coord x, Coord y, Coord z) 
void rdr(Coord x, Coord y, Coord z) 

If the current graphics position is (x, y, z), then rdr(abc) draws a line from (x, y, z) to (x+a, y+b, z+c), and leaves the current graphics position at (x+a, y+b, z+c).

The following table is a complete list of the relative line subroutines.

Relative Line Subroutines
Parameter type 2-D 3-D
short integer rmv2s rmvs
long integer rmv2i rmvi
floating point rmv2 rmv
short integer rdr2s rdrs
long integer rdr2i rdri
floating point rdr2 rdr

The following program draws a blue box identical to the blue box drawn by the previous example program. But this time, the program uses relative drawing subroutines.

#include <gl/gl.h>
main()
{
   prefposition(100, 500, 100, 500);
   winopen("bluebox2");
   color(BLACK);
   clear();
   color(BLUE);
   move2i(200, 200);
   rdr2i(0, 100);
   rdr2i(100, 0);
   rdr2i(0, -100);
   rdr2i(-100, 0);
   sleep(3);
}

Note: The first subroutine is still move2i; this initializes the current graphics position. If another box were to be drawn starting 200 units to the right of the first, it might begin with rmv2i(200, 0).

Polygons and Relative Polygons

GL provides subroutines to draw filled polygons and relative versions of filled polygons.

Filled Polygon Subroutines

The move-draw style subroutines that draw filled polygons are the pmv and pdr subroutines. The following table is a complete list of the filled polygon subroutines.

Filled Polygon Subroutines
Parameter type 2-D 3-D
short integer pmv2s pmvs
long integer pmv2i pmvi
floating point pmv2 pmv
short integer pdr2s pdrs
long integer pdr2i pdri
floating point pdr2 pdr

The syntax for the pmv and pdr subroutines is as follows:

void pmv(Coord x, Coord y, Coord z) 
void pdr(Coord x, Coord y, Coord z) 

Relative Filled Polygon Subroutines

The relative versions of the filled polygon routines are shown in the following table:

Relative Filled Polygon Routines
Parameter type 2-D 3-D
short integer rpmv2s rpmvs
long integer rpmv2i rpmvi
floating point rpmv2 rpmv
short integer rpdr2s rpdrs
long integer rpdr2i rpdri
floating point rpdr2 rpdr

The syntax for the rpmv and rpdr subroutines is as follows:

void rpmv(Coord x, Coord y, Coord z) 
void rpdr(Coord x, Coord y, Coord z) 

A polygon is specified by the following sequence of calls:

  1. A pmv subroutine (or rpmv subroutine) to locate the first point on the boundary
  2. A sequence of pdr (or rpdr) subroutines for each additional vertex
  3. The pclos subroutine to close and fill the polygon.

The pclos subroutine has no parameters All the other subroutines take either two or three parameters of the appropriate type.

After any polygon subroutine, the current graphics position is left at the original point of the polygon, the point identified by the pmv (or rpmv) subroutine.

The following sample program draws a filled blue polygon:

#include <gl/gl.h>
main()
{
   prefposition(100, 500, 100, 500);
   winopen("bluebox");
   color(BLACK);
   clear();
   color(BLUE);
   pmv2i(200, 200);
   pdr2i(200, 300);
   pdr2i(300, 300);
   pdr2i(300, 200);
   pclos();
   sleep(3);
}

Note: The pclos subroutine connects back to the original starting point.

Other drawing methods are explained in Drawing with Begin-End Style Subroutinesand Drawing Rectangles, Circles, Arcs, and Polygons.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]