home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / sgi_extensions / sprite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  7.4 KB  |  303 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* sprite.c
  19.  *    This program demonstrates GL_SGIX_sprite.
  20.  *
  21.  *    <s> key        - toggle GL_SGIX_sprite on/off
  22.  *    Escape key    - exit the program
  23.  */
  24. #include <GL/gl.h>
  25. #include <GL/glu.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <math.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32. /* Function Prototypes */
  33.  
  34. GLvoid  initgfx( GLvoid );
  35. GLvoid  drawScene( GLvoid );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  keyboard( GLubyte, GLint, GLint );
  38.  
  39. GLvoid toggleSpriteEnable( GLvoid );
  40.  
  41. void printHelp( char * );
  42.  
  43. /* Global Definitions */
  44.  
  45. #define KEY_ESC    27    /* ascii value for the escape key */
  46.  
  47. /* Global Variables */
  48.  
  49. static GLint    spriteMode;
  50.  
  51. static GLboolean spriteEnable = GL_FALSE;
  52. static GLboolean spriteSupported = GL_TRUE;
  53.  
  54. static GLfloat angle = 80.0;
  55.  
  56. static GLfloat spriteAxis[] = {.0, .0, 1.0, 1.0 };
  57. static GLfloat spriteTrans[] = {.0, .0, .0, 1.0 };
  58.  
  59. typedef struct VtxRec {
  60.     GLfloat crd[4]; /* coordinate */
  61.     GLfloat col[4];    /* color */
  62.     GLfloat nrm[3]; /* normal */
  63.     GLfloat tex[4]; /* texture coordinate */
  64. } Vtx;
  65.  
  66. typedef struct QdRec {
  67.     Vtx v0, v1, v2, v3;
  68. } Qd;
  69.  
  70. static Qd qd = {
  71.     { {0.1, 0.1, 0.0, 1.0}, {1.0, 1.0, 1.0, 1.0}, 
  72.       {1.0, 1.0, 1.0}, {0.1, 0.1, .0, 1.0} },
  73.     { {0.1, 0.2, .0, 1.0}, {1.0, .0, .0, .0},
  74.       {1.0, 1.0, 1.0}, {0.1, 0.9, .0, 1.0} },
  75.     { {0.2, 0.2, .0, 1.0}, {.0, 1.0, .0, .0},
  76.       {1.0, 1.0, 1.0}, {0.9, 0.9, .0, 1.0} },
  77.     { {0.2, 0.1, .0, 1.0}, {.0, .0, 1.0, .0},
  78.       {1.0, 1.0, 1.0}, {0.9, 0.1, .0, 1.0} },
  79. };
  80.  
  81. void
  82. main( int argc, char *argv[] )
  83. {
  84.     GLsizei     width, height;
  85.  
  86.     glutInit( &argc, argv );
  87.  
  88.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  89.     height = glutGet( GLUT_SCREEN_HEIGHT );
  90.     glutInitWindowPosition( width/4, height/4 ); 
  91.     glutInitWindowSize( width/2, height/2 );
  92.     glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
  93.     glutCreateWindow( argv[0] );
  94.     
  95.     initgfx();
  96.  
  97.     glutKeyboardFunc( keyboard );
  98.     glutReshapeFunc( reshape );
  99.     glutDisplayFunc( drawScene ); 
  100.  
  101.     printHelp( argv[0] );
  102.  
  103.     glutMainLoop();
  104. }
  105.  
  106. GLvoid
  107. printHelp( char *progname )
  108. {
  109.     fprintf(stdout,"\n%s - demonstrates sprites\n\n"
  110.         "<s> key        - toggle sprites\n"
  111.         "Escape key        - exit the program\n\n",
  112.         progname
  113.     );
  114. }
  115.  
  116. GLvoid
  117. initgfx( GLvoid )
  118. {
  119.     glClearColor(0, 0, 0, 0);
  120.     glClear(GL_COLOR_BUFFER_BIT);
  121.  
  122.     if (!glutExtensionSupported("GL_SGIX_sprite")) {
  123.         spriteSupported = GL_FALSE;
  124.         fprintf( stderr,
  125.            "GL_SGIX_sprite not supported on this machine\n");
  126.     }
  127. }
  128.  
  129. void toggleSpriteEnable( GLvoid )
  130. {
  131. #ifdef    GL_SGIX_sprite
  132.     if (spriteSupported) {
  133.         spriteEnable = !spriteEnable;
  134.         printf("Sprites are %s\n", (spriteEnable? "Enabled":"Disabled"));
  135.     } else {
  136.         printf("GL_SGIX_sprite not supported\n");
  137.     }
  138. #else
  139.     printf("Sprites are not supported on this client\n");
  140. #endif
  141. }
  142.  
  143. GLvoid 
  144. keyboard( GLubyte key, GLint x, GLint y )
  145. {
  146.     switch (key) {
  147.     case 's':    /* toggle sprite enable */
  148.         toggleSpriteEnable();
  149.         glutPostRedisplay();
  150.         break;
  151.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  152.         exit(0);
  153.     }
  154. }
  155.  
  156. GLvoid
  157. reshape( GLsizei width, GLsizei height )
  158. {
  159.     GLdouble    aspect;
  160.  
  161.     glViewport( 0, 0, width, height );
  162.  
  163.     aspect = (GLdouble) width / (GLdouble) height;
  164.  
  165.     glMatrixMode( GL_PROJECTION );
  166.     glLoadIdentity();
  167.     glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  168.     glMatrixMode( GL_MODELVIEW );
  169.     glLoadIdentity();
  170. }
  171.  
  172. static void
  173. drawObject(void)
  174. {
  175.     glBegin(GL_QUADS);
  176.         glColor4f(qd.v0.col[0], qd.v0.col[1], qd.v0.col[2], qd.v0.col[3]);
  177.         glTexCoord4f(qd.v0.tex[0], qd.v0.tex[1], qd.v0.tex[2], qd.v0.tex[3]);
  178.         glNormal3f(qd.v0.nrm[0], qd.v0.nrm[1], qd.v0.nrm[2]);
  179.         glVertex4f(qd.v0.crd[0], qd.v0.crd[1], qd.v0.crd[2], qd.v0.crd[3]);
  180.  
  181.         glColor4f(qd.v1.col[0], qd.v1.col[1], qd.v1.col[2], qd.v1.col[3]);
  182.         glTexCoord4f(qd.v1.tex[0], qd.v1.tex[1], qd.v1.tex[2], qd.v1.tex[3]);
  183.         glNormal3f(qd.v1.nrm[0], qd.v1.nrm[1], qd.v1.nrm[2]);
  184.         glVertex4f(qd.v1.crd[0], qd.v1.crd[1], qd.v1.crd[2], qd.v1.crd[3]);
  185.  
  186.         glColor4f(qd.v2.col[0], qd.v2.col[1], qd.v2.col[2], qd.v2.col[3]);
  187.         glTexCoord4f(qd.v2.tex[0], qd.v2.tex[1], qd.v2.tex[2], qd.v2.tex[3]);
  188.         glNormal3f(qd.v2.nrm[0], qd.v2.nrm[1], qd.v2.nrm[2]);
  189.         glVertex4f(qd.v2.crd[0], qd.v2.crd[1], qd.v2.crd[2], qd.v2.crd[3]);
  190.  
  191.         glColor4f(qd.v3.col[0], qd.v3.col[1], qd.v3.col[2], qd.v3.col[3]);
  192.         glTexCoord4f(qd.v3.tex[0], qd.v3.tex[1], qd.v3.tex[2], qd.v3.tex[3]);
  193.         glNormal3f(qd.v3.nrm[0], qd.v3.nrm[1], qd.v3.nrm[2]);
  194.         glVertex4f(qd.v3.crd[0], qd.v3.crd[1], qd.v3.crd[2], qd.v3.crd[3]);
  195.     glEnd();
  196.  
  197. }
  198.  
  199. GLvoid
  200. drawScene( GLvoid )
  201. {
  202.     int i,  slices = 8;
  203.     
  204.     glClear( GL_COLOR_BUFFER_BIT );
  205.     
  206.     drawObject();
  207.  
  208. #ifdef    GL_SGIX_sprite
  209.     if (spriteEnable) {
  210.         glEnable(GL_SPRITE_SGIX);
  211.         glSpriteParameteriSGIX(GL_SPRITE_MODE_SGIX, GL_SPRITE_AXIAL_SGIX);
  212.     }
  213. #endif
  214.  
  215.     /* axial mode (clipped geometry) */
  216.     glPushMatrix();
  217.         glTranslatef(.15, .0, .0);
  218.  
  219. #ifdef    GL_SGIX_sprite
  220.         if (spriteEnable) {
  221.             spriteAxis[0] = .2; spriteAxis[1] = .2; spriteAxis[2] = 1.0;
  222.             glSpriteParameterfvSGIX(GL_SPRITE_AXIS_SGIX, spriteAxis);
  223.  
  224.             spriteTrans[0] = .2; spriteTrans[1] = .0; spriteTrans[2] = .0;
  225.             glSpriteParameterfvSGIX(GL_SPRITE_TRANSLATION_SGIX, spriteTrans);
  226.         }
  227. #endif
  228.  
  229.         drawObject();
  230.     glPopMatrix();
  231.  
  232.     /* axial mode (non-clipped geometry) */
  233.     glPushMatrix();
  234.         glTranslatef(.3, .1, .0);
  235.  
  236. #ifdef    GL_SGIX_sprite
  237.         if (spriteEnable) {
  238.             spriteAxis[0] = .2; spriteAxis[1] = .2; spriteAxis[2] = 0.5;
  239.             glSpriteParameterfvSGIX(GL_SPRITE_AXIS_SGIX, spriteAxis);
  240.  
  241.             spriteTrans[0] = .2; spriteTrans[1] = .2; spriteTrans[2] = .0;
  242.             glSpriteParameterfvSGIX(GL_SPRITE_TRANSLATION_SGIX, spriteTrans);
  243.         }
  244. #endif
  245.  
  246.         drawObject();
  247.     glPopMatrix();
  248.  
  249. #ifdef    GL_SGIX_sprite
  250.     if (spriteEnable) {
  251.         /* object mode */
  252.         glSpriteParameteriSGIX(GL_SPRITE_MODE_SGIX, GL_SPRITE_OBJECT_ALIGNED_SGIX);
  253.     }
  254. #endif
  255.     glPushMatrix();
  256.         glTranslatef(.0, .12, .0);
  257.  
  258. #ifdef    GL_SGIX_sprite
  259.         if (spriteEnable) {
  260.             spriteAxis[0] = .8; spriteAxis[1] = .5; spriteAxis[2] = 1.0;
  261.             glSpriteParameterfvSGIX(GL_SPRITE_AXIS_SGIX, spriteAxis);
  262.  
  263.             spriteTrans[0] = .0; spriteTrans[1] = .3; spriteTrans[2] = .0;
  264.             glSpriteParameterfvSGIX(GL_SPRITE_TRANSLATION_SGIX, spriteTrans);
  265.         }
  266. #endif
  267.  
  268.         drawObject();
  269.     glPopMatrix();
  270.  
  271.  
  272. #ifdef    GL_SGIX_sprite
  273.     if (spriteEnable) {
  274.         /* eye mode */
  275.         glSpriteParameteriSGIX(GL_SPRITE_MODE_SGIX, GL_SPRITE_EYE_ALIGNED_SGIX);
  276.     }
  277. #endif
  278.     glPushMatrix();
  279.         glTranslatef(.15, .25, .0);
  280.  
  281. #ifdef    GL_SGIX_sprite
  282.         if (spriteEnable) {
  283.             spriteAxis[0] = .0; spriteAxis[1] = 1.0; spriteAxis[2] = 1.0;
  284.             glSpriteParameterfvSGIX(GL_SPRITE_AXIS_SGIX, spriteAxis);
  285.  
  286.             spriteTrans[0] = .2; spriteTrans[1] = .2; spriteTrans[2] = .0;
  287.             glSpriteParameterfvSGIX(GL_SPRITE_TRANSLATION_SGIX, spriteTrans);
  288.         }
  289. #endif
  290.  
  291.         drawObject();
  292.     glPopMatrix();
  293.  
  294. #ifdef    GL_SGIX_sprite
  295.     if (spriteEnable)
  296.         glDisable(GL_SPRITE_SGIX);
  297. #endif
  298.  
  299.     glutSwapBuffers();
  300.  
  301.     checkError("drawScene");
  302. }
  303.