home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / book / aapoly.c < prev    next >
C/C++ Source or Header  |  1998-02-14  |  5KB  |  173 lines

  1. /*
  2.  * Copyright (c) 1993-1997, 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(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. /*
  39.  *  aapoly.c
  40.  *  This program draws filled polygons with antialiased
  41.  *  edges.  The special GL_SRC_ALPHA_SATURATE blending 
  42.  *  function is used.
  43.  *  Pressing the 't' key turns the antialiasing on and off.
  44.  */
  45. #include <GL/glut.h>
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <string.h>
  49.  
  50. GLboolean polySmooth = GL_TRUE;
  51.  
  52. static void init(void)
  53. {
  54.    glCullFace (GL_BACK);
  55.    glEnable (GL_CULL_FACE);
  56.    glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
  57.    glClearColor (0.0, 0.0, 0.0, 0.0);
  58. }
  59.  
  60. #define NFACE 6
  61. #define NVERT 8
  62. void drawCube(GLdouble x0, GLdouble x1, GLdouble y0, GLdouble y1,
  63.         GLdouble z0, GLdouble z1)
  64. {
  65.    static GLfloat v[8][3];
  66.    static GLfloat c[8][4] = {
  67.       {0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0},
  68.       {0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0},
  69.       {0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0},
  70.       {0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}
  71.    };
  72.  
  73. /*  indices of front, top, left, bottom, right, back faces  */
  74.    static GLubyte indices[NFACE][4] = {
  75.       {4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},
  76.       {0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}
  77.    };
  78.  
  79.    v[0][0] = v[3][0] = v[4][0] = v[7][0] = x0;
  80.    v[1][0] = v[2][0] = v[5][0] = v[6][0] = x1;
  81.    v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
  82.    v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
  83.    v[0][2] = v[1][2] = v[2][2] = v[3][2] = z0;
  84.    v[4][2] = v[5][2] = v[6][2] = v[7][2] = z1;
  85.  
  86. #ifdef GL_VERSION_1_1
  87.    glEnableClientState (GL_VERTEX_ARRAY);
  88.    glEnableClientState (GL_COLOR_ARRAY);
  89.    glVertexPointer (3, GL_FLOAT, 0, v);
  90.    glColorPointer (4, GL_FLOAT, 0, c);
  91.    glDrawElements (GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);
  92.    glDisableClientState (GL_VERTEX_ARRAY);
  93.    glDisableClientState (GL_COLOR_ARRAY);
  94. #else
  95.    printf ("If this is GL Version 1.0, ");
  96.    printf ("vertex arrays are not supported.\n");
  97.    exit(1);
  98. #endif
  99. }
  100.  
  101. /*  Note:  polygons must be drawn from front to back
  102.  *  for proper blending.
  103.  */
  104. void display(void)
  105. {
  106.    if (polySmooth) {
  107.       glClear (GL_COLOR_BUFFER_BIT);
  108.       glEnable (GL_BLEND);
  109.       glEnable (GL_POLYGON_SMOOTH);
  110.       glDisable (GL_DEPTH_TEST);
  111.    }
  112.    else { 
  113.       glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  114.       glDisable (GL_BLEND);
  115.       glDisable (GL_POLYGON_SMOOTH);
  116.       glEnable (GL_DEPTH_TEST);
  117.    }
  118.  
  119.    glPushMatrix ();
  120.       glTranslatef (0.0, 0.0, -8.0);    
  121.       glRotatef (30.0, 1.0, 0.0, 0.0);
  122.       glRotatef (60.0, 0.0, 1.0, 0.0); 
  123.       drawCube(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
  124.    glPopMatrix ();
  125.  
  126.    glFlush ();
  127. }
  128.  
  129. void reshape(int w, int h)
  130. {
  131.    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  132.    glMatrixMode(GL_PROJECTION);
  133.    glLoadIdentity();
  134.    gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
  135.    glMatrixMode(GL_MODELVIEW);
  136.    glLoadIdentity();
  137. }
  138.  
  139. /* ARGSUSED1 */
  140. void keyboard(unsigned char key, int x, int y)
  141. {
  142.    switch (key) {
  143.       case 't':
  144.       case 'T':
  145.          polySmooth = !polySmooth;
  146.          glutPostRedisplay();
  147.          break;
  148.       case 27:
  149.          exit(0);  /*  Escape key  */
  150.          break;
  151.       default:
  152.          break;
  153.    }
  154. }
  155.  
  156. /*  Main Loop
  157.  */
  158. int main(int argc, char** argv)
  159. {
  160.    glutInit(&argc, argv);
  161.    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB 
  162.                         | GLUT_ALPHA | GLUT_DEPTH);
  163.    glutInitWindowSize(200, 200);
  164.    glutCreateWindow(argv[0]);
  165.    init ();
  166.    glutReshapeFunc (reshape);
  167.    glutKeyboardFunc (keyboard);
  168.    glutDisplayFunc (display);
  169.    glutMainLoop();
  170.    return 0;
  171. }
  172.  
  173.