Terry's Cybersphere VRML Tutor Win95 Tips Web Threads Research

UNIVERSITY OF ARKANSAS

AN INTRODUCTION TO PHIGS

A RESEARCH PAPER FOR
INTERACTIVE COMPUTER GRAPHICS

GRADUATE DEPARTMENT OF
COMPUTER SYSTEMS ENGINEERING

BY
TERRY SMITH

FAYETTEVILLE, ARKANSAS
DECEMBER 1995


Table of Contents


Introduction

Limitations of PHIGS
Features of PHIGS
How It Works
Data Types, Definitions, and Naming Conventions
How to Draw a Picture with PHIGS
Archives and Metafiles
Using PHIGS with Xlib

Appendix One Program Code

Works Cited


Introduction

PHIGS--the Programer's Hierarchical Interactive Graphics System--is a programming library for 3D graphics. Devised and maintained by the International Organization for Standardization (ISO), it is most commonly used on the X Window System and commonly available from several workstation venders. ISO standardized bindings exist for Fortran, Ada, and C (Schultz 10). The PHIGS high-level graphics library contains over 400 functions ranging from simple line drawing to lighting and shading. Fully one-half of them let you determine the state of an output device or recall settings made earlier in a program. It provides to a graphics application a standard interface to display devices of any manufacturer that supports the PHIGS standard.

Before displaying a picture, the application first stores all its graphics information in the PHIGS graphics database. Thus, PHIGS is referred to as a "display list" system. The application then appears on one or more display devices and then passes the list to them. Because of its display list system structure, PHIGS is well-suited to applications that create a graphics model and then edit portions of it frequently or view it in different ways (Gaskins 1).


Limitations of PHIGS

PHIGS does not do many things necessary for photorealistic and advanced animation purposes. Among them, it does neither ray tracing nor radiosity, it doesn't compute shadows or follow light as it bounces from one object to another, and it doesn't provide texture mapping. Several PHIGS implementations exist, and they themselves may have limits. Most implementations limit the number of light sources. Some don't do depth cueing. Some don't provide PHIGS' more advanced rendering features (Gaskins 5).

When specific high performance graphics hardware is used for 3D rendering, "Standard software graphics subsystems like PHIGS and PEXlib cannot be used because they sacrifice performance for standardization,"; according to Earnshaw and Jern (226). Thus, PHIGS is very appropriate for most engineering and industrial applications but may not be adequate for more advanced visualization.


Features of PHIGS

What can I do with PHIGS? To answer this quickly and concisely, most of the features of PHIGS are listed in Table 1 below:

[WARNING! The following table may appear rather difficent in a non-Netscape browser.]

Table1 - Features of PHIGS
ColorColor Models: RGB,CIE LUV, HSV, HLS
Lights: Ambient, Directional, Spot, & Positional
Shading: Flat, Gouraud, "Dot", and Phong Shading
Depth Cueing: How color changes with distance
Color Mapping: How the workstation approximates colors
Data Mapping: The conversion of application data to color
Area PrimitivesFill Area
Triangle Strips
Quadrilateral Mesh
B-spline Surface
TextText plane determined by text direction vectors
Slanted & Rotated Text
Can set text attributes, alignments, and character spacing
Apply perspective to text
Text Clipping
ModelingLocal & Global Transforms
Scaling & Positioning the entire model
Scaling, Positioning, and Orienting Subcomponents
Animating Models
Model Clipping: clipping plane, model-clipping volumes
ViewingSetting the view coordinate system
Placing the camera
Parallel and Perspective Projections
Panning and Zooming

How It Works

Just as Xlib generates X protocol, PHIGS in the X Window System generates PEX protocol. PEX is the "PHIGS Extension to X";. Since it is not intended for use directly by applications, there must be an application programmer's interface (API) like PHIGS to generate PEX protocol (Lewis 15).


Fig. 1. The relationship between the application, PHIGS, PEX, Xlib, and the
X server, from Tom Gaskins, PHIGS Programming Manual (O'Reilly &
Associates, 1992) 7.

An application can use both PHIGS and Xlib. When an Xlib function is called it transmits X protocol to the server, while calls to PHIGS functions cause transmission of PEX protocol (See Fig. 1). Since its creation by the M.I.T. X Consortium, PEX has moved beyond its original purpose of implementing PHIGS in the X Windows environment. PEX now supports 3-D applications in general and contains features beyond those needed to support PHIGS (Schultz 2).


Data Types, Definitions, and Naming Conventions

All the PHIGS data types, definitions, and function declarations are in the file "phigs.h" which must be included by all PHIGS C programs:

