home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / redbook / material.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  10.8 KB  |  295 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /*
  5.  * (c) Copyright 1993, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. /*
  41.  * material.c
  42.  * This program demonstrates the use of the GL lighting model.
  43.  * Several objects are drawn using different material characteristics.
  44.  * A single light source illuminates the objects.
  45.  */
  46. #include <stdlib.h>
  47. #include <GL/glut.h>
  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[] = { 0.0, 0.0, 0.0, 1.0 };
  55.     GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  56.     GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
  57.     GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
  58.     GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
  59.     GLfloat local_view[] = { 0.0 };
  60.  
  61.     glEnable(GL_DEPTH_TEST);
  62.     glDepthFunc(GL_LESS);
  63.  
  64.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  65.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  66.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  67.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  68.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  69.  
  70.     glEnable(GL_LIGHTING);
  71.     glEnable(GL_LIGHT0);
  72.  
  73.     glClearColor(0.0, 0.1, 0.1, 0.0);
  74. }
  75.  
  76. /*  Draw twelve spheres in 3 rows with 4 columns.  
  77.  *  The spheres in the first row have materials with no ambient reflection.
  78.  *  The second row has materials with significant ambient reflection.
  79.  *  The third row has materials with colored ambient reflection.
  80.  *
  81.  *  The first column has materials with blue, diffuse reflection only.
  82.  *  The second column has blue diffuse reflection, as well as specular
  83.  *  reflection with a low shininess exponent.
  84.  *  The third column has blue diffuse reflection, as well as specular
  85.  *  reflection with a high shininess exponent (a more concentrated highlight).
  86.  *  The fourth column has materials which also include an emissive component.
  87.  *
  88.  *  glTranslatef() is used to move spheres to their appropriate locations.
  89.  */
  90.  
  91. void display(void)
  92. {
  93.     GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
  94.     GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
  95.     GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
  96.     GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
  97.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  98.     GLfloat no_shininess[] = { 0.0 };
  99.     GLfloat low_shininess[] = { 5.0 };
  100.     GLfloat high_shininess[] = { 100.0 };
  101.     GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
  102.  
  103.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  104.  
  105. /*  draw sphere in first row, first column
  106.  *  diffuse reflection only; no ambient or specular  
  107.  */
  108.     glPushMatrix();
  109.     glTranslatef (-3.75, 3.0, 0.0);
  110.     glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  111.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  112.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  113.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  114.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  115.     glutSolidSphere(1.0, 16, 16);
  116.     glPopMatrix();
  117.  
  118. /*  draw sphere in first row, second column
  119.  *  diffuse and specular reflection; low shininess; no ambient
  120.  */
  121.     glPushMatrix();
  122.     glTranslatef (-1.25, 3.0, 0.0);
  123.     glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  124.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  125.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  126.     glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  127.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  128.     glutSolidSphere(1.0, 16, 16);
  129.     glPopMatrix();
  130.  
  131. /*  draw sphere in first row, third column
  132.  *  diffuse and specular reflection; high shininess; no ambient
  133.  */
  134.     glPushMatrix();
  135.     glTranslatef (1.25, 3.0, 0.0);
  136.     glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  137.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  138.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  139.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  140.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  141.     glutSolidSphere(1.0, 16, 16);
  142.     glPopMatrix();
  143.  
  144. /*  draw sphere in first row, fourth column
  145.  *  diffuse reflection; emission; no ambient or specular reflection
  146.  */
  147.     glPushMatrix();
  148.     glTranslatef (3.75, 3.0, 0.0);
  149.     glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
  150.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  151.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  152.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  153.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  154.     glutSolidSphere(1.0, 16, 16);
  155.     glPopMatrix();
  156.  
  157. /*  draw sphere in second row, first column
  158.  *  ambient and diffuse reflection; no specular  
  159.  */
  160.     glPushMatrix();
  161.     glTranslatef (-3.75, 0.0, 0.0);
  162.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  163.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  164.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  165.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  166.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  167.     glutSolidSphere(1.0, 16, 16);
  168.     glPopMatrix();
  169.  
  170. /*  draw sphere in second row, second column
  171.  *  ambient, diffuse and specular reflection; low shininess
  172.  */
  173.     glPushMatrix();
  174.     glTranslatef (-1.25, 0.0, 0.0);
  175.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  176.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  177.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  178.     glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  179.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  180.     glutSolidSphere(1.0, 16, 16);
  181.     glPopMatrix();
  182.  
  183. /*  draw sphere in second row, third column
  184.  *  ambient, diffuse and specular reflection; high shininess
  185.  */
  186.     glPushMatrix();
  187.     glTranslatef (1.25, 0.0, 0.0);
  188.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  189.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  190.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  191.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  192.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  193.     glutSolidSphere(1.0, 16, 16);
  194.     glPopMatrix();
  195.  
  196. /*  draw sphere in second row, fourth column
  197.  *  ambient and diffuse reflection; emission; no specular
  198.  */
  199.     glPushMatrix();
  200.     glTranslatef (3.75, 0.0, 0.0);
  201.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  202.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  203.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  204.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  205.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  206.     glutSolidSphere(1.0, 16, 16);
  207.     glPopMatrix();
  208.  
  209. /*  draw sphere in third row, first column
  210.  *  colored ambient and diffuse reflection; no specular  
  211.  */
  212.     glPushMatrix();
  213.     glTranslatef (-3.75, -3.0, 0.0);
  214.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  215.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  216.     glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
  217.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  218.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  219.     glutSolidSphere(1.0, 16, 16);
  220.     glPopMatrix();
  221.  
  222. /*  draw sphere in third row, second column
  223.  *  colored ambient, diffuse and specular reflection; low shininess
  224.  */
  225.     glPushMatrix();
  226.     glTranslatef (-1.25, -3.0, 0.0);
  227.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  228.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  229.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  230.     glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
  231.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  232.     glutSolidSphere(1.0, 16, 16);
  233.     glPopMatrix();
  234.  
  235. /*  draw sphere in third row, third column
  236.  *  colored ambient, diffuse and specular reflection; high shininess
  237.  */
  238.     glPushMatrix();
  239.     glTranslatef (1.25, -3.0, 0.0);
  240.     glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
  241.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  242.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  243.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  244.     glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  245.     glutSolidSphere(1.0, 16, 16);
  246.     glPopMatrix();
  247.  
  248. /*  draw sphere in third row, fourth column
  249.  *  colored ambient and diffuse reflection; emission; no specular
  250.  */
  251.     glPushMatrix();
  252.     glTranslatef (3.75, -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, no_mat);
  256.     glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
  257.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  258.     glutSolidSphere(1.0, 16, 16);
  259.     glPopMatrix();
  260.  
  261.     glFlush();
  262. }
  263.  
  264. void myReshape(int w, int h)
  265. {
  266.     glViewport(0, 0, w, h);
  267.     glMatrixMode(GL_PROJECTION);
  268.     glLoadIdentity();
  269.     if (w <= (h * 2))
  270.     glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w, 
  271.         3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
  272.     else
  273.     glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2), 
  274.         6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
  275.     glMatrixMode(GL_MODELVIEW);
  276. }
  277.  
  278. /*  Main Loop
  279.  *  Open window with initial window size, title bar, 
  280.  *  RGBA display mode, and handle input events.
  281.  */
  282. int main(int argc, char** argv)
  283. {
  284.     glutInit(&argc, argv);
  285.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  286.     glutInitWindowSize (600, 450);
  287.     glutCreateWindow(argv[0]);
  288.     myinit();
  289.     glutReshapeFunc(myReshape);
  290.     glutDisplayFunc(display);
  291.     glutMainLoop();
  292.     return 0;             /* ANSI C requires main to return int. */
  293. }
  294.  
  295.