Main Page | File List

material.c

00001 /*
00002  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
00003  * ALL RIGHTS RESERVED 
00004  * Permission to use, copy, modify, and distribute this software for 
00005  * any purpose and without fee is hereby granted, provided that the above
00006  * copyright notice appear in all copies and that both the copyright notice
00007  * and this permission notice appear in supporting documentation, and that 
00008  * the name of Silicon Graphics, Inc. not be used in advertising
00009  * or publicity pertaining to distribution of the software without specific,
00010  * written prior permission. 
00011  *
00012  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
00013  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
00014  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
00015  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
00016  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
00017  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
00018  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
00019  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
00020  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
00021  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
00022  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
00023  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
00024  * 
00025  * US Government Users Restricted Rights 
00026  * Use, duplication, or disclosure by the Government is subject to
00027  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
00028  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
00029  * clause at DFARS 252.227-7013 and/or in similar or successor
00030  * clauses in the FAR or the DOD or NASA FAR Supplement.
00031  * Unpublished-- rights reserved under the copyright laws of the
00032  * United States.  Contractor/manufacturer is Silicon Graphics,
00033  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
00034  *
00035  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
00036  */
00037 
00038 /*
00039  * material.c
00040  * This program demonstrates the use of the GL lighting model.
00041  * Several objects are drawn using different material characteristics.
00042  * A single light source illuminates the objects.
00043  */
00044 #include <stdlib.h>
00045 #include <GL/glut.h>
00046 
00047 /*  Initialize z-buffer, projection matrix, light source, 
00048  *  and lighting model.  Do not specify a material property here.
00049  */
00050 void init(void)
00051 {
00052    GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
00053    GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
00054    GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
00055    GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
00056    GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
00057    GLfloat local_view[] = { 0.0 };
00058 
00059    glClearColor(0.0, 0.1, 0.1, 0.0);
00060    glEnable(GL_DEPTH_TEST);
00061    glShadeModel(GL_SMOOTH);
00062 
00063    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
00064    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
00065    glLightfv(GL_LIGHT0, GL_POSITION, position);
00066    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
00067    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
00068 
00069    glEnable(GL_LIGHTING);
00070    glEnable(GL_LIGHT0);
00071 }
00072 
00073 /*  Draw twelve spheres in 3 rows with 4 columns.  
00074  *  The spheres in the first row have materials with no ambient reflection.
00075  *  The second row has materials with significant ambient reflection.
00076  *  The third row has materials with colored ambient reflection.
00077  *
00078  *  The first column has materials with blue, diffuse reflection only.
00079  *  The second column has blue diffuse reflection, as well as specular
00080  *  reflection with a low shininess exponent.
00081  *  The third column has blue diffuse reflection, as well as specular
00082  *  reflection with a high shininess exponent (a more concentrated highlight).
00083  *  The fourth column has materials which also include an emissive component.
00084  *
00085  *  glTranslatef() is used to move spheres to their appropriate locations.
00086  */
00087 
00088 void display(void)
00089 {
00090    GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
00091    GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
00092    GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
00093    GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
00094    GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
00095    GLfloat no_shininess[] = { 0.0 };
00096    GLfloat low_shininess[] = { 5.0 };
00097    GLfloat high_shininess[] = { 100.0 };
00098    GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
00099 
00100    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00101 
00102 /*  draw sphere in first row, first column
00103  *  diffuse reflection only; no ambient or specular  
00104  */
00105    glPushMatrix();
00106    glTranslatef (-3.75, 3.0, 0.0);
00107    glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
00108    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00109    glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
00110    glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
00111    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00112    glutSolidSphere(1.0, 16, 16);
00113    glPopMatrix();
00114 
00115 /*  draw sphere in first row, second column
00116  *  diffuse and specular reflection; low shininess; no ambient
00117  */
00118    glPushMatrix();
00119    glTranslatef (-1.25, 3.0, 0.0);
00120    glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
00121    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00122    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00123    glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
00124    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00125    glutSolidSphere(1.0, 16, 16);
00126    glPopMatrix();
00127 
00128 /*  draw sphere in first row, third column
00129  *  diffuse and specular reflection; high shininess; no ambient
00130  */
00131    glPushMatrix();
00132    glTranslatef (1.25, 3.0, 0.0);
00133    glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
00134    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00135    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00136    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
00137    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00138    glutSolidSphere(1.0, 16, 16);
00139    glPopMatrix();
00140 
00141 /*  draw sphere in first row, fourth column
00142  *  diffuse reflection; emission; no ambient or specular reflection
00143  */
00144    glPushMatrix();
00145    glTranslatef (3.75, 3.0, 0.0);
00146    glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
00147    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00148    glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
00149    glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
00150    glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
00151    glutSolidSphere(1.0, 16, 16);
00152    glPopMatrix();
00153 
00154 /*  draw sphere in second row, first column
00155  *  ambient and diffuse reflection; no specular  
00156  */
00157    glPushMatrix();
00158    glTranslatef (-3.75, 0.0, 0.0);
00159    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00160    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00161    glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
00162    glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
00163    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00164    glutSolidSphere(1.0, 16, 16);
00165    glPopMatrix();
00166 
00167 /*  draw sphere in second row, second column
00168  *  ambient, diffuse and specular reflection; low shininess
00169  */
00170    glPushMatrix();
00171    glTranslatef (-1.25, 0.0, 0.0);
00172    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00173    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00174    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00175    glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
00176    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00177    glutSolidSphere(1.0, 16, 16);
00178    glPopMatrix();
00179 
00180 /*  draw sphere in second row, third column
00181  *  ambient, diffuse and specular reflection; high shininess
00182  */
00183    glPushMatrix();
00184    glTranslatef (1.25, 0.0, 0.0);
00185    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00186    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00187    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00188    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
00189    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00190    glutSolidSphere(1.0, 16, 16);
00191    glPopMatrix();
00192 
00193 /*  draw sphere in second row, fourth column
00194  *  ambient and diffuse reflection; emission; no specular
00195  */
00196    glPushMatrix();
00197    glTranslatef (3.75, 0.0, 0.0);
00198    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00199    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00200    glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
00201    glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
00202    glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
00203    glutSolidSphere(1.0, 16, 16);
00204    glPopMatrix();
00205 
00206 /*  draw sphere in third row, first column
00207  *  colored ambient and diffuse reflection; no specular  
00208  */
00209    glPushMatrix();
00210    glTranslatef (-3.75, -3.0, 0.0);
00211    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
00212    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00213    glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
00214    glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
00215    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00216    glutSolidSphere(1.0, 16, 16);
00217    glPopMatrix();
00218 
00219 /*  draw sphere in third row, second column
00220  *  colored ambient, diffuse and specular reflection; low shininess
00221  */
00222    glPushMatrix();
00223    glTranslatef (-1.25, -3.0, 0.0);
00224    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
00225    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00226    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00227    glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
00228    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00229    glutSolidSphere(1.0, 16, 16);
00230    glPopMatrix();
00231 
00232 /*  draw sphere in third row, third column
00233  *  colored ambient, diffuse and specular reflection; high shininess
00234  */
00235    glPushMatrix();
00236    glTranslatef (1.25, -3.0, 0.0);
00237    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
00238    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00239    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00240    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
00241    glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00242    glutSolidSphere(1.0, 16, 16);
00243    glPopMatrix();
00244 
00245 /*  draw sphere in third row, fourth column
00246  *  colored ambient and diffuse reflection; emission; no specular
00247  */
00248    glPushMatrix();
00249    glTranslatef (3.75, -3.0, 0.0);
00250    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
00251    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00252    glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
00253    glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
00254    glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
00255    glutSolidSphere(1.0, 16, 16);
00256    glPopMatrix();
00257 
00258    glFlush();
00259 }
00260 
00261 void reshape(int w, int h)
00262 {
00263    glViewport(0, 0, w, h);
00264    glMatrixMode(GL_PROJECTION);
00265    glLoadIdentity();
00266    if (w <= (h * 2))
00267       glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w, 
00268          3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
00269    else
00270       glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2), 
00271          6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
00272    glMatrixMode(GL_MODELVIEW);
00273    glLoadIdentity();
00274 }
00275 
00276 void keyboard(unsigned char key, int x, int y)
00277 {
00278    switch (key) {
00279       case 27:
00280          exit(0);
00281          break;
00282    }
00283 }
00284 
00285 int main(int argc, char** argv)
00286 {
00287    glutInit(&argc, argv);
00288    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
00289    glutInitWindowSize (600, 450);
00290    glutCreateWindow(argv[0]);
00291    init();
00292    glutReshapeFunc(reshape);
00293    glutDisplayFunc(display);
00294    glutKeyboardFunc (keyboard);
00295    glutMainLoop();
00296    return 0; 
00297 }
00298 

Generated on Thu Aug 12 17:41:56 2004 by doxygen 1.3.6