#include <phigs/phigs.h>

The C data types used for PHIGS integer and floating point numbers are Pfloat and Pint. All PHIGS data types and macro definitions start with an uppercase letter "P", and all PHIGS functions begin with a lowercase letter "p" (Gaskins 21).


How to Draw a Picture with PHIGS

There are four steps to visualizing a model in PHIGS (Gaskins 15-16):

  1. Define the model in the PHIGS graphics database.
  2. Create a PHIGS workstation to display the model.
  3. Link the model's description to the workstation.
  4. Tell the workstation to display the model.

The PHIGS graphics database is called the Central Structure Store (CSS) and is composed of containers called structures. Structures, in turn, are grouped of related primitives (line, area, and text for example) and attributes (linetype, linewidth, and color for example). Structures provide a convenient method of organizing a model into objects that can be defined and edited easily.

As described in the PHIGS Programming Manual, "A PHIGS workstation is not an entire computer; it's a place on a computer screen to draw the picture. It may be a single window or the entire screen. It could also be a plotter or a PostScript file. A workstation is actually more than just a display area. It contains intelligence to control the picture and update it when you edit the model. (16)" You create a PHIGS workstation by opening it with the following:

popen_ws( Pint workstation_identifier, void* connection_identifier,
Pint workstation_type)

The connection identifier in most cases is (void *)NULL, indicating that the window should be created on the default server; and in this case, the workstation type is phigs_ws_type_x_tool. If the connection identifier is a drawable, (Pconnid_x_drawable*), then the workstation type is phigs_ws_type_x_drawable.

After the workstation is open, you tell it what to display by posting structures to it. Posting links the workstation to all the structures defining the model, but the structures are not displayed until you update the workstation (Gaskins 16-18).


Archives and Metafiles

Applications do not have to rebuild their graphics database every time they run. Instead, they can store commonly used objects in archive files and read those files at start up. A PHIGS archive is a Central Structure Store (CSS) in a file. Any structure in the CSS can be archived, and structures can be read from or written into an archive by calling one of several functions (Gaskins 557).

Metafiles hold picture descriptions, not model descriptions. A metafile captures the results of traversal; thus, they can be used to save a picture temporarily or to capture PHIGS output to disk. A metafile can also contain several images, which can be separate images or sequential images in an animated sequence. The most common metafile standard is the Computer Graphics Metafile, or "CGM.", which is an ANSI standard. CGM is a 2-D metafile, but still widely used for PHIGS implementations which project 3-D data to 2-D before storing it in a CGM file. There will eventually be a 3-D version of CGM corresponding to PHIGS (Kay 287).


Using PHIGS with Xlib

PHIGS can be used alone to create graphics; however, using PHIGS and X together, you can use Xlib or an X toolkit to create other features such a user interface and allow PHIGS to control the graphics. This section will introduce the use of Xlib with PHIGS by explaining the "ux_xlib.c" program (see Appendix A) which creates the triangle shown in Figure 2.


