home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / SciTex / examples / phong / object.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.7 KB  |  186 lines

  1. /******************************************************************************
  2.  *  object.c
  3.  ******************************************************************************
  4.  *
  5.  *  Purpose:
  6.  *      Descriptional text.
  7.  *
  8.  *  Authors:
  9.  *      Michael Teschner and Christian Henn
  10.  *
  11.  *  Note:
  12.  *      None.
  13.  *  
  14.  *  Revisions:
  15.  *      10.11.93    micha    Created file.
  16.  *
  17.  ******************************************************************************
  18.  *
  19.  *  COPYRIGHT (C)                  1992, 1993, 1994
  20.  *
  21.  *  BY  CHRISTIAN HENN             M.E. MUELLER-INSTITUTE FOR MICROSCOPY (MIM)
  22.  *      HENN@COMP.BIOZ.UNIBAS.CH   CH-4056 BASEL, SWITZERLAND   
  23.  *
  24.  *  AND MICHAEL WALDHERR-TESCHNER  SILICON GRAPHICS INDUSTRIES (SGI)
  25.  *      MICHA@BASEL.SGI.COM        CH-4125 RIEHEN, SWITZERLAND
  26.  *
  27.  ******************************************************************************
  28.  *
  29.  *  PERMISSION TO USE, COPY, MODIFY AND DISTRIBUTE THIS SOFTWARE AND ITS DOCU-
  30.  *  MENTATION FOR THE PURPOSE OF RESEARCH, DEVELOPMENT AND EDUCATION IS HEREBY
  31.  *  GRANTED FREE OF CHARGE, SUBJECT TO THE FOLLOWING RESTRICTIONS:
  32.  *
  33.  *  THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  34.  *  IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF DESIGN,
  35.  *  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A
  36.  *  COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  37.  *
  38.  *  IN NO EVENT SHALL SILICON GRAPHICS OR THE M.E. MUELLER-INSTITUTE BE LIABLE
  39.  *  FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  40.  *  OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  41.  *  WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  42.  *  LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  43.  *  THIS SOFTWARE.
  44.  *
  45.  ******************************************************************************
  46.  */
  47. #include <stdio.h>
  48. #include <math.h>
  49. #include <gl/gl.h>
  50.  
  51. #define PHI_TESS 16
  52. #define PSI_TESS 8
  53.  
  54. /* #define PHI_TESS 256
  55. #define PSI_TESS 128
  56. */
  57.  
  58. #define M_PHI (M_PI/(PHI_TESS.0-1.0)*2.0)
  59. #define M_PSI (M_PI/(PSI_TESS.0-1.0)*1.0)
  60.  
  61. float coord[PSI_TESS][PHI_TESS][3]; /* coords and normals of a sphere */
  62.  
  63. int shade_flag;
  64. int line_flag;
  65. int solid_flag;
  66. int mss_flag;
  67.  
  68. int object_create()
  69. {
  70.   float r,z;
  71.   float phi_angle,psi_angle;
  72.   int    phi,psi;
  73.  
  74.   psi_angle = 0.0;
  75.   for(psi = 0; psi < PSI_TESS; psi++) {
  76.     z =   fcos(psi_angle);
  77.     r =   fsin(psi_angle);
  78.     phi_angle = 0.0;
  79.     for(phi = 0; phi < PHI_TESS; phi++) {
  80.       coord[psi][phi][2] = z;
  81.       coord[psi][phi][1] = r * fsin(phi_angle);
  82.       coord[psi][phi][0] = r * fcos(phi_angle);
  83.       phi_angle += M_PHI;
  84.     }
  85.     psi_angle += M_PSI;
  86.   }
  87.  
  88. } /* END object_create */
  89.  
  90. float tcoord[4][3] = {
  91.   {0.0,0.0,0.0},
  92.   {1.0,0.0,0.0},
  93.   {1.0,1.0,0.0},
  94.   {0.0,1.0,0.0}
  95. };
  96.  
  97. float lcoord[4][3] = {
  98.   {-.5,-.5,0.0},
  99.   {0.5,-.5,0.0},
  100.   {0.5,0.5,0.0},
  101.   {-.5,0.5,0.0}
  102. };
  103.  
  104. int object_draw()
  105. {
  106.   int phi,psi;    /* we will draw a simple sphere */
  107.  
  108.   /* set the model color */
  109.   zbuffer(TRUE);
  110. /*   RGBcolor(230,210,255); */
  111.  
  112.   RGBcolor(255,255,255); 
  113.   /* 0----3->
  114.    * |    |
  115.    * |    |
  116.    * 1----2->
  117.    */
  118.  
  119.   if( solid_flag ) {
  120.     if( shade_flag == 1 ) {                          /* phong shading */
  121.     for(psi = 0; psi < (PSI_TESS-1); psi++) {
  122.       bgnqstrip();
  123.       t3f(&coord[psi  ][0][0]);
  124.       v3f(&coord[psi  ][0][0]);
  125.       t3f(&coord[psi+1][0][0]);
  126.       v3f(&coord[psi+1][0][0]);
  127.       for(phi = 0; phi <= (PHI_TESS-1); phi++) {
  128.         t3f(&coord[psi+1][phi][0]);
  129.         v3f(&coord[psi+1][phi][0]);
  130.         t3f(&coord[psi  ][phi][0]);
  131.         v3f(&coord[psi  ][phi][0]);
  132.         }
  133.       endqstrip();
  134.       }
  135.      }
  136.    else {                                            /* gouraud shading */
  137.     for(psi = 0; psi < (PSI_TESS-1); psi++) {
  138.       bgnqstrip();
  139.       n3f(&coord[psi  ][0][0]);
  140.       v3f(&coord[psi  ][0][0]);
  141.       n3f(&coord[psi+1][0][0]);
  142.       v3f(&coord[psi+1][0][0]);
  143.       for(phi = 0; phi <= (PHI_TESS-1); phi++) {
  144.         n3f(&coord[psi+1][phi][0]);
  145.         v3f(&coord[psi+1][phi][0]);
  146.         n3f(&coord[psi  ][phi][0]);
  147.         v3f(&coord[psi  ][phi][0]);
  148.         }
  149.       endqstrip();
  150.       }
  151.      }
  152.    }
  153.  
  154.   /* */
  155.  
  156.   if( line_flag ) {
  157.    shademodel(FLAT);
  158.    polymode (PYM_LINE);
  159.    sbr_settexture(0);
  160.    RGBcolor(255,255,255);
  161.    scale( 1.005,1.005,1.005);
  162.  
  163.    linesmooth( SML_ON);
  164.    blendfunction (BF_SA, BF_MSA);
  165.  
  166.     for(psi = 0; psi < (PSI_TESS-1); psi++) {
  167.       bgnqstrip();
  168.       v3f(&coord[psi  ][0][0]);
  169.       v3f(&coord[psi+1][0][0]);
  170.       for(phi = 0; phi <= (PHI_TESS-1); phi++) {
  171.         v3f(&coord[psi+1][phi][0]);
  172.         v3f(&coord[psi  ][phi][0]);
  173.         }
  174.       endqstrip();
  175.       }
  176.  
  177.    shademodel(GOURAUD);
  178.    polymode (PYM_FILL);
  179.  
  180.    linesmooth( SML_OFF );
  181.    blendfunction( BF_ONE, BF_ZERO );
  182.  
  183.    }
  184.  
  185. } /* END object_draw */
  186.