home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / book / texturesurf.c < prev    next >
C/C++ Source or Header  |  1999-09-23  |  5KB  |  156 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. /*  texturesurf.c
  41.  *  This program uses evaluators to generate a curved
  42.  *  surface and automatically generated texture coordinates.
  43.  */
  44.  
  45. #include <stdlib.h>
  46. #include <GL/glut.h>
  47. #include <math.h>
  48.  
  49. GLfloat ctrlpoints[4][4][3] = {
  50.     {{ -1.5, -1.5, 4.0}, { -0.5, -1.5, 2.0}, 
  51.     {0.5, -1.5, -1.0}, {1.5, -1.5, 2.0}}, 
  52.     {{ -1.5, -0.5, 1.0}, { -0.5, -0.5, 3.0}, 
  53.     {0.5, -0.5, 0.0}, {1.5, -0.5, -1.0}}, 
  54.     {{ -1.5, 0.5, 4.0}, { -0.5, 0.5, 0.0}, 
  55.     {0.5, 0.5, 3.0}, {1.5, 0.5, 4.0}}, 
  56.     {{ -1.5, 1.5, -2.0}, { -0.5, 1.5, -2.0}, 
  57.     {0.5, 1.5, 0.0}, {1.5, 1.5, -1.0}}
  58. };
  59.  
  60. GLfloat texpts[2][2][2] = {{{0.0, 0.0}, {0.0, 1.0}}, 
  61.             {{1.0, 0.0}, {1.0, 1.0}}};
  62.  
  63. void display(void)
  64. {
  65.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  66.     glColor3f(1.0, 1.0, 1.0);
  67.     glEvalMesh2(GL_FILL, 0, 20, 0, 20);
  68.     glFlush();
  69. }
  70.  
  71. #define    imageWidth 64
  72. #define    imageHeight 64
  73. GLubyte image[3*imageWidth*imageHeight];
  74.  
  75. void makeImage(void)
  76. {
  77.     int i, j;
  78.     float ti, tj;
  79.     
  80.     for (i = 0; i < imageWidth; i++) {
  81.     ti = 2.0*3.14159265*i/imageWidth;
  82.     for (j = 0; j < imageHeight; j++) {
  83.         tj = 2.0*3.14159265*j/imageHeight;
  84.  
  85.         image[3*(imageHeight*i+j)] = (GLubyte) 127*(1.0+sin(ti));
  86.         image[3*(imageHeight*i+j)+1] = (GLubyte) 127*(1.0+cos(2*tj));
  87.         image[3*(imageHeight*i+j)+2] = (GLubyte) 127*(1.0+cos(ti+tj));
  88.     }
  89.     }
  90. }
  91.  
  92. void myinit(void)
  93. {
  94.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
  95.         0, 1, 12, 4, &ctrlpoints[0][0][0]);
  96.     glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 
  97.         0, 1, 4, 2, &texpts[0][0][0]);
  98.     glEnable(GL_MAP2_TEXTURE_COORD_2);
  99.     glEnable(GL_MAP2_VERTEX_3);
  100.     glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
  101.     makeImage();
  102.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  103.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  104.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  105.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  106.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  107.     glTexImage2D(GL_TEXTURE_2D, 0, 3, imageWidth, imageHeight, 0,
  108.          GL_RGB, GL_UNSIGNED_BYTE, image);
  109.     glEnable(GL_TEXTURE_2D);
  110.     glEnable(GL_DEPTH_TEST);
  111.     glEnable(GL_NORMALIZE);
  112.     glShadeModel (GL_FLAT);
  113. }
  114.  
  115. void myReshape(int w, int h)
  116. {
  117.     glViewport(0, 0, w, h);
  118.     glMatrixMode(GL_PROJECTION);
  119.     glLoadIdentity();
  120.     if (w <= h)
  121.     glOrtho(-4.0, 4.0, -4.0*(GLfloat)h/(GLfloat)w, 
  122.         4.0*(GLfloat)h/(GLfloat)w, -4.0, 4.0);
  123.     else
  124.     glOrtho(-4.0*(GLfloat)w/(GLfloat)h, 
  125.         4.0*(GLfloat)w/(GLfloat)h, -4.0, 4.0, -4.0, 4.0);
  126.     glMatrixMode(GL_MODELVIEW);
  127.     glLoadIdentity();
  128.     glRotatef(85.0, 1.0, 1.0, 1.0);
  129. }
  130.  
  131. static void
  132. key(unsigned char k, int x, int y)
  133. {
  134.   switch (k) {
  135.   case 27:  /* Escape */
  136.     exit(0);
  137.     break;
  138.   default:
  139.     return;
  140.   }
  141.   glutPostRedisplay();
  142. }
  143.  
  144. int main(int argc, char** argv)
  145. {
  146.     glutInit(&argc, argv);
  147.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  148.     glutCreateWindow (argv[0]);
  149.     myinit();
  150.     glutReshapeFunc (myReshape);
  151.     glutDisplayFunc(display);
  152.     glutKeyboardFunc(key);
  153.     glutMainLoop();
  154.     return 0;             /* ANSI C requires main to return int. */
  155. }
  156.