home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / custEducation / opengl1 / lib / teapot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.6 KB  |  158 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, 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. #include <GL/gl.h>
  38. #include "aux.h"
  39. #include "teapot.h"
  40.  
  41. long GRD;
  42.  
  43. #define TEAPOTSOLID 0
  44. #define TEAPOTWIRE 1
  45.  
  46. static GLuint teapots[2] = {0, 0};
  47.  
  48. static float tex[2][2][2] = {{{0, 0},{1, 0}},{{0, 1},{1, 1}}};
  49.  
  50. static void solidTeapot(long grid, GLdouble scale)
  51. {
  52.     float p[4][4][3], q[4][4][3], r[4][4][3], s[4][4][3];
  53.     long i, j, k, l;
  54.  
  55.     if (grid < 2) grid = 7;
  56.     GRD = grid;
  57.     teapots[TEAPOTSOLID] = glGenLists (1);
  58.     glNewList(teapots[TEAPOTSOLID], GL_COMPILE);
  59.     glPushMatrix ();
  60.     glRotatef (270.0, 1.0, 0.0, 0.0);
  61.     glScalef (0.5*scale, 0.5*scale, 0.5*scale);
  62.     glTranslatef (0.0, 0.0, -1.5);
  63.     for (i = 0; i < 10; i++) {
  64.     for (j = 0; j < 4; j++)
  65.         for (k = 0; k < 4; k++) 
  66.         for (l = 0; l < 3; l++) {
  67.             p[j][k][l] = cpdata[patchdata[i][j*4+k]][l];
  68.             q[j][k][l] = cpdata[patchdata[i][j*4+(3-k)]][l];
  69.             if (l == 1) q[j][k][l] *= -1.0;
  70.             if (i < 6) {
  71.             r[j][k][l] = cpdata[patchdata[i][j*4+(3-k)]][l];
  72.             if (l == 0) r[j][k][l] *= -1.0;
  73.             s[j][k][l] = cpdata[patchdata[i][j*4+k]][l];
  74.             if (l == 0) s[j][k][l] *= -1.0;
  75.             if (l == 1) s[j][k][l] *= -1.0;
  76.             }
  77.         }
  78.     glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, &tex[0][0][0]);
  79.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &p[0][0][0]);
  80.     glEnable(GL_MAP2_VERTEX_3); glEnable(GL_MAP2_TEXTURE_COORD_2);
  81.     glMapGrid2f(GRD, 0.0, 1.0, GRD, 0.0, 1.0);
  82.     glEvalMesh2(GL_FILL, 0, GRD, 0, GRD);
  83.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &q[0][0][0]);
  84.     glEvalMesh2(GL_FILL, 0, GRD, 0, GRD);
  85.     if (i < 6) {
  86.         glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &r[0][0][0]);
  87.         glEvalMesh2(GL_FILL, 0, GRD, 0, GRD);
  88.         glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &s[0][0][0]);
  89.         glEvalMesh2(GL_FILL, 0, GRD, 0, GRD);
  90.     }
  91.     }
  92.     glDisable(GL_MAP2_VERTEX_3); glDisable(GL_MAP2_TEXTURE_COORD_2);
  93.     glPopMatrix ();
  94.     glEndList();
  95. }
  96.  
  97. static void wireTeapot(long grid, GLdouble scale)
  98. {
  99.     float p[4][4][3], q[4][4][3], r[4][4][3], s[4][4][3];
  100.     long i, j, k, l;
  101.     
  102.     if (grid < 2) grid = 7;
  103.     GRD = grid;
  104.     teapots[TEAPOTWIRE] = glGenLists (1);
  105.     glNewList(teapots[TEAPOTWIRE], GL_COMPILE);
  106.     glPushMatrix ();
  107.     glRotatef (270.0, 1.0, 0.0, 0.0);
  108.     glScalef (0.5*scale, 0.5*scale, 0.5*scale);
  109.     glTranslatef (0.0, 0.0, -1.5);
  110.     for (i = 0; i < 10; i++) {
  111.     for (j = 0; j < 4; j++)
  112.         for (k = 0; k < 4; k++) 
  113.         for (l = 0; l < 3; l++) {
  114.             p[j][k][l] = cpdata[patchdata[i][j*4+k]][l];
  115.             q[j][k][l] = cpdata[patchdata[i][j*4+(3-k)]][l];
  116.             if (l == 1) q[j][k][l] *= -1.0;
  117.             if (i < 6) {
  118.             r[j][k][l] = cpdata[patchdata[i][j*4+(3-k)]][l];
  119.             if (l == 0) r[j][k][l] *= -1.0;
  120.             s[j][k][l] = cpdata[patchdata[i][j*4+k]][l];
  121.             if (l == 0) s[j][k][l] *= -1.0;
  122.             if (l == 1) s[j][k][l] *= -1.0;
  123.             }
  124.         }
  125.     glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2, &tex[0][0][0]);
  126.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &p[0][0][0]);
  127.     glEnable(GL_MAP2_VERTEX_3); glEnable(GL_MAP2_TEXTURE_COORD_2);
  128.     glMapGrid2f(GRD, 0.0, 1.0, GRD, 0.0, 1.0);
  129.     glEvalMesh2(GL_LINE, 0, GRD, 0, GRD);
  130.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &q[0][0][0]);
  131.     glEvalMesh2(GL_LINE, 0, GRD, 0, GRD);
  132.     if (i < 6) {
  133.         glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &r[0][0][0]);
  134.         glEvalMesh2(GL_LINE, 0, GRD, 0, GRD);
  135.         glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &s[0][0][0]);
  136.         glEvalMesh2(GL_LINE, 0, GRD, 0, GRD);
  137.     }
  138.     }
  139.     glDisable(GL_MAP2_VERTEX_3); glDisable(GL_MAP2_TEXTURE_COORD_2);
  140.     glPopMatrix ();
  141.     glEndList();
  142. }
  143.  
  144. void auxSolidTeapot(GLdouble scale)
  145. {
  146.     if (glIsList(teapots[TEAPOTSOLID]) == 0)
  147.     solidTeapot (14, scale);
  148.     glCallList(teapots[TEAPOTSOLID]);
  149. }
  150.  
  151. void auxWireTeapot(GLdouble scale)
  152. {
  153.     if (glIsList(teapots[TEAPOTWIRE]) == 0)
  154.     wireTeapot (10, scale);
  155.     glCallList(teapots[TEAPOTWIRE]);
  156. }
  157.  
  158.