132001f49Smrg 232001f49Smrg/* Copyright (c) Mark J. Kilgard, 1994. */ 332001f49Smrg 432001f49Smrg/* 532001f49Smrg * (c) Copyright 1993, Silicon Graphics, Inc. 632001f49Smrg * ALL RIGHTS RESERVED 732001f49Smrg * Permission to use, copy, modify, and distribute this software for 832001f49Smrg * any purpose and without fee is hereby granted, provided that the above 932001f49Smrg * copyright notice appear in all copies and that both the copyright notice 1032001f49Smrg * and this permission notice appear in supporting documentation, and that 1132001f49Smrg * the name of Silicon Graphics, Inc. not be used in advertising 1232001f49Smrg * or publicity pertaining to distribution of the software without specific, 1332001f49Smrg * written prior permission. 1432001f49Smrg * 1532001f49Smrg * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" 1632001f49Smrg * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, 1732001f49Smrg * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR 1832001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON 1932001f49Smrg * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT, 2032001f49Smrg * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY 2132001f49Smrg * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, 2232001f49Smrg * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF 2332001f49Smrg * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN 2432001f49Smrg * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON 2532001f49Smrg * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE 2632001f49Smrg * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE. 2732001f49Smrg * 2832001f49Smrg * US Government Users Restricted Rights 2932001f49Smrg * Use, duplication, or disclosure by the Government is subject to 3032001f49Smrg * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph 3132001f49Smrg * (c)(1)(ii) of the Rights in Technical Data and Computer Software 3232001f49Smrg * clause at DFARS 252.227-7013 and/or in similar or successor 3332001f49Smrg * clauses in the FAR or the DOD or NASA FAR Supplement. 3432001f49Smrg * Unpublished-- rights reserved under the copyright laws of the 3532001f49Smrg * United States. Contractor/manufacturer is Silicon Graphics, 3632001f49Smrg * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311. 3732001f49Smrg * 3832001f49Smrg * OpenGL(TM) is a trademark of Silicon Graphics, Inc. 3932001f49Smrg */ 4032001f49Smrg/* stencil.c 4132001f49Smrg * This program draws two rotated tori in a window. 4232001f49Smrg * A diamond in the center of the window masks out part 4332001f49Smrg * of the scene. Within this mask, a different model 4432001f49Smrg * (a sphere) is drawn in a different color. 4532001f49Smrg */ 4632001f49Smrg 4732001f49Smrg/* 4832001f49Smrg * !!! NOTE !!! 4932001f49Smrg * 5032001f49Smrg * This demo is poorly written. The stencil buffer should be 5132001f49Smrg * redrawn in display(), not in the myReshape() function. 5232001f49Smrg * The reason is if the window gets "damaged" then the stencil buffer 5332001f49Smrg * contents will be in an undefined state (myReshape is not called when 5432001f49Smrg * a window is damaged and needs to be redrawn). If the stencil buffer 5532001f49Smrg * contents are undefined, the results of display() are unpredictable. 5632001f49Smrg * 5732001f49Smrg * -Brian 5832001f49Smrg */ 5932001f49Smrg 6032001f49Smrg 6132001f49Smrg#include <stdlib.h> 6232001f49Smrg#include "glut_wrap.h" 6332001f49Smrg 6432001f49Smrg#define YELLOWMAT 1 6532001f49Smrg#define BLUEMAT 2 6632001f49Smrg 6732001f49Smrgstatic void myinit (void) 6832001f49Smrg{ 6932001f49Smrg GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 }; 7032001f49Smrg GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 }; 7132001f49Smrg 7232001f49Smrg GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 }; 7332001f49Smrg GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 }; 7432001f49Smrg 7532001f49Smrg GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 }; 7632001f49Smrg 7732001f49Smrg glNewList(YELLOWMAT, GL_COMPILE); 7832001f49Smrg glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse); 7932001f49Smrg glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular); 8032001f49Smrg glMaterialf(GL_FRONT, GL_SHININESS, 64.0); 8132001f49Smrg glEndList(); 8232001f49Smrg 8332001f49Smrg glNewList(BLUEMAT, GL_COMPILE); 8432001f49Smrg glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse); 8532001f49Smrg glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular); 8632001f49Smrg glMaterialf(GL_FRONT, GL_SHININESS, 45.0); 8732001f49Smrg glEndList(); 8832001f49Smrg 8932001f49Smrg glLightfv(GL_LIGHT0, GL_POSITION, position_one); 9032001f49Smrg 9132001f49Smrg glEnable(GL_LIGHT0); 9232001f49Smrg glEnable(GL_LIGHTING); 9332001f49Smrg glDepthFunc(GL_LESS); 9432001f49Smrg glEnable(GL_DEPTH_TEST); 9532001f49Smrg 9632001f49Smrg glClearStencil(0x0); 9732001f49Smrg glEnable(GL_STENCIL_TEST); 9832001f49Smrg 9932001f49Smrg} 10032001f49Smrg 10132001f49Smrg/* Draw a sphere in a diamond-shaped section in the 10232001f49Smrg * middle of a window with 2 tori. 10332001f49Smrg */ 10432001f49Smrgstatic void display(void) 10532001f49Smrg{ 10632001f49Smrg glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 10732001f49Smrg 10832001f49Smrg glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP); 10932001f49Smrg 11032001f49Smrg/* draw blue sphere where the stencil is 1 */ 11132001f49Smrg glStencilFunc (GL_EQUAL, 0x1, 0x1); 11232001f49Smrg glCallList (BLUEMAT); 11332001f49Smrg glutSolidSphere (0.5, 15, 15); 11432001f49Smrg 11532001f49Smrg/* draw the tori where the stencil is not 1 */ 11632001f49Smrg glStencilFunc (GL_NOTEQUAL, 0x1, 0x1); 11732001f49Smrg glPushMatrix(); 11832001f49Smrg glRotatef (45.0, 0.0, 0.0, 1.0); 11932001f49Smrg glRotatef (45.0, 0.0, 1.0, 0.0); 12032001f49Smrg glCallList (YELLOWMAT); 12132001f49Smrg glutSolidTorus (0.275, 0.85, 15, 15); 12232001f49Smrg glPushMatrix(); 12332001f49Smrg glRotatef (90.0, 1.0, 0.0, 0.0); 12432001f49Smrg glutSolidTorus (0.275, 0.85, 15, 15); 12532001f49Smrg glPopMatrix(); 12632001f49Smrg glPopMatrix(); 12732001f49Smrg 12832001f49Smrg glFlush(); 12932001f49Smrg glutSwapBuffers(); 13032001f49Smrg} 13132001f49Smrg 13232001f49Smrg/* Whenever the window is reshaped, redefine the 13332001f49Smrg * coordinate system and redraw the stencil area. 13432001f49Smrg */ 13532001f49Smrgstatic void myReshape(int w, int h) 13632001f49Smrg{ 13732001f49Smrg glViewport(0, 0, w, h); 13832001f49Smrg 13932001f49Smrg glClear(GL_STENCIL_BUFFER_BIT); 14032001f49Smrg/* create a diamond shaped stencil area */ 14132001f49Smrg glMatrixMode(GL_PROJECTION); 14232001f49Smrg glLoadIdentity(); 14332001f49Smrg glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0); 14432001f49Smrg glMatrixMode(GL_MODELVIEW); 14532001f49Smrg glLoadIdentity(); 14632001f49Smrg 14732001f49Smrg glStencilFunc (GL_ALWAYS, 0x1, 0x1); 14832001f49Smrg glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE); 14932001f49Smrg glBegin(GL_QUADS); 15032001f49Smrg glVertex3f (-1.0, 0.0, 0.0); 15132001f49Smrg glVertex3f (0.0, 1.0, 0.0); 15232001f49Smrg glVertex3f (1.0, 0.0, 0.0); 15332001f49Smrg glVertex3f (0.0, -1.0, 0.0); 15432001f49Smrg glEnd(); 15532001f49Smrg 15632001f49Smrg glMatrixMode(GL_PROJECTION); 15732001f49Smrg glLoadIdentity(); 15832001f49Smrg gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0); 15932001f49Smrg glMatrixMode(GL_MODELVIEW); 16032001f49Smrg glLoadIdentity(); 16132001f49Smrg glTranslatef(0.0, 0.0, -5.0); 16232001f49Smrg} 16332001f49Smrg 16432001f49Smrgstatic void 16532001f49Smrgkey(unsigned char k, int x, int y) 16632001f49Smrg{ 16732001f49Smrg switch (k) { 16832001f49Smrg case 27: /* Escape */ 16932001f49Smrg exit(0); 17032001f49Smrg break; 17132001f49Smrg default: 17232001f49Smrg return; 17332001f49Smrg } 17432001f49Smrg glutPostRedisplay(); 17532001f49Smrg} 17632001f49Smrg 17732001f49Smrg/* Main Loop 17832001f49Smrg * Open window with initial window size, title bar, 17932001f49Smrg * RGBA display mode, and handle input events. 18032001f49Smrg */ 18132001f49Smrgint main(int argc, char** argv) 18232001f49Smrg{ 18332001f49Smrg glutInit(&argc, argv); 18432001f49Smrg glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL); 18532001f49Smrg glutInitWindowSize (400, 400); 18632001f49Smrg glutCreateWindow (argv[0]); 18732001f49Smrg myinit (); 18832001f49Smrg glutReshapeFunc (myReshape); 18932001f49Smrg glutDisplayFunc(display); 19032001f49Smrg glutKeyboardFunc(key); 19132001f49Smrg glutMainLoop(); 19232001f49Smrg return 0; /* ANSI C requires main to return int. */ 19332001f49Smrg} 194