home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / Mesa-aux / book.aux / teapots.c < prev    next >
C/C++ Source or Header  |  1999-06-25  |  7KB  |  202 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  *  teapots.c
  39.  *  This program demonstrates lots of material properties.
  40.  *  A single light source illuminates the objects.
  41.  */
  42. #include <GL/gl.h>
  43. #include <GL/glu.h>
  44. #include <stdlib.h>
  45. #include <GL/glaux.h>
  46.  
  47. /*
  48.  * Initialize depth buffer, projection matrix, light source, 
  49.  * *  and lighting model.  Do not specify a material property here.
  50.  */
  51. void myinit(void)
  52. {
  53.   GLfloat ambient[] =
  54.   {0.0, 0.0, 0.0, 1.0};
  55.   GLfloat diffuse[] =
  56.   {1.0, 1.0, 1.0, 1.0};
  57.   GLfloat position[] =
  58.   {0.0, 3.0, 3.0, 0.0};
  59.  
  60.   GLfloat lmodel_ambient[] =
  61.   {0.2, 0.2, 0.2, 1.0};
  62.   GLfloat local_view[] =
  63.   {0.0};
  64.  
  65.   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  66.   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  67.   glLightfv(GL_LIGHT0, GL_POSITION, position);
  68.   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  69.   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  70.  
  71.   glFrontFace(GL_CW);
  72.   glEnable(GL_LIGHTING);
  73.   glEnable(GL_LIGHT0);
  74.   glEnable(GL_AUTO_NORMAL);
  75.   glEnable(GL_NORMALIZE);
  76.   glEnable(GL_DEPTH_TEST);
  77.   glDepthFunc(GL_LESS);
  78. }
  79.  
  80. /*
  81.  * Move object into position.  Use 3rd through 12th parameters
  82.  * *  to specify the material property.  Draw a teapot.
  83.  */
  84. void renderTeapot(GLfloat x, GLfloat y,
  85.           GLfloat ambr, GLfloat ambg, GLfloat ambb,
  86.           GLfloat difr, GLfloat difg, GLfloat difb,
  87.           GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine)
  88. {
  89.   float mat[4];
  90.  
  91.   glPushMatrix();
  92.   glTranslatef(x, y, 0.0);
  93.   mat[0] = ambr;
  94.   mat[1] = ambg;
  95.   mat[2] = ambb;
  96.   mat[3] = 1.0;
  97.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
  98.   mat[0] = difr;
  99.   mat[1] = difg;
  100.   mat[2] = difb;
  101.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
  102.   mat[0] = specr;
  103.   mat[1] = specg;
  104.   mat[2] = specb;
  105.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
  106.   glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0);
  107.   auxSolidTeapot(1.0);
  108.   glPopMatrix();
  109. }
  110.  
  111. /*
  112.  * First column:  emerald, jade, obsidian, pearl, ruby, turquoise
  113.  * *  2nd column:  brass, bronze, chrome, copper, gold, silver
  114.  * *  3rd column:  black, cyan, green, red, white, yellow plastic
  115.  * *  4th column:  black, cyan, green, red, white, yellow rubber
  116.  */
  117. void display(void)
  118. {
  119.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  120.   renderTeapot(2.0, 17.0, 0.0215, 0.1745, 0.0215,
  121.            0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
  122.   renderTeapot(2.0, 14.0, 0.135, 0.2225, 0.1575,
  123.            0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 0.1);
  124.   renderTeapot(2.0, 11.0, 0.05375, 0.05, 0.06625,
  125.            0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435, 0.3);
  126.   renderTeapot(2.0, 8.0, 0.25, 0.20725, 0.20725,
  127.            1, 0.829, 0.829, 0.296648, 0.296648, 0.296648, 0.088);
  128.   renderTeapot(2.0, 5.0, 0.1745, 0.01175, 0.01175,
  129.            0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 0.6);
  130.   renderTeapot(2.0, 2.0, 0.1, 0.18725, 0.1745,
  131.            0.396, 0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 0.1);
  132.   renderTeapot(6.0, 17.0, 0.329412, 0.223529, 0.027451,
  133.            0.780392, 0.568627, 0.113725, 0.992157, 0.941176, 0.807843,
  134.            0.21794872);
  135.   renderTeapot(6.0, 14.0, 0.2125, 0.1275, 0.054,
  136.            0.714, 0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 0.2);
  137.   renderTeapot(6.0, 11.0, 0.25, 0.25, 0.25,
  138.            0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6);
  139.   renderTeapot(6.0, 8.0, 0.19125, 0.0735, 0.0225,
  140.            0.7038, 0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 0.1);
  141.   renderTeapot(6.0, 5.0, 0.24725, 0.1995, 0.0745,
  142.            0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
  143.   renderTeapot(6.0, 2.0, 0.19225, 0.19225, 0.19225,
  144.            0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 0.4);
  145.   renderTeapot(10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.01,
  146.            0.50, 0.50, 0.50, .25);
  147.   renderTeapot(10.0, 14.0, 0.0, 0.1, 0.06, 0.0, 0.50980392, 0.50980392,
  148.            0.50196078, 0.50196078, 0.50196078, .25);
  149.   renderTeapot(10.0, 11.0, 0.0, 0.0, 0.0,
  150.            0.1, 0.35, 0.1, 0.45, 0.55, 0.45, .25);
  151.   renderTeapot(10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
  152.            0.7, 0.6, 0.6, .25);
  153.   renderTeapot(10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55,
  154.            0.70, 0.70, 0.70, .25);
  155.   renderTeapot(10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0,
  156.            0.60, 0.60, 0.50, .25);
  157.   renderTeapot(14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01,
  158.            0.4, 0.4, 0.4, .078125);
  159.   renderTeapot(14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5,
  160.            0.04, 0.7, 0.7, .078125);
  161.   renderTeapot(14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4,
  162.            0.04, 0.7, 0.04, .078125);
  163.   renderTeapot(14.0, 8.0, 0.05, 0.0, 0.0, 0.5, 0.4, 0.4,
  164.            0.7, 0.04, 0.04, .078125);
  165.   renderTeapot(14.0, 5.0, 0.05, 0.05, 0.05, 0.5, 0.5, 0.5,
  166.            0.7, 0.7, 0.7, .078125);
  167.   renderTeapot(14.0, 2.0, 0.05, 0.05, 0.0, 0.5, 0.5, 0.4,
  168.            0.7, 0.7, 0.04, .078125);
  169.   glFlush();
  170. }
  171.  
  172. void myReshape(int w, int h)
  173. {
  174.   glViewport(0, 0, w, h);
  175.   glMatrixMode(GL_PROJECTION);
  176.   glLoadIdentity();
  177.   if (w <= h)
  178.     glOrtho(0.0, 16.0, 0.0, 16.0 * (GLfloat) h / (GLfloat) w,
  179.         -10.0, 10.0);
  180.   else
  181.     glOrtho(0.0, 16.0 * (GLfloat) w / (GLfloat) h, 0.0, 16.0,
  182.         -10.0, 10.0);
  183.   glMatrixMode(GL_MODELVIEW);
  184. }
  185.  
  186. /*
  187.  * Main Loop
  188.  * *  Open window with initial window size, title bar, 
  189.  * *  RGBA display mode, and handle input events.
  190.  */
  191. int main(int argc, char **argv)
  192. {
  193.   auxInitDisplayMode(AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  194.   auxInitPosition(0, 0, 500, 600);
  195.   if (!auxInitWindow(argv[0]))
  196.     auxQuit();
  197.   myinit();
  198.   auxReshapeFunc(myReshape);
  199.   auxMainLoop(display);
  200.   return 0;
  201. }
  202.