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 / interlace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  9.3 KB  |  379 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. /* interlace.c
  19.  *    This program demonstrates GL_SGIX_interlace.
  20.  *
  21.  *    <i> key        - toggle interlacing on/off
  22.  *    LEFT Arrow Key    - increase the X zoom value
  23.  *    RIGHT Arrow Key    - decrease the X zoom value
  24.  *    UP Arrow Key    - increase the Y zoom value
  25.  *    DOWN Arrow Key    - decrease the Y zoom value
  26.  *    Escape key    - exit the program
  27.  */
  28. #include <GL/gl.h>
  29. #include <GL/glu.h>
  30. #include <GL/glut.h>
  31.  
  32. #include <math.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35.  
  36. /* Function Prototypes */
  37.  
  38. GLvoid  initgfx( GLvoid );
  39. GLvoid  drawScene( GLvoid );
  40. GLvoid  reshape( GLsizei, GLsizei );
  41. GLvoid  keyboard( GLubyte, GLint, GLint );
  42. GLvoid  specialkeys( GLint, GLint, GLint );
  43.  
  44. GLvoid toggleInterlaceEnable( GLvoid );
  45.  
  46. GLvoid inc( GLfloat );
  47. GLvoid dec( GLfloat );
  48.  
  49. void printHelp( char * );
  50.  
  51. /* Global Definitions */
  52.  
  53. #define KEY_ESC    27    /* ascii value for the escape key */
  54.  
  55. #define TEX_W 64
  56. #define TEX_H 32
  57. #define TEX_H2 (TEX_H/2)
  58. #define MAG 1
  59.  
  60. /* Global Variables */
  61.  
  62. unsigned char *BlackImg;
  63. unsigned char *RampImg = 0;
  64. unsigned char *RampEven = 0;
  65. unsigned char *RampOdd = 0;
  66.  
  67. GLenum texfmt = GL_RGBA;
  68. GLenum textype = GL_UNSIGNED_BYTE;
  69.  
  70. GLfloat xzoom = 1.0;
  71. GLfloat yzoom = 1.0;
  72.  
  73. static GLboolean interlaceEnable = GL_FALSE;
  74. static GLboolean interlaceSupported = GL_TRUE;
  75. static GLboolean is_RE = GL_FALSE;
  76.  
  77. void
  78. main( int argc, char *argv[] )
  79. {
  80.     GLsizei     width, height;
  81.  
  82.     glutInit( &argc, argv );
  83.  
  84.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  85.     height = glutGet( GLUT_SCREEN_HEIGHT );
  86.     glutInitWindowPosition( width/4, height/4 ); 
  87.     glutInitWindowSize( width/2, height/2 );
  88.     glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
  89.     glutCreateWindow( argv[0] );
  90.     
  91.     initgfx();
  92.  
  93.     glutKeyboardFunc( keyboard );
  94.     glutSpecialFunc( specialkeys );
  95.     glutReshapeFunc( reshape );
  96.     glutDisplayFunc( drawScene ); 
  97.  
  98.     printHelp( argv[0] );
  99.  
  100.     glutMainLoop();
  101. }
  102.  
  103. GLvoid
  104. printHelp( char *progname )
  105. {
  106.     fprintf(stdout,"\n%s - demonstrates interlacing\n\n"\
  107.         "<i> key        - toggle interlacing\n"
  108.         "UP Arrow Key        - increase the Y zoom value\n"
  109.         "DOWN Arrow Key        - decrease the Y zoom value\n"
  110.         "LEFT Arrow Key        - increase the X zoom value\n"
  111.         "RIGHT Arrow Key    - decrease the X zoom value\n"
  112.         "Escape key        - exit the program\n\n",
  113.         progname
  114.     );
  115. }
  116.  
  117. static void
  118. CreateImages(void)
  119. {
  120.     int x,y;
  121.  
  122.     BlackImg = (unsigned char *) calloc(TEX_W * TEX_H * 4,1);
  123.     RampImg =  (unsigned char *) calloc(TEX_W * TEX_H * 4,1);
  124.     RampEven =  (unsigned char *) calloc(TEX_W * TEX_H * 4,1);
  125.     RampOdd =  (unsigned char *) calloc(TEX_W * TEX_H * 4,1);
  126.  
  127.     for (y=0; y<TEX_H; y++) {
  128.         for (x=0; x<TEX_W; x++) {
  129.             RampImg[y*TEX_W*4 + x*4 + 0] = y * 256./TEX_H;
  130.             RampImg[y*TEX_W*4 + x*4 + 3] = 255;
  131.             if (y&1) {
  132.                 RampImg[y*TEX_W*4 + x*4 + 1] = 0;
  133.                 RampImg[y*TEX_W*4 + x*4 + 2] = x * 256./TEX_W;
  134.                 RampOdd[(y/2)*TEX_W*4+x*4 + 0]= RampImg[y*TEX_W*4 + x*4 + 0];
  135.                 RampOdd[(y/2)*TEX_W*4+x*4 + 1]= RampImg[y*TEX_W*4 + x*4 + 1];
  136.                 RampOdd[(y/2)*TEX_W*4+x*4 + 2]= RampImg[y*TEX_W*4 + x*4 + 2];
  137.                 RampOdd[(y/2)*TEX_W*4+x*4 + 3]= RampImg[y*TEX_W*4 + x*4 + 3];
  138.             } else {
  139.                 RampImg[y*TEX_W*4 + x*4 + 1] = x * 256./TEX_W;
  140.                 RampImg[y*TEX_W*4 + x*4 + 2] = 0;
  141.                 RampEven[(y/2)*TEX_W*4+x*4 + 0]= RampImg[y*TEX_W*4 + x*4 + 0];
  142.                 RampEven[(y/2)*TEX_W*4+x*4 + 1]= RampImg[y*TEX_W*4 + x*4 + 1];
  143.                 RampEven[(y/2)*TEX_W*4+x*4 + 2]= RampImg[y*TEX_W*4 + x*4 + 2];
  144.                 RampEven[(y/2)*TEX_W*4+x*4 + 3]= RampImg[y*TEX_W*4 + x*4 + 3];
  145.             }
  146.         }
  147.     }
  148. }
  149.  
  150. /* Initialize graphics */
  151. GLvoid
  152. initgfx( GLvoid )
  153. {
  154.     glClearColor( 0.0, 0.0, 0.0, 0.0 );
  155.     glClear( GL_COLOR_BUFFER_BIT );
  156.  
  157.     if (!glutExtensionSupported("GL_SGIX_interlace")) {
  158.         interlaceSupported = GL_FALSE;
  159.         fprintf( stderr,
  160.            "GL_SGIX_interlace not supported on this machine\n");
  161.     }
  162.     is_RE = (strncmp(glGetString(GL_RENDERER), "RE", 2) == 0);
  163.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  164.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  165.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  166.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  167.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  168.  
  169.     CreateImages();
  170. }
  171.  
  172. GLvoid
  173. decXzoom( GLvoid )
  174. {
  175.     xzoom -= 0.5;
  176.     printf( "X Zoom = %4.2f\n", xzoom );
  177. }
  178.  
  179. GLvoid
  180. incXzoom( GLvoid )
  181. {
  182.     xzoom += 0.5;
  183.     printf( "X Zoom = %4.2f\n", xzoom );
  184. }
  185.  
  186. GLvoid
  187. decYzoom( GLvoid )
  188. {
  189.     yzoom -= 0.5;
  190.     printf( "Y Zoom = %4.2f\n", yzoom );
  191. }
  192.  
  193. GLvoid
  194. incYzoom( GLvoid )
  195. {
  196.     yzoom += 0.5;
  197.     printf( "Y Zoom = %4.2f\n", yzoom );
  198. }
  199.  
  200. void toggleInterlaceEnable( GLvoid )
  201. {
  202. #ifdef    GL_SGIX_interlace
  203.     if (interlaceSupported) {
  204.         interlaceEnable = !interlaceEnable;
  205.         if (interlaceEnable) {
  206.             glEnable(GL_INTERLACE_SGIX);
  207.         } else {
  208.             glDisable(GL_INTERLACE_SGIX);
  209.         }
  210.         printf("Interlacing %s\n", (interlaceEnable? "Enabled":"Disabled"));
  211.     } else 
  212.         printf("GL_SGIX_interlace not supported\n");
  213. #else
  214.     printf("GL_SGIX_interlace not supported\n");
  215. #endif
  216. }
  217.  
  218. GLvoid 
  219. keyboard( GLubyte key, GLint x, GLint y )
  220. {
  221.     switch (key) {
  222.     case 'i':    /* toggle interlacing enable */
  223.         toggleInterlaceEnable();
  224.         glutPostRedisplay();
  225.         break;
  226.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  227.         exit(0);
  228.     }
  229. }
  230.  
  231. GLvoid 
  232. specialkeys( GLint key, GLint x, GLint y )
  233. {
  234.     switch (key) {
  235.     case GLUT_KEY_UP:    /* increase y zoom factor */
  236.         incYzoom();
  237.         break;
  238.     case GLUT_KEY_DOWN:    /* decrease y zoom factor */
  239.         decYzoom();
  240.         break;
  241.     case GLUT_KEY_LEFT:    /* increase x zoom factor */
  242.         incXzoom();
  243.         break;
  244.     case GLUT_KEY_RIGHT:    /* decrease x zoom factor */
  245.         decXzoom();
  246.         break;
  247.     }
  248.     glutPostRedisplay();
  249. }
  250.  
  251. GLvoid
  252. reshape( GLsizei width, GLsizei height )
  253. {
  254.     GLdouble    aspect;
  255.  
  256.     glViewport( 0, 0, width, height );
  257.  
  258.     aspect = (GLdouble) width / (GLdouble) height;
  259.  
  260.     glMatrixMode( GL_PROJECTION );
  261.     glLoadIdentity();
  262.     glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  263.     glMatrixMode( GL_MODELVIEW );
  264.     glLoadIdentity();
  265. }
  266.  
  267. static void draw_prims(void)
  268. {
  269.     glBegin(GL_TRIANGLE_STRIP);
  270.         glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0);
  271.         glTexCoord2f(1.0, 0.0); glVertex2f( 1.0, -1.0);
  272.         glTexCoord2f(0.0, 1.0); glVertex2f(-1.0,  1.0);
  273.         glTexCoord2f(1.0, 1.0); glVertex2f( 1.0,  1.0);
  274.     glEnd();
  275. }
  276.  
  277. GLvoid
  278. drawScene( GLvoid )
  279. {
  280.     int vx=10, vy=20;
  281.     float rpx = -1., rpy_even = 1.;
  282.     float rpy_odd = 1. - (2./TEX_H);
  283.  
  284.     glClear( GL_COLOR_BUFFER_BIT );
  285.  
  286.     glPixelZoom(xzoom,  yzoom);
  287.  
  288.     /* render a raster image */
  289.  
  290.     glViewport(vx,vy,TEX_W,TEX_H);
  291.     glRasterPos2f(rpx, rpy_even);
  292.     glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampImg);
  293.     vx += TEX_W+4;
  294.  
  295. #ifdef    GL_SGIX_interlace
  296.     if (interlaceSupported)
  297.         glEnable(GL_INTERLACE_SGIX);
  298. #endif
  299.  
  300.     glViewport(vx,vy,TEX_W,TEX_H);
  301.     glRasterPos2f(rpx, rpy_even);
  302.     glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampEven);
  303.     vx += TEX_W+4;
  304.  
  305.     glViewport(vx,vy,TEX_W,TEX_H);
  306.     glRasterPos2f(rpx, rpy_odd);
  307.     glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampOdd);
  308.     vx += TEX_W+4;
  309.  
  310.  
  311.     glViewport(vx,vy,TEX_W,TEX_H);
  312.     glRasterPos2f(rpx, rpy_even);
  313.     glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampEven);
  314.     glRasterPos2f(rpx, rpy_odd);
  315.     glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampOdd);
  316.     vx += TEX_W+4;
  317.  
  318. #ifdef    GL_SGIX_interlace
  319.     if (interlaceSupported)
  320.         glDisable(GL_INTERLACE_SGIX);
  321. #endif
  322.  
  323.     /* rendering a textured polygon */
  324.  
  325.     glEnable(GL_TEXTURE_2D);
  326.     
  327.     /* reference image */
  328.     glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_W, TEX_H, 0, texfmt, textype, 
  329.             RampImg);    
  330.     glViewport(vx,vy,TEX_W,TEX_H);
  331.     draw_prims();
  332.     vx += TEX_W+4;
  333.  
  334.     /* must use a null texture when using subtexture on an RE systems */
  335.     glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_W, TEX_H, 0, texfmt, textype, 
  336.             (is_RE? NULL : BlackImg));
  337.  
  338. #ifdef    GL_SGIX_interlace
  339.     if (interlaceSupported)
  340.         glEnable(GL_INTERLACE_SGIX);
  341. #endif
  342.  
  343.     /* even fields */
  344.     glTexSubImage2DEXT(GL_TEXTURE_2D, 0, 0, 0, TEX_W, TEX_H2, texfmt, 
  345.             textype, RampEven);
  346.     glViewport(vx,vy,TEX_W,TEX_H);
  347.     draw_prims();
  348.     vx += TEX_W+4;
  349.  
  350.     /* odd fields */
  351.     glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_W, TEX_H2, 0, texfmt, textype, 
  352.             (is_RE? NULL : BlackImg));
  353.     /* start at y == 1 if interlacing is supported */
  354.     glTexSubImage2DEXT(GL_TEXTURE_2D, 0, 0, (interlaceSupported ? 1 : 0), 
  355.             TEX_W, TEX_H2, texfmt, textype, RampOdd);
  356.     glViewport(vx,vy,TEX_W,TEX_H);
  357.     draw_prims();
  358.     vx += TEX_W+4;
  359.  
  360.     /* both fields */
  361.     glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_W, TEX_H2, 0, texfmt, textype, 
  362.             (is_RE? NULL : BlackImg));
  363.     glTexSubImage2DEXT(GL_TEXTURE_2D, 0, 0, 0, TEX_W, TEX_H2, texfmt, 
  364.             textype, RampEven);
  365.     glViewport(vx,vy,TEX_W,TEX_H);
  366.     draw_prims();
  367.     vx += TEX_W+4;
  368.  
  369. #ifdef    GL_SGIX_interlace
  370.     if (interlaceSupported)
  371.         glDisable(GL_INTERLACE_SGIX);
  372. #endif
  373.     glDisable(GL_TEXTURE_2D);
  374.  
  375.     glFlush();
  376.  
  377.     checkError("drawScene");
  378. }
  379.