home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / book / mipmap.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  6KB  |  162 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. /*  mipmap.c
  38.  *  This program demonstrates using mipmaps for texture maps.
  39.  *  To overtly show the effect of mipmaps, each mipmap reduction
  40.  *  level has a solidly colored, contrasting texture image.
  41.  *  Thus, the quadrilateral which is drawn is drawn with several
  42.  *  different colors.
  43.  */
  44. #include <GL/gl.h>
  45. #include <GL/glu.h>
  46. #include <stdlib.h>
  47. #include "glaux.h"
  48.  
  49. GLubyte mipmapImage32[32][32][3];
  50. GLubyte mipmapImage16[16][16][3];
  51. GLubyte mipmapImage8[8][8][3];
  52. GLubyte mipmapImage4[4][4][3];
  53. GLubyte mipmapImage2[2][2][3];
  54. GLubyte mipmapImage1[1][1][3];
  55.  
  56. void makeImages(void)
  57. {
  58.     int i, j;
  59.     
  60.     for (i = 0; i < 32; i++) {
  61.     for (j = 0; j < 32; j++) {
  62.         mipmapImage32[i][j][0] = 255;
  63.         mipmapImage32[i][j][1] = 255;
  64.         mipmapImage32[i][j][2] = 0;
  65.     }
  66.     }
  67.     for (i = 0; i < 16; i++) {
  68.     for (j = 0; j < 16; j++) {
  69.         mipmapImage16[i][j][0] = 255;
  70.         mipmapImage16[i][j][1] = 0;
  71.         mipmapImage16[i][j][2] = 255;
  72.     }
  73.     }
  74.     for (i = 0; i < 8; i++) {
  75.     for (j = 0; j < 8; j++) {
  76.         mipmapImage8[i][j][0] = 255;
  77.         mipmapImage8[i][j][1] = 0;
  78.         mipmapImage8[i][j][2] = 0;
  79.     }
  80.     }
  81.     for (i = 0; i < 4; i++) {
  82.     for (j = 0; j < 4; j++) {
  83.         mipmapImage4[i][j][0] = 0;
  84.         mipmapImage4[i][j][1] = 255;
  85.         mipmapImage4[i][j][2] = 0;
  86.     }
  87.     }
  88.     for (i = 0; i < 2; i++) {
  89.     for (j = 0; j < 2; j++) {
  90.         mipmapImage2[i][j][0] = 0;
  91.         mipmapImage2[i][j][1] = 0;
  92.         mipmapImage2[i][j][2] = 255;
  93.     }
  94.     }
  95.     mipmapImage1[0][0][0] = 255;
  96.     mipmapImage1[0][0][1] = 255;
  97.     mipmapImage1[0][0][2] = 255;
  98. }
  99.  
  100. void myinit(void)
  101. {    
  102.     glEnable(GL_DEPTH_TEST);
  103.     glDepthFunc(GL_LESS);
  104.     glShadeModel(GL_FLAT);
  105.  
  106.     glTranslatef(0.0, 0.0, -3.6);
  107.     makeImages();
  108.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  109.     glTexImage2D(GL_TEXTURE_2D, 0, 3, 32, 32, 0,
  110.          GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage32[0][0][0]);
  111.     glTexImage2D(GL_TEXTURE_2D, 1, 3, 16, 16, 0,
  112.          GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage16[0][0][0]);
  113.     glTexImage2D(GL_TEXTURE_2D, 2, 3, 8, 8, 0,
  114.          GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage8[0][0][0]);
  115.     glTexImage2D(GL_TEXTURE_2D, 3, 3, 4, 4, 0,
  116.          GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage4[0][0][0]);
  117.     glTexImage2D(GL_TEXTURE_2D, 4, 3, 2, 2, 0,
  118.          GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage2[0][0][0]);
  119.     glTexImage2D(GL_TEXTURE_2D, 5, 3, 1, 1, 0,
  120.          GL_RGB, GL_UNSIGNED_BYTE, &mipmapImage1[0][0][0]);
  121.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  122.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  123.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  124.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
  125.     GL_NEAREST_MIPMAP_NEAREST);
  126.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  127.     glEnable(GL_TEXTURE_2D);
  128. }
  129.  
  130. void display(void)
  131. {
  132.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  133.     glBegin(GL_QUADS);
  134.     glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
  135.     glTexCoord2f(0.0, 8.0); glVertex3f(-2.0, 1.0, 0.0);
  136.     glTexCoord2f(8.0, 8.0); glVertex3f(2000.0, 1.0, -6000.0);
  137.     glTexCoord2f(8.0, 0.0); glVertex3f(2000.0, -1.0, -6000.0);
  138.     glEnd();
  139.     glFlush();
  140. }
  141.  
  142. void myReshape(int w, int h)
  143. {
  144.     glViewport(0, 0, w, h);
  145.     glMatrixMode(GL_PROJECTION);
  146.     glLoadIdentity();
  147.     gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30000.0);
  148.     glMatrixMode(GL_MODELVIEW);
  149.     glLoadIdentity();
  150. }
  151.  
  152. int main(int argc, char** argv)
  153. {
  154.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB | AUX_DEPTH);
  155.     auxInitPosition (0, 0, 500, 500);
  156.     auxInitWindow (argv[0]);
  157.     myinit();
  158.     auxReshapeFunc (myReshape);
  159.     auxMainLoop(display);
  160. }
  161.  
  162.