Fig. 2. The Xlib and PHIGS application created by "ux_xlib.c",
from Tom Gaskins, PHIGS Programming Manual
(O'Reilly & Associates, 1992) 85.

In a PHIGS X application you must create the PHIGS workstation yourself by (1) creating and mapping the window and (2) opening a PHIGS workstation for the window (Gaskins 78). After creating a PHIGS workstation for the window, PHIGS manages the window's resources. Therefore, you should not attempt to changes its resources. You can, however, use Xlib to monitor X events on the window.

The window can be created with XCreateWindow or XCreate-SimpleWindow. Some of the window's attributes you set are likely to be changed or ignored by PHIGS. For example, the background color of the workstation window is determined from the PHIGS workstation color table, so the background pixmap and background pixel attributes are ignored. Nevertheless, some X servers do not display the background correctly if these values are undefined, so we set them anyway. The window class must be InputOutput, and nearly always the backing store is set to NotUseful because keeping the backing store up-to-date slows down drawing (Gaskins 79).

After mapping the window, we open PHIGS with popen_xphigs while setting the mask parameter to PXPHIGS_INFO_NO_MON, telling PHIGS not to monitor X events for the workstation (Gaskins 820). The PHIGS workstation is created with popen_ws described earlier. Since the connection identifier is a drawable, we first set the Pconnid_x_drawable structure:

                             typedef struct {
                                  Display  *display;
                                  XID        drawable_id;
                             }  Pconnid_x_drawable;

The display and drawable_id are set to the currently opened display and window, respectively.

Our next step is to build our model, in this case a single structure, and post the structure to the Central Structure Store (CSS). There are a number of ways to create a structure, but the most common is to call popen_struct passing it an integer ID for the structure:

popen_struct( STRUCTURE_ID );

Then, we set a fill style and set a list of points to bound the structure.

Next, the structure is posted with ppost_struct, passing the workstation ID as the first parameter and the structure ID as the second parameter. The third parameter is the display priority. This priority, a value between 0 and 1, determines the order in which structures are traversed. Higher priority structures are drawn after lower priority structures (Gaskins 603). Structures can also be unposted, meaning it's removed from the screen at the next workstation update.

Finally, we select the X events to handle in our event loop, execute the event loop, and exit gracefully. On an expose event, the entire workstation is redrawn with a call to predraw_all_structs. After a button press exits the loop, we close the workstation, close PHIGS, and close the display.

The code for opening X and PHIGS and creating a PHIGS workstation is lengthy. A function which does all of this can easily be created by modifying "ux_xlib.c".


Appendix One
ux_xlib.c Program Code



/* ux_xlib.c 1.1 */
/* This program code is taken from Tom Gaskins, PHIGS   */
/* Programming Manual (O'Reilly & Associates, 1992) 85. */
#include <phigs/phigs.h>

/* PHIGS Identifiers */
#define WS_ID        (Pint) 1
#define STRUCTURE_ID (Pint) 1

/* Window layout information */
#define WS_X               50
#define WS_Y               50
#define WS_WIDTH           300
#define WS_HEIGHT          300
#define WS_BORDER_WIDTH    1

void main (int argc, char *argv[]);
{
   Display                *display;
   Window                 window;
   XEvent                 event;
   int                    done;
   XSetWindowAttributes   win_attrs;
   Pconnid_x_drawable     conn_id;
   Pxphigs_ info          xphigs_info;
   unsigned long          mask;

   static Ppoint points[] = {{.2,.2},{.8,.2},{.5,.8}};
   static Ppoint_list triangle = {3, points};

   /* Connect to the server and create a window. */
   if ( !(display = XOpenDisplay( NULL )) ) {
	 fprintf( stderr, "Can't open default display\n");
	 exit( -1 );
   }
   win_attrs.backing_store = NotUseFul;
   win_attrs.border_pixel = win_attrs.background_pixel =
	 BlackPixel(display, DefaultScreen(display));
   window = XCreateWindow( display,
	 RootWindow( display, DefaultScreen( display ) ),
	 WS_X, WS_Y, WS_WIDTH, WS_HEIGHT, WS_BORDER_WIDTH,
	 CopyFromParent, InputOutput, CopyFromParent,
	 CWBackingStore | CWBorderPixel | CWBackPixel |
	 &win_attrs );
   XMapWindow( display, window );

   /* Open PHIGS, telling it not to monitor window events.*/
   mask = PXPHIGS_INFO_FLAGS_NO_MON;
   xphigs_info.flags.no_monitor = 1;
   popen_xphigs( PDEF_ERR_FILE, PDEF_MEM_SIZE, mask,
	 &xphigs_info );
   
   /* Build the structure that we'll display. */
   popen_struct( STRUCTURE_ID );
   pset_int_style( PSTYLE_SOLID );
   pfill_area( &triangle );
   pclose_struct();

   /* Post the structure. */
   ppost_struct( WS_ID, STRUCTURE_ID, (Pfloat) 1.0 );

   /* Read and respond to X events. */
   XSelectInput( display, window, ExposureMask |
	 ButtonPressMask );

   done = 0;
   while( !done ) {
	 XNextEvent( display, &event );
	 switch ( event.type ) {
	    case Exposure:
		    if ( event.xexpose.count == 0 )
			   predraw_all_structs(WS_ID, PFLAG_ALWAYS);
		    break;
	    case ButtonPress:
		    done = 1;
		    break;
	 }

   pclose_ws( WS_ID );
   pclose_phigs();
   XCloseDisplay( display );
   return 0;
}

Works Cited

Earnshaw, Rae A. and Micael Jern. "Fundamental Approaches to Interactive Real-Time Visualization Systems." Scientific Visualization: Advances and Challenges. Hong Kong: Academic Press, 1994.

Gaskings, Tom. PHIGS Programming Manual. United States: O'Reilly & Associates, 1992.

Kay, David C. and John R. Levine. Graphics File Formats. New York: Windcrest, 1995.

Lewis, David B. "x-faq/part1." Usenet news comp.windows.x Frequently Asked Questions 1/4. Also available at bloom-picayune.mit.edu. 1992.

Schultz, Karl. "pex-faq" Usenet news comp.graphics.api.pexlib Frequently Asked Questions. 7 Aug. 1995.