home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / Mesa-aux / book.aux / material.c < prev    next >
C/C++ Source or Header  |  1999-06-25  |  11KB  |  322 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.  * material.c
  39.  * This program demonstrates the use of the GL lighting model.
  40.  * Several objects are drawn using different material characteristics.
  41.  * A single light source illuminates the objects.
  42.  */
  43. #include <GL/gl.h>
  44. #include <GL/glu.h>
  45. #include <stdlib.h>
  46. #include <GL/glaux.h>
  47.  
  48. /*
  49.  * Initialize z-buffer, projection matrix, light source, 
  50.  * *  and lighting model.  Do not specify a material property here.
  51.  */
  52. void myinit(void)
  53. {
  54.   GLfloat ambient[] =
  55.   {0.0, 0.0, 0.0, 1.0};
  56.   GLfloat diffuse[] =
  57.   {1.0, 1.0, 1.0, 1.0};
  58.   GLfloat specular[] =
  59.   {1.0, 1.0, 1.0, 1.0};
  60.   GLfloat position[] =
  61.   {0.0, 3.0, 2.0, 0.0};
  62.   GLfloat lmodel_ambient[] =
  63.   {0.4, 0.4, 0.4, 1.0};
  64.   GLfloat local_view[] =
  65.   {0.0};
  66.  
  67.   glEnable(GL_DEPTH_TEST);
  68.   glDepthFunc(GL_LESS);
  69.  
  70.   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  71.   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  72.   glLightfv(GL_LIGHT0, GL_POSITION, position);
  73.   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  74.   glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  75.  
  76.   glEnable(GL_LIGHTING);
  77.   glEnable(GL_LIGHT0);
  78.  
  79.   glClearColor(0.0, 0.1, 0.1, 0.0);
  80. }
  81.  
  82. /*
  83.  * Draw twelve spheres in 3 rows with 4 columns.  
  84.  * *  The spheres in the first row have materials with no ambient reflection.
  85.  * *  The second row has materials with significant ambient reflection.
  86.  * *  The third row has materials with colored ambient reflection.
  87.  * *
  88.  * *  The first column has materials with blue, diffuse reflection only.
  89.  * *  The second column has blue diffuse reflection, as well as specular
  90.  * *  reflection with a low shininess exponent.
  91.  * *  The third column has blue diffuse reflection, as well as specular
  92.  * *  reflection with a high shininess exponent (a more concentrated highlight).
  93.  * *  The fourth column has materials which also include an emissive component.
  94.  * *
  95.  * *  glTranslatef() is used to move spheres to their appropriate locations.
  96.  */
  97.  
  98. void display(void)
  99. {
  100.   GLfloat no_mat[] =
  101.   {0.0, 0.0, 0.0, 1.0};
  102.   GLfloat mat_ambient[] =
  103.   {0.7, 0.7, 0.7, 1.0};
  104.   GLfloat mat_ambient_color[] =
  105.   {0.8, 0.8, 0.2, 1.0};
  106.   GLfloat mat_diffuse[] =
  107.   {0.1, 0.5, 0.8, 1.0};
  108.   GLfloat mat_specular[] =
  109.   {1.0, 1.0, 1.0, 1.0};
  110.   GLfloat no_shininess[] =
  111.   {0.0};
  112.   GLfloat low_shininess[] =
  113.   {5.0};
  114.   GLfloat high_shininess[] =
  115.   {100.0};
  116.   GLfloat mat_emission[] =
  117.   {0.3, 0.2, 0.2, 0.0};
  118.  
  119.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  120.  
  121. /*
  122.  * draw sphere in first row, first column
  123.  * *  diffuse reflection only; no ambient or specular  
  124.  */
  125.   glPushMatrix();
  126.   glTranslatef(-3.75, 3.0, 0.0);
  127.   glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  128.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  129.   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  130.   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  131.   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  132.   auxSolidSphere(1.0);
  133.   glPopMatrix();
  134.  
  135. /*
  136.  * draw sphere in first row, second column
  137.  * *  diffuse and specular reflection; low shininess; no ambient
  138.  */
  139.   glPushMatrix();
  140.   glTranslatef(-1.25, 3.0, 0.0);
  141.   glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  142.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  143.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  144.   glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  145.   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  146.   auxSolidSphere(1.0);
  147.   glPopMatrix();
  148.  
  149. /*
  150.  * draw sphere in first row, third column
  151.  * *  diffuse and specular reflection; high shininess; no ambient
  152.  */
  153.   glPushMatrix();
  154.   glTranslatef(1.25, 3.0, 0.0);
  155.   glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  156.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  157.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  158.   glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  159.   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  160.   auxSolidSphere(1.0);
  161.   glPopMatrix();
  162.  
  163. /*
  164.  * draw sphere in first row, fourth column
  165.  * *  diffuse reflection; emission; no ambient or specular reflection
  166.  */
  167.   glPushMatrix();
  168.   glTranslatef(3.75, 3.0, 0.0);
  169.   glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  170.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  171.   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  172.   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  173.   glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  174.   auxSolidSphere(1.0);
  175.   glPopMatrix();
  176.  
  177. /*
  178.  * draw sphere in second row, first column
  179.  * *  ambient and diffuse reflection; no specular  
  180.  */
  181.   glPushMatrix();
  182.   glTranslatef(-3.75, 0.0, 0.0);
  183.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  184.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  185.   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  186.   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  187.   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  188.   auxSolidSphere(1.0);
  189.   glPopMatrix();
  190.  
  191. /*
  192.  * draw sphere in second row, second column
  193.  * *  ambient, diffuse and specular reflection; low shininess
  194.  */
  195.   glPushMatrix();
  196.   glTranslatef(-1.25, 0.0, 0.0);
  197.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  198.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  199.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  200.   glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  201.   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  202.   auxSolidSphere(1.0);
  203.   glPopMatrix();
  204.  
  205. /*
  206.  * draw sphere in second row, third column
  207.  * *  ambient, diffuse and specular reflection; high shininess
  208.  */
  209.   glPushMatrix();
  210.   glTranslatef(1.25, 0.0, 0.0);
  211.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  212.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  213.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  214.   glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  215.   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  216.   auxSolidSphere(1.0);
  217.   glPopMatrix();
  218.  
  219. /*
  220.  * draw sphere in second row, fourth column
  221.  * *  ambient and diffuse reflection; emission; no specular
  222.  */
  223.   glPushMatrix();
  224.   glTranslatef(3.75, 0.0, 0.0);
  225.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  226.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  227.   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  228.   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  229.   glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  230.   auxSolidSphere(1.0);
  231.   glPopMatrix();
  232.  
  233. /*
  234.  * draw sphere in third row, first column
  235.  * *  colored ambient and diffuse reflection; no specular  
  236.  */
  237.   glPushMatrix();
  238.   glTranslatef(-3.75, -3.0, 0.0);
  239.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  240.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  241.   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  242.   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  243.   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  244.   auxSolidSphere(1.0);
  245.   glPopMatrix();
  246.  
  247. /*
  248.  * draw sphere in third row, second column
  249.  * *  colored ambient, diffuse and specular reflection; low shininess
  250.  */
  251.   glPushMatrix();
  252.   glTranslatef(-1.25, -3.0, 0.0);
  253.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  254.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  255.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  256.   glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  257.   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  258.   auxSolidSphere(1.0);
  259.   glPopMatrix();
  260.  
  261. /*
  262.  * draw sphere in third row, third column
  263.  * *  colored ambient, diffuse and specular reflection; high shininess
  264.  */
  265.   glPushMatrix();
  266.   glTranslatef(1.25, -3.0, 0.0);
  267.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  268.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  269.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  270.   glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  271.   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  272.   auxSolidSphere(1.0);
  273.   glPopMatrix();
  274.  
  275. /*
  276.  * draw sphere in third row, fourth column
  277.  * *  colored ambient and diffuse reflection; emission; no specular
  278.  */
  279.   glPushMatrix();
  280.   glTranslatef(3.75, -3.0, 0.0);
  281.   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  282.   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  283.   glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  284.   glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  285.   glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  286.   auxSolidSphere(1.0);
  287.   glPopMatrix();
  288.  
  289.   glFlush();
  290. }
  291.  
  292. void myReshape(int w, int h)
  293. {
  294.   glViewport(0, 0, w, h);
  295.   glMatrixMode(GL_PROJECTION);
  296.   glLoadIdentity();
  297.   if (w <= (h * 2))
  298.     glOrtho(-6.0, 6.0, -3.0 * ((GLfloat) h * 2) / (GLfloat) w,
  299.         3.0 * ((GLfloat) h * 2) / (GLfloat) w, -10.0, 10.0);
  300.   else
  301.     glOrtho(-6.0 * (GLfloat) w / ((GLfloat) h * 2),
  302.         6.0 * (GLfloat) w / ((GLfloat) h * 2), -3.0, 3.0, -10.0, 10.0);
  303.   glMatrixMode(GL_MODELVIEW);
  304. }
  305.  
  306. /*
  307.  * Main Loop
  308.  * *  Open window with initial window size, title bar, 
  309.  * *  RGBA display mode, and handle input events.
  310.  */
  311. int main(int argc, char **argv)
  312. {
  313.   auxInitDisplayMode(AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  314.   auxInitPosition(0, 0, 600, 450);
  315.   if (!auxInitWindow(argv[0]))
  316.     auxQuit();
  317.   myinit();
  318.   auxReshapeFunc(myReshape);
  319.   auxMainLoop(display);
  320.   return 0;
  321. }
  322.