132001f49Smrg
232001f49Smrg/* Copyright (c) Mark J. Kilgard, 1994. */
332001f49Smrg
432001f49Smrg/**
532001f49Smrg * (c) Copyright 1993, Silicon Graphics, Inc.
632001f49Smrg * ALL RIGHTS RESERVED
732001f49Smrg * Permission to use, copy, modify, and distribute this software for
832001f49Smrg * any purpose and without fee is hereby granted, provided that the above
932001f49Smrg * copyright notice appear in all copies and that both the copyright notice
1032001f49Smrg * and this permission notice appear in supporting documentation, and that
1132001f49Smrg * the name of Silicon Graphics, Inc. not be used in advertising
1232001f49Smrg * or publicity pertaining to distribution of the software without specific,
1332001f49Smrg * written prior permission.
1432001f49Smrg *
1532001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
1632001f49Smrg * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
1732001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
1832001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
1932001f49Smrg * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
2032001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
2132001f49Smrg * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
2232001f49Smrg * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
2332001f49Smrg * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
2432001f49Smrg * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
2532001f49Smrg * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
2632001f49Smrg * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
2732001f49Smrg *
2832001f49Smrg * US Government Users Restricted Rights
2932001f49Smrg * Use, duplication, or disclosure by the Government is subject to
3032001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
3132001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software
3232001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor
3332001f49Smrg * clauses in the FAR or the DOD or NASA FAR Supplement.
3432001f49Smrg * Unpublished-- rights reserved under the copyright laws of the
3532001f49Smrg * United States.  Contractor/manufacturer is Silicon Graphics,
3632001f49Smrg * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
3732001f49Smrg *
3832001f49Smrg * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
3932001f49Smrg */
4032001f49Smrg/**
4132001f49Smrg *  teapots.c
4232001f49Smrg *  This program demonstrates lots of material properties.
4332001f49Smrg *  A single light source illuminates the objects.
4432001f49Smrg */
4532001f49Smrg#include <stdlib.h>
4632001f49Smrg#include "glut_wrap.h"
4732001f49Smrg
4832001f49Smrg/*
4932001f49Smrg * Initialize depth buffer, projection matrix, light source, and lighting
5032001f49Smrg * model.  Do not specify a material property here.
5132001f49Smrg */
5232001f49Smrgstatic void
5332001f49Smrgmyinit(void)
5432001f49Smrg{
5532001f49Smrg  GLfloat ambient[] =
5632001f49Smrg  {0.0, 0.0, 0.0, 1.0};
5732001f49Smrg  GLfloat diffuse[] =
5832001f49Smrg  {1.0, 1.0, 1.0, 1.0};
5932001f49Smrg  GLfloat position[] =
6032001f49Smrg  {0.0, 3.0, 3.0, 0.0};
6132001f49Smrg
6232001f49Smrg  GLfloat lmodel_ambient[] =
6332001f49Smrg  {0.2, 0.2, 0.2, 1.0};
6432001f49Smrg  GLfloat local_view[] =
6532001f49Smrg  {0.0};
6632001f49Smrg
6732001f49Smrg  glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
6832001f49Smrg  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
6932001f49Smrg  glLightfv(GL_LIGHT0, GL_POSITION, position);
7032001f49Smrg  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
7132001f49Smrg  glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
7232001f49Smrg
7332001f49Smrg  glFrontFace(GL_CW);
7432001f49Smrg  glEnable(GL_LIGHTING);
7532001f49Smrg  glEnable(GL_LIGHT0);
7632001f49Smrg  glEnable(GL_AUTO_NORMAL);
7732001f49Smrg  glEnable(GL_NORMALIZE);
7832001f49Smrg  glEnable(GL_DEPTH_TEST);
7932001f49Smrg  glDepthFunc(GL_LESS);
8032001f49Smrg}
8132001f49Smrg
8232001f49Smrg/*
8332001f49Smrg * Move object into position.  Use 3rd through 12th parameters to specify the
8432001f49Smrg * material property.  Draw a teapot.
8532001f49Smrg */
8632001f49Smrgstatic void
8732001f49SmrgrenderTeapot(GLfloat x, GLfloat y,
8832001f49Smrg  GLfloat ambr, GLfloat ambg, GLfloat ambb,
8932001f49Smrg  GLfloat difr, GLfloat difg, GLfloat difb,
9032001f49Smrg  GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine)
9132001f49Smrg{
9232001f49Smrg  float mat[4];
9332001f49Smrg
9432001f49Smrg  glPushMatrix();
9532001f49Smrg  glTranslatef(x, y, 0.0);
9632001f49Smrg  mat[0] = ambr;
9732001f49Smrg  mat[1] = ambg;
9832001f49Smrg  mat[2] = ambb;
9932001f49Smrg  mat[3] = 1.0;
10032001f49Smrg  glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
10132001f49Smrg  mat[0] = difr;
10232001f49Smrg  mat[1] = difg;
10332001f49Smrg  mat[2] = difb;
10432001f49Smrg  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
10532001f49Smrg  mat[0] = specr;
10632001f49Smrg  mat[1] = specg;
10732001f49Smrg  mat[2] = specb;
10832001f49Smrg  glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
10932001f49Smrg  glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0);
11032001f49Smrg  glutSolidTeapot(1.0);
11132001f49Smrg  glPopMatrix();
11232001f49Smrg}
11332001f49Smrg
11432001f49Smrg/**
11532001f49Smrg *  First column:  emerald, jade, obsidian, pearl, ruby, turquoise
11632001f49Smrg *  2nd column:  brass, bronze, chrome, copper, gold, silver
11732001f49Smrg *  3rd column:  black, cyan, green, red, white, yellow plastic
11832001f49Smrg *  4th column:  black, cyan, green, red, white, yellow rubber
11932001f49Smrg */
12032001f49Smrgstatic void
12132001f49Smrgdisplay(void)
12232001f49Smrg{
12332001f49Smrg  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
12432001f49Smrg  renderTeapot(2.0, 17.0, 0.0215, 0.1745, 0.0215,
12532001f49Smrg    0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
12632001f49Smrg  renderTeapot(2.0, 14.0, 0.135, 0.2225, 0.1575,
12732001f49Smrg    0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 0.1);
12832001f49Smrg  renderTeapot(2.0, 11.0, 0.05375, 0.05, 0.06625,
12932001f49Smrg    0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435, 0.3);
13032001f49Smrg  renderTeapot(2.0, 8.0, 0.25, 0.20725, 0.20725,
13132001f49Smrg    1, 0.829, 0.829, 0.296648, 0.296648, 0.296648, 0.088);
13232001f49Smrg  renderTeapot(2.0, 5.0, 0.1745, 0.01175, 0.01175,
13332001f49Smrg    0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 0.6);
13432001f49Smrg  renderTeapot(2.0, 2.0, 0.1, 0.18725, 0.1745,
13532001f49Smrg    0.396, 0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 0.1);
13632001f49Smrg  renderTeapot(6.0, 17.0, 0.329412, 0.223529, 0.027451,
13732001f49Smrg    0.780392, 0.568627, 0.113725, 0.992157, 0.941176, 0.807843,
13832001f49Smrg    0.21794872);
13932001f49Smrg  renderTeapot(6.0, 14.0, 0.2125, 0.1275, 0.054,
14032001f49Smrg    0.714, 0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 0.2);
14132001f49Smrg  renderTeapot(6.0, 11.0, 0.25, 0.25, 0.25,
14232001f49Smrg    0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6);
14332001f49Smrg  renderTeapot(6.0, 8.0, 0.19125, 0.0735, 0.0225,
14432001f49Smrg    0.7038, 0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 0.1);
14532001f49Smrg  renderTeapot(6.0, 5.0, 0.24725, 0.1995, 0.0745,
14632001f49Smrg    0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
14732001f49Smrg  renderTeapot(6.0, 2.0, 0.19225, 0.19225, 0.19225,
14832001f49Smrg    0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 0.4);
14932001f49Smrg  renderTeapot(10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.01,
15032001f49Smrg    0.50, 0.50, 0.50, .25);
15132001f49Smrg  renderTeapot(10.0, 14.0, 0.0, 0.1, 0.06, 0.0, 0.50980392, 0.50980392,
15232001f49Smrg    0.50196078, 0.50196078, 0.50196078, .25);
15332001f49Smrg  renderTeapot(10.0, 11.0, 0.0, 0.0, 0.0,
15432001f49Smrg    0.1, 0.35, 0.1, 0.45, 0.55, 0.45, .25);
15532001f49Smrg  renderTeapot(10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
15632001f49Smrg    0.7, 0.6, 0.6, .25);
15732001f49Smrg  renderTeapot(10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55,
15832001f49Smrg    0.70, 0.70, 0.70, .25);
15932001f49Smrg  renderTeapot(10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0,
16032001f49Smrg    0.60, 0.60, 0.50, .25);
16132001f49Smrg  renderTeapot(14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01,
16232001f49Smrg    0.4, 0.4, 0.4, .078125);
16332001f49Smrg  renderTeapot(14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5,
16432001f49Smrg    0.04, 0.7, 0.7, .078125);
16532001f49Smrg  renderTeapot(14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4,
16632001f49Smrg    0.04, 0.7, 0.04, .078125);
16732001f49Smrg  renderTeapot(14.0, 8.0, 0.05, 0.0, 0.0, 0.5, 0.4, 0.4,
16832001f49Smrg    0.7, 0.04, 0.04, .078125);
16932001f49Smrg  renderTeapot(14.0, 5.0, 0.05, 0.05, 0.05, 0.5, 0.5, 0.5,
17032001f49Smrg    0.7, 0.7, 0.7, .078125);
17132001f49Smrg  renderTeapot(14.0, 2.0, 0.05, 0.05, 0.0, 0.5, 0.5, 0.4,
17232001f49Smrg    0.7, 0.7, 0.04, .078125);
17332001f49Smrg  glFlush();
17432001f49Smrg}
17532001f49Smrg
17632001f49Smrgstatic void
17732001f49SmrgmyReshape(int w, int h)
17832001f49Smrg{
17932001f49Smrg  glViewport(0, 0, w, h);
18032001f49Smrg  glMatrixMode(GL_PROJECTION);
18132001f49Smrg  glLoadIdentity();
18232001f49Smrg  if (w <= h)
18332001f49Smrg    glOrtho(0.0, 16.0, 0.0, 18.0 * (GLfloat) h / (GLfloat) w,
18432001f49Smrg      -10.0, 10.0);
18532001f49Smrg  else
18632001f49Smrg    glOrtho(0.0, 16.0 * (GLfloat) w / (GLfloat) h, 0.0, 18.0,
18732001f49Smrg      -10.0, 10.0);
18832001f49Smrg  glMatrixMode(GL_MODELVIEW);
18932001f49Smrg}
19032001f49Smrg
19132001f49Smrgstatic void
19232001f49Smrgkey(unsigned char k, int x, int y)
19332001f49Smrg{
19432001f49Smrg  switch (k) {
19532001f49Smrg  case 27:  /* Escape */
19632001f49Smrg    exit(0);
19732001f49Smrg    break;
19832001f49Smrg  default:
19932001f49Smrg    return;
20032001f49Smrg  }
20132001f49Smrg  glutPostRedisplay();
20232001f49Smrg}
20332001f49Smrg
20432001f49Smrg/*
20532001f49Smrg * Main Loop Open window with initial window size, title bar, RGBA display
20632001f49Smrg * mode, and handle input events.
20732001f49Smrg */
20832001f49Smrgint
20932001f49Smrgmain(int argc, char **argv)
21032001f49Smrg{
21132001f49Smrg  glutInit(&argc, argv);
21232001f49Smrg  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
21332001f49Smrg  glutCreateWindow(argv[0]);
21432001f49Smrg  myinit();
21532001f49Smrg  glutReshapeFunc(myReshape);
21632001f49Smrg  glutDisplayFunc(display);
21732001f49Smrg  glutKeyboardFunc(key);
21832001f49Smrg  glutMainLoop();
21932001f49Smrg  return 0;             /* ANSI C requires main to return int. */
22032001f49Smrg}
221