This section covers the following topics:
The shade program introduces you to the areas of shading, lighting, and hidden surface removal. A shaded version of the geometry used in the using3d program is read and displayed in a 3D window.
------------------------------------------------------------------ /* * 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 : shading and lighting */ #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}; static float ltdir [3] = {0.1,0.1,-1.0}; static int inll [2] = {1,2}; FILE *fp; int wsid=1, viewid=1, strid=1, nvert=0, pdata=1, n2=2; int endflag, lcview, major, class, minor; float mata[16], matb[16], matc[16], lcpos[3], pts[10][6]; struct {int mode; float r,g,b;} ltcol = {2,1.0,1.0,1.0}; GPOPPH (" ",0); /* open graPHIGS */ GPOPWS (wsid,"*","X "); /* open a workstation */ if (shadingQ(wsid) == 0) { /* is shading supported? */ printf("No shading support, exiting program.\n"); GPCLPH (); /* close graPHIGS */ exit(1); } fp = fopen("USING3D.SH","r"); GPOPST(strid); /* open structure */ GPINLB(1); /* insert label 1 */ GPINLB(2); /* insert label 2 */ ------------------------------------------------------------------
0 0.0 -0.5 -0.5 1.0 0.0 0.0 0 0.0 -0.5 0.5 1.0 0.0 0.0 0 0.0 0.5 0.5 1.0 0.0 0.0 1 0.0 0.5 -0.5 1.0 0.0 0.0 0 0.0 -0.5 -0.5 1.0 0.0 0.0 0 0.0 0.5 -0.5 1.0 0.0 0.0 0 0.0 0.5 0.5 1.0 0.0 0.0 1 0.0 -0.5 0.5 1.0 0.0 0.0 -1 0.0 0.0 0.0 0.0 0.0 0.0
This information is found in:
/usr/lpp/graPHIGS/samples/gettingstarted/shade/USING3D.SH
cc -o shade shade.c -lgP
This program introduces a new concept, the graPHIGS API PROFILE. The PROFILE is an external file which is read by the graPHIGS API every time a program is executed. The PROFILE is used to alter certain graPHIGS API default values and to override parameters sent to graPHIGS API control functions. The instructions below set up a direct-color color table instead of the default color table.
* graPHIGS Start-Up - PROFILE for shade.c sample * AFMMNICK PROCOPT=((DIRCOLOR))
./shade
A window pops onto the screen. Inside the window is the shaded 3D geometry. The orientation of the viewer can be changed by moving the mouse pointer while it is inside the graPHIGS API window. To exit the program, position the mouse pointer in the graPHIGS API window and press any button.
This section describes the purpose of various sets of instructions in the shade program.
After opening the graPHIGS API and a graPHIGS API workstation, the program checks to see if the workstation supports shading. This check has been separated into a utility function called shadingQ (shading query). This utility function uses the graPHIGS API Inquire Light Source Facilities (GPQLSF) subroutine to check for shading support. If shading is supported, a value of 1 is returned. Otherwise a value of 0 is returned.
----------------------------------------------------------------- if (shadingQ(wsid) == 0) { /* is shading supported? */ printf("No shading support, exiting program.\n"); GPCLPH (); /* close graPHIGS */ exit(1); } · /* utility function to check for shading support */ int shadingQ(wsid) { int status,conlen,errind,maxe,totnum,ltype,maxa,npred; char wstype[10],connid[255] GPQRCT(wsid,255,&status,&conlen,connid,wstype); GPQLSF(wstype,1,0,&errind,&maxe,&totnum,;<ype,&maxa,&n pred); if (maxe == 0) return 0; return 1; } -----------------------------------------------------------------
This program uses several new subroutines in the geometry structure creation code:
GPICD | Set Interior Color Direct - Used in the program to set the color of the model. Allows you to pass in an array containing the red, green and blue color values defining the interior color of the geometry. |
GPICD | Set Interior Color Direct - Used in the program to set the color of the model. Allows you to pass in an array containing the red, green and blue color values defining the interior color of the geometry |
GPSCD | Set Specular Color Direct - Used to specify the specular color of the model. |
GPLMO | Set Lighting Mode - Used to activate lighting. |
GPLSS | Set Light Source State - Used to activate lighting. |
GPHID | Set HLHSR (Hidden Line and Hidden Surface Removal) - Used to activate hidden surface removal. |
GPPGD3 | Polygon with Data 3D - Used to create the actual geometry. Allows you to define 3D polygons with vertex normal vectors to simulate the curvature of the polygon. |
----------------------------------------------------------------- fp = fopen("USING3D.SH","r"); GPOPST(strid); /* open structure */ GPINLB(1); /* insert label 1: */ GPINLB(2); /* insert label 2 */ GPICD (<col.r); /* set interior color WHITE */ GPSCD (<col.r); /* set specular color WHITE */ GPSPR (0.3,0.4,0.4,9.0,1.0); /* set surface properties */ GPLMO (3); /* set lighting to gouraud */ GPIS (2); /* set interior style solid */ GPHID (1); /* activate hidden surface removal */ GPPGC (2); /* set polygon culling on */ GPLSS (2,inll,0,inll); /* activate 2 light sources */ do { /* loop to read X29 shade file */ fscanf(fp,"d %f %f %f %f %f %f",&endflag, &pts[nvert][0], &pts[nvert][1], &pts[nvert][2], &pts[nvert][3], &pts[nvert][4], &pts[nvert][5]); nvert++; if (endflag == 1) { GPPGD3(0,&pdata,0,1,&nvert,1,6,pts); /* create polygon */ nvert=0; } } while (endflag != -1); GPCLST(); /* close structure */ -----------------------------------------------------------------
The following two lines of code define the light sources. The two Set Light Source Representation (GPLSR) subroutines below define one ambient light source and one directional light source.
----------------------------------------------------------------- GPLSR(wsid,1,1,<col,0); /* define light source 1 */ GPLSR(wsid,2,2,<col,ltdir); /* define light source 2 */ -----------------------------------------------------------------
The only remaining new subroutine is Set Extended View Representation (GPXVR). This subroutine allows you to set viewing parameters individually and is sometimes used in place of the Set View Mapping 3D (GPVMP3) subroutine. This program uses GPXVR to activate hidden surface removal for the view specified by the view identifier viewid.
----------------------------------------------------------------- GPXVR (wsid,viewid,10,&n2); /* activate hidden surface */ -----------------------------------------------------------------
The last section of code is essentially the same as that of the using3d program. The only difference is that the using3d program sets a new view orientation while the shade program sets a new model orientation. This program sets the new model orientation (transformation matrix) into the open structure using the Set Modeling Transformation 3D (GPMLX3) 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 */ GPOPST(strid); /* open structure for edit */ GPDELB(1,2); /* delete between labels */ GPMLX3(matc,3); /* insert new model matrix */ GPCLST(); /* close structure */ GPUPWS(wsid,2); /* update workstation */ } GPAWEV(0.0,&major,&class,&minor); /* check for any events */ } while (class != 4); ------------------------------------------------------------------