[ Previous | Next | Contents | Glossary | Home | Search ]
The graPHIGS Programming Interface: Getting Started

Chapter 4. Creating Your First 3D graPHIGS API Program

This section covers the following topics:

Writing, Compiling, and Running the using3d Program

The next program, using3d, takes you into the realm of 3D. This program reads a wireframe geometry data file and creates a 3D wireframe model from the file's contents. A 3D perspective window is also defined for displaying the geometry.

  1. Enter the following program into a file called using3d.c
    -------------------------------------------------------------------
    /*
     * COMPONENT_NAME:  graPHIGS API Samples
     *
     * ORIGINS:  27
     *
     * (C) COPYRIGHT International Business Machines Corp. 1990
     * All Rights Reserved
     *
     * Licensed Materials - Property of IBM
     *
     * US Government Users Restricted Rights - Use, duplication or
     * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
     */
    /*--------------------------------------------------------------*/
    /* graPHIGS Start-Up : Using 3D                                 */
    #include <afmnc.h>
                       /* graPHIGS include file   */
    #include <stdio.h>
                        /* C standard IO          */
     
    main() {
      static float window[4] = {-0.8,0.8,-0.8,0.8};
      static float viewpt[6] = {0.0,1.0,0.0,1.0,0.0,1.0};
      static float prp   [3] = {0.0,0.0,2.4};
      static float oldpos[2] = {5.0,5.0};
      FILE *fp;
      int wsid=1, viewid=1, strid=1, np=0;
      int lcview, major, class, minor, md[3000];
      float mata[16], matb[16], matc[16], lcpos[3], 
    pts[3000][3];
     
      fp = fopen("USING3D.WF","r");
      while ( fscanf(fp,"%d %f %f %f", &md[np],
                     &pts[np][0],
                     &pts[np][1],
                     &pts[np][2]) != -1) {np++;}
     
      GPOPPH ("        ",0);         /* open graPHIGS                */
      GPOPWS (wsid,"*","X       ");  /* open a workstation           */
     
      GPOPST(strid);         /* open structure                       */
      GPPLCI(3);             /* set polyline color GREEN             */
      GPDPL3(np,3,pts,md);   /* add 3D disjoint polyline             */
      GPCLST();              /* close structure                      */
     
      GPVCH (wsid,viewid,2,2,2,  1,0,  2,1,  2);/* activate view     */
    -------------------------------------------------------------------
  2. Create a file called USING3D.WF using the following information to define a square:
         2   -0.5   -0.5    0.0
         2   -0.5    0.5    0.0
         2    0.5    0.5    0.0
         2    0.5   -0.5    0.0
         1   -0.5   -0.5    0.0

    This information is found in:

    /usr/lpp/graPHIGS/samples/gettingstarted/using3D/USING3D.WF
  3. Compile the program using the following AIX command:
    cc -o using3d using3d.c -lgP
  4. Run the program by entering:
    ./using3d

    A window pops up on the screen. Inside the window is the green wireframe geometry. The orientation of the viewer can be changed by moving the mouse pointer inside the graPHIGS API window. To exit the program, position the mouse pointer in the graPHIGS API window and press any button.

Examining the using3d Program

This section looks at the program in more detail, and describes the functions of various parts of the code.

Opening and Reading a File

The following lines of code open and read the wireframe geometry file. The data is stored in the pts[ ] and md[ ] arrays.

-----------------------------------------------------------------
fp = fopen("USING3D.WF","r");
while ( fscanf(fp,"%d %f %f %f",
               &md[np],
               &pts[np][0],
               &pts[np][1],
               &pts[np][2]) != -1) {np++;}
-----------------------------------------------------------------

Creating the Geometry Structure

The geometry creation code is similar to that of the square and flex programs. This program specifies a line color attribute, and then inserts a disjoint polyline primitive into the structure.

-----------------------------------------------------------------
GPOPST(strid);         /* open structure                       */
GPPLCI(3);             /* set polyline color GREEN             */
GPDPL3(np,3,pts,md);   /* add 3D disjoint polyline             */
GPCLST();              /* close structure                      */
-----------------------------------------------------------------

Defining a View

This is the first program which does not use the default graPHIGS API view 0. Instead, this program uses the Set View Characteristics (GPVCH) subroutine to activate the view specified by the view identifier, viewid. It also uses the Set View Mapping 3D (GPVMP3) subroutine to specify the set of parameters which define the view, such as:

Finally, the Set View Input Priority (GPVIP) subroutine ensures that input comes from the default view 0.

-----------------------------------------------------------------
GPVCH (wsid,viewid,2,2,2,  1,0,  2,1,  2);/* activate view     */
GPVMP3(wsid,viewid,window,viewpt,         /* set view mapping  */
  2,prp,  0.0,  1.6,-1.6);                /* perspective       */
GPVIP (wsid,0,viewid,1);/* set view input priority 0>1         */
-----------------------------------------------------------------

Using the Input Loop and Setting a New View Orientation

The last section of code is essentially the same as that of the flex program. The only difference is the animation of the model. This program sets the new view orientation into the view matrix using the Set View Matrix 3D (GPVMT3) subroutine.

-----------------------------------------------------------------
do {                             /* loop until mouse hit       */
  GPSMLC(wsid,1,&lcview,lcpos);  /* get the mouse position     */
  if (lcview == 0 &&
      (lcpos[0] != oldpos[0] ||
       lcpos[1] != oldpos[1]))
  {
    oldpos[0] = lcpos[0];
    oldpos[1] = lcpos[1];
    GPROTX(lcpos[0]*3.14,mata);  /* calculate X rot matrix     */
    GPROTY(lcpos[1]*3.14,matb);  /* calculate Y rot matrix     */
    GPCMT3(mata,matb,matc);      /* multiply matrices          */
    GPVMT3(wsid,viewid,matc);    /* set view matrix, move view */
    GPUPWS(wsid,2);              /* update workstation         */
  }
  GPAWEV(0.0,&major,&class,&minor);  /* check for any events   */
} while (class != 4);
-----------------------------------------------------------------

[ Previous | Next | Contents | Glossary | Home | Search ]