home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* interlace.c
- * This program demonstrates GL_SGIX_interlace.
- *
- * <i> key - toggle interlacing on/off
- * LEFT Arrow Key - increase the X zoom value
- * RIGHT Arrow Key - decrease the X zoom value
- * UP Arrow Key - increase the Y zoom value
- * DOWN Arrow Key - decrease the Y zoom value
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
-
- GLvoid toggleInterlaceEnable( GLvoid );
-
- GLvoid inc( GLfloat );
- GLvoid dec( GLfloat );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- #define TEX_W 64
- #define TEX_H 32
- #define TEX_H2 (TEX_H/2)
- #define MAG 1
-
- /* Global Variables */
-
- unsigned char *BlackImg;
- unsigned char *RampImg = 0;
- unsigned char *RampEven = 0;
- unsigned char *RampOdd = 0;
-
- GLenum texfmt = GL_RGBA;
- GLenum textype = GL_UNSIGNED_BYTE;
-
- GLfloat xzoom = 1.0;
- GLfloat yzoom = 1.0;
-
- static GLboolean interlaceEnable = GL_FALSE;
- static GLboolean interlaceSupported = GL_TRUE;
- static GLboolean is_RE = GL_FALSE;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width/4, height/4 );
- glutInitWindowSize( width/2, height/2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_SINGLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout,"\n%s - demonstrates interlacing\n\n"\
- "<i> key - toggle interlacing\n"
- "UP Arrow Key - increase the Y zoom value\n"
- "DOWN Arrow Key - decrease the Y zoom value\n"
- "LEFT Arrow Key - increase the X zoom value\n"
- "RIGHT Arrow Key - decrease the X zoom value\n"
- "Escape key - exit the program\n\n",
- progname
- );
- }
-
- static void
- CreateImages(void)
- {
- int x,y;
-
- BlackImg = (unsigned char *) calloc(TEX_W * TEX_H * 4,1);
- RampImg = (unsigned char *) calloc(TEX_W * TEX_H * 4,1);
- RampEven = (unsigned char *) calloc(TEX_W * TEX_H * 4,1);
- RampOdd = (unsigned char *) calloc(TEX_W * TEX_H * 4,1);
-
- for (y=0; y<TEX_H; y++) {
- for (x=0; x<TEX_W; x++) {
- RampImg[y*TEX_W*4 + x*4 + 0] = y * 256./TEX_H;
- RampImg[y*TEX_W*4 + x*4 + 3] = 255;
- if (y&1) {
- RampImg[y*TEX_W*4 + x*4 + 1] = 0;
- RampImg[y*TEX_W*4 + x*4 + 2] = x * 256./TEX_W;
- RampOdd[(y/2)*TEX_W*4+x*4 + 0]= RampImg[y*TEX_W*4 + x*4 + 0];
- RampOdd[(y/2)*TEX_W*4+x*4 + 1]= RampImg[y*TEX_W*4 + x*4 + 1];
- RampOdd[(y/2)*TEX_W*4+x*4 + 2]= RampImg[y*TEX_W*4 + x*4 + 2];
- RampOdd[(y/2)*TEX_W*4+x*4 + 3]= RampImg[y*TEX_W*4 + x*4 + 3];
- } else {
- RampImg[y*TEX_W*4 + x*4 + 1] = x * 256./TEX_W;
- RampImg[y*TEX_W*4 + x*4 + 2] = 0;
- RampEven[(y/2)*TEX_W*4+x*4 + 0]= RampImg[y*TEX_W*4 + x*4 + 0];
- RampEven[(y/2)*TEX_W*4+x*4 + 1]= RampImg[y*TEX_W*4 + x*4 + 1];
- RampEven[(y/2)*TEX_W*4+x*4 + 2]= RampImg[y*TEX_W*4 + x*4 + 2];
- RampEven[(y/2)*TEX_W*4+x*4 + 3]= RampImg[y*TEX_W*4 + x*4 + 3];
- }
- }
- }
- }
-
- /* Initialize graphics */
- GLvoid
- initgfx( GLvoid )
- {
- glClearColor( 0.0, 0.0, 0.0, 0.0 );
- glClear( GL_COLOR_BUFFER_BIT );
-
- if (!glutExtensionSupported("GL_SGIX_interlace")) {
- interlaceSupported = GL_FALSE;
- fprintf( stderr,
- "GL_SGIX_interlace not supported on this machine\n");
- }
- is_RE = (strncmp(glGetString(GL_RENDERER), "RE", 2) == 0);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-
- CreateImages();
- }
-
- GLvoid
- decXzoom( GLvoid )
- {
- xzoom -= 0.5;
- printf( "X Zoom = %4.2f\n", xzoom );
- }
-
- GLvoid
- incXzoom( GLvoid )
- {
- xzoom += 0.5;
- printf( "X Zoom = %4.2f\n", xzoom );
- }
-
- GLvoid
- decYzoom( GLvoid )
- {
- yzoom -= 0.5;
- printf( "Y Zoom = %4.2f\n", yzoom );
- }
-
- GLvoid
- incYzoom( GLvoid )
- {
- yzoom += 0.5;
- printf( "Y Zoom = %4.2f\n", yzoom );
- }
-
- void toggleInterlaceEnable( GLvoid )
- {
- #ifdef GL_SGIX_interlace
- if (interlaceSupported) {
- interlaceEnable = !interlaceEnable;
- if (interlaceEnable) {
- glEnable(GL_INTERLACE_SGIX);
- } else {
- glDisable(GL_INTERLACE_SGIX);
- }
- printf("Interlacing %s\n", (interlaceEnable? "Enabled":"Disabled"));
- } else
- printf("GL_SGIX_interlace not supported\n");
- #else
- printf("GL_SGIX_interlace not supported\n");
- #endif
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'i': /* toggle interlacing enable */
- toggleInterlaceEnable();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_UP: /* increase y zoom factor */
- incYzoom();
- break;
- case GLUT_KEY_DOWN: /* decrease y zoom factor */
- decYzoom();
- break;
- case GLUT_KEY_LEFT: /* increase x zoom factor */
- incXzoom();
- break;
- case GLUT_KEY_RIGHT: /* decrease x zoom factor */
- decXzoom();
- break;
- }
- glutPostRedisplay();
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
-
- static void draw_prims(void)
- {
- glBegin(GL_TRIANGLE_STRIP);
- glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0);
- glTexCoord2f(1.0, 0.0); glVertex2f( 1.0, -1.0);
- glTexCoord2f(0.0, 1.0); glVertex2f(-1.0, 1.0);
- glTexCoord2f(1.0, 1.0); glVertex2f( 1.0, 1.0);
- glEnd();
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- int vx=10, vy=20;
- float rpx = -1., rpy_even = 1.;
- float rpy_odd = 1. - (2./TEX_H);
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- glPixelZoom(xzoom, yzoom);
-
- /* render a raster image */
-
- glViewport(vx,vy,TEX_W,TEX_H);
- glRasterPos2f(rpx, rpy_even);
- glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampImg);
- vx += TEX_W+4;
-
- #ifdef GL_SGIX_interlace
- if (interlaceSupported)
- glEnable(GL_INTERLACE_SGIX);
- #endif
-
- glViewport(vx,vy,TEX_W,TEX_H);
- glRasterPos2f(rpx, rpy_even);
- glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampEven);
- vx += TEX_W+4;
-
- glViewport(vx,vy,TEX_W,TEX_H);
- glRasterPos2f(rpx, rpy_odd);
- glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampOdd);
- vx += TEX_W+4;
-
-
- glViewport(vx,vy,TEX_W,TEX_H);
- glRasterPos2f(rpx, rpy_even);
- glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampEven);
- glRasterPos2f(rpx, rpy_odd);
- glDrawPixels(TEX_W,TEX_H2, texfmt, textype, RampOdd);
- vx += TEX_W+4;
-
- #ifdef GL_SGIX_interlace
- if (interlaceSupported)
- glDisable(GL_INTERLACE_SGIX);
- #endif
-
- /* rendering a textured polygon */
-
- glEnable(GL_TEXTURE_2D);
-
- /* reference image */
- glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_W, TEX_H, 0, texfmt, textype,
- RampImg);
- glViewport(vx,vy,TEX_W,TEX_H);
- draw_prims();
- vx += TEX_W+4;
-
- /* must use a null texture when using subtexture on an RE systems */
- glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_W, TEX_H, 0, texfmt, textype,
- (is_RE? NULL : BlackImg));
-
- #ifdef GL_SGIX_interlace
- if (interlaceSupported)
- glEnable(GL_INTERLACE_SGIX);
- #endif
-
- /* even fields */
- glTexSubImage2DEXT(GL_TEXTURE_2D, 0, 0, 0, TEX_W, TEX_H2, texfmt,
- textype, RampEven);
- glViewport(vx,vy,TEX_W,TEX_H);
- draw_prims();
- vx += TEX_W+4;
-
- /* odd fields */
- glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_W, TEX_H2, 0, texfmt, textype,
- (is_RE? NULL : BlackImg));
- /* start at y == 1 if interlacing is supported */
- glTexSubImage2DEXT(GL_TEXTURE_2D, 0, 0, (interlaceSupported ? 1 : 0),
- TEX_W, TEX_H2, texfmt, textype, RampOdd);
- glViewport(vx,vy,TEX_W,TEX_H);
- draw_prims();
- vx += TEX_W+4;
-
- /* both fields */
- glTexImage2D(GL_TEXTURE_2D, 0, 4, TEX_W, TEX_H2, 0, texfmt, textype,
- (is_RE? NULL : BlackImg));
- glTexSubImage2DEXT(GL_TEXTURE_2D, 0, 0, 0, TEX_W, TEX_H2, texfmt,
- textype, RampEven);
- glViewport(vx,vy,TEX_W,TEX_H);
- draw_prims();
- vx += TEX_W+4;
-
- #ifdef GL_SGIX_interlace
- if (interlaceSupported)
- glDisable(GL_INTERLACE_SGIX);
- #endif
- glDisable(GL_TEXTURE_2D);
-
- glFlush();
-
- checkError("drawScene");
- }
-