[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
GL3.2 for AIX: Graphics Library (GL) Technical Reference

patch1.c Example C Language Program

/* Example C Language Program patch1.c */
/*
This program draws three surface patches. First, one based on
Bezier curves, then one based on B-Spline curves, and
finally one based on Cardinal curves.
*/
#include <gl/gl.h>
#include <gl/device.h>
Matrix beziermatrix = {
  { -1,  3, -3, 1 },
  {  3, -6,  3, 0 },
  { -3,  3,  0, 0 },
  {  1,  0,  0, 0 }};
Matrix cardinalmatrix = { 
  { -0.5,  1.5, -1.5,  0.5 },
  {  1.0, -2.5,  2.0, -0.5 },
  { -0.5,  0.0,  0.5,  0.0 },
  {  0.0,  1.0,  0.0,  0.0 }};
Matrix bsplinematrix = {
  { -1.0/6.0, 3.0/6.0, -3.0/6.0, 1.0/6.0 },
  {  3.0/6.0, -6.0/6.0, 3.0/6.0, 0.0     },
  { -3.0/6.0,  0.0,     3.0/6.0, 0.0     },
  {  1.0/6.0,  4.0/6.0, 1.0/6.0, 0.0     }};
#define BEZIER 1
#define CARDINAL 2
#define BSPLINE 3
Coord geomx[4][4] = {
  {   0.0, 100.0, 200.0, 300.0} ,
  {   0.0, 100.0, 200.0, 300.0},
  { 700.0, 600.0, 500.0, 400.0},
  { 700.0, 600.0, 500.0, 400.0}};
Coord geomy[4][4] = { 
  { 400.0, 500.0, 600.0, 700.0},
  {   0.0, 100.0, 200.0, 300.0},
  {   0.0, 100.0, 200.0, 300.0},
  { 400.0, 500.0, 600.0, 700.0}};
Coord geomz[4][4] = { 
  { 100.0, 200.0, 300.0, 400.0 },
  { 100.0, 200.0, 300.0, 400.0 },
  { 100.0, 200.0, 300.0, 400.0 },
  { 100.0, 200.0, 300.0, 400.0 }};
main()
{
  Device dev;
  short val;    
  initialize();    
  while (TRUE) {        
    if (qtest()) {
      dev = qread(&val);            
      if (dev == ESCKEY) {
        gexit();
        exit();
      }
      else if (dev == REDRAW) {
        reshapeviewport();
        drawpatch();
      }
    }
  }
}
initialize()
{
  int gid;    
  prefsize(400,400);
  gid = winopen("patch1");    
  qdevice(ESCKEY);
  qdevice(REDRAW);
  qenter(REDRAW,gid);    
  ortho(-100.0, 800.0, -100.0, 800.0, -800.0, 100.0);
}
drawpatch()
{
  int i,j,xx,yy;
  color(BLACK);
  clear();   
  defbasis(BEZIER, beziermatrix);    /* define a basis matrix 
                                        called BEZIER */
  defbasis(CARDINAL,cardinalmatrix); /* define a basis matrix 
                                        called CARDINAL */
  defbasis(BSPLINE,bsplinematrix);   /* define a basis matrix 
                                        called BSPLINE */
  patchbasis(BEZIER,BEZIER);     /* a Bezier basis will be used 
                                    for both directions in the 
                                    first patch */
  patchcurves(4,7);              /* seven curve segments will be 
                                    drawn in the u direction and 
                                    four in the v direction */
  patchprecision(20,20);         /* the curve segments in u 
                                    direction will consist of 20 
                                    line segments (the lowest 
                                    multiple of vcurves greater 
                                    than usegments) and the curve
                                    segments in the v direction 
                                    will consist of 21 line 
                                    segments (the lowest multiple
                                    of ucurves greater than 
                                    vsegments) */
  color(RED);
  patch(geomx,geomy,geomz);      /* the patch is drawn based on 
                                    the sixteen specified control
                                    points */
  patchbasis(CARDINAL,CARDINAL); /* the bases for both directions
                                    are reset */
  color(GREEN);
  patch(geomx,geomy,geomz);      /* another patch is drawn using 
                                    the same control points but a
                                    different basis */
  patchbasis(BSPLINE,BSPLINE);   /* the bases for both directions
                                    are reset again */
  color(BLUE);
  patch(geomx,geomy,geomz);      /* a third patch is drawn */
  color(WHITE);                  /* show the control points */
  for ( i = 0 ; i < 4 ; i++ )  {
    for ( j = 0 ; j < 4 ; j++ )  {
         for ( xx = -2 ; xx < 2 ; xx++) {
            for ( yy = -2 ; yy < 2 ; yy++) {
               pnt( geomx[i][j] + (Coord) xx , geomy[i][j] + (Coord) yy ,
               geomz[i][j] );
        }
      }
    }
  }
}
/*
  Changes:
     - The prefposition changed to a prefsize (400,400);
       Was:  prefposition(XMAXSCREEN/4, XMAXSCREEN*3/4, YMAXSCREEN/4, 
             YMAXSCREEN*3/4);
     - The ortho parameters where changed
          ortho(0.0-20.0, (float)(XMAXSCREEN*3/4), 
              0.0-20.0, (float)(YMAXSCREEN*3/4), 
              (float)XMAXSCREEN, -(float)XMAXSCREEN);
       Now:
          ortho(-100.0, 800.0, -100.0, 800.0, -800.0, 100.0);
*/

Related Information

The patch subroutine , patchbasis subroutine , patchcurves subroutine , patchprecision subroutine .

Drawing Surfaces in GL3.2 Version 4 for AIX: Programming Concepts describes the mathematics and programming statements involved in creating three-dimensional surfaces (or patches) with wire frames of curve segments.


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