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