PageRenderTime 63ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 11ms

/opengles/redbook/stencil.c

http://ftk.googlecode.com/
C | 174 lines | 99 code | 19 blank | 56 comment | 2 complexity | 717178ace59b95fcf568ff7842ec8b36 MD5 | raw file
  1/*
  2 * License Applicability. Except to the extent portions of this file are
  3 * made subject to an alternative license as permitted in the SGI Free
  4 * Software License B, Version 1.1 (the "License"), the contents of this
  5 * file are subject only to the provisions of the License. You may not use
  6 * this file except in compliance with the License. You may obtain a copy
  7 * of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
  8 * Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
  9 * 
 10 * http://oss.sgi.com/projects/FreeB
 11 * 
 12 * Note that, as provided in the License, the Software is distributed on an
 13 * "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
 14 * DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
 15 * CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
 16 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
 17 * 
 18 * Original Code. The Original Code is: OpenGL Sample Implementation,
 19 * Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
 20 * Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
 21 * Copyright in any portions created by third parties is as indicated
 22 * elsewhere herein. All Rights Reserved.
 23 * 
 24 * Additional Notice Provisions: The application programming interfaces
 25 * established by SGI in conjunction with the Original Code are The
 26 * OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
 27 * April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
 28 * 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
 29 * Window System(R) (Version 1.3), released October 19, 1998. This software
 30 * was created using the OpenGL(R) version 1.2.1 Sample Implementation
 31 * published by SGI, but has not been independently verified as being
 32 * compliant with the OpenGL(R) version 1.2.1 Specification.
 33 *
 34 */
 35
 36/*
 37 *  stencil.c
 38 *  This program demonstrates use of the stencil buffer for
 39 *  masking nonrectangular regions.  
 40 *  Whenever the window is redrawn, a value of 1 is drawn 
 41 *  into a diamond-shaped region in the stencil buffer.  
 42 *  Elsewhere in the stencil buffer, the value is 0.
 43 *  Then a blue sphere is drawn where the stencil value is 1,
 44 *  and yellow torii are drawn where the stencil value is not 1.
 45 */
 46#include <GL/glut.h>
 47#include <stdlib.h>
 48
 49#define YELLOWMAT   1
 50#define BLUEMAT 2
 51
 52void init (void) 
 53{
 54   GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
 55   GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 };
 56
 57   GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 };
 58   GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 };
 59
 60   GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 };
 61
 62   glNewList(YELLOWMAT, GL_COMPILE);
 63   glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
 64   glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
 65   glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
 66   glEndList();
 67
 68   glNewList(BLUEMAT, GL_COMPILE);
 69   glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
 70   glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
 71   glMaterialf(GL_FRONT, GL_SHININESS, 45.0);
 72   glEndList();
 73
 74   glLightfv(GL_LIGHT0, GL_POSITION, position_one);
 75
 76   glEnable(GL_LIGHT0);
 77   glEnable(GL_LIGHTING);
 78   glEnable(GL_DEPTH_TEST);
 79
 80   glClearStencil(0x0);
 81   glEnable(GL_STENCIL_TEST);
 82}
 83
 84/* Draw a sphere in a diamond-shaped section in the
 85 * middle of a window with 2 torii.
 86 */
 87void display(void)
 88{
 89   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 90
 91/* draw blue sphere where the stencil is 1 */
 92   glStencilFunc (GL_EQUAL, 0x1, 0x1);
 93   glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
 94   glCallList (BLUEMAT);
 95   glutSolidSphere (0.5, 15, 15);
 96
 97/* draw the tori where the stencil is not 1 */
 98   glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
 99   glPushMatrix();
100      glRotatef (45.0, 0.0, 0.0, 1.0);
101      glRotatef (45.0, 0.0, 1.0, 0.0);
102      glCallList (YELLOWMAT);
103      glutSolidTorus (0.275, 0.85, 15, 15);
104      glPushMatrix();
105         glRotatef (90.0, 1.0, 0.0, 0.0);
106         glutSolidTorus (0.275, 0.85, 15, 15);
107      glPopMatrix();
108   glPopMatrix();
109}
110
111/*  Whenever the window is reshaped, redefine the 
112 *  coordinate system and redraw the stencil area.
113 */
114void reshape(int w, int h)
115{
116   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
117
118/* create a diamond shaped stencil area */
119   glMatrixMode(GL_PROJECTION);
120   glLoadIdentity();
121   if (w <= h)
122      gluOrtho2D(-3.0, 3.0, -3.0*(GLfloat)h/(GLfloat)w,
123                 3.0*(GLfloat)h/(GLfloat)w);
124   else
125      gluOrtho2D(-3.0*(GLfloat)w/(GLfloat)h,
126                 3.0*(GLfloat)w/(GLfloat)h, -3.0, 3.0);
127   glMatrixMode(GL_MODELVIEW);
128   glLoadIdentity();
129
130   glClear(GL_STENCIL_BUFFER_BIT);
131   glStencilFunc (GL_ALWAYS, 0x1, 0x1);
132   glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
133   glBegin(GL_QUADS);
134      glVertex2f (-1.0, 0.0);
135      glVertex2f (0.0, 1.0);
136      glVertex2f (1.0, 0.0);
137      glVertex2f (0.0, -1.0);
138   glEnd();
139
140   glMatrixMode(GL_PROJECTION);
141   glLoadIdentity();
142   gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);
143   glMatrixMode(GL_MODELVIEW);
144   glLoadIdentity();
145   glTranslatef(0.0, 0.0, -5.0);
146}
147
148void keyboard(unsigned char key, int x, int y)
149{
150   switch (key) {
151      case 27:
152         exit(0);
153         break;
154   }
155}
156
157/* Main Loop
158 * Be certain to request stencil bits.
159 */
160int main(int argc, char** argv)
161{
162   glutInit(&argc, argv);
163   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB 
164                        | GLUT_DEPTH | GLUT_STENCIL);
165   glutInitWindowSize (400, 400);
166   glutInitWindowPosition (100, 100);
167   glutCreateWindow (argv[0]);
168   init ();
169   glutReshapeFunc(reshape);
170   glutDisplayFunc(display);
171   glutKeyboardFunc(keyboard);
172   glutMainLoop();
173   return 0;
174}